Skip to content

Commit

Permalink
Implement /npc respawn command
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Jul 14, 2013
1 parent 4c80294 commit cc1c4e4
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 5 deletions.
18 changes: 16 additions & 2 deletions src/main/java/net/citizensnpcs/EventListen.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,17 +186,31 @@ public void onEntityDamage(EntityDamageEvent event) {

@EventHandler(ignoreCancelled = true)
public void onEntityDeath(EntityDeathEvent event) {
NPC npc = npcRegistry.getNPC(event.getEntity());
final NPC npc = npcRegistry.getNPC(event.getEntity());
if (npc == null)
return;
Bukkit.getPluginManager().callEvent(new NPCDeathEvent(npc, event));
final Location location = npc.getBukkitEntity().getLocation();
npc.despawn(DespawnReason.DEATH);

if (npc.data().get(NPC.RESPAWN_DELAY_METADATA, -1) >= 0) {
int delay = npc.data().get(NPC.RESPAWN_DELAY_METADATA, -1);
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
@Override
public void run() {
if (!npc.isSpawned()) {
npc.spawn(location);
}
}
}, delay);
}
}

@EventHandler(priority = EventPriority.HIGHEST)
public void onEntitySpawn(CreatureSpawnEvent event) {
if (event.isCancelled() && npcRegistry.isNPC(event.getEntity()))
if (event.isCancelled() && npcRegistry.isNPC(event.getEntity())) {
event.setCancelled(false);
}
}

@EventHandler
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/net/citizensnpcs/commands/NPCCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,25 @@ public void rename(CommandContext args, CommandSender sender, NPC npc) {
Messaging.sendTr(sender, Messages.NPC_RENAMED, oldName, newName);
}

@Command(
aliases = { "npc" },
usage = "respawn [delay in ticks]",
desc = "Sets an NPC's respawn delay in ticks",
modifiers = { "respawn" },
min = 1,
max = 2,
permission = "citizens.npc.respawn")
public void respawn(CommandContext args, CommandSender sender, NPC npc) {
if (args.argsLength() > 1) {
int delay = args.getInteger(1);
npc.data().setPersistent(NPC.RESPAWN_DELAY_METADATA, delay);
Messaging.sendTr(sender, Messages.RESPAWN_DELAY_SET, delay);
} else {
Messaging.sendTr(sender, Messages.RESPAWN_DELAY_DESCRIBE, npc.data().get(NPC.RESPAWN_DELAY_METADATA, -1));
}

}

@Command(
aliases = { "npc" },
usage = "select|sel [id|name] (--r range)",
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/net/citizensnpcs/trait/LookClose.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,12 @@ public void onDespawn() {
public void run() {
if (!enabled || !npc.isSpawned() || npc.getNavigator().isNavigating())
return;
if (hasInvalidTarget())
if (hasInvalidTarget()) {
findNewTarget();
if (lookingAt != null && canSeeTarget())
}
if (lookingAt != null && canSeeTarget()) {
Util.faceEntity(npc.getBukkitEntity(), lookingAt);
}
}

@Override
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/net/citizensnpcs/util/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ public class Messages {
public static final String REMOVE_INCORRECT_SYNTAX = "citizens.commands.npc.remove.incorrect-syntax";
public static final String REMOVED_ALL_NPCS = "citizens.commands.npc.remove.removed-all";
public static final String REMOVED_FROM_PLAYERLIST = "citizens.commands.npc.playerlist.removed";
public static final String RESPAWN_DELAY_DESCRIBE = "citizens.commands.npc.respawn.describe";
public static final String RESPAWN_DELAY_SET = "citizens.commands.npc.respawn.delay-set";
public static final String SADDLED_SET = "citizens.editors.equipment.saddled-set";
public static final String SADDLED_STOPPED = "citizens.editors.equipment.saddled-stopped";
public static final String SCRIPT_COMPILED = "citizens.commands.script.compiled";
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/citizensnpcs/util/NMS.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public static float getSpeedFor(NPC npc) {
return DEFAULT_SPEED;
// this is correct, but too slow. TODO: investigate
// return (float)
// NMS.getHandle(npc.getBukkitEntity()).a(GenericAttributes.d).b();
// NMS.getHandle(npc.getBukkitEntity()).getAttributeInstance(GenericAttributes.d).getValue();
return DEFAULT_SPEED;
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/messages_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ citizens.commands.npc.remove.incorrect-syntax=Incorrect syntax. /npc remove (all
citizens.commands.npc.remove.removed-all=You permanently removed all NPCs.
citizens.commands.npc.remove.removed=You permanently removed [[{0}]].
citizens.commands.npc.rename.renamed=You renamed [[{0}]] to [[{1}]].
citizens.commands.npc.respawn.delay-set=Respawn delay set to [[{0}]].
citizens.commands.npc.respawn.describe=Respawn delay is currently [[{0}]].
citizens.commands.npc.select.already-selected=You already have that NPC selected.
citizens.commands.npc.size.description={0}''s size is [[{1}]].
citizens.commands.npc.size.set={0}''s size set to [[{1}]].
Expand Down

0 comments on commit cc1c4e4

Please sign in to comment.