From 7cb7b365072271cc4a701cb0bd02e983222222ed Mon Sep 17 00:00:00 2001 From: fullwall Date: Tue, 2 Jul 2019 01:22:15 +0800 Subject: [PATCH] Improve /npc controllable with horses --- .../nms/v1_13_R2/entity/HorseController.java | 59 +++++++++++++------ .../nms/v1_14_R1/entity/HorseController.java | 29 ++++++++- 2 files changed, 67 insertions(+), 21 deletions(-) diff --git a/v1_13_R2/src/main/java/net/citizensnpcs/nms/v1_13_R2/entity/HorseController.java b/v1_13_R2/src/main/java/net/citizensnpcs/nms/v1_13_R2/entity/HorseController.java index 26f332f9a..41fe89721 100644 --- a/v1_13_R2/src/main/java/net/citizensnpcs/nms/v1_13_R2/entity/HorseController.java +++ b/v1_13_R2/src/main/java/net/citizensnpcs/nms/v1_13_R2/entity/HorseController.java @@ -14,12 +14,14 @@ import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.nms.v1_13_R2.util.NMSImpl; import net.citizensnpcs.npc.CitizensNPC; import net.citizensnpcs.npc.ai.NPCHolder; +import net.citizensnpcs.trait.Controllable; import net.citizensnpcs.trait.HorseModifiers; import net.citizensnpcs.util.NMS; import net.citizensnpcs.util.Util; import net.minecraft.server.v1_13_R2.BlockPosition; import net.minecraft.server.v1_13_R2.DamageSource; import net.minecraft.server.v1_13_R2.EntityHorse; +import net.minecraft.server.v1_13_R2.GenericAttributes; import net.minecraft.server.v1_13_R2.IBlockData; import net.minecraft.server.v1_13_R2.NBTTagCompound; import net.minecraft.server.v1_13_R2.SoundEffect; @@ -42,7 +44,9 @@ public class HorseController extends MobEntityController { } public static class EntityHorseNPC extends EntityHorse implements NPCHolder { + private double baseMovementSpeed; private final CitizensNPC npc; + private boolean riding; public EntityHorseNPC(World world) { this(world, null); @@ -54,6 +58,7 @@ public class HorseController extends MobEntityController { if (npc != null) { NMSImpl.clearGoals(goalSelector, targetSelector); ((Horse) getBukkitEntity()).setDomestication(((Horse) getBukkitEntity()).getMaxDomestication()); + baseMovementSpeed = this.getAttributeInstance(GenericAttributes.MOVEMENT_SPEED).getValue(); } } @@ -85,8 +90,18 @@ public class HorseController extends MobEntityController { } @Override - protected SoundEffect cs() { - return NMSImpl.getSoundEffect(npc, super.cs(), NPC.DEATH_SOUND_METADATA); + public boolean bT() { + if (npc != null && riding) { + return true; + } + return super.bT(); + } + + @Override + public void c(float f, float f1) { + if (npc == null || !npc.isFlyable()) { + super.c(f, f1); + } } @Override @@ -99,6 +114,11 @@ public class HorseController extends MobEntityController { } } + @Override + protected SoundEffect cs() { + return NMSImpl.getSoundEffect(npc, super.cs(), NPC.DEATH_SOUND_METADATA); + } + @Override protected SoundEffect d(DamageSource damagesource) { return NMSImpl.getSoundEffect(npc, super.d(damagesource), NPC.HURT_SOUND_METADATA); @@ -110,10 +130,8 @@ public class HorseController extends MobEntityController { } @Override - public void c(float f, float f1) { - if (npc == null || !npc.isFlyable()) { - super.c(f, f1); - } + protected SoundEffect D() { + return NMSImpl.getSoundEffect(npc, super.D(), NPC.AMBIENT_SOUND_METADATA); } @Override @@ -151,11 +169,6 @@ public class HorseController extends MobEntityController { // cancelled. } - @Override - protected SoundEffect D() { - return NMSImpl.getSoundEffect(npc, super.D(), NPC.AMBIENT_SOUND_METADATA); - } - @Override public CraftEntity getBukkitEntity() { if (npc != null && !(bukkitEntity instanceof NPCHolder)) { @@ -169,6 +182,13 @@ public class HorseController extends MobEntityController { return npc; } + @Override + protected void I() { + if (npc == null) { + super.I(); + } + } + @Override public boolean isLeashed() { if (npc == null) @@ -182,17 +202,20 @@ public class HorseController extends MobEntityController { return false; // shouldLeash } - @Override - protected void I() { - if (npc == null) { - super.I(); - } - } - @Override public void mobTick() { super.mobTick(); if (npc != null) { + if (npc.hasTrait(Controllable.class) && npc.getTrait(Controllable.class).isEnabled()) { + riding = getBukkitEntity().getPassengers().size() > 0; + getAttributeInstance(GenericAttributes.MOVEMENT_SPEED) + .setValue(baseMovementSpeed * npc.getNavigator().getDefaultParameters().speedModifier()); + } else { + riding = false; + } + if (riding) { + d(4, true); // datawatcher method + } NMS.setStepHeight(getBukkitEntity(), 1); npc.update(); } diff --git a/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/entity/HorseController.java b/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/entity/HorseController.java index 98dd679fa..7badef30d 100644 --- a/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/entity/HorseController.java +++ b/v1_14_R1/src/main/java/net/citizensnpcs/nms/v1_14_R1/entity/HorseController.java @@ -14,6 +14,7 @@ import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.nms.v1_14_R1.util.NMSImpl; import net.citizensnpcs.npc.CitizensNPC; import net.citizensnpcs.npc.ai.NPCHolder; +import net.citizensnpcs.trait.Controllable; import net.citizensnpcs.trait.HorseModifiers; import net.citizensnpcs.util.NMS; import net.citizensnpcs.util.Util; @@ -22,6 +23,7 @@ import net.minecraft.server.v1_14_R1.DamageSource; import net.minecraft.server.v1_14_R1.DataWatcherObject; import net.minecraft.server.v1_14_R1.EntityHorse; import net.minecraft.server.v1_14_R1.EntityTypes; +import net.minecraft.server.v1_14_R1.GenericAttributes; import net.minecraft.server.v1_14_R1.IBlockData; import net.minecraft.server.v1_14_R1.NBTTagCompound; import net.minecraft.server.v1_14_R1.SoundEffect; @@ -45,9 +47,10 @@ public class HorseController extends MobEntityController { } public static class EntityHorseNPC extends EntityHorse implements NPCHolder { - boolean calledNMSHeight = false; - + private double baseMovementSpeed; + private boolean calledNMSHeight = false; private final CitizensNPC npc; + private boolean riding; public EntityHorseNPC(EntityTypes types, World world) { this(types, world, null); @@ -58,7 +61,9 @@ public class HorseController extends MobEntityController { this.npc = (CitizensNPC) npc; if (npc != null) { NMSImpl.clearGoals(goalSelector, targetSelector); - ((Horse) getBukkitEntity()).setDomestication(((Horse) getBukkitEntity()).getMaxDomestication()); + Horse horse = (Horse) getBukkitEntity(); + horse.setDomestication(horse.getMaxDomestication()); + baseMovementSpeed = this.getAttributeInstance(GenericAttributes.MOVEMENT_SPEED).getValue(); } } @@ -87,6 +92,14 @@ public class HorseController extends MobEntityController { } } + @Override + public boolean ca() { + if (npc != null && riding) { + return true; + } + return super.ca(); + } + @Override protected void checkDespawn() { if (npc == null) { @@ -207,6 +220,16 @@ public class HorseController extends MobEntityController { public void mobTick() { super.mobTick(); if (npc != null) { + if (npc.hasTrait(Controllable.class) && npc.getTrait(Controllable.class).isEnabled()) { + riding = getBukkitEntity().getPassengers().size() > 0; + getAttributeInstance(GenericAttributes.MOVEMENT_SPEED) + .setValue(baseMovementSpeed * npc.getNavigator().getDefaultParameters().speedModifier()); + } else { + riding = false; + } + if (riding) { + d(4, true); // datawatcher method + } NMS.setStepHeight(getBukkitEntity(), 1); npc.update(); }