not working

This commit is contained in:
aPunch 2012-02-11 03:21:02 -06:00
parent f01212036d
commit 6dc01e57ab
4 changed files with 96 additions and 23 deletions

View File

@ -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<Class<? extends Trait>> 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<Character> characterManager = new DefaultInstanceFactory<Character>();
@ -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<String, Boolean> children = new HashMap<String, Boolean>();
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()));

View File

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

View File

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

View File

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