diff --git a/src/main/java/net/citizensnpcs/Citizens.java b/src/main/java/net/citizensnpcs/Citizens.java index 8a5e04b0a..8922a5624 100644 --- a/src/main/java/net/citizensnpcs/Citizens.java +++ b/src/main/java/net/citizensnpcs/Citizens.java @@ -28,6 +28,7 @@ import net.citizensnpcs.command.command.EditorCommands; import net.citizensnpcs.command.command.HelpCommands; import net.citizensnpcs.command.command.NPCCommands; import net.citizensnpcs.command.command.ScriptCommands; +import net.citizensnpcs.command.command.TraitCommands; import net.citizensnpcs.command.exception.CommandException; import net.citizensnpcs.command.exception.CommandUsageException; import net.citizensnpcs.command.exception.ServerCommandException; @@ -231,6 +232,7 @@ public class Citizens extends JavaPlugin implements CitizensPlugin { commands.register(HelpCommands.class); commands.register(NPCCommands.class); commands.register(ScriptCommands.class); + commands.register(TraitCommands.class); } private void registerScriptHelpers() { diff --git a/src/main/java/net/citizensnpcs/Settings.java b/src/main/java/net/citizensnpcs/Settings.java index c99044257..ed6bbf5c6 100644 --- a/src/main/java/net/citizensnpcs/Settings.java +++ b/src/main/java/net/citizensnpcs/Settings.java @@ -51,13 +51,13 @@ public class Settings { DATABASE_URL("storage.database.url", ""), DATABASE_USERNAME("storage.database.username", ""), DEBUG_MODE("general.debug-mode", false), - DEFAULT_LOOK_CLOSE("npc.default.look-close", false), - DEFAULT_LOOK_CLOSE_RANGE("npc.default.look-close-range", 5), - DEFAULT_PATHFINDING_RANGE("npc.pathing.default-pathfinding-range", 25F), + DEFAULT_LOOK_CLOSE("npc.default.look-close.enabled", false), + DEFAULT_LOOK_CLOSE_RANGE("npc.default.look-close.range", 5), + DEFAULT_PATHFINDING_RANGE("npc.default.pathfinding.range", 25F), DEFAULT_RANDOM_TALKER("npc.default.random-talker", true), DEFAULT_REALISTIC_LOOKING("npc.default.realistic-looking", false), - DEFAULT_TALK_CLOSE("npc.default.talk-close", false), - DEFAULT_TALK_CLOSE_RANGE("npc.default.talk-close-range", 5), + DEFAULT_TALK_CLOSE("npc.default.talk-close.enabled", false), + DEFAULT_TALK_CLOSE_RANGE("npc.default.talk-close.range", 5), DEFAULT_TEXT("npc.default.text.0", "Hi, I'm !") { @Override public void loadFromKey(DataKey root) { diff --git a/src/main/java/net/citizensnpcs/command/command/NPCCommands.java b/src/main/java/net/citizensnpcs/command/command/NPCCommands.java index e01018000..e4a31b2b3 100644 --- a/src/main/java/net/citizensnpcs/command/command/NPCCommands.java +++ b/src/main/java/net/citizensnpcs/command/command/NPCCommands.java @@ -8,16 +8,17 @@ import net.citizensnpcs.Settings.Setting; import net.citizensnpcs.api.CitizensAPI; import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.npc.NPCRegistry; -import net.citizensnpcs.api.trait.Trait; import net.citizensnpcs.api.trait.trait.MobType; import net.citizensnpcs.api.trait.trait.Owner; import net.citizensnpcs.api.trait.trait.Spawned; +import net.citizensnpcs.api.util.DataKey; +import net.citizensnpcs.api.util.MemoryDataKey; import net.citizensnpcs.command.Command; -import net.citizensnpcs.command.CommandConfigurable; import net.citizensnpcs.command.CommandContext; import net.citizensnpcs.command.Requirements; import net.citizensnpcs.command.exception.CommandException; import net.citizensnpcs.command.exception.NoPermissionsException; +import net.citizensnpcs.npc.CitizensNPC; import net.citizensnpcs.npc.NPCSelector; import net.citizensnpcs.trait.Age; import net.citizensnpcs.trait.Behaviour; @@ -123,6 +124,30 @@ public class NPCCommands { } } + @Command( + aliases = { "npc" }, + usage = "copy (--name newname)", + desc = "Copies an NPC", + modifiers = { "copy" }, + min = 1, + max = 1, + permission = "npc.copy") + public void copy(CommandContext args, CommandSender sender, NPC npc) { + EntityType type = npc.getTrait(MobType.class).getType(); + String name = args.getFlag("name", npc.getFullName()); + CitizensNPC copy = (CitizensNPC) npcRegistry.createNPC(type, name); + CitizensNPC from = (CitizensNPC) npc; + + DataKey key = new MemoryDataKey(); + from.save(key); + copy.load(key); + + if (copy.isSpawned() && sender instanceof Player) + copy.getBukkitEntity().teleport((Player) sender); + + Messaging.sendF(sender, ChatColor.GREEN + "%s has been copied.", npc.getName()); + } + @Command( aliases = { "npc" }, usage = "create [name] ((-b) --type (type) --char (char) --behaviour (behaviour))", @@ -525,59 +550,4 @@ public class NPCCommands { npc.getBukkitEntity().teleport(player, TeleportCause.COMMAND); Messaging.send(player, StringHelper.wrap(npc.getName()) + " was teleported to your location."); } - - @Command( - aliases = { "npc" }, - usage = "trait [trait name]", - desc = "Adds a trait to the NPC", - modifiers = { "trait" }, - min = 2, - max = 2, - flags = "r", - permission = "npc.trait") - public void trait(CommandContext args, CommandSender sender, NPC npc) throws CommandException { - String traitName = args.getString(1); - if (!sender.hasPermission("citizens.npc.trait." + traitName)) - throw new NoPermissionsException(); - if (args.hasFlag('r')) { - Class clazz = CitizensAPI.getTraitFactory().getTraitClass(args.getString(1)); - if (clazz == null) - throw new CommandException("Trait not found."); - if (!npc.hasTrait(clazz)) - throw new CommandException("The NPC doesn't have that trait."); - npc.removeTrait(clazz); - Messaging.sendF(sender, ChatColor.GREEN + "Trait %s removed successfully.", - StringHelper.wrap(traitName)); - return; - } - Trait trait = CitizensAPI.getTraitFactory().getTrait(traitName); - if (trait == null) - throw new CommandException("Trait not found."); - npc.addTrait(trait); - Messaging.sendF(sender, ChatColor.GREEN + "Trait %s added successfully.", - StringHelper.wrap(traitName)); - } - - @Command( - aliases = { "npc" }, - usage = "traitc|tc [trait name] [flags]", - desc = "Configures a trait", - modifiers = { "traitc", "tc" }, - min = 2, - flags = "*", - permission = "npc.trait-configure") - public void traitConfigure(CommandContext args, CommandSender sender, NPC npc) throws CommandException { - String traitName = args.getString(1); - if (!sender.hasPermission("citizens.npc.trait-configure." + traitName)) - throw new NoPermissionsException(); - Class clazz = CitizensAPI.getTraitFactory().getTraitClass(args.getString(1)); - if (clazz == null) - throw new CommandException("Trait not found."); - if (!clazz.isAssignableFrom(CommandConfigurable.class)) - throw new CommandException("That trait is not configurable"); - if (!npc.hasTrait(clazz)) - throw new CommandException("The NPC doesn't have that trait."); - CommandConfigurable trait = (CommandConfigurable) npc.getTrait(clazz); - trait.configure(args); - } } \ No newline at end of file diff --git a/src/main/java/net/citizensnpcs/command/command/TraitCommands.java b/src/main/java/net/citizensnpcs/command/command/TraitCommands.java new file mode 100644 index 000000000..d5f53d4ae --- /dev/null +++ b/src/main/java/net/citizensnpcs/command/command/TraitCommands.java @@ -0,0 +1,73 @@ +package net.citizensnpcs.command.command; + +import net.citizensnpcs.api.CitizensAPI; +import net.citizensnpcs.api.npc.NPC; +import net.citizensnpcs.api.trait.Trait; +import net.citizensnpcs.command.Command; +import net.citizensnpcs.command.CommandConfigurable; +import net.citizensnpcs.command.CommandContext; +import net.citizensnpcs.command.exception.CommandException; +import net.citizensnpcs.command.exception.NoPermissionsException; +import net.citizensnpcs.util.Messaging; +import net.citizensnpcs.util.StringHelper; + +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; + +public class TraitCommands { + + @Command( + aliases = { "trait", "tr" }, + usage = "[trait name]", + desc = "Adds a trait to the NPC", + modifiers = { "*" }, + min = 1, + max = 1, + flags = "r", + permission = "npc.trait") + public void add(CommandContext args, CommandSender sender, NPC npc) throws CommandException { + String traitName = args.getString(1); + if (!sender.hasPermission("citizens.npc.trait." + traitName)) + throw new NoPermissionsException(); + if (args.hasFlag('r')) { + Class clazz = CitizensAPI.getTraitFactory().getTraitClass(args.getString(1)); + if (clazz == null) + throw new CommandException("Trait not found."); + if (!npc.hasTrait(clazz)) + throw new CommandException("The NPC doesn't have that trait."); + npc.removeTrait(clazz); + Messaging.sendF(sender, ChatColor.GREEN + "Trait %s removed successfully.", + StringHelper.wrap(traitName)); + return; + } + Trait trait = CitizensAPI.getTraitFactory().getTrait(traitName); + if (trait == null) + throw new CommandException("Trait not found."); + npc.addTrait(trait); + Messaging.sendF(sender, ChatColor.GREEN + "Trait %s added successfully.", + StringHelper.wrap(traitName)); + } + + @Command( + aliases = { "traitc, trc,tc" }, + usage = "[trait name] [flags]", + desc = "Configures a trait", + modifiers = { "*" }, + min = 2, + flags = "*", + permission = "npc.trait-configure") + public void configure(CommandContext args, CommandSender sender, NPC npc) throws CommandException { + String traitName = args.getString(1); + if (!sender.hasPermission("citizens.npc.trait-configure." + traitName)) + throw new NoPermissionsException(); + Class clazz = CitizensAPI.getTraitFactory().getTraitClass(args.getString(1)); + if (clazz == null) + throw new CommandException("Trait not found."); + if (!clazz.isAssignableFrom(CommandConfigurable.class)) + throw new CommandException("That trait is not configurable"); + if (!npc.hasTrait(clazz)) + throw new CommandException("The NPC doesn't have that trait."); + CommandConfigurable trait = (CommandConfigurable) npc.getTrait(clazz); + trait.configure(args); + } +} diff --git a/src/main/java/net/citizensnpcs/trait/text/StartPrompt.java b/src/main/java/net/citizensnpcs/trait/text/StartPrompt.java index 863e9b70a..645770255 100644 --- a/src/main/java/net/citizensnpcs/trait/text/StartPrompt.java +++ b/src/main/java/net/citizensnpcs/trait/text/StartPrompt.java @@ -7,7 +7,6 @@ import org.bukkit.command.CommandSender; import org.bukkit.conversations.ConversationContext; import org.bukkit.conversations.Prompt; import org.bukkit.conversations.StringPrompt; -import org.bukkit.entity.Player; public class StartPrompt extends StringPrompt { private final Text text; @@ -25,23 +24,30 @@ public class StartPrompt extends StringPrompt { else if (input.equalsIgnoreCase("remove")) return new TextRemovePrompt(text); else if (input.equalsIgnoreCase("random")) - Messaging.send((Player) context.getForWhom(), + Messaging.send((CommandSender) context.getForWhom(), "Random talker set to " + text.toggleRandomTalker() + "."); else if (input.equalsIgnoreCase("realistic looking")) Messaging.send((CommandSender) context.getForWhom(), "Realistic looking set to " + text.toggleRealisticLooking() + "."); else if (input.equalsIgnoreCase("close")) - Messaging.send((Player) context.getForWhom(), "Close talker set to " + text.toggle() - + "."); - else - Messaging.sendError((Player) context.getForWhom(), "Invalid edit type."); + Messaging.send((CommandSender) context.getForWhom(), + "Close talker set to " + text.toggle() + "."); + else if (input.equalsIgnoreCase("help")) { + context.setSessionData("said-text", false); + Messaging.send((CommandSender) context.getForWhom(), getPromptText(context)); + } else + Messaging.sendError((CommandSender) context.getForWhom(), "Invalid edit type."); return new StartPrompt(text); } @Override public String getPromptText(ConversationContext context) { - return StringHelper - .parseColors("Type add to add an entry, edit to edit entries, remove to remove entries, close to toggle the NPC as a close talker, and random to toggle the NPC as a random talker."); + if (context.getSessionData("said-text") == Boolean.TRUE) + return ""; + String text = StringHelper + .parseColors("Type add to add an entry, edit to edit entries, remove to remove entries, close to toggle the NPC as a close talker, and random to toggle the NPC as a random talker. Type help to show this again."); + context.setSessionData("said-text", Boolean.TRUE); + return text; } } \ No newline at end of file diff --git a/src/main/java/net/citizensnpcs/trait/text/Text.java b/src/main/java/net/citizensnpcs/trait/text/Text.java index 6262fda20..cfa011f88 100644 --- a/src/main/java/net/citizensnpcs/trait/text/Text.java +++ b/src/main/java/net/citizensnpcs/trait/text/Text.java @@ -9,6 +9,7 @@ import java.util.Random; import java.util.concurrent.TimeUnit; import net.citizensnpcs.Settings.Setting; +import net.citizensnpcs.api.CitizensAPI; import net.citizensnpcs.api.event.NPCRightClickEvent; import net.citizensnpcs.api.exception.NPCLoadException; import net.citizensnpcs.api.trait.Trait; @@ -44,7 +45,7 @@ public class Text extends Trait implements Runnable, Toggleable, Listener, Conve public Text() { super("text"); - this.plugin = Bukkit.getPluginManager().getPlugin("Citizens"); + this.plugin = CitizensAPI.getPlugin(); } public void add(String string) { @@ -62,8 +63,9 @@ public class Text extends Trait implements Runnable, Toggleable, Listener, Conve public Editor getEditor(final Player player) { final Conversation conversation = new ConversationFactory(plugin) - .addConversationAbandonedListener(this).withLocalEcho(false).withEscapeSequence("/npc text") - .withModality(false).withFirstPrompt(new StartPrompt(this)).buildConversation(player); + .addConversationAbandonedListener(this).withLocalEcho(false).withEscapeSequence("npc text") + .withEscapeSequence("exit").withModality(false).withFirstPrompt(new StartPrompt(this)) + .buildConversation(player); return new Editor() { @Override @@ -125,7 +127,7 @@ public class Text extends Trait implements Runnable, Toggleable, Listener, Conve return; EntityHuman search = null; EntityLiving handle = ((CitizensNPC) npc).getHandle(); - if ((search = handle.world.findNearbyPlayer(handle, 5)) != null && talkClose) { + if ((search = handle.world.findNearbyPlayer(handle, range)) != null && talkClose) { Player player = (Player) search.getBukkitEntity(); // If the cooldown is not expired, do not send text if (cooldowns.get(player.getName()) != null) { diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 30b8ba369..c242cbd1f 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -4,6 +4,12 @@ version: 2.0 main: net.citizensnpcs.Citizens website: http://www.citizensnpcs.net commands: + traitc: + description: Configures traits + aliases: [trc, tc] + trait: + description: Trait commands + aliases: [tr] script: description: Scripting commands citizens: