From 7410f2088d6461b8d261991e58e172fd4360cdc0 Mon Sep 17 00:00:00 2001 From: Flowsqy <47575244+Flowsqy@users.noreply.github.com> Date: Wed, 29 Dec 2021 21:07:26 +0100 Subject: [PATCH] Add FakeItem implementation in reflection --- .../nms/reflection/FakeEntityImpl.java | 2 +- .../nms/reflection/FakeItemImpl.java | 56 ++++++++++++++++++- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/nms/reflection/src/main/java/de/epiceric/shopchest/nms/reflection/FakeEntityImpl.java b/nms/reflection/src/main/java/de/epiceric/shopchest/nms/reflection/FakeEntityImpl.java index 27152fb..b2385c9 100644 --- a/nms/reflection/src/main/java/de/epiceric/shopchest/nms/reflection/FakeEntityImpl.java +++ b/nms/reflection/src/main/java/de/epiceric/shopchest/nms/reflection/FakeEntityImpl.java @@ -7,7 +7,7 @@ import org.inventivetalent.reflection.resolver.minecraft.NMSClassResolver; public abstract class FakeEntityImpl implements FakeEntity { protected final NMSClassResolver nmsClassResolver = new NMSClassResolver(); - private final Class packetPlayOutEntityDestroyClass = nmsClassResolver.resolveSilent("network.protocol.game.PacketPlayOutEntityDestroy"); + protected final Class packetPlayOutEntityDestroyClass = nmsClassResolver.resolveSilent("network.protocol.game.PacketPlayOutEntityDestroy"); protected final Class packetPlayOutEntityMetadataClass = nmsClassResolver.resolveSilent("network.protocol.game.PacketPlayOutEntityMetadata"); protected final Class dataWatcherClass = nmsClassResolver.resolveSilent("network.syncher.DataWatcher"); diff --git a/nms/reflection/src/main/java/de/epiceric/shopchest/nms/reflection/FakeItemImpl.java b/nms/reflection/src/main/java/de/epiceric/shopchest/nms/reflection/FakeItemImpl.java index 2250ad3..f8712c9 100644 --- a/nms/reflection/src/main/java/de/epiceric/shopchest/nms/reflection/FakeItemImpl.java +++ b/nms/reflection/src/main/java/de/epiceric/shopchest/nms/reflection/FakeItemImpl.java @@ -1,22 +1,76 @@ package de.epiceric.shopchest.nms.reflection; import de.epiceric.shopchest.nms.FakeItem; +import org.bukkit.Location; +import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +import org.inventivetalent.reflection.resolver.minecraft.OBCClassResolver; + +import java.util.UUID; public class FakeItemImpl extends FakeEntityImpl implements FakeItem { + private final OBCClassResolver obcClassResolver = new OBCClassResolver(); + private final Class packetPlayOutEntityVelocityClass = nmsClassResolver.resolveSilent("network.protocol.game.PacketPlayOutEntityVelocity"); + private final Class vec3dClass = nmsClassResolver.resolveSilent("world.phys.Vec3D"); + private final Class craftItemStackClass = obcClassResolver.resolveSilent("inventory.CraftItemStack"); + public FakeItemImpl(ShopChestDebug debug) { super(debug); + + Class nmsItemStackClass = nmsClassResolver.resolveSilent("world.item.ItemStack"); + + Class[] requiredClasses = new Class[] { + nmsItemStackClass, craftItemStackClass, packetPlayOutEntityMetadataClass, dataWatcherClass, + packetPlayOutEntityDestroyClass, packetPlayOutEntityVelocityClass, + }; + + for (Class c : requiredClasses) { + if (c == null) { + debug.debug("Failed to create shop item: Could not find all required classes"); + return; + } + } } @Override public void sendData(ItemStack item, Iterable receivers) { - + try { + Object nmsItemStack = craftItemStackClass.getMethod("asNMSCopy", ItemStack.class).invoke(null, item); + Object dataWatcher = ReflectionUtils.createDataWatcher(debug, null, nmsItemStack); + for (Player receiver : receivers) { + ReflectionUtils.sendPacket(debug, packetPlayOutEntityMetadataClass.getConstructor(int.class, dataWatcherClass, boolean.class).newInstance(entityId, dataWatcher, true), receiver); + } + }catch (ReflectiveOperationException e){ + // TODO Handle this properly + throw new RuntimeException(e); + } } @Override public void resetVelocity(Iterable receivers) { + try{ + Object velocityPacket; + if (ReflectionUtils.getMajorVersion() < 14) { + velocityPacket = packetPlayOutEntityVelocityClass.getConstructor(int.class, double.class, double.class, double.class).newInstance(entityId, 0D, 0D, 0D); + } else { + Object vec3d = vec3dClass.getConstructor(double.class, double.class, double.class).newInstance(0D, 0D, 0D); + velocityPacket = packetPlayOutEntityVelocityClass.getConstructor(int.class, vec3dClass).newInstance(entityId, vec3d); + } + for(Player receiver : receivers) { + ReflectionUtils.sendPacket(debug, velocityPacket, receiver); + } + }catch (ReflectiveOperationException e){ + // TODO Handle this properly + throw new RuntimeException(e); + } + } + @Override + public void spawn(UUID uuid, Location location, Iterable receivers) { + for(Player receiver : receivers) { + ReflectionUtils.sendPacket(debug, ReflectionUtils.createPacketSpawnEntity(debug, entityId, uuid, location, EntityType.DROPPED_ITEM), receiver); + } } }