Allow colors in money item shop editor

This commit is contained in:
fullwall 2024-04-15 22:30:53 +08:00
parent edb4f78ceb
commit 1b32596c37

View File

@ -11,6 +11,7 @@ import org.bukkit.inventory.ItemStack;
import net.citizensnpcs.api.gui.InputMenus; import net.citizensnpcs.api.gui.InputMenus;
import net.citizensnpcs.api.gui.InventoryMenuPage; import net.citizensnpcs.api.gui.InventoryMenuPage;
import net.citizensnpcs.api.persistence.Persist; import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.util.InventoryMultiplexer; import net.citizensnpcs.util.InventoryMultiplexer;
import net.citizensnpcs.util.Util; import net.citizensnpcs.util.Util;
import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.economy.Economy;
@ -18,6 +19,8 @@ import net.milkbowl.vault.economy.Economy;
public class MoneyAction extends NPCShopAction { public class MoneyAction extends NPCShopAction {
@Persist @Persist
public double money; public double money;
@Persist
private String formattedMoney;
public MoneyAction() { public MoneyAction() {
} }
@ -29,7 +32,11 @@ public class MoneyAction extends NPCShopAction {
@Override @Override
public String describe() { public String describe() {
Economy economy = Bukkit.getServicesManager().getRegistration(Economy.class).getProvider(); Economy economy = Bukkit.getServicesManager().getRegistration(Economy.class).getProvider();
return money + " " + economy.currencyNamePlural(); return getFormattedMoney() + " " + economy.currencyNamePlural();
}
private String getFormattedMoney() {
return formattedMoney == null || formattedMoney.isEmpty() ? Double.toString(money) : formattedMoney;
} }
@Override @Override
@ -81,11 +88,12 @@ public class MoneyAction extends NPCShopAction {
MoneyAction action = previous == null ? new MoneyAction() : (MoneyAction) previous; MoneyAction action = previous == null ? new MoneyAction() : (MoneyAction) previous;
return InputMenus.filteredStringSetter(() -> Double.toString(action.money), s -> { return InputMenus.filteredStringSetter(() -> Double.toString(action.money), s -> {
try { try {
double result = Double.parseDouble(s); double result = Double.parseDouble(Messaging.stripColor(s));
if (result < 0) if (result < 0)
return false; return false;
action.money = result; action.money = result;
action.formattedMoney = !Messaging.stripColor(s).equals(s) ? s : null;
} catch (NumberFormatException nfe) { } catch (NumberFormatException nfe) {
return false; return false;
} }