From 785ca38b15bd6b92ef9479dd8c7ecb661bd6b309 Mon Sep 17 00:00:00 2001 From: haykam821 <24855774+haykam821@users.noreply.github.com> Date: Sat, 23 Dec 2023 20:21:43 -0500 Subject: [PATCH] Allow players to interact with ornaments to see additional details --- .../nucleoid/extras/lobby/tree/Ornament.java | 13 ++++++++ .../extras/lobby/tree/OrnamentModel.java | 32 +++++++++++++++++-- .../data/nucleoid_extras/lang/en_us.json | 2 ++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/main/java/xyz/nucleoid/extras/lobby/tree/Ornament.java b/src/main/java/xyz/nucleoid/extras/lobby/tree/Ornament.java index f9b8741..5f28476 100644 --- a/src/main/java/xyz/nucleoid/extras/lobby/tree/Ornament.java +++ b/src/main/java/xyz/nucleoid/extras/lobby/tree/Ornament.java @@ -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 CODEC = RecordCodecBuilder.create(instance -> instance.group( Registries.ITEM.getCodec().fieldOf("item").forGetter(Ornament::item), @@ -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) + .map(Text::literal) + .orElse(UNKNOWN_OWNER); + } + public boolean canBeRemovedBy(ServerPlayerEntity player) { return player.getUuid().equals(this.owner) || player.isCreative(); } diff --git a/src/main/java/xyz/nucleoid/extras/lobby/tree/OrnamentModel.java b/src/main/java/xyz/nucleoid/extras/lobby/tree/OrnamentModel.java index 5b022c4..75da964 100644 --- a/src/main/java/xyz/nucleoid/extras/lobby/tree/OrnamentModel.java +++ b/src/main/java/xyz/nucleoid/extras/lobby/tree/OrnamentModel.java @@ -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; @@ -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); diff --git a/src/main/resources/data/nucleoid_extras/lang/en_us.json b/src/main/resources/data/nucleoid_extras/lang/en_us.json index c4c0004..da6f733 100644 --- a/src/main/resources/data/nucleoid_extras/lang/en_us.json +++ b/src/main/resources/data/nucleoid_extras/lang/en_us.json @@ -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",