From 7c8b136e1d1d9eaa0e533b20fe29c5711f0a7470 Mon Sep 17 00:00:00 2001 From: fullwall Date: Mon, 1 Nov 2021 20:56:54 +0800 Subject: [PATCH] Add /npc command expcost --- .../main/java/net/citizensnpcs/Settings.java | 2 ++ .../net/citizensnpcs/commands/NPCCommands.java | 5 ++++- .../net/citizensnpcs/trait/CommandTrait.java | 18 ++++++++++++++++++ .../java/net/citizensnpcs/util/Messages.java | 1 + main/src/main/resources/messages_en.properties | 3 ++- 5 files changed, 27 insertions(+), 2 deletions(-) diff --git a/main/src/main/java/net/citizensnpcs/Settings.java b/main/src/main/java/net/citizensnpcs/Settings.java index 1a855bee8..a09cfd999 100644 --- a/main/src/main/java/net/citizensnpcs/Settings.java +++ b/main/src/main/java/net/citizensnpcs/Settings.java @@ -131,6 +131,8 @@ public class Settings { NPC_COMMAND_MISSING_ITEM_MESSAGE("npc.commands.error-messages.missing-item", "Missing {1} {0}"), NPC_COMMAND_NO_PERMISSION_MESSAGE("npc.commands.error-messages.no-permission", "You don't have permission to do that."), + NPC_COMMAND_NOT_ENOUGH_EXPERIENCE_MESSAGE("npc.commands.error-messages.not-enough-experience", + "You need at least {0} experience."), NPC_COMMAND_NOT_ENOUGH_MONEY_MESSAGE("npc.commands.error-messages.not-enough-money", "You need at least ${0}."), NPC_COMMAND_ON_COOLDOWN_MESSAGE("npc.commands.error-messages.on-cooldown", "Please wait for {minutes} minutes and {seconds_over} seconds."), diff --git a/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java b/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java index e771cc100..1ee4144d7 100644 --- a/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java +++ b/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java @@ -343,7 +343,7 @@ public class NPCCommands { @Command( aliases = { "npc" }, - usage = "command|cmd (add [command] | remove [id] | permissions [permissions] | sequential | random | cost [cost]) (-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 | random | (exp|item)cost [cost]) (-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", help = Messages.NPC_COMMAND_HELP, modifiers = { "command", "cmd" }, @@ -391,6 +391,9 @@ 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("expcost")) { + commands.setExperienceCost((float) args.getDouble(2)); + Messaging.sendTr(sender, Messages.COMMAND_EXPERIENCE_COST_SET, args.getDouble(2)); } else if (args.getString(1).equalsIgnoreCase("random")) { commands.setExecutionMode( commands.getExecutionMode() == ExecutionMode.RANDOM ? ExecutionMode.LINEAR : ExecutionMode.RANDOM); diff --git a/main/src/main/java/net/citizensnpcs/trait/CommandTrait.java b/main/src/main/java/net/citizensnpcs/trait/CommandTrait.java index 3abfb367f..499f87d80 100644 --- a/main/src/main/java/net/citizensnpcs/trait/CommandTrait.java +++ b/main/src/main/java/net/citizensnpcs/trait/CommandTrait.java @@ -64,6 +64,8 @@ public class CommandTrait extends Trait { @Persist private ExecutionMode executionMode = ExecutionMode.LINEAR; @Persist + private float experienceCost = -1; + @Persist private final Map globalCooldowns = Maps.newHashMap(); @Persist private List itemRequirements = Lists.newArrayList(); @@ -97,6 +99,13 @@ public class CommandTrait extends Trait { Messaging.severe("Unable to find Vault when checking command cost - is it installed?"); } } + if (experienceCost > 0) { + if (player.getExp() < experienceCost) { + sendErrorMessage(player, CommandTraitMessages.MISSING_EXPERIENCE, null, experienceCost); + return false; + } + player.setExp(player.getExp() - experienceCost); + } if (itemRequirements.size() > 0) { List req = Lists.newArrayList(itemRequirements); Inventory tempInventory = Bukkit.createInventory(null, 54); @@ -271,6 +280,10 @@ public class CommandTrait extends Trait { return executionMode; } + public float getExperienceCost() { + return experienceCost; + } + private int getNewId() { int i = 0; while (commands.containsKey(String.valueOf(i))) { @@ -312,6 +325,10 @@ public class CommandTrait extends Trait { this.executionMode = mode; } + public void setExperienceCost(float experienceCost) { + this.experienceCost = experienceCost; + } + public void setTemporaryPermissions(List permissions) { temporaryPermissions.clear(); temporaryPermissions.addAll(permissions); @@ -319,6 +336,7 @@ public class CommandTrait extends Trait { private enum CommandTraitMessages { MAXIMUM_TIMES_USED(Setting.NPC_COMMAND_MAXIMUM_TIMES_USED_MESSAGE), + MISSING_EXPERIENCE(Setting.NPC_COMMAND_NOT_ENOUGH_EXPERIENCE_MESSAGE), MISSING_ITEM(Setting.NPC_COMMAND_MISSING_ITEM_MESSAGE), MISSING_MONEY(Setting.NPC_COMMAND_NOT_ENOUGH_MONEY_MESSAGE), NO_PERMISSION(Setting.NPC_COMMAND_NO_PERMISSION_MESSAGE), diff --git a/main/src/main/java/net/citizensnpcs/util/Messages.java b/main/src/main/java/net/citizensnpcs/util/Messages.java index 2571832e1..873f0c42e 100644 --- a/main/src/main/java/net/citizensnpcs/util/Messages.java +++ b/main/src/main/java/net/citizensnpcs/util/Messages.java @@ -57,6 +57,7 @@ public class Messages { public static final String COMMAND_ADDED = "citizens.commands.npc.command.command-added"; public static final String COMMAND_AGE_HELP = "citizens.commands.npc.age.help"; public static final String COMMAND_COST_SET = "citizens.commands.npc.command.cost-set"; + public static final String COMMAND_EXPERIENCE_COST_SET = "citizens.commands.npc.command.experience-cost-set"; public static final String COMMAND_HELP_HEADER = "citizens.commands.help.header"; 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"; diff --git a/main/src/main/resources/messages_en.properties b/main/src/main/resources/messages_en.properties index aa977d0fc..b166cc3a8 100644 --- a/main/src/main/resources/messages_en.properties +++ b/main/src/main/resources/messages_en.properties @@ -51,11 +51,12 @@ citizens.commands.npc.collidable.set=[[{0}]] will now collide with entities. citizens.commands.npc.collidable.unset=[[{0}]] will no longer collide with entities. citizens.commands.npc.command.none-added=No commands have been added. citizens.commands.npc.command.cost-set=Set cost per click to [[{0}]]. +citizens.commands.npc.command.experience-cost-set=Set experience cost per click to [[{0}]]. citizens.commands.npc.command.left-hand-header=Commands to run on [[left click]]: citizens.commands.npc.command.right-hand-header=Commands to run on [[right click]]: 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.help=
Use the [[-l]] flag to make the command run on left click, [[-r]] on right click (default).
Set the per-player cooldown before the command can be used again using [[--cooldown]] (in [[seconds]]).
Set the server-wide cooldown in seconds using [[--gcooldown]].
[[--delay]] will wait the specified amount in [[ticks]] before executing the command.
[[--permissions]] will set the command to require specific permissions (separate multiple with commas).
[[--n]] will only let the player run the command that number of times.
Use [[-o]] to temporarily execute the command as an op and [[-p]] to run the command as the clicking player instead of the server.
To give the player temporary permissions instead of op, use [[/npc command permissions]].
Set the cost of each click with [[/npc command cost]].
Commands can be executed one by one instead of all at once by using [[/npc command sequential]]. +citizens.commands.npc.command.help=
Use the [[-l]] flag to make the command run on left click, [[-r]] on right click (default).
Set the per-player cooldown before the command can be used again using [[--cooldown]] (in [[seconds]]).
Set the server-wide cooldown in seconds using [[--gcooldown]].
[[--delay]] will wait the specified amount in [[ticks]] before executing the command.
[[--permissions]] will set the command to require specific permissions (separate multiple with commas).
[[--n]] will only let the player run the command that number of times.
Use [[-o]] to temporarily execute the command as an op and [[-p]] to run the command as the clicking player instead of the server.
To give the player temporary permissions instead of op, use [[/npc command permissions]].
Set the cost of each click with [[/npc command cost/expcost/itemcost]].
Commands can be executed one by one instead of all at once by using [[/npc command sequential]]. 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.