Add implementation for FakeArmorStand in reflection

This commit is contained in:
Flowsqy 2021-12-29 20:29:43 +01:00
parent fe27bd5e60
commit 5f0b25c412
2 changed files with 72 additions and 10 deletions

View File

@ -2,21 +2,78 @@ package de.epiceric.shopchest.nms.reflection;
import de.epiceric.shopchest.nms.FakeArmorStand; import de.epiceric.shopchest.nms.FakeArmorStand;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.lang.reflect.Field;
import java.util.UUID;
public class FakeArmorStandImpl extends FakeEntityImpl implements FakeArmorStand { public class FakeArmorStandImpl extends FakeEntityImpl implements FakeArmorStand {
private final Class<?> packetPlayOutEntityTeleportClass = nmsClassResolver.resolveSilent("network.protocol.game.PacketPlayOutEntityTeleport");
public FakeArmorStandImpl(ShopChestDebug debug) { public FakeArmorStandImpl(ShopChestDebug debug) {
super(debug); super(debug);
} }
@Override @Override
public void sendData(String name, Iterable<Player> receivers) { public void sendData(String name, Iterable<Player> receivers) {
Object dataWatcher = ReflectionUtils.createDataWatcher(debug, name, null);
try {
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 @Override
public void setLocation(Location location, Iterable<Player> receivers) { public void setLocation(Location location, Iterable<Player> receivers) {
double y = location.getY() + (ReflectionUtils.getServerVersion().equals("v1_8_R1") ? 0 : 1.975);
try {
Object packet = packetPlayOutEntityTeleportClass.getConstructor().newInstance();
Field[] fields = packetPlayOutEntityTeleportClass.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
}
boolean isPre9 = ReflectionUtils.getMajorVersion() < 9;
fields[0].set(packet, entityId);
if (isPre9) {
fields[1].set(packet, (int)(location.getX() * 32));
fields[2].set(packet, (int)(y * 32));
fields[3].set(packet, (int)(location.getZ() * 32));
} else {
fields[1].set(packet, location.getX());
fields[2].set(packet, y);
fields[3].set(packet, location.getZ());
}
fields[4].set(packet, (byte) 0);
fields[5].set(packet, (byte) 0);
fields[6].set(packet, true);
if (packet == null) {
debug.getLogger().severe("Could not set hologram location");
debug.debug("Could not set armor stand location: Packet is null");
return;
}
for (Player receiver : receivers) {
ReflectionUtils.sendPacket(debug, packet, receiver);
}
}catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
@Override
public void spawn(UUID uuid, Location location, Iterable<Player> receivers) {
for(Player receiver : receivers) {
ReflectionUtils.sendPacket(debug, ReflectionUtils.createPacketSpawnEntity(debug, entityId, uuid, location, EntityType.ARMOR_STAND), receiver);
}
} }
} }

View File

@ -1,12 +1,15 @@
package de.epiceric.shopchest.nms.reflection; package de.epiceric.shopchest.nms.reflection;
import de.epiceric.shopchest.nms.FakeEntity; import de.epiceric.shopchest.nms.FakeEntity;
import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.inventivetalent.reflection.resolver.minecraft.NMSClassResolver;
import java.util.UUID; public abstract class FakeEntityImpl implements FakeEntity {
public class FakeEntityImpl implements FakeEntity { protected final NMSClassResolver nmsClassResolver = new NMSClassResolver();
private 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");
protected final int entityId; protected final int entityId;
protected final ShopChestDebug debug; protected final ShopChestDebug debug;
@ -21,13 +24,15 @@ public class FakeEntityImpl implements FakeEntity {
return entityId; return entityId;
} }
@Override
public void spawn(UUID uuid, Location location, Iterable<Player> receivers) {
}
@Override @Override
public void remove(Iterable<Player> receivers) { public void remove(Iterable<Player> receivers) {
try {
for(Player receiver : receivers) {
ReflectionUtils.sendPacket(debug, packetPlayOutEntityDestroyClass.getConstructor(int[].class).newInstance((Object) new int[]{entityId}), receiver);
}
} catch (ReflectiveOperationException e){
// TODO Handle this properly
throw new RuntimeException(e);
}
} }
} }