2019-09-23 14:29:31 +02:00
|
|
|
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;
|
2019-10-01 08:16:00 +02:00
|
|
|
import net.citizensnpcs.api.persistence.DelegatePersistence;
|
2019-09-23 14:29:31 +02:00
|
|
|
import net.citizensnpcs.api.persistence.Persist;
|
|
|
|
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;
|
2020-01-16 12:40:32 +01:00
|
|
|
import net.citizensnpcs.api.util.Placeholders;
|
2019-09-23 14:29:31 +02:00
|
|
|
import net.citizensnpcs.util.Messages;
|
|
|
|
|
|
|
|
@TraitName("commandtrait")
|
|
|
|
public class CommandTrait extends Trait {
|
|
|
|
@Persist
|
2019-10-01 08:16:00 +02:00
|
|
|
@DelegatePersistence(NPCCommandPersister.class)
|
2019-09-23 14:29:31 +02:00
|
|
|
private final Map<String, NPCCommand> commands = Maps.newHashMap();
|
|
|
|
|
|
|
|
public CommandTrait() {
|
|
|
|
super("commandtrait");
|
|
|
|
}
|
|
|
|
|
2019-12-29 14:14:38 +01:00
|
|
|
public int addCommand(String command, Hand hand, boolean player, boolean op) {
|
2019-09-23 14:29:31 +02:00
|
|
|
int id = getNewId();
|
2019-12-29 14:14:38 +01:00
|
|
|
commands.put(String.valueOf(id), new NPCCommand(String.valueOf(id), command, hand, player, op));
|
2019-09-23 14:29:31 +02:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-17 10:11:23 +01:00
|
|
|
* Send a brief description of the current state of the trait to the supplied
|
|
|
|
* {@link CommandSender}.
|
2019-09-23 14:29:31 +02:00
|
|
|
*/
|
|
|
|
public void describe(CommandSender sender) {
|
|
|
|
List<NPCCommand> left = Lists.newArrayList();
|
|
|
|
List<NPCCommand> right = Lists.newArrayList();
|
|
|
|
for (NPCCommand command : commands.values()) {
|
2020-01-17 10:11:23 +01:00
|
|
|
if (command.hand == Hand.LEFT || command.hand == Hand.BOTH) {
|
2019-09-23 14:29:31 +02:00
|
|
|
left.add(command);
|
2020-01-17 10:11:23 +01:00
|
|
|
}
|
|
|
|
if (command.hand == Hand.RIGHT || command.hand == Hand.BOTH) {
|
2019-09-23 14:29:31 +02:00
|
|
|
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()) {
|
2020-01-17 10:11:23 +01:00
|
|
|
if (command.hand != hand && command.hand != Hand.BOTH)
|
2019-09-23 14:29:31 +02:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2019-10-01 08:16:00 +02:00
|
|
|
public static enum Hand {
|
2020-01-17 10:11:23 +01:00
|
|
|
BOTH, LEFT, RIGHT;
|
2019-09-23 14:29:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static class NPCCommand {
|
|
|
|
String command;
|
|
|
|
Hand hand;
|
|
|
|
String id;
|
2019-12-02 04:27:15 +01:00
|
|
|
boolean player;
|
2019-12-29 14:14:38 +01:00
|
|
|
boolean op;
|
2019-09-23 14:29:31 +02:00
|
|
|
|
2019-12-29 14:14:38 +01:00
|
|
|
public NPCCommand(String id, String command, Hand hand, boolean player, boolean op) {
|
2019-09-23 14:29:31 +02:00
|
|
|
this.id = id;
|
|
|
|
this.command = command;
|
|
|
|
this.hand = hand;
|
2019-12-02 04:27:15 +01:00
|
|
|
this.player = player;
|
2019-12-29 14:14:38 +01:00
|
|
|
this.op = op;
|
2019-09-23 14:29:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void run(NPC npc, Player clicker) {
|
2020-01-16 12:40:32 +01:00
|
|
|
String interpolatedCommand = Placeholders.replace(command, clicker, npc);
|
2019-12-02 04:27:15 +01:00
|
|
|
if (player) {
|
2020-01-17 10:11:23 +01:00
|
|
|
boolean wasOp = clicker.isOp();
|
|
|
|
if (op) {
|
|
|
|
clicker.setOp(true);
|
|
|
|
}
|
2020-01-17 10:15:21 +01:00
|
|
|
try {
|
|
|
|
clicker.performCommand(interpolatedCommand);
|
|
|
|
} catch (Throwable t) {
|
|
|
|
t.printStackTrace();
|
|
|
|
}
|
2019-12-29 14:14:38 +01:00
|
|
|
if (op) {
|
2020-01-17 10:11:23 +01:00
|
|
|
clicker.setOp(wasOp);
|
2019-12-29 14:14:38 +01:00
|
|
|
}
|
2019-12-02 04:27:15 +01:00
|
|
|
} else {
|
|
|
|
Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), interpolatedCommand);
|
|
|
|
}
|
2019-09-23 14:29:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class NPCCommandPersister implements Persister<NPCCommand> {
|
|
|
|
public NPCCommandPersister() {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public NPCCommand create(DataKey root) {
|
2019-12-02 04:27:15 +01:00
|
|
|
return new NPCCommand(root.name(), root.getString("command"), Hand.valueOf(root.getString("hand")),
|
2019-12-29 14:14:38 +01:00
|
|
|
Boolean.valueOf(root.getString("player")), Boolean.valueOf(root.getString("op")));
|
2019-09-23 14:29:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void save(NPCCommand instance, DataKey root) {
|
|
|
|
root.setString("command", instance.command);
|
|
|
|
root.setString("hand", instance.hand.name());
|
2019-12-02 04:27:15 +01:00
|
|
|
root.setBoolean("player", instance.player);
|
2019-12-29 14:14:38 +01:00
|
|
|
root.setBoolean("op", instance.op);
|
2019-09-23 14:29:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|