From 0de701800c1d0fddf9e946442e5b5c6768831e66 Mon Sep 17 00:00:00 2001 From: aPunch Date: Sat, 11 Feb 2012 03:21:02 -0600 Subject: [PATCH] not working --- src/net/citizensnpcs/Citizens.java | 26 +------- .../command/command/NPCCommands.java | 17 +++++ .../npc/entity/CitizensHumanNPC.java | 13 ++++ src/net/citizensnpcs/trait/Sneak.java | 63 +++++++++++++++++++ 4 files changed, 96 insertions(+), 23 deletions(-) create mode 100644 src/net/citizensnpcs/trait/Sneak.java diff --git a/src/net/citizensnpcs/Citizens.java b/src/net/citizensnpcs/Citizens.java index b3b3ea505..4fd53fc54 100644 --- a/src/net/citizensnpcs/Citizens.java +++ b/src/net/citizensnpcs/Citizens.java @@ -1,9 +1,7 @@ package net.citizensnpcs; import java.io.File; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.logging.Level; import net.citizensnpcs.Settings.Setting; @@ -35,6 +33,7 @@ import net.citizensnpcs.storage.Storage; import net.citizensnpcs.storage.YamlStorage; import net.citizensnpcs.trait.Inventory; import net.citizensnpcs.trait.LookClose; +import net.citizensnpcs.trait.Sneak; import net.citizensnpcs.util.Messaging; import net.citizensnpcs.util.StringHelper; @@ -45,8 +44,6 @@ import org.bukkit.command.CommandSender; import org.bukkit.craftbukkit.CraftServer; import org.bukkit.entity.CreatureType; import org.bukkit.entity.Player; -import org.bukkit.permissions.Permission; -import org.bukkit.permissions.PermissionDefault; import org.bukkit.plugin.java.JavaPlugin; import com.google.common.collect.Lists; @@ -56,7 +53,7 @@ public class Citizens extends JavaPlugin { @SuppressWarnings("unchecked") private static final List> defaultTraits = Lists.newArrayList(Owner.class, Spawned.class, - LookClose.class, SpawnLocation.class, Inventory.class); + LookClose.class, SpawnLocation.class, Inventory.class, Sneak.class); private volatile CitizensNPCManager npcManager; private final DefaultInstanceFactory characterManager = new DefaultInstanceFactory(); @@ -188,9 +185,8 @@ public class Citizens extends JavaPlugin { // Register events getServer().getPluginManager().registerEvents(new EventListen(npcManager), this); - // Register commands and permissions + // Register commands registerCommands(); - registerPermissions(); // Register default traits traitManager.registerAll(defaultTraits); @@ -245,22 +241,6 @@ public class Citizens extends JavaPlugin { cmdManager.register(HelpCommands.class); } - private void registerPermissions() { - // TODO There has to be a better way than this (maybe use Permission - // annotation to register permissions?) - Map children = new HashMap(); - children.put("citizens.npc.create", true); - children.put("citizens.npc.spawn", true); - children.put("citizens.npc.despawn", true); - children.put("citizens.npc.select", true); - children.put("citizens.npc.tp", true); - children.put("citizens.npc.tphere", true); - children.put("citizens.npc.look-close", true); - - Permission perm = new Permission("citizens.*", PermissionDefault.OP, children); - getServer().getPluginManager().addPermission(perm); - } - private void saveNPCs() { for (NPC npc : npcManager) npc.save(saves.getKey("npc." + npc.getId())); diff --git a/src/net/citizensnpcs/command/command/NPCCommands.java b/src/net/citizensnpcs/command/command/NPCCommands.java index e8946c148..2681e4765 100644 --- a/src/net/citizensnpcs/command/command/NPCCommands.java +++ b/src/net/citizensnpcs/command/command/NPCCommands.java @@ -15,6 +15,7 @@ import net.citizensnpcs.command.annotation.Command; import net.citizensnpcs.command.annotation.Requirements; import net.citizensnpcs.npc.CitizensNPCManager; import net.citizensnpcs.trait.LookClose; +import net.citizensnpcs.trait.Sneak; import net.citizensnpcs.util.Messaging; import net.citizensnpcs.util.StringHelper; @@ -180,6 +181,22 @@ public class NPCCommands { npc.setCharacter(character); } + @Command( + aliases = { "npc" }, + usage = "sneak", + desc = "Toggle whether an NPC should sneak", + modifiers = { "sneak" }, + min = 1, + max = 1, + permission = "npc.sneak") + public void toggleSneak(CommandContext args, Player player, NPC npc) { + Sneak trait = npc.getTrait(Sneak.class); + trait.toggle(); + String msg = StringHelper.wrap(npc.getName()) + " will " + + (trait.isSneaking() ? "now sneak" : "no longer sneak"); + Messaging.send(player, msg += "."); + } + @Command( aliases = { "npc" }, usage = "spawn [id]", diff --git a/src/net/citizensnpcs/npc/entity/CitizensHumanNPC.java b/src/net/citizensnpcs/npc/entity/CitizensHumanNPC.java index c0cefda96..c548cb159 100644 --- a/src/net/citizensnpcs/npc/entity/CitizensHumanNPC.java +++ b/src/net/citizensnpcs/npc/entity/CitizensHumanNPC.java @@ -1,9 +1,12 @@ package net.citizensnpcs.npc.entity; +import net.citizensnpcs.api.DataKey; +import net.citizensnpcs.api.exception.NPCLoadException; import net.citizensnpcs.npc.CitizensNPC; import net.citizensnpcs.npc.CitizensNPCManager; import net.citizensnpcs.resource.lib.EntityHumanNPC; import net.citizensnpcs.util.StringHelper; + import net.minecraft.server.EntityLiving; import net.minecraft.server.ItemInWorldManager; import net.minecraft.server.WorldServer; @@ -23,6 +26,11 @@ public class CitizensHumanNPC extends CitizensNPC { return (Player) getHandle().getBukkitEntity(); } + @Override + public EntityHumanNPC getHandle() { + return (EntityHumanNPC) mcEntity; + } + @Override public void update() { super.update(); @@ -47,4 +55,9 @@ public class CitizensHumanNPC extends CitizensNPC { handle.setPositionRotation(loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch()); return handle; } + + @Override + public void load(DataKey key) throws NPCLoadException { + super.load(key); + } } \ No newline at end of file diff --git a/src/net/citizensnpcs/trait/Sneak.java b/src/net/citizensnpcs/trait/Sneak.java new file mode 100644 index 000000000..29dba9173 --- /dev/null +++ b/src/net/citizensnpcs/trait/Sneak.java @@ -0,0 +1,63 @@ +package net.citizensnpcs.trait; + +import net.citizensnpcs.api.DataKey; +import net.citizensnpcs.api.exception.NPCLoadException; +import net.citizensnpcs.api.npc.NPC; +import net.citizensnpcs.api.npc.trait.SaveId; +import net.citizensnpcs.api.npc.trait.Trait; +import net.citizensnpcs.npc.entity.CitizensHumanNPC; + +import net.minecraft.server.DataWatcher; +import net.minecraft.server.Packet40EntityMetadata; + +import org.bukkit.craftbukkit.entity.CraftPlayer; +import org.bukkit.entity.Player; + +@SaveId("sneak") +public class Sneak extends Trait implements Runnable { + private final NPC npc; + private boolean sneak; + + public Sneak(NPC npc) { + this.npc = npc; + } + + @Override + public void load(DataKey key) throws NPCLoadException { + sneak = key.getBoolean(""); + } + + @Override + public void save(DataKey key) { + key.setBoolean("", sneak); + } + + @Override + public void run() { + if (npc instanceof CitizensHumanNPC) { + ((Player) npc.getBukkitEntity()).setSneaking(sneak); + DataWatcher dw = ((CitizensHumanNPC) npc).getHandle().getDataWatcher(); + dw.watch(1, sneak); + for (Player player : npc.getBukkitEntity().getServer().getOnlinePlayers()) + ((CraftPlayer) player).getHandle().netServerHandler.sendPacket(new Packet40EntityMetadata(npc + .getBukkitEntity().getEntityId(), dw)); + } + } + + public void setSneaking(boolean sneak) { + this.sneak = sneak; + } + + public boolean isSneaking() { + return sneak; + } + + public void toggle() { + sneak = !sneak; + } + + @Override + public String toString() { + return "Sneak{" + sneak + "}"; + } +} \ No newline at end of file