First pass of /npc command sequential

This commit is contained in:
fullwall 2020-06-11 02:24:03 +08:00
parent 500e8527e1
commit 1fd6ccbad9
4 changed files with 63 additions and 12 deletions

View File

@ -288,7 +288,7 @@ public class NPCCommands {
@Command(
aliases = { "npc" },
usage = "command|cmd (add [command] | remove [id] | permissions [permissions]) (-l[eft]/-r[ight]) (-p[layer] -o[p]), --cooldown [seconds] --delay [ticks] --permissions [perms] --n [max # of uses]",
usage = "command|cmd (add [command] | remove [id] | permissions [permissions] | sequential) (-l[eft]/-r[ight]) (-p[layer] -o[p]), --cooldown [seconds] --delay [ticks] --permissions [perms] --n [max # of uses]",
desc = "Controls commands which will be run when clicking on an NPC",
modifiers = { "command", "cmd" },
min = 1,
@ -313,6 +313,10 @@ public class NPCCommands {
.op(args.hasFlag('o')).cooldown(args.getFlagInteger("cooldown", 0))
.n(args.getFlagInteger("n", -1)).delay(args.getFlagInteger("delay", 0)));
Messaging.sendTr(sender, Messages.COMMAND_ADDED, command, id);
} else if (args.getString(1).equalsIgnoreCase("sequential")) {
commands.setSequential(!commands.isSequential());
Messaging.sendTr(sender,
commands.isSequential() ? Messages.COMMANDS_SEQUENTIAL_SET : Messages.COMMANDS_SEQUENTIAL_UNSET);
} else if (args.getString(1).equalsIgnoreCase("remove")) {
if (args.argsLength() == 2)
throw new CommandUsageException();

View File

@ -1,5 +1,7 @@
package net.citizensnpcs.trait;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
@ -8,7 +10,9 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.permissions.PermissionAttachment;
import com.google.common.base.Predicate;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.io.ByteArrayDataOutput;
@ -37,6 +41,8 @@ public class CommandTrait extends Trait {
@DelegatePersistence(PlayerNPCCommandPersister.class)
private final Map<String, PlayerNPCCommand> cooldowns = Maps.newHashMap();
@Persist
private boolean sequential = false;
@Persist
private final List<String> temporaryPermissions = Lists.newArrayList();
public CommandTrait() {
@ -45,7 +51,7 @@ public class CommandTrait extends Trait {
public int addCommand(NPCCommandBuilder builder) {
int id = getNewId();
commands.put(String.valueOf(id), builder.build(String.valueOf(id)));
commands.put(String.valueOf(id), builder.build(id));
return id;
}
@ -98,14 +104,39 @@ public class CommandTrait extends Trait {
Runnable task = new Runnable() {
@Override
public void run() {
for (NPCCommand command : commands.values()) {
if (command.hand != hand && command.hand != Hand.BOTH)
continue;
Iterable<NPCCommand> commandList = Iterables.filter(commands.values(), new Predicate<NPCCommand>() {
@Override
public boolean apply(NPCCommand command) {
return command.hand == hand || command.hand == Hand.BOTH;
}
});
int max = -1;
if (sequential) {
commandList = Lists.newArrayList(commandList);
List<NPCCommand> downcast = (List<NPCCommand>) commandList;
Collections.sort(downcast, new Comparator<NPCCommand>() {
@Override
public int compare(NPCCommand o1, NPCCommand o2) {
return Integer.compare(o1.id, o2.id);
}
});
max = downcast.size() > 0 ? downcast.get(downcast.size() - 1).id : -1;
}
for (NPCCommand command : commandList) {
if (sequential) {
PlayerNPCCommand info = cooldowns.get(player.getUniqueId().toString());
if (info != null && command.id < info.lastUsedId) {
if (info.lastUsedId == max) {
info.lastUsedId = -1;
}
continue;
}
}
Runnable runnable = new Runnable() {
@Override
public void run() {
PlayerNPCCommand info = cooldowns.get(player.getUniqueId().toString());
if (info == null && (command.cooldown > 0 || command.n > 0)) {
if (info == null && (command.cooldown > 0 || command.n > 0 || sequential)) {
cooldowns.put(player.getUniqueId().toString(), info = new PlayerNPCCommand());
}
if (info != null && !info.canUse(player, command)) {
@ -148,10 +179,18 @@ public class CommandTrait extends Trait {
return commands.containsKey(String.valueOf(id));
}
public boolean isSequential() {
return this.sequential;
}
public void removeCommandById(int id) {
commands.remove(String.valueOf(id));
}
public void setSequential(boolean sequential) {
this.sequential = sequential;
}
public void setTemporaryPermissions(List<String> permissions) {
temporaryPermissions.clear();
temporaryPermissions.addAll(permissions);
@ -169,13 +208,13 @@ public class CommandTrait extends Trait {
int cooldown;
int delay;
Hand hand;
String id;
int id;
int n;
boolean op;
List<String> perms;
boolean player;
public NPCCommand(String id, String command, Hand hand, boolean player, boolean op, int cooldown,
public NPCCommand(int id, String command, Hand hand, boolean player, boolean op, int cooldown,
List<String> perms, int n, int delay) {
this.id = id;
this.command = command;
@ -246,7 +285,7 @@ public class CommandTrait extends Trait {
return this;
}
private NPCCommand build(String id) {
private NPCCommand build(int id) {
return new NPCCommand(id, command, hand, player, op, cooldown, perms, n, delay);
}
@ -291,9 +330,10 @@ public class CommandTrait extends Trait {
for (DataKey key : root.getRelative("permissions").getIntegerSubKeys()) {
perms.add(key.getString(""));
}
return new NPCCommand(root.name(), root.getString("command"), Hand.valueOf(root.getString("hand")),
Boolean.valueOf(root.getString("player")), Boolean.valueOf(root.getString("op")),
root.getInt("cooldown"), perms, root.getInt("n"), root.getInt("delay"));
return new NPCCommand(Integer.parseInt(root.name()), root.getString("command"),
Hand.valueOf(root.getString("hand")), Boolean.valueOf(root.getString("player")),
Boolean.valueOf(root.getString("op")), root.getInt("cooldown"), perms, root.getInt("n"),
root.getInt("delay"));
}
@Override
@ -315,6 +355,8 @@ public class CommandTrait extends Trait {
@Persist(valueType = Long.class)
Map<String, Long> lastUsed = Maps.newHashMap();
@Persist
int lastUsedId = -1;
@Persist
Map<String, Integer> nUsed = Maps.newHashMap();
public PlayerNPCCommand() {
@ -343,6 +385,7 @@ public class CommandTrait extends Trait {
if (command.n > 0) {
nUsed.put(command.command, previouslyUsed + 1);
}
lastUsedId = command.id;
return true;
}
}

View File

@ -66,6 +66,8 @@ public class Messages {
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 COMMANDS_SEQUENTIAL_SET = "citizens.commands.npc.commands.sequential-set";
public static final String COMMANDS_SEQUENTIAL_UNSET = "citizens.commands.npc.commands.sequential-unset";
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";

View File

@ -48,6 +48,8 @@ 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.command.temporary-permissions-set=Temporary permissions set to [[{0}]].
citizens.commands.npc.commands.sequential-set=Commands will now execute sequentially.
citizens.commands.npc.commands.sequential-unset=Commands will no longer execute sequentially.
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.