From 686b25d59bf5f632669c16dbc48a54d2e391412f Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Sun, 22 Dec 2013 12:36:06 +1300 Subject: [PATCH] Add a way to get and set the bounding box of disguises --- .../libraryaddict/disguise/LibsDisguises.java | 12 +++ .../disguise/utilities/DisguiseValues.java | 18 +++++ .../disguise/utilities/FakeBoundingBox.java | 26 +++++++ .../disguise/utilities/ReflectionManager.java | 76 +++++++++++++++++++ 4 files changed, 132 insertions(+) create mode 100644 src/me/libraryaddict/disguise/utilities/FakeBoundingBox.java diff --git a/src/me/libraryaddict/disguise/LibsDisguises.java b/src/me/libraryaddict/disguise/LibsDisguises.java index c08c3911..9039e7ce 100644 --- a/src/me/libraryaddict/disguise/LibsDisguises.java +++ b/src/me/libraryaddict/disguise/LibsDisguises.java @@ -25,6 +25,7 @@ import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Ageable; import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Zombie; import org.bukkit.plugin.java.JavaPlugin; import com.comphenix.protocol.wrappers.WrappedDataWatcher; @@ -207,6 +208,17 @@ public class LibsDisguises extends JavaPlugin { sound.setDamageSoundVolume((Float) soundStrength); } } + + // Get the bounding box + + disguiseValues.setAdultBox(ReflectionManager.getBoundingBox(bukkitEntity)); + if (bukkitEntity instanceof Ageable) { + ((Ageable) bukkitEntity).setBaby(); + disguiseValues.setBabyBox(ReflectionManager.getBoundingBox(bukkitEntity)); + } else if (bukkitEntity instanceof Zombie) { + ((Zombie) bukkitEntity).setBaby(true); + disguiseValues.setBabyBox(ReflectionManager.getBoundingBox(bukkitEntity)); + } } catch (Exception ex) { System.out.print("[LibsDisguises] Uh oh! Trouble while making values for the disguise " + disguiseType.name() + "!"); diff --git a/src/me/libraryaddict/disguise/utilities/DisguiseValues.java b/src/me/libraryaddict/disguise/utilities/DisguiseValues.java index c9cdbda2..34d5b958 100644 --- a/src/me/libraryaddict/disguise/utilities/DisguiseValues.java +++ b/src/me/libraryaddict/disguise/utilities/DisguiseValues.java @@ -46,6 +46,8 @@ public class DisguiseValues { private int enumEntitySize; private HashMap metaValues = new HashMap(); private Class nmsEntityClass; + private FakeBoundingBox adultBox; + private FakeBoundingBox babyBox; public DisguiseValues(DisguiseType type, Class classType, int entitySize) { values.put(type, this); @@ -53,6 +55,22 @@ public class DisguiseValues { nmsEntityClass = classType; } + public FakeBoundingBox getBabyBox() { + return babyBox; + } + + public void setAdultBox(FakeBoundingBox newBox) { + adultBox = newBox; + } + + public void setBabyBox(FakeBoundingBox newBox) { + babyBox = newBox; + } + + public FakeBoundingBox getAdultBox() { + return adultBox; + } + public int getEntitySize(double paramDouble) { double d = paramDouble - (((int) Math.floor(paramDouble)) + 0.5D); diff --git a/src/me/libraryaddict/disguise/utilities/FakeBoundingBox.java b/src/me/libraryaddict/disguise/utilities/FakeBoundingBox.java new file mode 100644 index 00000000..5c516af7 --- /dev/null +++ b/src/me/libraryaddict/disguise/utilities/FakeBoundingBox.java @@ -0,0 +1,26 @@ +package me.libraryaddict.disguise.utilities; + +public class FakeBoundingBox { + private double xMod; + private double yMod; + private double zMod; + + public FakeBoundingBox(double xMod, double yMod, double zMod) { + this.xMod = xMod; + this.yMod = yMod; + this.zMod = zMod; + } + + public double getX() { + return xMod; + } + + public double getY() { + return yMod; + } + + public double getZ() { + return zMod; + } + +} diff --git a/src/me/libraryaddict/disguise/utilities/ReflectionManager.java b/src/me/libraryaddict/disguise/utilities/ReflectionManager.java index c3a6a0f9..e0ebb16d 100644 --- a/src/me/libraryaddict/disguise/utilities/ReflectionManager.java +++ b/src/me/libraryaddict/disguise/utilities/ReflectionManager.java @@ -55,6 +55,82 @@ public class ReflectionManager { } } + public static FakeBoundingBox getBoundingBox(Entity entity) { + try { + Object boundingBox = getNmsClass("Entity").getField("boundingBox").get(getNmsEntity(entity)); + double x = 0, y = 0, z = 0; + int stage = 0; + for (Field field : boundingBox.getClass().getFields()) { + if (field.getType().getSimpleName().equals("Double")) { + stage++; + switch (stage) { + case 1: + x -= field.getDouble(boundingBox); + break; + case 2: + y -= field.getDouble(boundingBox); + break; + case 3: + z -= field.getDouble(boundingBox); + break; + case 4: + x += field.getDouble(boundingBox); + break; + case 5: + y += field.getDouble(boundingBox); + break; + case 6: + z += field.getDouble(boundingBox); + break; + default: + throw new Exception("Error while setting the bounding box, more doubles than I thought??"); + } + return new FakeBoundingBox(x, y, z); + } + } + } catch (Exception ex) { + ex.printStackTrace(); + } + return null; + } + + public static void setBoundingBox(Entity entity, double newX, double newY, double newZ) { + try { + Object boundingBox = getNmsClass("Entity").getField("boundingBox").get(getNmsEntity(entity)); + double x = 0, y = 0, z = 0; + int stage = 0; + for (Field field : boundingBox.getClass().getFields()) { + if (field.getType().getSimpleName().equals("Double")) { + stage++; + switch (stage) { + case 1: + x = field.getDouble(boundingBox); + break; + case 2: + y = field.getDouble(boundingBox); + break; + case 3: + z = field.getDouble(boundingBox); + break; + case 4: + field.setDouble(boundingBox, x); + break; + case 5: + field.setDouble(boundingBox, y); + break; + case 6: + field.setDouble(boundingBox, z); + break; + default: + throw new Exception("Error while setting the bounding box, more doubles than I thought??"); + } + } + } + } catch (Exception ex) { + ex.printStackTrace(); + } + } + public static Object createEntityInstance(String entityName) { try { Class entityClass = getNmsClass("Entity" + entityName);