Charge costs directly

This commit is contained in:
fullwall 2023-07-11 11:25:26 +08:00
parent b7bda87394
commit 29a98c8da6
1 changed files with 51 additions and 51 deletions

View File

@ -94,7 +94,7 @@ public class CommandTrait extends Trait {
return id; return id;
} }
private Transaction chargeCommandCosts(Player player, Hand hand, int id) { private Transaction chargeCommandCosts(Player player, Hand hand, NPCCommand command) {
NPCShopAction action = null; NPCShopAction action = null;
if (player.hasPermission("citizens.npc.command.ignoreerrors.*")) if (player.hasPermission("citizens.npc.command.ignoreerrors.*"))
return Transaction.success(); return Transaction.success();
@ -118,22 +118,23 @@ public class CommandTrait extends Trait {
stack.getAmount()); stack.getAmount());
} }
} }
if (hasCost(id) && !player.hasPermission("citizens.npc.command.ignoreerrors.cost")) { if (command.cost != -1 && !player.hasPermission("citizens.npc.command.ignoreerrors.cost")) {
action = new MoneyAction(commands.get(id).cost); action = new MoneyAction(command.cost);
if (!action.take(player, 1).isPossible()) { if (!action.take(player, 1).isPossible()) {
sendErrorMessage(player, CommandTraitError.MISSING_MONEY, null, commands.get(id).cost); sendErrorMessage(player, CommandTraitError.MISSING_MONEY, null, command.cost);
} }
} }
if (hasExperienceCost(id) && !player.hasPermission("citizens.npc.command.ignoreerrors.expcost")) { if (command.experienceCost != -1 && !player.hasPermission("citizens.npc.command.ignoreerrors.expcost")) {
action = new ExperienceAction(commands.get(id).experienceCost); action = new ExperienceAction(command.experienceCost);
if (!action.take(player, 1).isPossible()) { if (!action.take(player, 1).isPossible()) {
sendErrorMessage(player, CommandTraitError.MISSING_EXPERIENCE, null, commands.get(id).experienceCost); sendErrorMessage(player, CommandTraitError.MISSING_EXPERIENCE, null, command.experienceCost);
} }
} }
if (hasItemCost(id) && !player.hasPermission("citizens.npc.command.ignoreerrors.itemcost")) { if (command.itemCost != null && command.itemCost.size() > 0
action = new ItemAction(commands.get(id).itemCost); && !player.hasPermission("citizens.npc.command.ignoreerrors.itemcost")) {
action = new ItemAction(command.itemCost);
if (!action.take(player, 1).isPossible()) { if (!action.take(player, 1).isPossible()) {
ItemStack stack = commands.get(id).itemCost.get(0); ItemStack stack = command.itemCost.get(0);
sendErrorMessage(player, CommandTraitError.MISSING_ITEM, null, Util.prettyEnum(stack.getType()), sendErrorMessage(player, CommandTraitError.MISSING_ITEM, null, Util.prettyEnum(stack.getType()),
stack.getAmount()); stack.getAmount());
} }
@ -229,12 +230,11 @@ public class CommandTrait extends Trait {
} }
private String describe(NPCCommand command) { private String describe(NPCCommand command) {
String output = Messaging.tr(Messages.COMMAND_DESCRIBE_TEMPLATE, String output = Messaging.tr(Messages.COMMAND_DESCRIBE_TEMPLATE, command.command,
command.command, StringHelper.wrap(command.cooldown != 0 ? command.cooldown
StringHelper.wrap(command.cooldown != 0 ? command.cooldown : Setting.NPC_COMMAND_GLOBAL_COMMAND_COOLDOWN.asSeconds()), : Setting.NPC_COMMAND_GLOBAL_COMMAND_COOLDOWN.asSeconds()),
StringHelper.wrap(hasCost(command.id) ? command.cost : "default"), StringHelper.wrap(hasCost(command.id) ? command.cost : "default"),
StringHelper.wrap(hasExperienceCost(command.id) ? command.experienceCost : "default"), StringHelper.wrap(hasExperienceCost(command.id) ? command.experienceCost : "default"), command.id);
command.id);
if (command.globalCooldown > 0) { if (command.globalCooldown > 0) {
output += "[global " + StringHelper.wrap(command.globalCooldown) + "s]"; output += "[global " + StringHelper.wrap(command.globalCooldown) + "s]";
} }
@ -315,7 +315,7 @@ public class CommandTrait extends Trait {
} }
Transaction charge = null; Transaction charge = null;
if (charged == null) { if (charged == null) {
charge = chargeCommandCosts(player, hand, command.id); charge = chargeCommandCosts(player, hand, command);
if (!charge.isPossible()) { if (!charge.isPossible()) {
charged = false; charged = false;
return; return;
@ -429,7 +429,8 @@ public class CommandTrait extends Trait {
} }
} }
private void sendErrorMessage(Player player, CommandTraitError msg, Function<String, String> transform, Object... objects) { private void sendErrorMessage(Player player, CommandTraitError msg, Function<String, String> transform,
Object... objects) {
if (hideErrorMessages) { if (hideErrorMessages) {
return; return;
} }
@ -476,6 +477,11 @@ public class CommandTrait extends Trait {
this.hideErrorMessages = hide; this.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) { public void setPersistSequence(boolean persistSequence) {
this.persistSequence = persistSequence; this.persistSequence = persistSequence;
} }
@ -485,11 +491,6 @@ public class CommandTrait extends Trait {
temporaryPermissions.addAll(permissions); temporaryPermissions.addAll(permissions);
} }
public void setItemCost(List<ItemStack> itemCost, int id) {
commands.get(id).itemCost.clear();
commands.get(id).itemCost.addAll(itemCost);
}
public enum CommandTraitError { public enum CommandTraitError {
MAXIMUM_TIMES_USED(Setting.NPC_COMMAND_MAXIMUM_TIMES_USED_MESSAGE), MAXIMUM_TIMES_USED(Setting.NPC_COMMAND_MAXIMUM_TIMES_USED_MESSAGE),
MISSING_EXPERIENCE(Setting.NPC_COMMAND_NOT_ENOUGH_EXPERIENCE_MESSAGE), MISSING_EXPERIENCE(Setting.NPC_COMMAND_NOT_ENOUGH_EXPERIENCE_MESSAGE),
@ -527,9 +528,9 @@ public class CommandTrait extends Trait {
@Menu(title = "Drag items for requirements", type = InventoryType.CHEST, dimensions = { 5, 9 }) @Menu(title = "Drag items for requirements", type = InventoryType.CHEST, dimensions = { 5, 9 })
public static class ItemRequirementGUI extends InventoryMenuPage { public static class ItemRequirementGUI extends InventoryMenuPage {
private int id = -1;
private Inventory inventory; private Inventory inventory;
private CommandTrait trait; private CommandTrait trait;
private int id = -1;
private ItemRequirementGUI() { private ItemRequirementGUI() {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
@ -551,8 +552,7 @@ public class CommandTrait extends Trait {
for (ItemStack stack : trait.itemRequirements) { for (ItemStack stack : trait.itemRequirements) {
inventory.addItem(stack.clone()); inventory.addItem(stack.clone());
} }
} } else {
else {
for (ItemStack stack : trait.commands.get(id).itemCost) { for (ItemStack stack : trait.commands.get(id).itemCost) {
inventory.addItem(stack.clone()); inventory.addItem(stack.clone());
} }
@ -575,8 +575,7 @@ public class CommandTrait extends Trait {
if (id == -1) { if (id == -1) {
this.trait.itemRequirements.clear(); this.trait.itemRequirements.clear();
this.trait.itemRequirements.addAll(requirements); this.trait.itemRequirements.addAll(requirements);
} } else {
else {
this.trait.setItemCost(requirements, id); this.trait.setItemCost(requirements, id);
} }
} }
@ -586,22 +585,22 @@ public class CommandTrait extends Trait {
String bungeeServer; String bungeeServer;
String command; String command;
int cooldown; int cooldown;
double cost;
int delay; int delay;
int experienceCost;
int globalCooldown; int globalCooldown;
Hand hand; Hand hand;
int id; int id;
List<ItemStack> itemCost;
String key; String key;
int n; int n;
boolean op; boolean op;
List<String> perms; List<String> perms;
boolean player; boolean player;
double cost;
int experienceCost;
List<ItemStack> itemCost;
public NPCCommand(int id, String command, Hand hand, boolean player, boolean op, int cooldown, public NPCCommand(int id, String command, Hand hand, boolean player, boolean op, int cooldown,
List<String> perms, int n, int delay, int globalCooldown, double cost, List<String> perms, int n, int delay, int globalCooldown, double cost, int experienceCost,
int experienceCost, List<ItemStack> itemCost) { List<ItemStack> itemCost) {
this.id = id; this.id = id;
this.command = command; this.command = command;
this.hand = hand; this.hand = hand;
@ -633,16 +632,16 @@ public class CommandTrait extends Trait {
public static class NPCCommandBuilder { public static class NPCCommandBuilder {
String command; String command;
int cooldown; int cooldown;
double cost = -1;
int delay; int delay;
int experienceCost = -1;
int globalCooldown; int globalCooldown;
Hand hand; Hand hand;
List<ItemStack> itemCost = Lists.newArrayList();
int n = -1; int n = -1;
boolean op; boolean op;
List<String> perms = Lists.newArrayList(); List<String> perms = Lists.newArrayList();
boolean player; boolean player;
double cost = -1;
int experienceCost = -1;
List<ItemStack> itemCost = Lists.newArrayList();
public NPCCommandBuilder(String command, Hand hand) { public NPCCommandBuilder(String command, Hand hand) {
this.command = command; this.command = command;
@ -660,7 +659,8 @@ public class CommandTrait extends Trait {
} }
private NPCCommand build(int id) { private NPCCommand build(int id) {
return new NPCCommand(id, command, hand, player, op, cooldown, perms, n, delay, globalCooldown, cost, experienceCost, itemCost); return new NPCCommand(id, command, hand, player, op, cooldown, perms, n, delay, globalCooldown, cost,
experienceCost, itemCost);
} }
public NPCCommandBuilder command(String command) { public NPCCommandBuilder command(String command) {
@ -677,11 +677,21 @@ public class CommandTrait extends Trait {
return this; return this;
} }
public NPCCommandBuilder cost(double cost) {
this.cost = cost;
return this;
}
public NPCCommandBuilder delay(int delay) { public NPCCommandBuilder delay(int delay) {
this.delay = delay; this.delay = delay;
return this; return this;
} }
public NPCCommandBuilder experienceCost(int experienceCost) {
this.experienceCost = experienceCost;
return this;
}
public NPCCommandBuilder globalCooldown(Duration cd) { public NPCCommandBuilder globalCooldown(Duration cd) {
return globalCooldown(Util.convert(TimeUnit.SECONDS, cd)); return globalCooldown(Util.convert(TimeUnit.SECONDS, cd));
} }
@ -691,6 +701,11 @@ public class CommandTrait extends Trait {
return this; return this;
} }
public NPCCommandBuilder itemCost(List<ItemStack> itemCost) {
this.itemCost = itemCost;
return this;
}
public NPCCommandBuilder n(int n) { public NPCCommandBuilder n(int n) {
this.n = n; this.n = n;
return this; return this;
@ -705,21 +720,6 @@ public class CommandTrait extends Trait {
this.player = player; this.player = player;
return this; return this;
} }
public NPCCommandBuilder cost(double cost) {
this.cost = cost;
return this;
}
public NPCCommandBuilder experienceCost(int experienceCost) {
this.experienceCost = experienceCost;
return this;
}
public NPCCommandBuilder itemCost(List<ItemStack> itemCost) {
this.itemCost = itemCost;
return this;
}
} }
private static class NPCCommandPersister implements Persister<NPCCommand> { private static class NPCCommandPersister implements Persister<NPCCommand> {