Skip to content

Commit

Permalink
Allow players to interact with ornaments to see additional details
Browse files Browse the repository at this point in the history
  • Loading branch information
haykam821 committed Dec 24, 2023
1 parent e112f3d commit 785ca38
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/main/java/xyz/nucleoid/extras/lobby/tree/Ornament.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@

import java.util.UUID;

import com.mojang.authlib.GameProfile;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;

import net.minecraft.item.Item;
import net.minecraft.registry.Registries;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.math.Vec3d;
import xyz.nucleoid.extras.util.ExtraCodecs;

public record Ornament(Item item, Vec3d offset, float yaw, float hookYaw, UUID owner) {
private static final Text UNKNOWN_OWNER = Text.translatable("commands.banlist.entry.unknown");

public static final Codec<Ornament> CODEC = RecordCodecBuilder.create(instance ->
instance.group(
Registries.ITEM.getCodec().fieldOf("item").forGetter(Ornament::item),
Expand All @@ -22,6 +27,14 @@ public record Ornament(Item item, Vec3d offset, float yaw, float hookYaw, UUID o
).apply(instance, Ornament::new)
);

public Text getOwnerName(MinecraftServer server) {
return server.getUserCache()
.getByUuid(owner)
.map(GameProfile::getName)
.<Text>map(Text::literal)
.orElse(UNKNOWN_OWNER);
}

public boolean canBeRemovedBy(ServerPlayerEntity player) {
return player.getUuid().equals(this.owner) || player.isCreative();
}
Expand Down
32 changes: 30 additions & 2 deletions src/main/java/xyz/nucleoid/extras/lobby/tree/OrnamentModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@
import eu.pb4.polymer.virtualentity.api.elements.InteractionElement;
import eu.pb4.polymer.virtualentity.api.elements.ItemDisplayElement;
import eu.pb4.polymer.virtualentity.api.elements.VirtualElement.InteractionHandler;
import net.minecraft.block.Block;
import net.minecraft.item.Items;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.Text;
import net.minecraft.util.Hand;
import net.minecraft.util.math.MathHelper;
import xyz.nucleoid.extras.lobby.block.TreeDecorationBlockEntity;

Expand Down Expand Up @@ -47,10 +53,32 @@ public OrnamentModel(TreeDecorationBlockEntity blockEntity, Ornament ornament) {
this.updateTransformations(true);
}

private void wobble(ServerPlayerEntity player, boolean careful) {
this.wobbleTicks = 10;
this.wobbleStrength = Math.min(this.wobbleStrength + 10, careful ? 20 : 60);

var pos = this.blockEntity.getPos().toCenterPos().add(this.ornament.offset());
float pitch = 1.3f + player.getRandom().nextFloat() * 0.2f;

player.getWorld().playSound(null, pos.getX(), pos.getY(), pos.getZ(), SoundEvents.BLOCK_CHAIN_HIT, SoundCategory.BLOCKS, 0.5f, pitch);
}

@Override
public void interact(ServerPlayerEntity player, Hand hand) {
this.wobble(player, true);

MinecraftServer server = this.blockEntity.getWorld().getServer();
Text ownerName = this.ornament.getOwnerName(server);

Block block = Block.getBlockFromItem(this.ornament.item());
Text itemName = block.getName();

player.sendMessage(Text.translatable("text.nucleoid_extras.ornament.details", itemName, ownerName), true);
}

@Override
public void attack(ServerPlayerEntity player) {
this.wobbleTicks = 10;
this.wobbleStrength = Math.min(this.wobbleStrength + 10, 60);
this.wobble(player, false);

if (this.ornament.canBeRemovedBy(player) && this.wobbleStrength > 40) {
this.blockEntity.removeOrnament(ornament);
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/data/nucleoid_extras/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,8 @@
"text.nucleoid_extras.lobby_items": "Do not use this block outside of lobby!",
"text.nucleoid_extras.lobby_only": "(Lobby Only!)",

"text.nucleoid_extras.ornament.details": "This %s ornament was placed by %s",

"text.nucleoid_extras.statistics.web_url": "Click here to check out all the stats for this game!",
"text.nucleoid_extras.statistics.bundle_header": "Statistics for %s",
"text.nucleoid_extras.statistics.stat": "%s: %s",
Expand Down

0 comments on commit 785ca38

Please sign in to comment.