Workaround Spigot knockback bug by setting high knockback resistance on protected NPCs

This commit is contained in:
fullwall 2016-03-31 17:44:25 +08:00
parent bf1b116735
commit 5fc1fe33db
3 changed files with 13 additions and 3 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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<Class<?>, Integer> ENTITY_CLASS_TO_INT;
private static Map<Class<?>, 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 {