Fixes for /npc hologram, add /npc command random

This commit is contained in:
fullwall 2020-07-03 22:59:18 +08:00
parent 878263359a
commit 942b354967
5 changed files with 79 additions and 48 deletions

View File

@ -85,6 +85,7 @@ import net.citizensnpcs.trait.Age;
import net.citizensnpcs.trait.Anchors; import net.citizensnpcs.trait.Anchors;
import net.citizensnpcs.trait.ArmorStandTrait; import net.citizensnpcs.trait.ArmorStandTrait;
import net.citizensnpcs.trait.CommandTrait; import net.citizensnpcs.trait.CommandTrait;
import net.citizensnpcs.trait.CommandTrait.ExecutionMode;
import net.citizensnpcs.trait.CommandTrait.NPCCommandBuilder; import net.citizensnpcs.trait.CommandTrait.NPCCommandBuilder;
import net.citizensnpcs.trait.Controllable; import net.citizensnpcs.trait.Controllable;
import net.citizensnpcs.trait.CurrentLocation; import net.citizensnpcs.trait.CurrentLocation;
@ -317,9 +318,11 @@ public class NPCCommands {
.n(args.getFlagInteger("n", -1)).delay(args.getFlagInteger("delay", 0))); .n(args.getFlagInteger("n", -1)).delay(args.getFlagInteger("delay", 0)));
Messaging.sendTr(sender, Messages.COMMAND_ADDED, command, id); Messaging.sendTr(sender, Messages.COMMAND_ADDED, command, id);
} else if (args.getString(1).equalsIgnoreCase("sequential")) { } else if (args.getString(1).equalsIgnoreCase("sequential")) {
commands.setSequential(!commands.isSequential()); commands.setExecutionMode(commands.getExecutionMode() == ExecutionMode.SEQUENTIAL ? ExecutionMode.LINEAR
: ExecutionMode.SEQUENTIAL);
Messaging.sendTr(sender, Messaging.sendTr(sender,
commands.isSequential() ? Messages.COMMANDS_SEQUENTIAL_SET : Messages.COMMANDS_SEQUENTIAL_UNSET); commands.getExecutionMode() == ExecutionMode.SEQUENTIAL ? Messages.COMMANDS_SEQUENTIAL_SET
: Messages.COMMANDS_SEQUENTIAL_UNSET);
} else if (args.getString(1).equalsIgnoreCase("remove")) { } else if (args.getString(1).equalsIgnoreCase("remove")) {
if (args.argsLength() == 2) if (args.argsLength() == 2)
throw new CommandUsageException(); throw new CommandUsageException();
@ -336,6 +339,11 @@ public class NPCCommands {
} else if (args.getString(1).equalsIgnoreCase("cost")) { } else if (args.getString(1).equalsIgnoreCase("cost")) {
commands.setCost(args.getDouble(2)); commands.setCost(args.getDouble(2));
Messaging.sendTr(sender, Messages.COMMAND_COST_SET, args.getDouble(2)); Messaging.sendTr(sender, Messages.COMMAND_COST_SET, args.getDouble(2));
} else if (args.getString(1).equalsIgnoreCase("random")) {
commands.setExecutionMode(
commands.getExecutionMode() == ExecutionMode.RANDOM ? ExecutionMode.LINEAR : ExecutionMode.RANDOM);
Messaging.sendTr(sender, commands.getExecutionMode() == ExecutionMode.RANDOM ? Messages.COMMANDS_RANDOM_SET
: Messages.COMMANDS_RANDOM_UNSET);
} else { } else {
throw new CommandUsageException(); throw new CommandUsageException();
} }

View File

@ -34,6 +34,7 @@ import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.api.util.Placeholders; import net.citizensnpcs.api.util.Placeholders;
import net.citizensnpcs.util.Messages; import net.citizensnpcs.util.Messages;
import net.citizensnpcs.util.StringHelper; import net.citizensnpcs.util.StringHelper;
import net.citizensnpcs.util.Util;
import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.economy.Economy;
@TraitName("commandtrait") @TraitName("commandtrait")
@ -47,7 +48,7 @@ public class CommandTrait extends Trait {
@Persist @Persist
private double cost = -1; private double cost = -1;
@Persist @Persist
private boolean sequential = false; private ExecutionMode executionMode = ExecutionMode.LINEAR;
@Persist @Persist
private final List<String> temporaryPermissions = Lists.newArrayList(); private final List<String> temporaryPermissions = Lists.newArrayList();
@ -133,26 +134,28 @@ public class CommandTrait extends Trait {
Runnable task = new Runnable() { Runnable task = new Runnable() {
@Override @Override
public void run() { public void run() {
Iterable<NPCCommand> commandList = Iterables.filter(commands.values(), new Predicate<NPCCommand>() { List<NPCCommand> commandList = Lists
@Override .newArrayList(Iterables.filter(commands.values(), new Predicate<NPCCommand>() {
public boolean apply(NPCCommand command) { @Override
return command.hand == hand || command.hand == Hand.BOTH; public boolean apply(NPCCommand command) {
} return command.hand == hand || command.hand == Hand.BOTH;
}); }
}));
if (executionMode == ExecutionMode.RANDOM && commandList.size() > 0) {
runCommand(player, commandList.get(Util.getFastRandom().nextInt(commandList.size())));
}
int max = -1; int max = -1;
if (sequential) { if (executionMode == ExecutionMode.SEQUENTIAL) {
commandList = Lists.newArrayList(commandList); Collections.sort(commandList, new Comparator<NPCCommand>() {
List<NPCCommand> downcast = (List<NPCCommand>) commandList;
Collections.sort(downcast, new Comparator<NPCCommand>() {
@Override @Override
public int compare(NPCCommand o1, NPCCommand o2) { public int compare(NPCCommand o1, NPCCommand o2) {
return Integer.compare(o1.id, o2.id); return Integer.compare(o1.id, o2.id);
} }
}); });
max = downcast.size() > 0 ? downcast.get(downcast.size() - 1).id : -1; max = commandList.size() > 0 ? commandList.get(commandList.size() - 1).id : -1;
} }
for (NPCCommand command : commandList) { for (NPCCommand command : commandList) {
if (sequential) { if (executionMode == ExecutionMode.SEQUENTIAL) {
PlayerNPCCommand info = cooldowns.get(player.getUniqueId().toString()); PlayerNPCCommand info = cooldowns.get(player.getUniqueId().toString());
if (info != null && command.id <= info.lastUsedId) { if (info != null && command.id <= info.lastUsedId) {
if (info.lastUsedId == max) { if (info.lastUsedId == max) {
@ -162,31 +165,36 @@ public class CommandTrait extends Trait {
} }
} }
} }
Runnable runnable = new Runnable() { runCommand(player, command);
@Override }
public void run() { }
PlayerNPCCommand info = cooldowns.get(player.getUniqueId().toString());
if (info == null && (sequential || PlayerNPCCommand.requiresTracking(command))) { private void runCommand(final Player player, NPCCommand command) {
cooldowns.put(player.getUniqueId().toString(), info = new PlayerNPCCommand()); Runnable runnable = new Runnable() {
} @Override
if (info != null && !info.canUse(player, command)) { public void run() {
return; PlayerNPCCommand info = cooldowns.get(player.getUniqueId().toString());
} if (info == null && (executionMode == ExecutionMode.SEQUENTIAL
PermissionAttachment attachment = player.addAttachment(CitizensAPI.getPlugin()); || PlayerNPCCommand.requiresTracking(command))) {
if (temporaryPermissions.size() > 0) { cooldowns.put(player.getUniqueId().toString(), info = new PlayerNPCCommand());
for (String permission : temporaryPermissions) {
attachment.setPermission(permission, true);
}
}
command.run(npc, player);
attachment.remove();
} }
}; if (info != null && !info.canUse(player, command)) {
if (command.delay <= 0) { return;
runnable.run(); }
} else { PermissionAttachment attachment = player.addAttachment(CitizensAPI.getPlugin());
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), runnable, command.delay); if (temporaryPermissions.size() > 0) {
for (String permission : temporaryPermissions) {
attachment.setPermission(permission, true);
}
}
command.run(npc, player);
attachment.remove();
} }
};
if (command.delay <= 0) {
runnable.run();
} else {
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), runnable, command.delay);
} }
} }
}; };
@ -197,6 +205,10 @@ public class CommandTrait extends Trait {
} }
} }
public ExecutionMode getExecutionMode() {
return executionMode;
}
private int getNewId() { private int getNewId() {
int i = 0; int i = 0;
while (commands.containsKey(String.valueOf(i))) { while (commands.containsKey(String.valueOf(i))) {
@ -209,10 +221,6 @@ public class CommandTrait extends Trait {
return commands.containsKey(String.valueOf(id)); return commands.containsKey(String.valueOf(id));
} }
public boolean isSequential() {
return this.sequential;
}
public void removeCommandById(int id) { public void removeCommandById(int id) {
commands.remove(String.valueOf(id)); commands.remove(String.valueOf(id));
} }
@ -221,8 +229,8 @@ public class CommandTrait extends Trait {
this.cost = cost; this.cost = cost;
} }
public void setSequential(boolean sequential) { public void setExecutionMode(ExecutionMode mode) {
this.sequential = sequential; this.executionMode = mode;
} }
public void setTemporaryPermissions(List<String> permissions) { public void setTemporaryPermissions(List<String> permissions) {
@ -230,6 +238,12 @@ public class CommandTrait extends Trait {
temporaryPermissions.addAll(permissions); temporaryPermissions.addAll(permissions);
} }
public enum ExecutionMode {
LINEAR,
RANDOM,
SEQUENTIAL;
}
public static enum Hand { public static enum Hand {
BOTH, BOTH,
LEFT, LEFT,

View File

@ -43,7 +43,7 @@ public class HologramTrait extends Trait {
} }
private double getHeight(int lineNumber) { private double getHeight(int lineNumber) {
return (lineHeight == -1 ? Setting.DEFAULT_NPC_HOLOGRAM_LINE_HEIGHT.asDouble() : lineHeight) * lineNumber; return (lineHeight == -1 ? Setting.DEFAULT_NPC_HOLOGRAM_LINE_HEIGHT.asDouble() : lineHeight) * (lineNumber + 1);
} }
public List<String> getLines() { public List<String> getLines() {
@ -74,6 +74,11 @@ public class HologramTrait extends Trait {
unload(); unload();
} }
@Override
public void onRemove() {
unload();
}
@Override @Override
public void onSpawn() { public void onSpawn() {
load(); load();
@ -88,10 +93,10 @@ public class HologramTrait extends Trait {
@Override @Override
public void run() { public void run() {
if (!npc.isSpawned()) { if (!npc.isSpawned()) {
onDespawn(); unload();
return; return;
} }
boolean update = currentLoc.distanceSquared(npc.getStoredLocation()) > 0.5; boolean update = currentLoc.distanceSquared(npc.getStoredLocation()) >= 0.01;
if (update) { if (update) {
currentLoc = npc.getStoredLocation(); currentLoc = npc.getStoredLocation();
} }
@ -101,7 +106,7 @@ public class HologramTrait extends Trait {
if (hologram == null) if (hologram == null)
continue; continue;
if (update) { if (update) {
hologramNPC.teleport(currentLoc.clone().add(0, npc.getEntity().getHeight() + lineHeight * i, 0), hologramNPC.teleport(currentLoc.clone().add(0, npc.getEntity().getHeight() + getHeight(i), 0),
TeleportCause.PLUGIN); TeleportCause.PLUGIN);
} }
String text = lines.get(i); String text = lines.get(i);

View File

@ -67,6 +67,8 @@ public class Messages {
public static final String COMMAND_TOO_FEW_ARGUMENTS = "citizens.commands.requirements.too-few-arguments"; 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_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 COMMAND_UNKNOWN_COMMAND_ID = "citizens.commands.npc.command.unknown-id";
public static final String COMMANDS_RANDOM_SET = "citizens.commands.npc.commands.random-set";
public static final String COMMANDS_RANDOM_UNSET = "citizens.commands.npc.commands.random-unset";
public static final String COMMANDS_SEQUENTIAL_SET = "citizens.commands.npc.commands.sequential-set"; 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 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_REMOVED = "citizens.commands.npc.controllable.removed";

View File

@ -52,6 +52,8 @@ 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.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-set=Commands will now execute sequentially.
citizens.commands.npc.commands.sequential-unset=Commands will no longer execute sequentially. citizens.commands.npc.commands.sequential-unset=Commands will no longer execute sequentially.
citizens.commands.npc.commands.random-set=Commands will now execute at random.
citizens.commands.npc.commands.random-unset=Commands will no longer execute at random.
citizens.commands.npc.controllable.not-controllable=[[{0}]] is not controllable. 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.removed=[[{0}]] can no longer be controlled.
citizens.commands.npc.controllable.set=[[{0}]] can now be controlled. citizens.commands.npc.controllable.set=[[{0}]] can now be controlled.