Move trait commands, /npc copy

This commit is contained in:
fullwall 2012-08-05 21:07:42 +08:00
parent 52e393b44b
commit 358cf996b1
7 changed files with 133 additions and 74 deletions

View File

@ -28,6 +28,7 @@ import net.citizensnpcs.command.command.EditorCommands;
import net.citizensnpcs.command.command.HelpCommands; import net.citizensnpcs.command.command.HelpCommands;
import net.citizensnpcs.command.command.NPCCommands; import net.citizensnpcs.command.command.NPCCommands;
import net.citizensnpcs.command.command.ScriptCommands; import net.citizensnpcs.command.command.ScriptCommands;
import net.citizensnpcs.command.command.TraitCommands;
import net.citizensnpcs.command.exception.CommandException; import net.citizensnpcs.command.exception.CommandException;
import net.citizensnpcs.command.exception.CommandUsageException; import net.citizensnpcs.command.exception.CommandUsageException;
import net.citizensnpcs.command.exception.ServerCommandException; import net.citizensnpcs.command.exception.ServerCommandException;
@ -231,6 +232,7 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
commands.register(HelpCommands.class); commands.register(HelpCommands.class);
commands.register(NPCCommands.class); commands.register(NPCCommands.class);
commands.register(ScriptCommands.class); commands.register(ScriptCommands.class);
commands.register(TraitCommands.class);
} }
private void registerScriptHelpers() { private void registerScriptHelpers() {

View File

@ -51,13 +51,13 @@ public class Settings {
DATABASE_URL("storage.database.url", ""), DATABASE_URL("storage.database.url", ""),
DATABASE_USERNAME("storage.database.username", ""), DATABASE_USERNAME("storage.database.username", ""),
DEBUG_MODE("general.debug-mode", false), DEBUG_MODE("general.debug-mode", false),
DEFAULT_LOOK_CLOSE("npc.default.look-close", false), DEFAULT_LOOK_CLOSE("npc.default.look-close.enabled", false),
DEFAULT_LOOK_CLOSE_RANGE("npc.default.look-close-range", 5), DEFAULT_LOOK_CLOSE_RANGE("npc.default.look-close.range", 5),
DEFAULT_PATHFINDING_RANGE("npc.pathing.default-pathfinding-range", 25F), DEFAULT_PATHFINDING_RANGE("npc.default.pathfinding.range", 25F),
DEFAULT_RANDOM_TALKER("npc.default.random-talker", true), DEFAULT_RANDOM_TALKER("npc.default.random-talker", true),
DEFAULT_REALISTIC_LOOKING("npc.default.realistic-looking", false), DEFAULT_REALISTIC_LOOKING("npc.default.realistic-looking", false),
DEFAULT_TALK_CLOSE("npc.default.talk-close", false), DEFAULT_TALK_CLOSE("npc.default.talk-close.enabled", false),
DEFAULT_TALK_CLOSE_RANGE("npc.default.talk-close-range", 5), DEFAULT_TALK_CLOSE_RANGE("npc.default.talk-close.range", 5),
DEFAULT_TEXT("npc.default.text.0", "Hi, I'm <npc>!") { DEFAULT_TEXT("npc.default.text.0", "Hi, I'm <npc>!") {
@Override @Override
public void loadFromKey(DataKey root) { public void loadFromKey(DataKey root) {

View File

@ -8,16 +8,17 @@ import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI; import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.NPCRegistry; 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.MobType;
import net.citizensnpcs.api.trait.trait.Owner; import net.citizensnpcs.api.trait.trait.Owner;
import net.citizensnpcs.api.trait.trait.Spawned; 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.Command;
import net.citizensnpcs.command.CommandConfigurable;
import net.citizensnpcs.command.CommandContext; import net.citizensnpcs.command.CommandContext;
import net.citizensnpcs.command.Requirements; import net.citizensnpcs.command.Requirements;
import net.citizensnpcs.command.exception.CommandException; import net.citizensnpcs.command.exception.CommandException;
import net.citizensnpcs.command.exception.NoPermissionsException; import net.citizensnpcs.command.exception.NoPermissionsException;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.NPCSelector; import net.citizensnpcs.npc.NPCSelector;
import net.citizensnpcs.trait.Age; import net.citizensnpcs.trait.Age;
import net.citizensnpcs.trait.Behaviour; 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( @Command(
aliases = { "npc" }, aliases = { "npc" },
usage = "create [name] ((-b) --type (type) --char (char) --behaviour (behaviour))", usage = "create [name] ((-b) --type (type) --char (char) --behaviour (behaviour))",
@ -525,59 +550,4 @@ public class NPCCommands {
npc.getBukkitEntity().teleport(player, TeleportCause.COMMAND); npc.getBukkitEntity().teleport(player, TeleportCause.COMMAND);
Messaging.send(player, StringHelper.wrap(npc.getName()) + " was teleported to your location."); 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<? extends Trait> 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<? extends Trait> 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);
}
} }

View File

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

View File

@ -7,7 +7,6 @@ import org.bukkit.command.CommandSender;
import org.bukkit.conversations.ConversationContext; import org.bukkit.conversations.ConversationContext;
import org.bukkit.conversations.Prompt; import org.bukkit.conversations.Prompt;
import org.bukkit.conversations.StringPrompt; import org.bukkit.conversations.StringPrompt;
import org.bukkit.entity.Player;
public class StartPrompt extends StringPrompt { public class StartPrompt extends StringPrompt {
private final Text text; private final Text text;
@ -25,23 +24,30 @@ public class StartPrompt extends StringPrompt {
else if (input.equalsIgnoreCase("remove")) else if (input.equalsIgnoreCase("remove"))
return new TextRemovePrompt(text); return new TextRemovePrompt(text);
else if (input.equalsIgnoreCase("random")) else if (input.equalsIgnoreCase("random"))
Messaging.send((Player) context.getForWhom(), Messaging.send((CommandSender) context.getForWhom(),
"<e>Random talker <a>set to <e>" + text.toggleRandomTalker() + "<a>."); "<e>Random talker <a>set to <e>" + text.toggleRandomTalker() + "<a>.");
else if (input.equalsIgnoreCase("realistic looking")) else if (input.equalsIgnoreCase("realistic looking"))
Messaging.send((CommandSender) context.getForWhom(), Messaging.send((CommandSender) context.getForWhom(),
"<e>Realistic looking <a>set to <e>" + text.toggleRealisticLooking() + "<a>."); "<e>Realistic looking <a>set to <e>" + text.toggleRealisticLooking() + "<a>.");
else if (input.equalsIgnoreCase("close")) else if (input.equalsIgnoreCase("close"))
Messaging.send((Player) context.getForWhom(), "<e>Close talker <a>set to <e>" + text.toggle() Messaging.send((CommandSender) context.getForWhom(),
+ "<a>."); "<e>Close talker <a>set to <e>" + text.toggle() + "<a>.");
else else if (input.equalsIgnoreCase("help")) {
Messaging.sendError((Player) context.getForWhom(), "Invalid edit type."); 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); return new StartPrompt(text);
} }
@Override @Override
public String getPromptText(ConversationContext context) { public String getPromptText(ConversationContext context) {
return StringHelper if (context.getSessionData("said-text") == Boolean.TRUE)
.parseColors("<a>Type <e>add <a>to add an entry, <e>edit <a>to edit entries, <e>remove <a>to remove entries, <e>close <a>to toggle the NPC as a close talker, and <e>random <a>to toggle the NPC as a random talker."); return "";
String text = StringHelper
.parseColors("<a>Type <e>add <a>to add an entry, <e>edit <a>to edit entries, <e>remove <a>to remove entries, <e>close <a>to toggle the NPC as a close talker, and <e>random <a>to toggle the NPC as a random talker. Type <e>help<a> to show this again.");
context.setSessionData("said-text", Boolean.TRUE);
return text;
} }
} }

View File

@ -9,6 +9,7 @@ import java.util.Random;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import net.citizensnpcs.Settings.Setting; import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.event.NPCRightClickEvent; import net.citizensnpcs.api.event.NPCRightClickEvent;
import net.citizensnpcs.api.exception.NPCLoadException; import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.trait.Trait; import net.citizensnpcs.api.trait.Trait;
@ -44,7 +45,7 @@ public class Text extends Trait implements Runnable, Toggleable, Listener, Conve
public Text() { public Text() {
super("text"); super("text");
this.plugin = Bukkit.getPluginManager().getPlugin("Citizens"); this.plugin = CitizensAPI.getPlugin();
} }
public void add(String string) { 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) { public Editor getEditor(final Player player) {
final Conversation conversation = new ConversationFactory(plugin) final Conversation conversation = new ConversationFactory(plugin)
.addConversationAbandonedListener(this).withLocalEcho(false).withEscapeSequence("/npc text") .addConversationAbandonedListener(this).withLocalEcho(false).withEscapeSequence("npc text")
.withModality(false).withFirstPrompt(new StartPrompt(this)).buildConversation(player); .withEscapeSequence("exit").withModality(false).withFirstPrompt(new StartPrompt(this))
.buildConversation(player);
return new Editor() { return new Editor() {
@Override @Override
@ -125,7 +127,7 @@ public class Text extends Trait implements Runnable, Toggleable, Listener, Conve
return; return;
EntityHuman search = null; EntityHuman search = null;
EntityLiving handle = ((CitizensNPC) npc).getHandle(); 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(); Player player = (Player) search.getBukkitEntity();
// If the cooldown is not expired, do not send text // If the cooldown is not expired, do not send text
if (cooldowns.get(player.getName()) != null) { if (cooldowns.get(player.getName()) != null) {

View File

@ -4,6 +4,12 @@ version: 2.0
main: net.citizensnpcs.Citizens main: net.citizensnpcs.Citizens
website: http://www.citizensnpcs.net website: http://www.citizensnpcs.net
commands: commands:
traitc:
description: Configures traits
aliases: [trc, tc]
trait:
description: Trait commands
aliases: [tr]
script: script:
description: Scripting commands description: Scripting commands
citizens: citizens: