2012-10-28 05:37:37 +01:00
|
|
|
package net.citizensnpcs.npc.entity;
|
|
|
|
|
2015-11-24 18:55:12 +01:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.craftbukkit.v1_8_R3.CraftServer;
|
|
|
|
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftBat;
|
|
|
|
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftEntity;
|
|
|
|
import org.bukkit.entity.Bat;
|
|
|
|
import org.bukkit.util.Vector;
|
|
|
|
|
2016-02-02 14:10:15 +01:00
|
|
|
import net.citizensnpcs.api.event.NPCEnderTeleportEvent;
|
2012-10-28 05:37:37 +01:00
|
|
|
import net.citizensnpcs.api.event.NPCPushEvent;
|
|
|
|
import net.citizensnpcs.api.npc.NPC;
|
|
|
|
import net.citizensnpcs.npc.CitizensNPC;
|
2012-12-14 15:07:25 +01:00
|
|
|
import net.citizensnpcs.npc.MobEntityController;
|
2012-10-28 05:37:37 +01:00
|
|
|
import net.citizensnpcs.npc.ai.NPCHolder;
|
|
|
|
import net.citizensnpcs.util.NMS;
|
|
|
|
import net.citizensnpcs.util.Util;
|
2015-05-19 11:59:38 +02:00
|
|
|
import net.minecraft.server.v1_8_R3.EntityBat;
|
|
|
|
import net.minecraft.server.v1_8_R3.NBTTagCompound;
|
|
|
|
import net.minecraft.server.v1_8_R3.World;
|
2012-10-28 05:37:37 +01:00
|
|
|
|
2012-12-08 13:08:58 +01:00
|
|
|
public class BatController extends MobEntityController {
|
|
|
|
public BatController() {
|
|
|
|
super(EntityBatNPC.class);
|
2012-10-28 05:37:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Bat getBukkitEntity() {
|
|
|
|
return (Bat) super.getBukkitEntity();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class BatNPC extends CraftBat implements NPCHolder {
|
|
|
|
private final CitizensNPC npc;
|
|
|
|
|
|
|
|
public BatNPC(EntityBatNPC entity) {
|
|
|
|
super((CraftServer) Bukkit.getServer(), entity);
|
|
|
|
this.npc = entity.npc;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public NPC getNPC() {
|
|
|
|
return npc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class EntityBatNPC extends EntityBat implements NPCHolder {
|
|
|
|
private final CitizensNPC npc;
|
|
|
|
|
|
|
|
public EntityBatNPC(World world) {
|
|
|
|
this(world, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public EntityBatNPC(World world, NPC npc) {
|
|
|
|
super(world);
|
|
|
|
this.npc = (CitizensNPC) npc;
|
|
|
|
if (npc != null) {
|
|
|
|
NMS.clearGoals(goalSelector, targetSelector);
|
2013-06-08 12:43:19 +02:00
|
|
|
setFlying(false);
|
2012-10-28 05:37:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-23 10:34:02 +01:00
|
|
|
@Override
|
2015-03-07 14:07:45 +01:00
|
|
|
protected String bo() {
|
2015-05-26 13:30:58 +02:00
|
|
|
return npc == null || !npc.data().has(NPC.HURT_SOUND_METADATA) ? super.bo()
|
|
|
|
: npc.data().get(NPC.HURT_SOUND_METADATA, super.bo());
|
2014-03-23 10:34:02 +01:00
|
|
|
}
|
|
|
|
|
2014-01-29 14:37:27 +01:00
|
|
|
@Override
|
2015-03-07 14:07:45 +01:00
|
|
|
protected String bp() {
|
2015-05-26 13:30:58 +02:00
|
|
|
return npc == null || !npc.data().has(NPC.DEATH_SOUND_METADATA) ? super.bp()
|
|
|
|
: npc.data().get(NPC.DEATH_SOUND_METADATA, super.bp());
|
2014-01-29 14:37:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-03-07 14:07:45 +01:00
|
|
|
public boolean cc() {
|
2014-03-23 10:34:02 +01:00
|
|
|
if (npc == null) {
|
2015-03-07 14:07:45 +01:00
|
|
|
return super.cc();
|
2013-07-09 18:08:19 +02:00
|
|
|
}
|
2013-07-09 16:01:00 +02:00
|
|
|
boolean protectedDefault = npc.data().get(NPC.DEFAULT_PROTECTED_METADATA, true);
|
|
|
|
if (!protectedDefault || !npc.data().get(NPC.LEASH_PROTECTED_METADATA, protectedDefault))
|
2015-03-07 14:07:45 +01:00
|
|
|
return super.cc();
|
|
|
|
if (super.cc()) {
|
2013-09-20 18:01:39 +02:00
|
|
|
unleash(true, false); // clearLeash with client update
|
2013-07-09 18:08:19 +02:00
|
|
|
}
|
2013-07-09 16:01:00 +02:00
|
|
|
return false; // shouldLeash
|
|
|
|
}
|
|
|
|
|
2012-10-28 05:37:37 +01:00
|
|
|
@Override
|
2015-05-19 11:59:38 +02:00
|
|
|
public void collide(net.minecraft.server.v1_8_R3.Entity entity) {
|
2012-10-28 05:37:37 +01:00
|
|
|
// this method is called by both the entities involved - cancelling
|
|
|
|
// it will not stop the NPC from moving.
|
|
|
|
super.collide(entity);
|
2015-03-07 14:07:45 +01:00
|
|
|
if (npc != null) {
|
2012-12-14 15:07:25 +01:00
|
|
|
Util.callCollisionEvent(npc, entity.getBukkitEntity());
|
2015-03-07 14:07:45 +01:00
|
|
|
}
|
2012-10-28 05:37:37 +01:00
|
|
|
}
|
|
|
|
|
2014-11-16 16:43:01 +01:00
|
|
|
@Override
|
|
|
|
public boolean d(NBTTagCompound save) {
|
|
|
|
return npc == null ? super.d(save) : false;
|
|
|
|
}
|
|
|
|
|
2014-11-28 16:22:11 +01:00
|
|
|
@Override
|
|
|
|
protected void D() {
|
|
|
|
if (npc == null) {
|
|
|
|
super.D();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void E() {
|
2014-12-04 07:38:05 +01:00
|
|
|
if (npc == null) {
|
|
|
|
super.E();
|
2015-03-07 14:07:45 +01:00
|
|
|
} else {
|
2014-12-04 07:38:05 +01:00
|
|
|
NMS.updateAI(this);
|
2014-11-28 16:22:11 +01:00
|
|
|
npc.update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-02 14:10:15 +01:00
|
|
|
@Override
|
|
|
|
public void enderTeleportTo(double d0, double d1, double d2) {
|
|
|
|
if (npc == null)
|
|
|
|
super.enderTeleportTo(d0, d1, d2);
|
|
|
|
NPCEnderTeleportEvent event = new NPCEnderTeleportEvent(npc);
|
|
|
|
Bukkit.getPluginManager().callEvent(event);
|
|
|
|
if (!event.isCancelled()) {
|
|
|
|
super.enderTeleportTo(d0, d1, d2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-28 05:37:37 +01:00
|
|
|
@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
|
2013-01-19 13:42:03 +01:00
|
|
|
public CraftEntity getBukkitEntity() {
|
2012-10-28 05:37:37 +01:00
|
|
|
if (bukkitEntity == null && npc != null)
|
|
|
|
bukkitEntity = new BatNPC(this);
|
|
|
|
return super.getBukkitEntity();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public NPC getNPC() {
|
|
|
|
return npc;
|
|
|
|
}
|
2012-10-28 05:54:18 +01:00
|
|
|
|
2013-06-08 12:43:19 +02:00
|
|
|
public void setFlying(boolean flying) {
|
2014-07-11 04:52:40 +02:00
|
|
|
setAsleep(flying);
|
2013-06-08 12:43:19 +02:00
|
|
|
}
|
2014-01-29 14:37:27 +01:00
|
|
|
|
|
|
|
@Override
|
2014-11-28 16:22:11 +01:00
|
|
|
protected String z() {
|
2015-05-26 13:30:58 +02:00
|
|
|
return npc == null || !npc.data().has(NPC.AMBIENT_SOUND_METADATA) ? super.z()
|
|
|
|
: npc.data().get(NPC.AMBIENT_SOUND_METADATA, super.z());
|
2014-02-20 04:57:24 +01:00
|
|
|
}
|
2012-10-28 05:37:37 +01:00
|
|
|
}
|
|
|
|
}
|