Skip to content

Commit

Permalink
Add new entity controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Nov 29, 2014
1 parent 22124ea commit 54418e3
Show file tree
Hide file tree
Showing 11 changed files with 735 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/main/java/net/citizensnpcs/Citizens.java
Original file line number Diff line number Diff line change
Expand Up @@ -454,5 +454,5 @@ private boolean suggestClosestModifier(CommandSender sender, String command, Str
return false;
}

private static final String COMPATIBLE_MC_VERSION = "1.7.10";
private static final String COMPATIBLE_MC_VERSION = "1.8";
}
8 changes: 4 additions & 4 deletions src/main/java/net/citizensnpcs/commands/NPCCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public void copy(CommandContext args, CommandSender sender, NPC npc) throws Comm
}

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();
Expand Down Expand Up @@ -344,7 +344,7 @@ public void create(CommandContext args, CommandSender sender, NPC npc) throws Co
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();
Expand Down Expand Up @@ -1021,7 +1021,7 @@ public void pose(CommandContext args, CommandSender sender, NPC npc) throws Comm
@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(
Expand All @@ -1044,7 +1044,7 @@ public void profession(CommandContext args, CommandSender sender, NPC npc) throw
}

@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) {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/net/citizensnpcs/npc/EntityControllers.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
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;
Expand All @@ -20,6 +22,7 @@
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;
Expand All @@ -32,6 +35,7 @@
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;
Expand Down Expand Up @@ -89,6 +93,7 @@ public static void setEntityControllerForType(EntityType type, Class<? extends E

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);
Expand All @@ -103,12 +108,14 @@ public static void setEntityControllerForType(EntityType type, Class<? extends E
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);
Expand All @@ -126,6 +133,7 @@ public static void setEntityControllerForType(EntityType type, Class<? extends E
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public NPC getNPC() {
}

public static class EntityEndermanNPC extends EntityEnderman implements NPCHolder {
private int jumpTicks;
private final CitizensNPC npc;

public EntityEndermanNPC(World world) {
Expand Down
185 changes: 185 additions & 0 deletions src/main/java/net/citizensnpcs/npc/entity/EndermiteController.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
27 changes: 13 additions & 14 deletions src/main/java/net/citizensnpcs/npc/entity/EntityHumanNPC.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -103,8 +103,9 @@ public void g(double x, double y, double z) {
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);
Expand All @@ -129,8 +130,9 @@ public void g(float f, float f1) {

@Override
public CraftPlayer getBukkitEntity() {
if (npc != null && bukkitEntity == null)
if (npc != null && bukkitEntity == null) {
bukkitEntity = new PlayerNPC(this);
}
return super.getBukkitEntity();
}

Expand All @@ -153,7 +155,7 @@ private Packet getListPacket(Player player, boolean removeFromPlayerList) {
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) {
}
}
Expand All @@ -176,15 +178,8 @@ private void initialise(MinecraftServer minecraftServer) {
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
}

Expand All @@ -193,10 +188,12 @@ private void initialise(MinecraftServer minecraftServer) {
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() {
Expand Down Expand Up @@ -246,15 +243,17 @@ public void s_() {
// (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
}

Expand Down
Loading

0 comments on commit 54418e3

Please sign in to comment.