From 5fc1fe33db88d6e3e89f0084b240e8126b64de6a Mon Sep 17 00:00:00 2001 From: fullwall Date: Thu, 31 Mar 2016 17:44:25 +0800 Subject: [PATCH] Workaround Spigot knockback bug by setting high knockback resistance on protected NPCs --- src/main/java/net/citizensnpcs/npc/CitizensNPC.java | 6 ++++++ .../net/citizensnpcs/npc/entity/CreeperController.java | 3 ++- src/main/java/net/citizensnpcs/util/NMS.java | 7 +++++-- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/citizensnpcs/npc/CitizensNPC.java b/src/main/java/net/citizensnpcs/npc/CitizensNPC.java index 50c9602b3..aecd663cc 100644 --- a/src/main/java/net/citizensnpcs/npc/CitizensNPC.java +++ b/src/main/java/net/citizensnpcs/npc/CitizensNPC.java @@ -311,6 +311,12 @@ public class CitizensNPC extends AbstractNPC { if (getEntity() instanceof LivingEntity) { boolean nameplateVisible = data().get(NPC.NAMEPLATE_VISIBLE_METADATA, true); ((LivingEntity) getEntity()).setCustomNameVisible(nameplateVisible); + + if (data().get(NPC.DEFAULT_PROTECTED_METADATA, true)) { + NMS.setKnockbackResistance((LivingEntity) getEntity(), 1D); + } else { + NMS.setKnockbackResistance((LivingEntity) getEntity(), 0D); + } } } catch (Exception ex) { Throwable error = Throwables.getRootCause(ex); diff --git a/src/main/java/net/citizensnpcs/npc/entity/CreeperController.java b/src/main/java/net/citizensnpcs/npc/entity/CreeperController.java index a9f310120..05a0fdddb 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CreeperController.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CreeperController.java @@ -130,8 +130,9 @@ public class CreeperController extends MobEntityController { return; } if (NPCPushEvent.getHandlerList().getRegisteredListeners().length == 0) { - if (!npc.data().get(NPC.DEFAULT_PROTECTED_METADATA, true)) + if (!npc.data().get(NPC.DEFAULT_PROTECTED_METADATA, true)) { super.g(x, y, z); + } return; } Vector vector = new Vector(x, y, z); diff --git a/src/main/java/net/citizensnpcs/util/NMS.java b/src/main/java/net/citizensnpcs/util/NMS.java index f1bb3834f..fd1d791ad 100644 --- a/src/main/java/net/citizensnpcs/util/NMS.java +++ b/src/main/java/net/citizensnpcs/util/NMS.java @@ -737,6 +737,11 @@ public class NMS { handle.aP = yaw; } + public static void setKnockbackResistance(org.bukkit.entity.LivingEntity entity, double d) { + EntityLiving handle = NMS.getHandle(entity); + handle.getAttributeInstance(GenericAttributes.c).setValue(d); + } + public static void setProfile(SkullMeta meta, GameProfile profile) { if (SKULL_PROFILE_FIELD == null) { try { @@ -894,7 +899,6 @@ public class NMS { } } - private static Field CRAFT_TEAM_FIELD; private static final float DEFAULT_SPEED = 1F; private static Map, Integer> ENTITY_CLASS_TO_INT; private static Map, String> ENTITY_CLASS_TO_NAME; @@ -909,7 +913,6 @@ public class NMS { private static final Field RABBIT_FIELD = getField(EntityRabbit.class, "bv"); private static final Random RANDOM = Util.getFastRandom(); private static Field SKULL_PROFILE_FIELD; - private static Field TRACKED_ENTITY_SET = NMS.getField(EntityTracker.class, "c"); static {