Implement /npc command cost

This commit is contained in:
fullwall 2020-06-30 15:20:02 +08:00
parent 82bdbe31c1
commit 05cc8de3c7
5 changed files with 43 additions and 6 deletions

View File

@ -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;

View File

@ -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();
}

View File

@ -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<String, PlayerNPCCommand> cooldowns = Maps.newHashMap();
@Persist
private double cost = -1;
@Persist
private boolean sequential = false;
@Persist
private final List<String> 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<Economy> 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;
}

View File

@ -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";

View File

@ -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=<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>[[--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>Commands can be executed one by one instead of all at once by using [[/npc command sequential]].
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>[[--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]].<br>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.