From 05a7ee468b38e7fdb370c68df8fb10048e5c490e Mon Sep 17 00:00:00 2001 From: fullwall Date: Thu, 19 Apr 2012 22:57:54 +0800 Subject: [PATCH] Simple controllable command --- .../command/command/NPCCommands.java | 21 ++++++++ .../net/citizensnpcs/trait/Controllable.java | 51 ++++++++++++++----- 2 files changed, 58 insertions(+), 14 deletions(-) diff --git a/src/main/java/net/citizensnpcs/command/command/NPCCommands.java b/src/main/java/net/citizensnpcs/command/command/NPCCommands.java index a6d82d474..2bef6d0d8 100644 --- a/src/main/java/net/citizensnpcs/command/command/NPCCommands.java +++ b/src/main/java/net/citizensnpcs/command/command/NPCCommands.java @@ -17,9 +17,11 @@ import net.citizensnpcs.command.CommandContext; import net.citizensnpcs.command.Requirements; import net.citizensnpcs.command.exception.CommandException; import net.citizensnpcs.command.exception.NoPermissionsException; +import net.citizensnpcs.npc.CitizensNPC; import net.citizensnpcs.npc.CitizensNPCManager; import net.citizensnpcs.npc.CitizensTraitManager; import net.citizensnpcs.trait.Age; +import net.citizensnpcs.trait.Controllable; import net.citizensnpcs.trait.CurrentLocation; import net.citizensnpcs.trait.LookClose; import net.citizensnpcs.trait.Powered; @@ -85,6 +87,25 @@ public class NPCCommands { Messaging.send(player, "Age " + (trait.toggle() ? "locked" : "unlocked") + "."); } + @Command( + aliases = { "npc" }, + usage = "controllable", + desc = "Toggles whether the NPC can be ridden and controlled", + modifiers = { "controllable" }, + min = 2, + max = 2, + permission = "npc.controllable") + public void toggleControllable(CommandContext args, Player player, NPC npc) { + if (npc.hasTrait(Controllable.class)) { + npc.removeTrait(Controllable.class); + Messaging.send(player, StringHelper.wrap(npc.getName()) + " can no longer be controlled."); + } else { + npc.addTrait(new Controllable((CitizensNPC) npc)); + Messaging.send(player, StringHelper.wrap(npc.getName()) + " can now be controlled."); + } + + } + @Command( aliases = { "npc" }, usage = "character [character]", diff --git a/src/main/java/net/citizensnpcs/trait/Controllable.java b/src/main/java/net/citizensnpcs/trait/Controllable.java index 3cd05f912..ffa72c3a9 100644 --- a/src/main/java/net/citizensnpcs/trait/Controllable.java +++ b/src/main/java/net/citizensnpcs/trait/Controllable.java @@ -5,40 +5,63 @@ import net.citizensnpcs.api.exception.NPCLoadException; import net.citizensnpcs.api.trait.Trait; import net.citizensnpcs.api.util.DataKey; import net.citizensnpcs.npc.CitizensNPC; +import net.minecraft.server.EntityPlayer; import org.bukkit.craftbukkit.entity.CraftPlayer; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; +import org.bukkit.event.player.PlayerInteractEvent; +//TODO: reduce reliance on CitizensNPC public class Controllable extends Trait implements Runnable, Listener { private final CitizensNPC npc; - private boolean mounted; public Controllable(CitizensNPC npc) { this.npc = npc; } - @EventHandler - public void onRightClick(NPCRightClickEvent event) { - if (!event.getNPC().equals(npc) || npc.getBukkitEntity().getPassenger() != null) + private void jump() { + if (!npc.getHandle().onGround) return; - ((CraftPlayer) event.getClicker()).getHandle().setPassengerOf(npc.getHandle()); - mounted = true; - } - - @Override - public void run() { - if (!mounted) - return; - npc.getHandle().motX += npc.getHandle().passenger.motX; - npc.getHandle().motZ += npc.getHandle().passenger.motZ; + npc.getHandle().motY = JUMP_VELOCITY; } @Override public void load(DataKey key) throws NPCLoadException { } + @EventHandler + public void onPlayerInteract(PlayerInteractEvent event) { + EntityPlayer handle = ((CraftPlayer) event.getPlayer()).getHandle(); + if (event.getAction() == Action.PHYSICAL || !handle.equals(npc.getHandle().passenger)) + return; + if (event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.LEFT_CLICK_BLOCK) { + jump(); + } else { + event.getPlayer().leaveVehicle(); + } + } + + @EventHandler + public void onRightClick(NPCRightClickEvent event) { + if (!event.getNPC().equals(npc) || npc.getHandle().passenger != null) + return; + EntityPlayer handle = ((CraftPlayer) event.getClicker()).getHandle(); + handle.setPassengerOf(npc.getHandle()); + } + + @Override + public void run() { + if (!npc.isSpawned() || npc.getHandle().passenger == null) + return; + npc.getHandle().motX += npc.getHandle().passenger.motX; + npc.getHandle().motZ += npc.getHandle().passenger.motZ; + } + @Override public void save(DataKey key) { } + + private static final double JUMP_VELOCITY = 0.4; }