Rework individual command cost to be a value flag rather than an argument, reduce public-facing command ID exposure

This commit is contained in:
fullwall 2024-03-03 18:20:09 +08:00
parent a24064ddae
commit 473135c7fb
6 changed files with 16 additions and 84 deletions

View File

@ -215,11 +215,7 @@
<relocation>
<pattern>gnu.trove</pattern>
<shadedPattern>clib.trove</shadedPattern>
</relocation>
<relocation>
<pattern>ch.ethz.globis.phtree</pattern>
<shadedPattern>clib.phtree</shadedPattern>
</relocation>
</relocation>
<relocation>
<pattern>net.kyori</pattern>
<shadedPattern>clib.net.kyori</shadedPattern>

View File

@ -7,7 +7,6 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Pattern;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
@ -78,8 +77,6 @@ import net.citizensnpcs.npc.NPCSelector;
import net.citizensnpcs.npc.Template;
import net.citizensnpcs.npc.profile.ProfileFetcher;
import net.citizensnpcs.npc.skin.Skin;
import net.citizensnpcs.trait.ClickRedirectTrait;
import net.citizensnpcs.trait.CommandTrait;
import net.citizensnpcs.trait.ShopTrait;
import net.citizensnpcs.util.Messages;
import net.citizensnpcs.util.NMS;
@ -287,8 +284,8 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
lib.addMavenCentral();
lib.setLogLevel(LogLevel.WARN);
// Unfortunately, transitive dependency management is not supported in this library.
lib.loadLibrary(Library.builder().groupId("ch{}ethz{}globis{}phtree").artifactId("phtree").version("2.8.0")
.relocate("ch{}ethz{}globis{}phtree", "clib{}phtree").build());
lib.loadLibrary(
Library.builder().groupId("ch{}ethz{}globis{}phtree").artifactId("phtree").version("2.8.0").build());
lib.loadLibrary(Library.builder().groupId("net{}sf{}trove4j").artifactId("trove4j").version("3.0.3")
.relocate("gnu{}trove", "clib{}trove").build());
lib.loadLibrary(Library.builder().groupId("net{}kyori").artifactId("adventure-text-minimessage")
@ -400,12 +397,6 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
Bukkit.getPluginManager().registerEvents(new EventListen(), this);
Bukkit.getPluginManager().registerEvents(new Placeholders(), this);
Placeholders.registerNPCPlaceholder(Pattern.compile("command_[a-zA-Z_0-9]+"), (npc, sender, input) -> {
npc = npc.hasTrait(ClickRedirectTrait.class) ? npc.getTraitNullable(ClickRedirectTrait.class).getNPC()
: npc;
CommandTrait trait = npc.getTraitNullable(CommandTrait.class);
return trait == null ? "" : trait.fillPlaceholder(sender, input);
});
Plugin papi = Bukkit.getPluginManager().getPlugin("PlaceholderAPI");
if (papi != null && papi.isEnabled()) {
@ -559,7 +550,6 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
}
}
public void storeNPCs() {
storeNPCs(false);
}

View File

@ -468,6 +468,8 @@ public class NPCCommands {
permission = "citizens.npc.command")
public void command(CommandContext args, CommandSender sender, NPC npc,
@Flag(value = { "permissions", "permission" }) String permissions,
@Flag(value = "cost", defValue = "0") Double cost,
@Flag(value = "expcost", defValue = "0") Integer experienceCost,
@Flag(value = "cooldown", defValue = "0") Duration cooldown,
@Flag(value = "gcooldown", defValue = "0") Duration gcooldown, @Flag(value = "n", defValue = "-1") int n,
@Flag(value = "delay", defValue = "0") Duration delay,
@ -502,7 +504,7 @@ public class NPCCommands {
try {
int id = commands.addCommand(new NPCCommandBuilder(command, hand).addPerms(perms)
.player(args.hasFlag('p') || args.hasFlag('o')).op(args.hasFlag('o')).cooldown(cooldown)
.globalCooldown(gcooldown).n(n).delay(delay));
.cost(cost).experienceCost(experienceCost).globalCooldown(gcooldown).n(n).delay(delay));
Messaging.sendTr(sender, Messages.COMMAND_ADDED, command, id);
} catch (NumberFormatException ex) {
throw new CommandException(CommandMessages.INVALID_NUMBER);
@ -561,25 +563,13 @@ public class NPCCommands {
} else if (action.equalsIgnoreCase("cost")) {
if (args.argsLength() == 2)
throw new CommandException(Messages.COMMAND_MISSING_COST);
if (args.argsLength() == 4) {
commands.setCost(args.getDouble(2), args.getInteger(3));
Messaging.sendTr(sender, Messages.COMMAND_INDIVIDUAL_COST_SET,
args.getDouble(2) == -1 ? "-1 (default)" : args.getDouble(2), args.getInteger(3));
} else {
commands.setCost(args.getDouble(2));
Messaging.sendTr(sender, Messages.COMMAND_COST_SET, args.getDouble(2));
}
commands.setCost(args.getDouble(2));
Messaging.sendTr(sender, Messages.COMMAND_COST_SET, args.getDouble(2));
} else if (action.equalsIgnoreCase("expcost")) {
if (args.argsLength() == 2)
throw new CommandException(Messages.COMMAND_MISSING_COST);
if (args.argsLength() == 4) {
commands.setExperienceCost(args.getInteger(2), args.getInteger(3));
Messaging.sendTr(sender, Messages.COMMAND_INDIVIDUAL_EXPERIENCE_COST_SET,
args.getInteger(2) == -1 ? "-1 (default)" : args.getInteger(2), args.getInteger(3));
} else {
commands.setExperienceCost(args.getInteger(2));
Messaging.sendTr(sender, Messages.COMMAND_EXPERIENCE_COST_SET, args.getInteger(2));
}
commands.setExperienceCost(args.getInteger(2));
Messaging.sendTr(sender, Messages.COMMAND_EXPERIENCE_COST_SET, args.getInteger(2));
} else if (action.equalsIgnoreCase("hideerrors")) {
commands.setHideErrorMessages(!commands.isHideErrorMessages());
Messaging.sendTr(sender, commands.isHideErrorMessages() ? Messages.COMMAND_HIDE_ERROR_MESSAGES_SET

View File

@ -236,8 +236,8 @@ public class CommandTrait extends Trait {
String output = Messaging.tr(Messages.COMMAND_DESCRIBE_TEMPLATE, command.command,
StringHelper.wrap(command.cooldown != 0 ? command.cooldown
: Setting.NPC_COMMAND_GLOBAL_COMMAND_COOLDOWN.asSeconds()),
StringHelper.wrap(hasCost(command.id) ? command.cost : "default"),
StringHelper.wrap(hasExperienceCost(command.id) ? command.experienceCost : "default"), command.id);
StringHelper.wrap(command.cost > 0 ? command.cost : "default"),
StringHelper.wrap(command.experienceCost > 0 ? command.experienceCost : "default"), command.id);
if (command.globalCooldown > 0) {
output += "[global " + StringHelper.wrap(command.globalCooldown) + "s]";
}
@ -362,34 +362,18 @@ public class CommandTrait extends Trait {
}
}
public String fillPlaceholder(CommandSender sender, String input) {
return null;
}
public double getCost() {
return cost;
}
public double getCost(int id) {
return commands.get(id).cost;
}
public ExecutionMode getExecutionMode() {
return executionMode;
}
public float getExperienceCost() {
public int getExperienceCost() {
return experienceCost;
}
public int getExperienceCost(int id) {
return commands.get(id).experienceCost;
}
public List<ItemStack> getItemCost(int id) {
return commands.get(id).itemCost;
}
private int getNewId() {
int i = 0;
while (commands.containsKey(i)) {
@ -402,18 +386,6 @@ public class CommandTrait extends Trait {
return commands.containsKey(id);
}
public boolean hasCost(int id) {
return commands.get(id).cost != -1;
}
public boolean hasExperienceCost(int id) {
return commands.get(id).experienceCost != -1;
}
public boolean hasItemCost(int id) {
return !commands.get(id).itemCost.isEmpty();
}
public boolean isHideErrorMessages() {
return hideErrorMessages;
}
@ -462,10 +434,6 @@ public class CommandTrait extends Trait {
this.cost = cost;
}
public void setCost(double cost, int id) {
commands.get(id).cost = cost;
}
public void setCustomErrorMessage(CommandTraitError which, String message) {
customErrorMessages.put(which, message);
}
@ -478,19 +446,10 @@ public class CommandTrait extends Trait {
this.experienceCost = experienceCost;
}
public void setExperienceCost(int experienceCost, int id) {
commands.get(id).experienceCost = experienceCost;
}
public void setHideErrorMessages(boolean hide) {
hideErrorMessages = hide;
}
public void setItemCost(List<ItemStack> itemCost, int id) {
commands.get(id).itemCost.clear();
commands.get(id).itemCost.addAll(itemCost);
}
public void setPersistSequence(boolean persistSequence) {
this.persistSequence = persistSequence;
}
@ -586,7 +545,8 @@ public class CommandTrait extends Trait {
trait.itemRequirements.clear();
trait.itemRequirements.addAll(requirements);
} else {
trait.setItemCost(requirements, id);
trait.commands.get(id).itemCost.clear();
trait.commands.get(id).itemCost.addAll(requirements);
}
}
}

View File

@ -62,8 +62,6 @@ public class Messages {
public static final String COMMAND_HELP_HEADER = "citizens.commands.help.header";
public static final String COMMAND_HIDE_ERROR_MESSAGES_SET = "citizens.commands.npc.command.hide-error-messages-set";
public static final String COMMAND_HIDE_ERROR_MESSAGES_UNSET = "citizens.commands.npc.command.hide-error-messages-unset";
public static final String COMMAND_INDIVIDUAL_COST_SET = "citizens.commands.npc.command.individual-cost-set";
public static final String COMMAND_INDIVIDUAL_EXPERIENCE_COST_SET = "citizens.commands.npc.command.individual-experience-cost-set";
public static final String COMMAND_INVALID_MOBTYPE = "citizens.commands.invalid-mobtype";
public static final String COMMAND_LEFT_HAND_HEADER = "citizens.commands.npc.command.left-hand-header";
public static final String COMMAND_MISSING = "citizens.commands.help.command-missing";

View File

@ -97,9 +97,7 @@
"citizens.commands.npc.command.experience-cost-set" : "Set xp level cost per click to [[{0}]].",
"citizens.commands.npc.command.help" : "<br>Use the [[-l]] flag to make the command run on left click, [[-r]] on right click (default).<br>Set the per-player cooldown before the command can be used again using [[--cooldown]] (in [[seconds]]).<br>Set the server-wide cooldown in seconds using [[--gcooldown]].<br>[[--delay]] will wait the specified amount in [[ticks]] before executing the command.<br>[[--permissions]] will set the command to require specific permissions (separate multiple with commas).<br>[[--n]] will only let the player run the command that number of times.<br>Use [[-o]] to temporarily execute the command as an op and [[-p]] to run the command as the clicking player instead of the server.<br>To give the player temporary permissions instead of op, use [[/npc command permissions]].<br>Set the cost of each click with [[/npc command cost/expcost/itemcost]].<br>Commands can be executed one by one instead of all at once by using [[/npc command sequential]] or [[/npc command cycle]].",
"citizens.commands.npc.command.hide-error-messages-set" : "Now hiding error messages.",
"citizens.commands.npc.command.hide-error-messages-unset" : "No longer hiding error messages.",
"citizens.commands.npc.command.individual-cost-set" : "Set cost per click to [[{0}]] for command id [[{1}]].",
"citizens.commands.npc.command.individual-experience-cost-set" : "Set xp level cost per click to [[{0}]] for command id [[{1}]].",
"citizens.commands.npc.command.hide-error-messages-unset" : "No longer hiding error messages.",
"citizens.commands.npc.command.invalid-error-message" : "Invalid error message. Valid messages are [[{0}]].",
"citizens.commands.npc.command.left-hand-header" : "Commands to run on [[left click]]:",
"citizens.commands.npc.command.none-added" : "No commands have been added.",