Simple controllable command

This commit is contained in:
fullwall 2012-04-19 22:57:54 +08:00
parent 7a53a0c163
commit 05a7ee468b
2 changed files with 58 additions and 14 deletions

View File

@ -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, "<a>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]",

View File

@ -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;
}