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.ArmorStandTrait;
import net.citizensnpcs.trait.CommandTrait;
import net.citizensnpcs.trait.CommandTrait.ExecutionMode;
import net.citizensnpcs.trait.CommandTrait.NPCCommandBuilder;
import net.citizensnpcs.trait.Controllable;
import net.citizensnpcs.trait.CurrentLocation;
@ -317,9 +318,11 @@ public class NPCCommands {
.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());
commands.setExecutionMode(commands.getExecutionMode() == ExecutionMode.SEQUENTIAL ? ExecutionMode.LINEAR
: ExecutionMode.SEQUENTIAL);
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")) {
if (args.argsLength() == 2)
throw new CommandUsageException();
@ -336,6 +339,11 @@ public class NPCCommands {
} else if (args.getString(1).equalsIgnoreCase("cost")) {
commands.setCost(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 {
throw new CommandUsageException();
}

View File

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

View File

@ -43,7 +43,7 @@ public class HologramTrait extends Trait {
}
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() {
@ -74,6 +74,11 @@ public class HologramTrait extends Trait {
unload();
}
@Override
public void onRemove() {
unload();
}
@Override
public void onSpawn() {
load();
@ -88,10 +93,10 @@ public class HologramTrait extends Trait {
@Override
public void run() {
if (!npc.isSpawned()) {
onDespawn();
unload();
return;
}
boolean update = currentLoc.distanceSquared(npc.getStoredLocation()) > 0.5;
boolean update = currentLoc.distanceSquared(npc.getStoredLocation()) >= 0.01;
if (update) {
currentLoc = npc.getStoredLocation();
}
@ -101,7 +106,7 @@ public class HologramTrait extends Trait {
if (hologram == null)
continue;
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);
}
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_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_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_UNSET = "citizens.commands.npc.commands.sequential-unset";
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.commands.sequential-set=Commands will now 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.removed=[[{0}]] can no longer be controlled.
citizens.commands.npc.controllable.set=[[{0}]] can now be controlled.