Citizens2/main/src/main/java/net/citizensnpcs/trait/CommandTrait.java

156 lines
4.9 KiB
Java
Raw Normal View History

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;
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;
import net.citizensnpcs.util.Messages;
@TraitName("commandtrait")
public class CommandTrait extends Trait {
@Persist
2019-10-01 08:16:00 +02:00
@DelegatePersistence(NPCCommandPersister.class)
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) {
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));
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}.
*/
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) {
left.add(command);
2020-01-17 10:11:23 +01:00
}
if (command.hand == Hand.RIGHT || command.hand == Hand.BOTH) {
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)
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;
}
private static class NPCCommand {
String command;
Hand hand;
String id;
boolean player;
2019-12-29 14:14:38 +01:00
boolean op;
2019-12-29 14:14:38 +01:00
public NPCCommand(String id, String command, Hand hand, boolean player, boolean op) {
this.id = id;
this.command = command;
this.hand = hand;
this.player = player;
2019-12-29 14:14:38 +01:00
this.op = op;
}
public void run(NPC npc, Player clicker) {
2020-01-16 12:40:32 +01:00
String interpolatedCommand = Placeholders.replace(command, clicker, npc);
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
}
} else {
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")),
2019-12-29 14:14:38 +01:00
Boolean.valueOf(root.getString("player")), Boolean.valueOf(root.getString("op")));
}
@Override
public void save(NPCCommand instance, DataKey root) {
root.setString("command", instance.command);
root.setString("hand", instance.hand.name());
root.setBoolean("player", instance.player);
2019-12-29 14:14:38 +01:00
root.setBoolean("op", instance.op);
}
}
}