From a8fb6e3539bd1f04bb3bf9579d0168e2a64be3ce Mon Sep 17 00:00:00 2001 From: OliverSchlueter Date: Sat, 3 Aug 2024 12:39:12 +0200 Subject: [PATCH] Add FS_ItemDisplay --- .../api/entities/FS_ItemDisplay.java | 31 +++++++++++++++++++ .../utils/entityData/FS_ItemDisplayData.java | 12 +++++++ .../ClientboundSetEntityDataPacketImpl.java | 5 +++ .../fancysitula/commands/FancySitulaCMD.java | 22 ++++++++----- 4 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 api/src/main/java/de/oliver/fancysitula/api/entities/FS_ItemDisplay.java create mode 100644 api/src/main/java/de/oliver/fancysitula/api/utils/entityData/FS_ItemDisplayData.java diff --git a/api/src/main/java/de/oliver/fancysitula/api/entities/FS_ItemDisplay.java b/api/src/main/java/de/oliver/fancysitula/api/entities/FS_ItemDisplay.java new file mode 100644 index 0000000..ba1ebae --- /dev/null +++ b/api/src/main/java/de/oliver/fancysitula/api/entities/FS_ItemDisplay.java @@ -0,0 +1,31 @@ +package de.oliver.fancysitula.api.entities; + +import de.oliver.fancysitula.api.packets.FS_ClientboundSetEntityDataPacket; +import de.oliver.fancysitula.api.utils.entityData.FS_ItemDisplayData; +import org.bukkit.entity.EntityType; + +import java.util.List; + +public class FS_ItemDisplay extends FS_Display { + + protected FS_ClientboundSetEntityDataPacket.EntityData itemData = new FS_ClientboundSetEntityDataPacket.EntityData(FS_ItemDisplayData.ITEM, null); + + public FS_ItemDisplay() { + super(EntityType.ITEM_DISPLAY); + } + + public org.bukkit.inventory.ItemStack getItem() { + return (org.bukkit.inventory.ItemStack) this.itemData.getValue(); + } + + public void setItem(org.bukkit.inventory.ItemStack item) { + this.itemData.setValue(item); + } + + @Override + public List getEntityData() { + List entityData = super.getEntityData(); + entityData.add(this.itemData); + return entityData; + } +} diff --git a/api/src/main/java/de/oliver/fancysitula/api/utils/entityData/FS_ItemDisplayData.java b/api/src/main/java/de/oliver/fancysitula/api/utils/entityData/FS_ItemDisplayData.java new file mode 100644 index 0000000..012cf46 --- /dev/null +++ b/api/src/main/java/de/oliver/fancysitula/api/utils/entityData/FS_ItemDisplayData.java @@ -0,0 +1,12 @@ +package de.oliver.fancysitula.api.utils.entityData; + +import de.oliver.fancysitula.api.packets.FS_ClientboundSetEntityDataPacket; + +public class FS_ItemDisplayData { + + /** + * Use {@link org.bukkit.inventory.ItemStack} as value + */ + public static final FS_ClientboundSetEntityDataPacket.EntityDataAccessor ITEM = new FS_ClientboundSetEntityDataPacket.EntityDataAccessor("net.minecraft.world.entity.Display$ItemDisplay", "DATA_ITEM_STACK_ID"); + +} diff --git a/implementations/1_20_6/src/main/java/de/oliver/fancysitula/versions/v1_20_6/packets/ClientboundSetEntityDataPacketImpl.java b/implementations/1_20_6/src/main/java/de/oliver/fancysitula/versions/v1_20_6/packets/ClientboundSetEntityDataPacketImpl.java index 3f09594..5f125ed 100644 --- a/implementations/1_20_6/src/main/java/de/oliver/fancysitula/versions/v1_20_6/packets/ClientboundSetEntityDataPacketImpl.java +++ b/implementations/1_20_6/src/main/java/de/oliver/fancysitula/versions/v1_20_6/packets/ClientboundSetEntityDataPacketImpl.java @@ -9,6 +9,7 @@ import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket; import net.minecraft.network.syncher.SynchedEntityData; import net.minecraft.server.level.ServerPlayer; +import org.bukkit.inventory.ItemStack; import java.util.ArrayList; import java.util.List; @@ -37,6 +38,10 @@ public Object createPacket() { vanillaValue = PaperAdventure.asVanilla(c); } + if (data.getValue() instanceof ItemStack i) { + vanillaValue = net.minecraft.world.item.ItemStack.fromBukkitCopy(i); + } + dataValues.add(SynchedEntityData.DataValue.create(accessor, vanillaValue)); } catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); diff --git a/test_plugin/src/main/java/de/oliver/fancysitula/commands/FancySitulaCMD.java b/test_plugin/src/main/java/de/oliver/fancysitula/commands/FancySitulaCMD.java index 1f86e6b..a215a2c 100644 --- a/test_plugin/src/main/java/de/oliver/fancysitula/commands/FancySitulaCMD.java +++ b/test_plugin/src/main/java/de/oliver/fancysitula/commands/FancySitulaCMD.java @@ -1,7 +1,7 @@ package de.oliver.fancysitula.commands; +import de.oliver.fancysitula.api.entities.FS_ItemDisplay; import de.oliver.fancysitula.api.entities.FS_RealPlayer; -import de.oliver.fancysitula.api.entities.FS_TextDisplay; import de.oliver.fancysitula.api.packets.FS_ClientboundPlayerInfoUpdatePacket; import de.oliver.fancysitula.api.utils.FS_GameProfile; import de.oliver.fancysitula.api.utils.FS_GameType; @@ -33,14 +33,20 @@ public boolean execute(@NotNull CommandSender sender, @NotNull String s, @NotNul // Wrap the real player into an FS_Player instance FS_RealPlayer fsPlayer = new FS_RealPlayer(p); - FS_TextDisplay fakeTextDisplay = new FS_TextDisplay(); - fakeTextDisplay.setBillboardRenderConstraints((byte) 3); - fakeTextDisplay.setScale(new Vector3f(5f)); - fakeTextDisplay.setLocation(p.getLocation()); - fakeTextDisplay.setText(Component.text("Hello, World!")); - fakeTextDisplay.setBackground(0xFF00C8FF); +// FS_TextDisplay fakeTextDisplay = new FS_TextDisplay(); +// fakeTextDisplay.setBillboardRenderConstraints((byte) 3); +// fakeTextDisplay.setScale(new Vector3f(5f)); +// fakeTextDisplay.setLocation(p.getLocation()); +// fakeTextDisplay.setText(Component.text("Hello, World!")); +// fakeTextDisplay.setBackground(0xFF00C8FF); +// FancySitula.ENTITY_FACTORY.spawnEntityFor(fsPlayer, fakeTextDisplay); - FancySitula.ENTITY_FACTORY.spawnEntityFor(fsPlayer, fakeTextDisplay); + FS_ItemDisplay fakeItemDisplay = new FS_ItemDisplay(); + fakeItemDisplay.setBillboardRenderConstraints((byte) 3); + fakeItemDisplay.setScale(new Vector3f(5f)); + fakeItemDisplay.setLocation(p.getLocation()); + fakeItemDisplay.setItem(p.getInventory().getItemInMainHand()); + FancySitula.ENTITY_FACTORY.spawnEntityFor(fsPlayer, fakeItemDisplay); return true; }