From 05cc8de3c769b9982fc9a3cbec2beea36157b466 Mon Sep 17 00:00:00 2001 From: fullwall Date: Tue, 30 Jun 2020 15:20:02 +0800 Subject: [PATCH] Implement /npc command cost --- .../net/citizensnpcs/PaymentListener.java | 10 +++--- .../citizensnpcs/commands/NPCCommands.java | 3 ++ .../net/citizensnpcs/trait/CommandTrait.java | 32 +++++++++++++++++++ .../java/net/citizensnpcs/util/Messages.java | 1 + .../src/main/resources/messages_en.properties | 3 +- 5 files changed, 43 insertions(+), 6 deletions(-) diff --git a/main/src/main/java/net/citizensnpcs/PaymentListener.java b/main/src/main/java/net/citizensnpcs/PaymentListener.java index 76497858a..d084faf04 100644 --- a/main/src/main/java/net/citizensnpcs/PaymentListener.java +++ b/main/src/main/java/net/citizensnpcs/PaymentListener.java @@ -1,5 +1,10 @@ package net.citizensnpcs; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; + +import com.google.common.base.Preconditions; + import net.citizensnpcs.Settings.Setting; import net.citizensnpcs.api.event.PlayerCreateNPCEvent; import net.citizensnpcs.api.util.Messaging; @@ -7,11 +12,6 @@ import net.citizensnpcs.util.Messages; import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.economy.EconomyResponse; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; - -import com.google.common.base.Preconditions; - public class PaymentListener implements Listener { private final Economy provider; diff --git a/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java b/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java index 615329c71..6f58de9aa 100644 --- a/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java +++ b/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java @@ -332,6 +332,9 @@ public class NPCCommands { commands.setTemporaryPermissions(temporaryPermissions); Messaging.sendTr(sender, Messages.COMMAND_TEMPORARY_PERMISSIONS_SET, Joiner.on(' ').join(temporaryPermissions)); + } else if (args.getString(1).equalsIgnoreCase("cost")) { + commands.setCost(args.getDouble(2)); + Messaging.sendTr(sender, Messages.COMMAND_COST_SET, args.getDouble(2)); } else { throw new CommandUsageException(); } diff --git a/main/src/main/java/net/citizensnpcs/trait/CommandTrait.java b/main/src/main/java/net/citizensnpcs/trait/CommandTrait.java index be45f9221..bf3baf57c 100644 --- a/main/src/main/java/net/citizensnpcs/trait/CommandTrait.java +++ b/main/src/main/java/net/citizensnpcs/trait/CommandTrait.java @@ -9,6 +9,7 @@ import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.permissions.PermissionAttachment; +import org.bukkit.plugin.RegisteredServiceProvider; import com.google.common.base.Predicate; import com.google.common.base.Splitter; @@ -20,6 +21,7 @@ import com.google.common.io.ByteStreams; import net.citizensnpcs.api.CitizensAPI; import net.citizensnpcs.api.command.CommandMessages; +import net.citizensnpcs.api.event.NPCCommandDispatchEvent; import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.persistence.DelegatePersistence; import net.citizensnpcs.api.persistence.Persist; @@ -32,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.milkbowl.vault.economy.Economy; @TraitName("commandtrait") public class CommandTrait extends Trait { @@ -42,6 +45,8 @@ public class CommandTrait extends Trait { @DelegatePersistence(PlayerNPCCommandPersister.class) private final Map cooldowns = Maps.newHashMap(); @Persist + private double cost = -1; + @Persist private boolean sequential = false; @Persist private final List temporaryPermissions = Lists.newArrayList(); @@ -56,6 +61,23 @@ public class CommandTrait extends Trait { return id; } + private boolean checkPreconditions(Player player, Hand hand) { + if (cost > 0) { + try { + RegisteredServiceProvider provider = Bukkit.getServicesManager() + .getRegistration(Economy.class); + if (provider != null && provider.getProvider() != null) { + Economy economy = provider.getProvider(); + if (!economy.has(player, cost)) + return false; + economy.withdrawPlayer(player, cost); + } + } catch (NoClassDefFoundError e) { + } + } + return true; + } + /** * Send a brief description of the current state of the trait to the supplied {@link CommandSender}. */ @@ -102,6 +124,12 @@ public class CommandTrait extends Trait { } public void dispatch(final Player player, final Hand hand) { + NPCCommandDispatchEvent event = new NPCCommandDispatchEvent(npc, player); + event.setCancelled(!checkPreconditions(player, hand)); + Bukkit.getServer().getPluginManager().callEvent(event); + if (event.isCancelled()) { + return; + } Runnable task = new Runnable() { @Override public void run() { @@ -189,6 +217,10 @@ public class CommandTrait extends Trait { commands.remove(String.valueOf(id)); } + public void setCost(double cost) { + this.cost = cost; + } + public void setSequential(boolean sequential) { this.sequential = sequential; } diff --git a/main/src/main/java/net/citizensnpcs/util/Messages.java b/main/src/main/java/net/citizensnpcs/util/Messages.java index dede266b2..99c4055dc 100644 --- a/main/src/main/java/net/citizensnpcs/util/Messages.java +++ b/main/src/main/java/net/citizensnpcs/util/Messages.java @@ -48,6 +48,7 @@ public class Messages { public static final String COLLIDABLE_UNSET = "citizens.commands.npc.collidable.unset"; 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_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 830ff3d81..ac956ed46 100644 --- a/main/src/main/resources/messages_en.properties +++ b/main/src/main/resources/messages_en.properties @@ -42,11 +42,12 @@ citizens.commands.npc.cat.type-set=Type set to [[{0}]]. 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.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]]).
[[--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]].
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]]).
[[--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.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.