From 278b4462f3be8f9b89a415e7e79370c52b3dffef Mon Sep 17 00:00:00 2001 From: Flowsqy <47575244+Flowsqy@users.noreply.github.com> Date: Wed, 29 Dec 2021 16:34:05 +0100 Subject: [PATCH] Add FakeItem implementation --- .../de/epiceric/shopchest/nms/FakeItem.java | 2 ++ .../shopchest/nms/v1_17_R1/FakeItemImpl.java | 28 +++++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/nms/interface/src/main/java/de/epiceric/shopchest/nms/FakeItem.java b/nms/interface/src/main/java/de/epiceric/shopchest/nms/FakeItem.java index 9e5b1c8..d1eeda2 100644 --- a/nms/interface/src/main/java/de/epiceric/shopchest/nms/FakeItem.java +++ b/nms/interface/src/main/java/de/epiceric/shopchest/nms/FakeItem.java @@ -7,4 +7,6 @@ public interface FakeItem extends FakeEntity{ void sendData(ItemStack item, Iterable receivers); + void resetVelocity(Iterable receivers); + } diff --git a/nms/v1_17_R1/src/main/java/de/epiceric/shopchest/nms/v1_17_R1/FakeItemImpl.java b/nms/v1_17_R1/src/main/java/de/epiceric/shopchest/nms/v1_17_R1/FakeItemImpl.java index d09c7f0..f21cdcf 100644 --- a/nms/v1_17_R1/src/main/java/de/epiceric/shopchest/nms/v1_17_R1/FakeItemImpl.java +++ b/nms/v1_17_R1/src/main/java/de/epiceric/shopchest/nms/v1_17_R1/FakeItemImpl.java @@ -1,22 +1,46 @@ package de.epiceric.shopchest.nms.v1_17_R1; import de.epiceric.shopchest.nms.FakeItem; +import net.minecraft.network.protocol.game.ClientboundSetEntityMotionPacket; +import net.minecraft.network.syncher.EntityDataAccessor; import net.minecraft.network.syncher.SynchedEntityData; import net.minecraft.world.entity.EntityType; +import net.minecraft.world.entity.item.ItemEntity; +import net.minecraft.world.phys.Vec3; +import org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +import java.lang.reflect.Field; import java.util.List; public class FakeItemImpl extends FakeEntityImpl implements FakeItem { + private final static EntityDataAccessor DATA_ITEM; + + static { + try{ + final Field dataItemField = ItemEntity.class.getDeclaredField("c"); // DATA_ITEM + dataItemField.setAccessible(true); + DATA_ITEM = forceCast(dataItemField.get(null)); + }catch (ReflectiveOperationException e){ + throw new RuntimeException(e); + } + } + public FakeItemImpl() { super(); } @Override public void sendData(ItemStack item, Iterable receivers) { + sendData(receivers, item); + } + @Override + public void resetVelocity(Iterable receivers) { + final ClientboundSetEntityMotionPacket velocityPacket = new ClientboundSetEntityMotionPacket(entityId, Vec3.ZERO); + sendPacket(velocityPacket, receivers); } @Override @@ -26,11 +50,11 @@ public class FakeItemImpl extends FakeEntityImpl implements FakeItem @Override protected int getDataItemCount() { - return 0; + return 1; } @Override protected void addSpecificData(List> packedItems, ItemStack data) { - + packedItems.add(new SynchedEntityData.DataItem<>(DATA_ITEM, CraftItemStack.asNMSCopy(data))); } }