Fix bugs with controllables, waypoints, add waypoint editing slot, call damage event

This commit is contained in:
fullwall 2012-05-12 15:13:58 +08:00
parent 8a07f129b9
commit e6d76e696c
6 changed files with 81 additions and 32 deletions

View File

@ -1,6 +1,7 @@
package net.citizensnpcs;
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.event.NPCDamageEvent;
import net.citizensnpcs.api.event.NPCLeftClickEvent;
import net.citizensnpcs.api.event.NPCRightClickEvent;
import net.citizensnpcs.api.npc.NPC;
@ -85,22 +86,27 @@ public class EventListen implements Listener {
if (!npcManager.isNPC(event.getEntity()))
return;
event.setCancelled(true);
if (event instanceof EntityDamageByEntityEvent) {
EntityDamageByEntityEvent e = (EntityDamageByEntityEvent) event;
if (e.getDamager() instanceof Player) {
Player damager = (Player) e.getDamager();
NPC npc = npcManager.getNPC(event.getEntity());
NPC npc = npcManager.getNPC(event.getEntity());
// Call left-click event
NPCLeftClickEvent leftClickEvent = new NPCLeftClickEvent(npc, damager);
Bukkit.getPluginManager().callEvent(leftClickEvent);
if (leftClickEvent.isCancelled())
return;
NPCDamageEvent damageEvent = new NPCDamageEvent(npc, e.getDamager());
Bukkit.getPluginManager().callEvent(event);
event.setCancelled(damageEvent.isCancelled());
if (!damageEvent.isCancelled() || !(e.getDamager() instanceof Player))
return;
Player damager = (Player) e.getDamager();
// Call left-click event
NPCLeftClickEvent leftClickEvent = new NPCLeftClickEvent(npc, damager);
Bukkit.getPluginManager().callEvent(leftClickEvent);
if (leftClickEvent.isCancelled())
return;
if (npc.getCharacter() != null)
npc.getCharacter().onLeftClick(npc, damager);
if (npc.getCharacter() != null)
npc.getCharacter().onLeftClick(npc, damager);
}
}
}

View File

@ -194,7 +194,12 @@ public abstract class CitizensNPC extends AbstractNPC {
@Override
public void update() {
super.update();
ai.update();
try {
super.update();
ai.update();
} catch (Exception ex) {
Messaging.log("Exception while updating " + getId() + ": " + ex.getMessage() + ".");
ex.printStackTrace();
}
}
}

View File

@ -35,7 +35,6 @@ public class NPCSelector implements Listener {
public void onNPCRightClick(NPCRightClickEvent event) {
Player player = event.getClicker();
NPC npc = event.getNPC();
npc.getMetadata(null);
List<MetadataValue> selected = player.getMetadata("selected");
if (selected == null || selected.size() == 0 || selected.get(0).asInt() != npc.getId()) {
if (Util.isSettingFulfilled(player, Setting.SELECTION_ITEM)

View File

@ -57,10 +57,10 @@ public class EntityHumanNPC extends EntityPlayer implements NPCHandle {
getControllerLook().a();
getControllerJump().b();
if (aZ) {
if (aV() || aU()) {
if (aT()) {
motY += 0.04;
} else if (onGround && q == 0) {
motY = 0.5;
motY = 0.6;
q = 10;
}
} else {

View File

@ -6,6 +6,7 @@ import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.npc.CitizensNPC;
import net.minecraft.server.EntityLiving;
import net.minecraft.server.EntityPlayer;
import org.bukkit.craftbukkit.entity.CraftPlayer;
@ -38,7 +39,7 @@ public class Controllable extends Trait implements Runnable, Listener, Toggleabl
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
if (!npc.isSpawned())
if (!npc.isSpawned() || !enabled)
return;
EntityPlayer handle = ((CraftPlayer) event.getPlayer()).getHandle();
Action performed = event.getAction();
@ -67,9 +68,10 @@ public class Controllable extends Trait implements Runnable, Listener, Toggleabl
public void run() {
if (!npc.isSpawned() || npc.getHandle().passenger == null)
return;
boolean onGround = npc.getHandle().onGround;
npc.getHandle().motX += npc.getHandle().passenger.motX * (onGround ? GROUND_SPEED : AIR_SPEED);
npc.getHandle().motZ += npc.getHandle().passenger.motZ * (onGround ? GROUND_SPEED : AIR_SPEED);
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);
}
@Override

View File

@ -7,6 +7,7 @@ import net.citizensnpcs.api.ai.NavigationCallback;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.editor.Editor;
import net.citizensnpcs.util.Messaging;
import net.citizensnpcs.util.StringHelper;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
@ -15,6 +16,7 @@ import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerItemHeldEvent;
import com.google.common.collect.Lists;
@ -25,6 +27,7 @@ public class LinearWaypointProvider implements WaypointProvider, Iterable<Waypoi
@Override
public Editor createEditor(final Player player) {
return new Editor() {
int editingSlot = waypoints.size() - 1;
@Override
public void begin() {
@ -37,6 +40,11 @@ public class LinearWaypointProvider implements WaypointProvider, Iterable<Waypoi
player.sendMessage(ChatColor.AQUA + "Exited the linear waypoint editor.");
}
private String formatLoc(Location location) {
return String.format("<e>%d<a>, <e>%d<a>, <e>%d<a>", location.getBlockX(), location.getBlockY(),
location.getBlockZ());
}
@EventHandler
@SuppressWarnings("unused")
public void onPlayerInteract(PlayerInteractEvent event) {
@ -46,16 +54,45 @@ public class LinearWaypointProvider implements WaypointProvider, Iterable<Waypoi
if (event.getClickedBlock() == null)
return;
Location at = event.getClickedBlock().getLocation();
waypoints.add(new Waypoint(at));
Messaging.send(player, String.format("<e>Added<a> a waypoint at (<e>%d<a>, <e>%d<a>, <e>%d<a>).",
at.getBlockX(), at.getBlockY(), at.getBlockZ()));
waypoints.add(Math.max(0, editingSlot), new Waypoint(at));
editingSlot = Math.min(editingSlot + 1, waypoints.size());
Messaging.send(player, String.format("<e>Added<a> a waypoint at (" + formatLoc(at)
+ ") (<e>%d<a>, <e>%d<a>)", editingSlot + 1, waypoints.size()));
} else if (waypoints.size() > 0) {
waypoints.remove(waypoints.size() - 1);
Messaging.send(player,
String.format("<e>Removed<a> a waypoint (<e>%d<a> remaining)", waypoints.size()));
waypoints.remove(editingSlot);
editingSlot = Math.max(0, editingSlot - 1);
Messaging.send(player, String.format("<e>Removed<a> a waypoint (<e>%d<a> remaining) (<e>%d<a>)",
waypoints.size(), editingSlot + 1));
}
callback.onProviderChanged();
}
@EventHandler
@SuppressWarnings("unused")
public void onPlayerItemHeldChange(PlayerItemHeldEvent event) {
if (!event.getPlayer().equals(player) || waypoints.size() == 0)
return;
int previousSlot = event.getPreviousSlot(), newSlot = event.getNewSlot();
// handle wrap-arounds
if (previousSlot == 0 && newSlot == LARGEST_SLOT) {
editingSlot--;
} else if (previousSlot == LARGEST_SLOT && newSlot == 0) {
editingSlot++;
} else {
int diff = newSlot - previousSlot;
if (Math.abs(diff) != 1)
return; // the player isn't scrolling
editingSlot += diff > 0 ? 1 : -1;
}
if (editingSlot >= waypoints.size())
editingSlot = 0;
if (editingSlot < 0)
editingSlot = waypoints.size() - 1;
Messaging.send(player, "<a>Editing slot set to " + StringHelper.wrap(editingSlot) + " ("
+ formatLoc(waypoints.get(editingSlot).getLocation()) + ").");
}
private static final int LARGEST_SLOT = 8;
};
}
@ -69,11 +106,6 @@ public class LinearWaypointProvider implements WaypointProvider, Iterable<Waypoi
return waypoints.iterator();
}
@Override
public void onAttach() {
callback.onProviderChanged();
}
@Override
public void load(DataKey key) {
for (DataKey root : key.getRelative("points").getIntegerSubKeys()) {
@ -84,6 +116,11 @@ public class LinearWaypointProvider implements WaypointProvider, Iterable<Waypoi
}
}
@Override
public void onAttach() {
callback.onProviderChanged();
}
@Override
public void save(DataKey key) {
key.removeKey("points");