Skip to content

Instantly share code, notes, and snippets.

@CaptainBern
Created March 30, 2014 09:12
Show Gist options
  • Save CaptainBern/9870028 to your computer and use it in GitHub Desktop.
Save CaptainBern/9870028 to your computer and use it in GitHub Desktop.
public class EntityUtil {
public static PacketPlayOutAttachEntity craftAttachPacket(int that, int to) {
PacketPlayOutAttachEntity packet = new PacketPlayOutAttachEntity();
setField(getField(packet.getClass(), "a"), that);
setField(getField(packet.getClass(), "b"), to);
setField(getField(packet.getClass(), "c"), 0);
}
public static PacketPlayOutSpawnEntityLiving craftSlimeSpawnPacket(int entityType, int id, Location location, int size) {
PacketPlayOutSpawnEntityLiving packet = new PacketPlayOutSpawnEntityLiving();
setField(getField(packet.getClass(), "a"), id);
setField(getField(packet.getClass(), "b"), entityType);
setField(getField(packet.getClass(), "c"), floor(location.getX());
setField(getField(packet.getClass(), "d"), floor(location.getY());
setField(getField(packet.getClass(), "e"), floor(location.getZ());
setField(getField(packet.getClass(), "i"), asCompressedAngle(location.getYaw());
setField(getField(packet.getClass(), "j"), asCompressedAngle(location.getPitch());
DataWatcher watcher = new DataWatcher(null);
watcher.a(6, (Object) 20.0)
watcher.a(16, (Object) size);
setField(getField(packet.getClass(), "l"), watcher);
return packet;
}
public static int floor(double d) {
return (int) (d * 32.0D);
}
public static byte asCompressedAngle(float f) {
return (byte) (f * 256.0F / 360.0F);
}
public static Field getField(Class<?> clazz, String field) {
try {
return clazz.getDeclaredField(field);
} catch (Exception e) {
e.printStacktrace();
System.out.println("Failed to retrieve field: \"" + field + "\" from: \"" + clazz.getSimpleName() + "\"");
}
}
public static void setField(Field field, Object instance, Object value) {
try {
field.set(instance, value);
} catch (Exception e) {
e.printStacktrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment