Citizens2/src/main/java/net/citizensnpcs/trait/Controllable.java

89 lines
2.9 KiB
Java
Raw Normal View History

2012-04-19 05:52:07 +02:00
package net.citizensnpcs.trait;
2012-05-31 08:31:31 +02:00
import net.citizensnpcs.api.abstraction.EventHandler;
2012-06-23 12:01:28 +02:00
import net.citizensnpcs.api.abstraction.Listener;
2012-05-26 17:46:57 +02:00
import net.citizensnpcs.api.attachment.Attachment;
2012-04-19 05:52:07 +02:00
import net.citizensnpcs.api.event.NPCRightClickEvent;
import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.npc.NPC;
2012-04-19 05:52:07 +02:00
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.npc.CitizensNPC;
2012-06-23 12:01:28 +02:00
import net.minecraft.server.EntityLiving;
import net.minecraft.server.EntityPlayer;
2012-04-19 05:52:07 +02:00
2012-06-23 12:01:28 +02:00
import org.bukkit.craftbukkit.entity.CraftPlayer;
2012-04-19 05:52:07 +02:00
2012-04-19 16:57:54 +02:00
//TODO: reduce reliance on CitizensNPC
2012-05-26 17:46:57 +02:00
public class Controllable extends Attachment implements Runnable, Listener, Toggleable {
2012-04-19 05:52:07 +02:00
private final CitizensNPC npc;
private boolean enabled;
2012-04-19 05:52:07 +02:00
public Controllable(NPC npc) {
this.npc = (CitizensNPC) npc;
2012-04-19 05:52:07 +02:00
}
2012-04-19 16:57:54 +02:00
private void jump() {
boolean allowed = npc.getHandle().onGround;
if (!allowed)
2012-04-19 16:57:54 +02:00
return;
npc.getHandle().motY = JUMP_VELOCITY;
// TODO: make jumping work in liquid or make liquids float the npc
2012-04-19 16:57:54 +02:00
}
@Override
public void load(DataKey key) throws NPCLoadException {
2012-04-26 11:01:12 +02:00
enabled = key.getBoolean("");
2012-04-19 16:57:54 +02:00
}
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
if (!npc.isSpawned() || !enabled)
return;
2012-04-19 16:57:54 +02:00
EntityPlayer handle = ((CraftPlayer) event.getPlayer()).getHandle();
Action performed = event.getAction();
if (performed == Action.PHYSICAL || !handle.equals(npc.getHandle().passenger))
2012-04-19 16:57:54 +02:00
return;
if (performed == Action.LEFT_CLICK_AIR || performed == Action.LEFT_CLICK_BLOCK) {
2012-04-19 16:57:54 +02:00
jump();
}
}
2012-04-19 05:52:07 +02:00
@EventHandler
public void onRightClick(NPCRightClickEvent event) {
if (!npc.isSpawned() || !event.getNPC().equals(npc))
2012-04-19 05:52:07 +02:00
return;
2012-04-19 16:57:54 +02:00
EntityPlayer handle = ((CraftPlayer) event.getClicker()).getHandle();
if (npc.getHandle().passenger != null) {
if (npc.getHandle().passenger == handle) {
event.getClicker().leaveVehicle();
}
return;
}
2012-04-19 16:57:54 +02:00
handle.setPassengerOf(npc.getHandle());
2012-04-19 05:52:07 +02:00
}
@Override
public void run() {
2012-04-19 16:57:54 +02:00
if (!npc.isSpawned() || npc.getHandle().passenger == null)
2012-04-19 05:52:07 +02:00
return;
EntityLiving handle = npc.getHandle();
boolean onGround = handle.onGround;
handle.motX += handle.passenger.motX * (onGround ? GROUND_SPEED : AIR_SPEED);
handle.motZ += handle.passenger.motZ * (onGround ? GROUND_SPEED : AIR_SPEED);
2012-04-19 05:52:07 +02:00
}
@Override
public void save(DataKey key) {
key.setBoolean("enabled", enabled);
2012-04-19 05:52:07 +02:00
}
2012-04-19 16:57:54 +02:00
private static final double GROUND_SPEED = 4;
private static final double AIR_SPEED = 1.5;
private static final double JUMP_VELOCITY = 0.6;
@Override
public boolean toggle() {
return (enabled = !enabled);
}
2012-04-19 05:52:07 +02:00
}