From 1b02573b9c85818d7a4b7d1429c50b38e52d222d Mon Sep 17 00:00:00 2001 From: fullwall Date: Sat, 29 Nov 2014 20:04:02 +0800 Subject: [PATCH] Add new entity controllers --- src/main/java/net/citizensnpcs/Citizens.java | 2 +- .../citizensnpcs/commands/NPCCommands.java | 8 +- .../citizensnpcs/npc/EntityControllers.java | 8 + .../npc/entity/EndermanController.java | 1 - .../npc/entity/EndermiteController.java | 185 ++++++++++++++++ .../npc/entity/EntityHumanNPC.java | 27 ++- .../npc/entity/GuardianController.java | 199 ++++++++++++++++++ .../npc/entity/RabbitController.java | 199 ++++++++++++++++++ .../npc/entity/WitherController.java | 1 - .../nonliving/ArmorStandController.java | 121 +++++++++++ .../nonliving/FishingHookController.java | 10 +- 11 files changed, 735 insertions(+), 26 deletions(-) create mode 100644 src/main/java/net/citizensnpcs/npc/entity/EndermiteController.java create mode 100644 src/main/java/net/citizensnpcs/npc/entity/GuardianController.java create mode 100644 src/main/java/net/citizensnpcs/npc/entity/RabbitController.java create mode 100644 src/main/java/net/citizensnpcs/npc/entity/nonliving/ArmorStandController.java diff --git a/src/main/java/net/citizensnpcs/Citizens.java b/src/main/java/net/citizensnpcs/Citizens.java index 6f03db631..b83ac3718 100644 --- a/src/main/java/net/citizensnpcs/Citizens.java +++ b/src/main/java/net/citizensnpcs/Citizens.java @@ -454,5 +454,5 @@ public class Citizens extends JavaPlugin implements CitizensPlugin { return false; } - private static final String COMPATIBLE_MC_VERSION = "1.7.10"; + private static final String COMPATIBLE_MC_VERSION = "1.8"; } diff --git a/src/main/java/net/citizensnpcs/commands/NPCCommands.java b/src/main/java/net/citizensnpcs/commands/NPCCommands.java index aaa6c4d01..08a6f66fc 100644 --- a/src/main/java/net/citizensnpcs/commands/NPCCommands.java +++ b/src/main/java/net/citizensnpcs/commands/NPCCommands.java @@ -268,7 +268,7 @@ public class NPCCommands { } CommandSenderCreateNPCEvent event = sender instanceof Player ? new PlayerCreateNPCEvent((Player) sender, copy) - : new CommandSenderCreateNPCEvent(sender, copy); + : new CommandSenderCreateNPCEvent(sender, copy); Bukkit.getPluginManager().callEvent(event); if (event.isCancelled()) { event.getNPC().destroy(); @@ -344,7 +344,7 @@ public class NPCCommands { spawnLoc = args.getSenderLocation(); } CommandSenderCreateNPCEvent event = sender instanceof Player ? new PlayerCreateNPCEvent((Player) sender, npc) - : new CommandSenderCreateNPCEvent(sender, npc); + : new CommandSenderCreateNPCEvent(sender, npc); Bukkit.getPluginManager().callEvent(event); if (event.isCancelled()) { npc.destroy(); @@ -1021,7 +1021,7 @@ public class NPCCommands { @Requirements(selected = true, ownership = true, types = { EntityType.CREEPER }) public void power(CommandContext args, CommandSender sender, NPC npc) { Messaging - .sendTr(sender, npc.getTrait(Powered.class).toggle() ? Messages.POWERED_SET : Messages.POWERED_STOPPED); + .sendTr(sender, npc.getTrait(Powered.class).toggle() ? Messages.POWERED_SET : Messages.POWERED_STOPPED); } @Command( @@ -1044,7 +1044,7 @@ public class NPCCommands { } @Command(aliases = { "npc" }, usage = "remove|rem (all|id|name)", desc = "Remove a NPC", modifiers = { "remove", - "rem" }, min = 1, max = 2) + "rem" }, min = 1, max = 2) @Requirements public void remove(final CommandContext args, final CommandSender sender, NPC npc) throws CommandException { if (args.argsLength() == 2) { diff --git a/src/main/java/net/citizensnpcs/npc/EntityControllers.java b/src/main/java/net/citizensnpcs/npc/EntityControllers.java index fa23a642a..500d649fe 100644 --- a/src/main/java/net/citizensnpcs/npc/EntityControllers.java +++ b/src/main/java/net/citizensnpcs/npc/EntityControllers.java @@ -10,8 +10,10 @@ import net.citizensnpcs.npc.entity.CowController; import net.citizensnpcs.npc.entity.CreeperController; import net.citizensnpcs.npc.entity.EnderDragonController; import net.citizensnpcs.npc.entity.EndermanController; +import net.citizensnpcs.npc.entity.EndermiteController; import net.citizensnpcs.npc.entity.GhastController; import net.citizensnpcs.npc.entity.GiantController; +import net.citizensnpcs.npc.entity.GuardianController; import net.citizensnpcs.npc.entity.HorseController; import net.citizensnpcs.npc.entity.HumanController; import net.citizensnpcs.npc.entity.IronGolemController; @@ -20,6 +22,7 @@ import net.citizensnpcs.npc.entity.MushroomCowController; import net.citizensnpcs.npc.entity.OcelotController; import net.citizensnpcs.npc.entity.PigController; import net.citizensnpcs.npc.entity.PigZombieController; +import net.citizensnpcs.npc.entity.RabbitController; import net.citizensnpcs.npc.entity.SheepController; import net.citizensnpcs.npc.entity.SilverfishController; import net.citizensnpcs.npc.entity.SkeletonController; @@ -32,6 +35,7 @@ import net.citizensnpcs.npc.entity.WitchController; import net.citizensnpcs.npc.entity.WitherController; import net.citizensnpcs.npc.entity.WolfController; import net.citizensnpcs.npc.entity.ZombieController; +import net.citizensnpcs.npc.entity.nonliving.ArmorStandController; import net.citizensnpcs.npc.entity.nonliving.ArrowController; import net.citizensnpcs.npc.entity.nonliving.BoatController; import net.citizensnpcs.npc.entity.nonliving.EggController; @@ -89,6 +93,7 @@ public class EntityControllers { static { TYPES.put(EntityType.ARROW, ArrowController.class); + TYPES.put(EntityType.ARMOR_STAND, ArmorStandController.class); TYPES.put(EntityType.BAT, BatController.class); TYPES.put(EntityType.BLAZE, BlazeController.class); TYPES.put(EntityType.BOAT, BoatController.class); @@ -103,12 +108,14 @@ public class EntityControllers { TYPES.put(EntityType.ENDER_PEARL, EnderPearlController.class); TYPES.put(EntityType.ENDER_SIGNAL, EnderSignalController.class); TYPES.put(EntityType.ENDERMAN, EndermanController.class); + TYPES.put(EntityType.ENDERMITE, EndermiteController.class); TYPES.put(EntityType.FALLING_BLOCK, FallingBlockController.class); TYPES.put(EntityType.FIREWORK, FireworkController.class); TYPES.put(EntityType.FIREBALL, LargeFireballController.class); TYPES.put(EntityType.FISHING_HOOK, FishingHookController.class); TYPES.put(EntityType.GHAST, GhastController.class); TYPES.put(EntityType.GIANT, GiantController.class); + TYPES.put(EntityType.GUARDIAN, GuardianController.class); TYPES.put(EntityType.HORSE, HorseController.class); TYPES.put(EntityType.IRON_GOLEM, IronGolemController.class); TYPES.put(EntityType.ITEM_FRAME, ItemFrameController.class); @@ -126,6 +133,7 @@ public class EntityControllers { TYPES.put(EntityType.PIG, PigController.class); TYPES.put(EntityType.PIG_ZOMBIE, PigZombieController.class); TYPES.put(EntityType.PLAYER, HumanController.class); + TYPES.put(EntityType.RABBIT, RabbitController.class); TYPES.put(EntityType.SHEEP, SheepController.class); TYPES.put(EntityType.SILVERFISH, SilverfishController.class); TYPES.put(EntityType.SKELETON, SkeletonController.class); diff --git a/src/main/java/net/citizensnpcs/npc/entity/EndermanController.java b/src/main/java/net/citizensnpcs/npc/entity/EndermanController.java index 5766ae80e..bc02785b4 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/EndermanController.java +++ b/src/main/java/net/citizensnpcs/npc/entity/EndermanController.java @@ -45,7 +45,6 @@ public class EndermanController extends MobEntityController { } public static class EntityEndermanNPC extends EntityEnderman implements NPCHolder { - private int jumpTicks; private final CitizensNPC npc; public EntityEndermanNPC(World world) { diff --git a/src/main/java/net/citizensnpcs/npc/entity/EndermiteController.java b/src/main/java/net/citizensnpcs/npc/entity/EndermiteController.java new file mode 100644 index 000000000..444266fb2 --- /dev/null +++ b/src/main/java/net/citizensnpcs/npc/entity/EndermiteController.java @@ -0,0 +1,185 @@ +package net.citizensnpcs.npc.entity; + +import net.citizensnpcs.api.event.NPCPushEvent; +import net.citizensnpcs.api.npc.NPC; +import net.citizensnpcs.npc.CitizensNPC; +import net.citizensnpcs.npc.MobEntityController; +import net.citizensnpcs.npc.ai.NPCHolder; +import net.citizensnpcs.util.NMS; +import net.citizensnpcs.util.Util; +import net.minecraft.server.v1_8_R1.Block; +import net.minecraft.server.v1_8_R1.BlockPosition; +import net.minecraft.server.v1_8_R1.EntityEndermite; +import net.minecraft.server.v1_8_R1.NBTTagCompound; +import net.minecraft.server.v1_8_R1.World; + +import org.bukkit.Bukkit; +import org.bukkit.craftbukkit.v1_8_R1.CraftServer; +import org.bukkit.craftbukkit.v1_8_R1.entity.CraftEndermite; +import org.bukkit.craftbukkit.v1_8_R1.entity.CraftEntity; +import org.bukkit.entity.Endermite; +import org.bukkit.util.Vector; + +public class EndermiteController extends MobEntityController { + public EndermiteController() { + super(EntityEndermiteNPC.class); + } + + @Override + public Endermite getBukkitEntity() { + return (Endermite) super.getBukkitEntity(); + } + + public static class EndermiteNPC extends CraftEndermite implements NPCHolder { + private final CitizensNPC npc; + + public EndermiteNPC(EntityEndermiteNPC entity) { + super((CraftServer) Bukkit.getServer(), entity); + this.npc = entity.npc; + } + + @Override + public NPC getNPC() { + return npc; + } + } + + public static class EntityEndermiteNPC extends EntityEndermite implements NPCHolder { + private final CitizensNPC npc; + + public EntityEndermiteNPC(World world) { + this(world, null); + } + + public EntityEndermiteNPC(World world, NPC npc) { + super(world); + this.npc = (CitizensNPC) npc; + if (npc != null) { + NMS.clearGoals(goalSelector, targetSelector); + } + } + + @Override + protected void a(double d0, boolean flag, Block block, BlockPosition blockposition) { + if (npc == null || !npc.isFlyable()) { + super.a(d0, flag, block, blockposition); + } + } + + @Override + protected String bn() { + return npc == null ? super.bn() : npc.data().get(NPC.HURT_SOUND_METADATA, super.bn()); + } + + @Override + protected String bo() { + return npc == null ? super.bo() : npc.data().get(NPC.DEATH_SOUND_METADATA, super.bo()); + } + + @Override + public boolean cb() { + if (npc == null) + return super.cb(); + boolean protectedDefault = npc.data().get(NPC.DEFAULT_PROTECTED_METADATA, true); + if (!protectedDefault || !npc.data().get(NPC.LEASH_PROTECTED_METADATA, protectedDefault)) + return super.cb(); + if (super.cb()) { + unleash(true, false); // clearLeash with client update + } + return false; // shouldLeash + } + + @Override + public void collide(net.minecraft.server.v1_8_R1.Entity entity) { + // this method is called by both the entities involved - cancelling + // it will not stop the NPC from moving. + super.collide(entity); + if (npc != null) + Util.callCollisionEvent(npc, entity.getBukkitEntity()); + } + + @Override + public boolean d(NBTTagCompound save) { + return npc == null ? super.d(save) : false; + } + + @Override + protected void D() { + if (npc == null) { + super.D(); + } + } + + @Override + public void doTick() { + super.doTick(); + if (npc != null) + npc.update(); + } + + @Override + public void e(float f, float f1) { + if (npc == null || !npc.isFlyable()) { + super.e(f, f1); + } + } + + @Override + public void g(double x, double y, double z) { + if (npc == null) { + super.g(x, y, z); + return; + } + if (NPCPushEvent.getHandlerList().getRegisteredListeners().length == 0) { + if (!npc.data().get(NPC.DEFAULT_PROTECTED_METADATA, true)) + super.g(x, y, z); + return; + } + Vector vector = new Vector(x, y, z); + NPCPushEvent event = Util.callPushEvent(npc, vector); + if (!event.isCancelled()) { + vector = event.getCollisionVector(); + super.g(vector.getX(), vector.getY(), vector.getZ()); + } + // when another entity collides, this method is called to push the + // NPC so we prevent it from doing anything if the event is + // cancelled. + } + + @Override + public void g(float f, float f1) { + if (npc == null || !npc.isFlyable()) { + super.g(f, f1); + } else { + NMS.flyingMoveLogic(this, f, f1); + } + } + + @Override + public CraftEntity getBukkitEntity() { + if (bukkitEntity == null && npc != null) + bukkitEntity = new EndermiteNPC(this); + return super.getBukkitEntity(); + } + + @Override + public NPC getNPC() { + return npc; + } + + @Override + public boolean j_() { + if (npc == null || !npc.isFlyable()) { + return super.j_(); + } else { + return false; + } + } + + @Override + protected String z() { + return npc == null || !npc.data().has(NPC.AMBIENT_SOUND_METADATA) ? super.z() : npc.data().get( + NPC.AMBIENT_SOUND_METADATA, super.z()); + } + } +} \ No newline at end of file diff --git a/src/main/java/net/citizensnpcs/npc/entity/EntityHumanNPC.java b/src/main/java/net/citizensnpcs/npc/entity/EntityHumanNPC.java index 8652637e4..cce152e93 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/EntityHumanNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/EntityHumanNPC.java @@ -64,10 +64,10 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder { public EntityHumanNPC(MinecraftServer minecraftServer, WorldServer world, GameProfile gameProfile, PlayerInteractManager playerInteractManager, NPC npc) { super(minecraftServer, world, gameProfile, playerInteractManager); - playerInteractManager.setGameMode(EnumGamemode.SURVIVAL); this.npc = (CitizensNPC) npc; if (npc != null) { + playerInteractManager.setGameMode(EnumGamemode.SURVIVAL); initialise(minecraftServer); } } @@ -103,8 +103,9 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder { 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); @@ -129,8 +130,9 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder { @Override public CraftPlayer getBukkitEntity() { - if (npc != null && bukkitEntity == null) + if (npc != null && bukkitEntity == null) { bukkitEntity = new PlayerNPC(this); + } return super.getBukkitEntity(); } @@ -153,7 +155,7 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder { try { return (Packet) (removeFromPlayerList ? PLAYER_INFO_REMOVE_METHOD.invoke(null, ((CraftPlayer) player).getHandle()) : PLAYER_INFO_ADD_METHOD.invoke(null, - ((CraftPlayer) player).getHandle())); + ((CraftPlayer) player).getHandle())); } catch (Exception e) { } } @@ -176,15 +178,8 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder { conn = new EmptyNetworkManager(EnumProtocolDirection.CLIENTBOUND); playerConnection = new EmptyNetHandler(minecraftServer, conn, this); conn.a(playerConnection); - } catch (IOException e) { - // swallow - } - - NMS.setStepHeight(this, 1); // the default (0) breaks step climbing - - try { socket.close(); - } catch (IOException ex) { + } catch (IOException e) { // swallow } @@ -193,10 +188,12 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder { range = this.getAttributeMap().b(GenericAttributes.b); } range.setValue(Setting.DEFAULT_PATHFINDING_RANGE.asDouble()); + controllerJump = new PlayerControllerJump(this); controllerLook = new PlayerControllerLook(this); controllerMove = new PlayerControllerMove(this); navigation = new PlayerNavigation(this, world); + NMS.setStepHeight(this, 1); // the default (0) breaks step climbing } public boolean isNavigating() { @@ -246,15 +243,17 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHolder { // (onGround is normally updated by the client) } - if (Math.abs(motX) < EPSILON && Math.abs(motY) < EPSILON && Math.abs(motZ) < EPSILON) + if (Math.abs(motX) < EPSILON && Math.abs(motY) < EPSILON && Math.abs(motZ) < EPSILON) { motX = motY = motZ = 0; + } if (navigating) { if (!NMS.isNavigationFinished(navigation)) { NMS.updateNavigation(navigation); } moveOnCurrentHeading(); } else if (motX != 0 || motZ != 0 || motY != 0) { - e(0, 0); // is this necessary? it does controllable but sometimes + g(0, 0); // is this necessary? it does controllable but + // sometimes // players sink into the ground } diff --git a/src/main/java/net/citizensnpcs/npc/entity/GuardianController.java b/src/main/java/net/citizensnpcs/npc/entity/GuardianController.java new file mode 100644 index 000000000..62ba4fed1 --- /dev/null +++ b/src/main/java/net/citizensnpcs/npc/entity/GuardianController.java @@ -0,0 +1,199 @@ +package net.citizensnpcs.npc.entity; + +import net.citizensnpcs.api.event.NPCPushEvent; +import net.citizensnpcs.api.npc.NPC; +import net.citizensnpcs.npc.CitizensNPC; +import net.citizensnpcs.npc.MobEntityController; +import net.citizensnpcs.npc.ai.NPCHolder; +import net.citizensnpcs.util.NMS; +import net.citizensnpcs.util.Util; +import net.minecraft.server.v1_8_R1.Block; +import net.minecraft.server.v1_8_R1.BlockPosition; +import net.minecraft.server.v1_8_R1.EntityGuardian; +import net.minecraft.server.v1_8_R1.NBTTagCompound; +import net.minecraft.server.v1_8_R1.World; + +import org.bukkit.Bukkit; +import org.bukkit.craftbukkit.v1_8_R1.CraftServer; +import org.bukkit.craftbukkit.v1_8_R1.entity.CraftEntity; +import org.bukkit.craftbukkit.v1_8_R1.entity.CraftGuardian; +import org.bukkit.entity.Guardian; +import org.bukkit.util.Vector; + +public class GuardianController extends MobEntityController { + public GuardianController() { + super(EntityGuardianNPC.class); + } + + @Override + public Guardian getBukkitEntity() { + return (Guardian) super.getBukkitEntity(); + } + + public static class EntityGuardianNPC extends EntityGuardian implements NPCHolder { + private final CitizensNPC npc; + + public EntityGuardianNPC(World world) { + this(world, null); + } + + public EntityGuardianNPC(World world, NPC npc) { + super(world); + this.npc = (CitizensNPC) npc; + if (npc != null) { + NMS.clearGoals(goalSelector, targetSelector); + + } + } + + @Override + public void a(boolean flag) { + float oldw = width; + float oldl = length; + super.a(flag); + if (oldw != width || oldl != length) { + this.setPosition(locX - 0.01, locY, locZ - 0.01); + this.setPosition(locX + 0.01, locY, locZ + 0.01); + } + } + + @Override + protected void a(double d0, boolean flag, Block block, BlockPosition blockposition) { + if (npc == null || !npc.isFlyable()) { + super.a(d0, flag, block, blockposition); + } + } + + @Override + protected String bn() { + return npc == null ? super.bn() : npc.data().get(NPC.HURT_SOUND_METADATA, super.bn()); + } + + @Override + protected String bo() { + return npc == null ? super.bo() : npc.data().get(NPC.DEATH_SOUND_METADATA, super.bo()); + } + + @Override + public boolean cb() { + if (npc == null) + return super.cb(); + boolean protectedDefault = npc.data().get(NPC.DEFAULT_PROTECTED_METADATA, true); + if (!protectedDefault || !npc.data().get(NPC.LEASH_PROTECTED_METADATA, protectedDefault)) + return super.cb(); + if (super.cb()) { + unleash(true, false); // clearLeash with client update + } + return false; // shouldLeash + } + + @Override + public void collide(net.minecraft.server.v1_8_R1.Entity entity) { + // this method is called by both the entities involved - cancelling + // it will not stop the NPC from moving. + super.collide(entity); + if (npc != null) { + Util.callCollisionEvent(npc, entity.getBukkitEntity()); + } + } + + @Override + public boolean d(NBTTagCompound save) { + return npc == null ? super.d(save) : false; + } + + @Override + protected void D() { + if (npc == null) { + super.D(); + } + } + + @Override + public void doTick() { + super.doTick(); + if (npc != null) { + npc.update(); + } + } + + @Override + public void e(float f, float f1) { + if (npc == null || !npc.isFlyable()) { + super.e(f, f1); + } + } + + @Override + public void g(double x, double y, double z) { + if (npc == null) { + super.g(x, y, z); + return; + } + if (NPCPushEvent.getHandlerList().getRegisteredListeners().length == 0) { + if (!npc.data().get(NPC.DEFAULT_PROTECTED_METADATA, true)) + super.g(x, y, z); + return; + } + Vector vector = new Vector(x, y, z); + NPCPushEvent event = Util.callPushEvent(npc, vector); + if (!event.isCancelled()) { + vector = event.getCollisionVector(); + super.g(vector.getX(), vector.getY(), vector.getZ()); + } + // when another entity collides, this method is called to push the + // NPC so we prevent it from doing anything if the event is + // cancelled. + } + + @Override + public void g(float f, float f1) { + if (npc == null || !npc.isFlyable()) { + super.g(f, f1); + } else { + NMS.flyingMoveLogic(this, f, f1); + } + } + + @Override + public CraftEntity getBukkitEntity() { + if (bukkitEntity == null && npc != null) + bukkitEntity = new GuardianNPC(this); + return super.getBukkitEntity(); + } + + @Override + public NPC getNPC() { + return npc; + } + + @Override + public boolean j_() { + if (npc == null || !npc.isFlyable()) { + return super.j_(); + } else { + return false; + } + } + + @Override + protected String z() { + return npc == null || !npc.data().has(NPC.AMBIENT_SOUND_METADATA) ? super.z() : npc.data().get( + NPC.AMBIENT_SOUND_METADATA, super.z()); + } + } + + public static class GuardianNPC extends CraftGuardian implements NPCHolder { + private final CitizensNPC npc; + + public GuardianNPC(EntityGuardianNPC entity) { + super((CraftServer) Bukkit.getServer(), entity); + this.npc = entity.npc; + } + + @Override + public NPC getNPC() { + return npc; + } + } +} \ No newline at end of file diff --git a/src/main/java/net/citizensnpcs/npc/entity/RabbitController.java b/src/main/java/net/citizensnpcs/npc/entity/RabbitController.java new file mode 100644 index 000000000..95e788eed --- /dev/null +++ b/src/main/java/net/citizensnpcs/npc/entity/RabbitController.java @@ -0,0 +1,199 @@ +package net.citizensnpcs.npc.entity; + +import net.citizensnpcs.api.event.NPCPushEvent; +import net.citizensnpcs.api.npc.NPC; +import net.citizensnpcs.npc.CitizensNPC; +import net.citizensnpcs.npc.MobEntityController; +import net.citizensnpcs.npc.ai.NPCHolder; +import net.citizensnpcs.util.NMS; +import net.citizensnpcs.util.Util; +import net.minecraft.server.v1_8_R1.Block; +import net.minecraft.server.v1_8_R1.BlockPosition; +import net.minecraft.server.v1_8_R1.EntityRabbit; +import net.minecraft.server.v1_8_R1.NBTTagCompound; +import net.minecraft.server.v1_8_R1.World; + +import org.bukkit.Bukkit; +import org.bukkit.craftbukkit.v1_8_R1.CraftServer; +import org.bukkit.craftbukkit.v1_8_R1.entity.CraftEntity; +import org.bukkit.craftbukkit.v1_8_R1.entity.CraftRabbit; +import org.bukkit.entity.Rabbit; +import org.bukkit.util.Vector; + +public class RabbitController extends MobEntityController { + public RabbitController() { + super(EntityRabbitNPC.class); + } + + @Override + public Rabbit getBukkitEntity() { + return (Rabbit) super.getBukkitEntity(); + } + + public static class EntityRabbitNPC extends EntityRabbit implements NPCHolder { + private final CitizensNPC npc; + + public EntityRabbitNPC(World world) { + this(world, null); + } + + public EntityRabbitNPC(World world, NPC npc) { + super(world); + this.npc = (CitizensNPC) npc; + if (npc != null) { + NMS.clearGoals(goalSelector, targetSelector); + + } + } + + @Override + public void a(boolean flag) { + float oldw = width; + float oldl = length; + super.a(flag); + if (oldw != width || oldl != length) { + this.setPosition(locX - 0.01, locY, locZ - 0.01); + this.setPosition(locX + 0.01, locY, locZ + 0.01); + } + } + + @Override + protected void a(double d0, boolean flag, Block block, BlockPosition blockposition) { + if (npc == null || !npc.isFlyable()) { + super.a(d0, flag, block, blockposition); + } + } + + @Override + protected String bn() { + return npc == null ? super.bn() : npc.data().get(NPC.HURT_SOUND_METADATA, super.bn()); + } + + @Override + protected String bo() { + return npc == null ? super.bo() : npc.data().get(NPC.DEATH_SOUND_METADATA, super.bo()); + } + + @Override + public boolean cb() { + if (npc == null) + return super.cb(); + boolean protectedDefault = npc.data().get(NPC.DEFAULT_PROTECTED_METADATA, true); + if (!protectedDefault || !npc.data().get(NPC.LEASH_PROTECTED_METADATA, protectedDefault)) + return super.cb(); + if (super.cb()) { + unleash(true, false); // clearLeash with client update + } + return false; // shouldLeash + } + + @Override + public void collide(net.minecraft.server.v1_8_R1.Entity entity) { + // this method is called by both the entities involved - cancelling + // it will not stop the NPC from moving. + super.collide(entity); + if (npc != null) { + Util.callCollisionEvent(npc, entity.getBukkitEntity()); + } + } + + @Override + public boolean d(NBTTagCompound save) { + return npc == null ? super.d(save) : false; + } + + @Override + protected void D() { + if (npc == null) { + super.D(); + } + } + + @Override + public void doTick() { + super.doTick(); + if (npc != null) { + npc.update(); + } + } + + @Override + public void e(float f, float f1) { + if (npc == null || !npc.isFlyable()) { + super.e(f, f1); + } + } + + @Override + public void g(double x, double y, double z) { + if (npc == null) { + super.g(x, y, z); + return; + } + if (NPCPushEvent.getHandlerList().getRegisteredListeners().length == 0) { + if (!npc.data().get(NPC.DEFAULT_PROTECTED_METADATA, true)) + super.g(x, y, z); + return; + } + Vector vector = new Vector(x, y, z); + NPCPushEvent event = Util.callPushEvent(npc, vector); + if (!event.isCancelled()) { + vector = event.getCollisionVector(); + super.g(vector.getX(), vector.getY(), vector.getZ()); + } + // when another entity collides, this method is called to push the + // NPC so we prevent it from doing anything if the event is + // cancelled. + } + + @Override + public void g(float f, float f1) { + if (npc == null || !npc.isFlyable()) { + super.g(f, f1); + } else { + NMS.flyingMoveLogic(this, f, f1); + } + } + + @Override + public CraftEntity getBukkitEntity() { + if (bukkitEntity == null && npc != null) + bukkitEntity = new RabbitNPC(this); + return super.getBukkitEntity(); + } + + @Override + public NPC getNPC() { + return npc; + } + + @Override + public boolean j_() { + if (npc == null || !npc.isFlyable()) { + return super.j_(); + } else { + return false; + } + } + + @Override + protected String z() { + return npc == null || !npc.data().has(NPC.AMBIENT_SOUND_METADATA) ? super.z() : npc.data().get( + NPC.AMBIENT_SOUND_METADATA, super.z()); + } + } + + public static class RabbitNPC extends CraftRabbit implements NPCHolder { + private final CitizensNPC npc; + + public RabbitNPC(EntityRabbitNPC entity) { + super((CraftServer) Bukkit.getServer(), entity); + this.npc = entity.npc; + } + + @Override + public NPC getNPC() { + return npc; + } + } +} \ No newline at end of file diff --git a/src/main/java/net/citizensnpcs/npc/entity/WitherController.java b/src/main/java/net/citizensnpcs/npc/entity/WitherController.java index c1df63ec5..9561f497f 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/WitherController.java +++ b/src/main/java/net/citizensnpcs/npc/entity/WitherController.java @@ -29,7 +29,6 @@ public class WitherController extends MobEntityController { } public static class EntityWitherNPC extends EntityWither implements NPCHolder { - private int jumpTicks; private final CitizensNPC npc; public EntityWitherNPC(World world) { diff --git a/src/main/java/net/citizensnpcs/npc/entity/nonliving/ArmorStandController.java b/src/main/java/net/citizensnpcs/npc/entity/nonliving/ArmorStandController.java new file mode 100644 index 000000000..9d0ad3f52 --- /dev/null +++ b/src/main/java/net/citizensnpcs/npc/entity/nonliving/ArmorStandController.java @@ -0,0 +1,121 @@ +package net.citizensnpcs.npc.entity.nonliving; + +import net.citizensnpcs.api.event.NPCPushEvent; +import net.citizensnpcs.api.npc.NPC; +import net.citizensnpcs.npc.CitizensNPC; +import net.citizensnpcs.npc.MobEntityController; +import net.citizensnpcs.npc.ai.NPCHolder; +import net.citizensnpcs.util.Util; +import net.minecraft.server.v1_8_R1.EntityArmorStand; +import net.minecraft.server.v1_8_R1.EntityHuman; +import net.minecraft.server.v1_8_R1.NBTTagCompound; +import net.minecraft.server.v1_8_R1.Vec3D; +import net.minecraft.server.v1_8_R1.World; + +import org.bukkit.Bukkit; +import org.bukkit.craftbukkit.v1_8_R1.CraftServer; +import org.bukkit.craftbukkit.v1_8_R1.entity.CraftArmorStand; +import org.bukkit.craftbukkit.v1_8_R1.entity.CraftEntity; +import org.bukkit.entity.ArmorStand; +import org.bukkit.util.Vector; + +public class ArmorStandController extends MobEntityController { + public ArmorStandController() { + super(EntityArmorStandNPC.class); + } + + @Override + public ArmorStand getBukkitEntity() { + return (ArmorStand) super.getBukkitEntity(); + } + + public static class ArmorStandNPC extends CraftArmorStand implements NPCHolder { + private final CitizensNPC npc; + + public ArmorStandNPC(EntityArmorStandNPC entity) { + super((CraftServer) Bukkit.getServer(), entity); + this.npc = entity.npc; + } + + @Override + public NPC getNPC() { + return npc; + } + } + + public static class EntityArmorStandNPC extends EntityArmorStand implements NPCHolder { + private final CitizensNPC npc; + + public EntityArmorStandNPC(World world) { + this(world, null); + } + + public EntityArmorStandNPC(World world, NPC npc) { + super(world); + this.npc = (CitizensNPC) npc; + } + + @Override + public boolean a(EntityHuman paramEntityHuman, Vec3D paramVec3D) { + return true; + } + + @Override + public void collide(net.minecraft.server.v1_8_R1.Entity entity) { + // this method is called by both the entities involved - cancelling + // it will not stop the NPC from moving. + super.collide(entity); + if (npc != null) { + Util.callCollisionEvent(npc, entity.getBukkitEntity()); + } + } + + @Override + public boolean d(NBTTagCompound save) { + return npc == null ? super.d(save) : false; + } + + @Override + public void g(double x, double y, double z) { + if (npc == null) { + super.g(x, y, z); + return; + } + if (NPCPushEvent.getHandlerList().getRegisteredListeners().length == 0) { + if (!npc.data().get(NPC.DEFAULT_PROTECTED_METADATA, true)) + super.g(x, y, z); + return; + } + Vector vector = new Vector(x, y, z); + NPCPushEvent event = Util.callPushEvent(npc, vector); + if (!event.isCancelled()) { + vector = event.getCollisionVector(); + super.g(vector.getX(), vector.getY(), vector.getZ()); + } + // when another entity collides, this method is called to push the + // NPC so we prevent it from doing anything if the event is + // cancelled. + } + + @Override + public CraftEntity getBukkitEntity() { + if (bukkitEntity == null && npc != null) { + bukkitEntity = new ArmorStandNPC(this); + } + return super.getBukkitEntity(); + } + + @Override + public NPC getNPC() { + return npc; + } + + @Override + public void s_() { + super.s_(); + if (npc != null) { + npc.update(); + } + } + } +} \ No newline at end of file diff --git a/src/main/java/net/citizensnpcs/npc/entity/nonliving/FishingHookController.java b/src/main/java/net/citizensnpcs/npc/entity/nonliving/FishingHookController.java index a259cb195..00a326339 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/nonliving/FishingHookController.java +++ b/src/main/java/net/citizensnpcs/npc/entity/nonliving/FishingHookController.java @@ -39,11 +39,6 @@ public class FishingHookController extends MobEntityController { this.npc = (CitizensNPC) npc; } - @Override - public boolean d(NBTTagCompound save) { - return npc == null ? super.d(save) : false; - } - @Override public void collide(net.minecraft.server.v1_8_R1.Entity entity) { // this method is called by both the entities involved - cancelling @@ -54,6 +49,11 @@ public class FishingHookController extends MobEntityController { } } + @Override + public boolean d(NBTTagCompound save) { + return npc == null ? super.d(save) : false; + } + @Override public void g(double x, double y, double z) { if (npc == null) {