Add a super simple command - /npc command. More features as required
This commit is contained in:
parent
64e47912c7
commit
c072a0887a
|
@ -90,6 +90,7 @@ import net.citizensnpcs.api.util.Messaging;
|
|||
import net.citizensnpcs.api.util.SpigotUtil;
|
||||
import net.citizensnpcs.editor.Editor;
|
||||
import net.citizensnpcs.npc.skin.SkinUpdateTracker;
|
||||
import net.citizensnpcs.trait.CommandTrait;
|
||||
import net.citizensnpcs.trait.Controllable;
|
||||
import net.citizensnpcs.trait.CurrentLocation;
|
||||
import net.citizensnpcs.util.ChunkCoord;
|
||||
|
@ -263,6 +264,9 @@ public class EventListen implements Listener {
|
|||
|
||||
NPCLeftClickEvent leftClickEvent = new NPCLeftClickEvent(npc, damager);
|
||||
Bukkit.getPluginManager().callEvent(leftClickEvent);
|
||||
if (npc.hasTrait(CommandTrait.class)) {
|
||||
npc.getTrait(CommandTrait.class).dispatch(damager, CommandTrait.Hand.LEFT);
|
||||
}
|
||||
} else if (event instanceof EntityDamageByBlockEvent) {
|
||||
Bukkit.getPluginManager().callEvent(new NPCDamageByBlockEvent(npc, (EntityDamageByBlockEvent) event));
|
||||
} else {
|
||||
|
@ -467,6 +471,9 @@ public class EventListen implements Listener {
|
|||
Player player = event.getPlayer();
|
||||
NPCRightClickEvent rightClickEvent = new NPCRightClickEvent(npc, player);
|
||||
Bukkit.getPluginManager().callEvent(rightClickEvent);
|
||||
if (npc.hasTrait(CommandTrait.class)) {
|
||||
npc.getTrait(CommandTrait.class).dispatch(player, CommandTrait.Hand.RIGHT);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR)
|
||||
|
|
|
@ -48,6 +48,7 @@ import net.citizensnpcs.api.command.CommandContext;
|
|||
import net.citizensnpcs.api.command.CommandMessages;
|
||||
import net.citizensnpcs.api.command.Requirements;
|
||||
import net.citizensnpcs.api.command.exception.CommandException;
|
||||
import net.citizensnpcs.api.command.exception.CommandUsageException;
|
||||
import net.citizensnpcs.api.command.exception.NoPermissionsException;
|
||||
import net.citizensnpcs.api.command.exception.RequirementMissingException;
|
||||
import net.citizensnpcs.api.command.exception.ServerCommandException;
|
||||
|
@ -75,6 +76,7 @@ import net.citizensnpcs.npc.skin.SkinnableEntity;
|
|||
import net.citizensnpcs.trait.Age;
|
||||
import net.citizensnpcs.trait.Anchors;
|
||||
import net.citizensnpcs.trait.ArmorStandTrait;
|
||||
import net.citizensnpcs.trait.CommandTrait;
|
||||
import net.citizensnpcs.trait.Controllable;
|
||||
import net.citizensnpcs.trait.CurrentLocation;
|
||||
import net.citizensnpcs.trait.FollowTrait;
|
||||
|
@ -274,6 +276,38 @@ public class NPCCommands {
|
|||
|
||||
}
|
||||
|
||||
@Command(
|
||||
aliases = { "npc" },
|
||||
usage = "command|cmd (add [command] | remove [id]) (-l/-r)",
|
||||
desc = "Controls commands which will be run when clicking on an NPC",
|
||||
modifiers = { "command", "cmd" },
|
||||
min = 1,
|
||||
flags = "lr",
|
||||
permission = "citizens.npc.command")
|
||||
public void command(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
|
||||
CommandTrait commands = npc.getTrait(CommandTrait.class);
|
||||
if (args.argsLength() == 1) {
|
||||
commands.describe(sender);
|
||||
} else if (args.getString(1).equalsIgnoreCase("add")) {
|
||||
if (args.argsLength() == 2)
|
||||
throw new CommandUsageException();
|
||||
String command = args.getJoinedStrings(2);
|
||||
CommandTrait.Hand hand = args.hasFlag('l') ? CommandTrait.Hand.LEFT : CommandTrait.Hand.RIGHT;
|
||||
int id = commands.addCommand(command, hand);
|
||||
Messaging.sendTr(sender, Messages.COMMAND_ADDED, command, id);
|
||||
} else if (args.getString(1).equalsIgnoreCase("remove")) {
|
||||
if (args.argsLength() == 2)
|
||||
throw new CommandUsageException();
|
||||
int id = args.getInteger(2);
|
||||
if (!commands.hasCommandId(id))
|
||||
throw new CommandException(Messages.COMMAND_UNKNOWN_COMMAND_ID, id);
|
||||
commands.removeCommandById(id);
|
||||
Messaging.sendTr(sender, Messages.COMMAND_REMOVED, id);
|
||||
} else {
|
||||
throw new CommandUsageException();
|
||||
}
|
||||
}
|
||||
|
||||
@Command(
|
||||
aliases = { "npc" },
|
||||
usage = "controllable|control (-m(ount),-y,-n,-o)",
|
||||
|
|
|
@ -23,6 +23,7 @@ import net.citizensnpcs.api.trait.trait.Speech;
|
|||
import net.citizensnpcs.trait.Age;
|
||||
import net.citizensnpcs.trait.Anchors;
|
||||
import net.citizensnpcs.trait.ArmorStandTrait;
|
||||
import net.citizensnpcs.trait.CommandTrait;
|
||||
import net.citizensnpcs.trait.Controllable;
|
||||
import net.citizensnpcs.trait.CurrentLocation;
|
||||
import net.citizensnpcs.trait.FollowTrait;
|
||||
|
@ -56,6 +57,7 @@ public class CitizensTraitFactory implements TraitFactory {
|
|||
registerTrait(TraitInfo.create(Age.class));
|
||||
registerTrait(TraitInfo.create(ArmorStandTrait.class));
|
||||
registerTrait(TraitInfo.create(Anchors.class));
|
||||
registerTrait(TraitInfo.create(CommandTrait.class));
|
||||
registerTrait(TraitInfo.create(Controllable.class));
|
||||
registerTrait(TraitInfo.create(CurrentLocation.class));
|
||||
registerTrait(TraitInfo.create(Equipment.class));
|
||||
|
|
|
@ -0,0 +1,135 @@
|
|||
package net.citizensnpcs.trait;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
|
||||
import net.citizensnpcs.api.npc.NPC;
|
||||
import net.citizensnpcs.api.persistence.Persist;
|
||||
import net.citizensnpcs.api.persistence.PersistenceLoader;
|
||||
import net.citizensnpcs.api.persistence.Persister;
|
||||
import net.citizensnpcs.api.trait.Trait;
|
||||
import net.citizensnpcs.api.trait.TraitName;
|
||||
import net.citizensnpcs.api.util.DataKey;
|
||||
import net.citizensnpcs.api.util.Messaging;
|
||||
import net.citizensnpcs.util.Messages;
|
||||
|
||||
@TraitName("commandtrait")
|
||||
public class CommandTrait extends Trait {
|
||||
@Persist
|
||||
private final Map<String, NPCCommand> commands = Maps.newHashMap();
|
||||
|
||||
public CommandTrait() {
|
||||
super("commandtrait");
|
||||
}
|
||||
|
||||
public int addCommand(String command, Hand hand) {
|
||||
int id = getNewId();
|
||||
commands.put(String.valueOf(id), new NPCCommand(String.valueOf(id), command, hand));
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a brief description of the current state of the trait to the supplied {@link CommandSender}.
|
||||
*/
|
||||
public void describe(CommandSender sender) {
|
||||
List<NPCCommand> left = Lists.newArrayList();
|
||||
List<NPCCommand> right = Lists.newArrayList();
|
||||
for (NPCCommand command : commands.values()) {
|
||||
if (command.hand == Hand.LEFT) {
|
||||
left.add(command);
|
||||
} else {
|
||||
right.add(command);
|
||||
}
|
||||
}
|
||||
String output = "";
|
||||
if (left.size() > 0) {
|
||||
output += Messaging.tr(Messages.COMMAND_LEFT_HAND_HEADER);
|
||||
for (NPCCommand command : left) {
|
||||
output += "<br> - [" + command.id + "]: " + command.command;
|
||||
}
|
||||
}
|
||||
if (right.size() > 0) {
|
||||
output += Messaging.tr(Messages.COMMAND_RIGHT_HAND_HEADER);
|
||||
for (NPCCommand command : right) {
|
||||
output += "<br> - [" + command.id + "]: " + command.command;
|
||||
}
|
||||
}
|
||||
if (output.isEmpty()) {
|
||||
output = Messaging.tr(Messages.COMMAND_NO_COMMANDS_ADDED);
|
||||
}
|
||||
Messaging.send(sender, output);
|
||||
}
|
||||
|
||||
public void dispatch(Player player, Hand hand) {
|
||||
for (NPCCommand command : commands.values()) {
|
||||
if (command.hand != hand)
|
||||
continue;
|
||||
command.run(npc, player);
|
||||
}
|
||||
}
|
||||
|
||||
private int getNewId() {
|
||||
int i = 0;
|
||||
while (commands.containsKey(String.valueOf(i))) {
|
||||
i++;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
public boolean hasCommandId(int id) {
|
||||
return commands.containsKey(String.valueOf(id));
|
||||
}
|
||||
|
||||
public void removeCommandById(int id) {
|
||||
commands.remove(String.valueOf(id));
|
||||
}
|
||||
|
||||
public enum Hand {
|
||||
LEFT,
|
||||
RIGHT;
|
||||
}
|
||||
|
||||
private static class NPCCommand {
|
||||
String command;
|
||||
Hand hand;
|
||||
String id;
|
||||
|
||||
public NPCCommand(String id, String command, Hand hand) {
|
||||
this.id = id;
|
||||
this.command = command;
|
||||
this.hand = hand;
|
||||
}
|
||||
|
||||
public void run(NPC npc, Player clicker) {
|
||||
String interpolatedCommand = command.replace("<npc>", npc.getName()).replace("<p>", clicker.getName());
|
||||
Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), interpolatedCommand);
|
||||
}
|
||||
}
|
||||
|
||||
private static class NPCCommandPersister implements Persister<NPCCommand> {
|
||||
public NPCCommandPersister() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public NPCCommand create(DataKey root) {
|
||||
return new NPCCommand(root.name(), root.getString("command"), Hand.valueOf(root.getString("hand")));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(NPCCommand instance, DataKey root) {
|
||||
root.setString("command", instance.command);
|
||||
root.setString("hand", instance.hand.name());
|
||||
}
|
||||
}
|
||||
|
||||
static {
|
||||
PersistenceLoader.registerPersistDelegate(NPCCommand.class, NPCCommandPersister.class);
|
||||
}
|
||||
}
|
|
@ -116,6 +116,13 @@ public class LookClose extends Trait implements Toggleable, CommandConfigurable
|
|||
@Override
|
||||
public void load(DataKey key) {
|
||||
range = key.getDouble("range");
|
||||
// TODO: remove in a later version, defaults weren't saving properly
|
||||
if (randomPitchRange == null) {
|
||||
randomPitchRange = new float[] { -10, 0 };
|
||||
}
|
||||
if (randomYawRange == null) {
|
||||
randomYawRange = new float[] { 0, 360 };
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -39,19 +39,25 @@ public class Messages {
|
|||
public static final String COLLAR_COLOUR_NOT_SUPPORTED = "citizens.commands.npc.wolf.collar-color-unsupported";
|
||||
public static final String COLLIDABLE_SET = "citizens.commands.npc.collidable.set";
|
||||
public static final String COLLIDABLE_UNSET = "citizens.commands.npc.collidable.unset";
|
||||
public static final String COMMAND_ADDED = "citizens.commands.npc.command.command-added";
|
||||
public static final String COMMAND_AGE_HELP = "citizens.commands.npc.age.help";
|
||||
public static final String COMMAND_HELP_HEADER = "citizens.commands.help.header";
|
||||
public static final String COMMAND_INVALID_MOBTYPE = "citizens.commands.invalid-mobtype";
|
||||
public static final String COMMAND_LEFT_HAND_HEADER = "citizens.commands.npc.command.left-hand-header";
|
||||
public static final String COMMAND_MISSING = "citizens.commands.help.command-missing";
|
||||
public static final String COMMAND_MUST_BE_INGAME = "citizens.commands.requirements.must-be-ingame";
|
||||
public static final String COMMAND_MUST_BE_OWNER = "citizens.commands.requirements.must-be-owner";
|
||||
public static final String COMMAND_MUST_HAVE_SELECTED = "citizens.commands.requirements.must-have-selected";
|
||||
public static final String COMMAND_NO_COMMANDS_ADDED = "citizens.commands.npc.command.none-added";
|
||||
public static final String COMMAND_PAGE_MISSING = "citizens.commands.page-missing";
|
||||
public static final String COMMAND_REMOVED = "citizens.commands.npc.command.command-removed";
|
||||
public static final String COMMAND_REPORT_ERROR = "citizens.commands.console-error";
|
||||
public static final String COMMAND_REQUIREMENTS_INVALID_MOB_TYPE = "citizens.commands.requirements.disallowed-mobtype";
|
||||
public static final String COMMAND_RIGHT_HAND_HEADER = "citizens.commands.npc.command.right-hand-header";
|
||||
public static final String COMMAND_SAVE_HELP = "citizens.commands.citizens.save.help";
|
||||
public static final String COMMAND_TOO_FEW_ARGUMENTS = "citizens.commands.requirements.too-few-arguments";
|
||||
public static final String COMMAND_TOO_MANY_ARGUMENTS = "citizens.commands.requirements.too-many-arguments";
|
||||
public static final String COMMAND_UNKNOWN_COMMAND_ID = "citizens.commands.npc.command.unknown-id";
|
||||
public static final String CONTROLLABLE_REMOVED = "citizens.commands.npc.controllable.removed";
|
||||
public static final String CONTROLLABLE_SET = "citizens.commands.npc.controllable.set";
|
||||
public static final String COPIER_EDITOR_BEGIN = "citizens.editors.copier.begin";
|
||||
|
|
|
@ -33,6 +33,12 @@ citizens.commands.npc.cat.sitting-stop=[[{0}]] stopped sitting.
|
|||
citizens.commands.npc.cat.type-set=Type set to [[{0}]].
|
||||
citizens.commands.npc.collidable.set=[[{0}]] will now collide with entities.
|
||||
citizens.commands.npc.collidable.unset=[[{0}]] will no longer collide with entities.
|
||||
citizens.commands.npc.command.none-added=No commands have been added.
|
||||
citizens.commands.npc.command.left-hand-header=Commands to run on [[left click]]:
|
||||
citizens.commands.npc.command.right-hand-header=Commands to run on [[right click]]:
|
||||
citizens.commands.npc.command.command-removed=Command [[{0}]] removed.
|
||||
citizens.commands.npc.command.command-added=Command [[{0}]] added with id [[{1}]].
|
||||
citizens.commands.npc.command.unknown-id=Unknown command id [[{0}]] for this NPC.
|
||||
citizens.commands.npc.controllable.not-controllable=[[{0}]] is not controllable.
|
||||
citizens.commands.npc.controllable.removed=[[{0}]] can no longer be controlled.
|
||||
citizens.commands.npc.controllable.set=[[{0}]] can now be controlled.
|
||||
|
|
Loading…
Reference in New Issue