npc shop changes

This commit is contained in:
fullwall 2023-05-07 22:03:07 +08:00
parent deb9976a9b
commit 55bda3ac13

View File

@ -12,6 +12,7 @@ import org.bukkit.ChatColor;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.HumanEntity; import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType; import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -253,18 +254,20 @@ public class ShopTrait extends Trait {
} }
public static class NPCShopItem implements Cloneable, Persistable { public static class NPCShopItem implements Cloneable, Persistable {
@Persist
private String clickMessage;
@Persist @Persist
private String clickToConfirmMessage; private String clickToConfirmMessage;
@Persist @Persist
private final List<NPCShopAction> cost = Lists.newArrayList(); private final List<NPCShopAction> cost = Lists.newArrayList();
@Persist @Persist
private String costMessage;
@Persist
private ItemStack display; private ItemStack display;
@Persist @Persist
private boolean maxRepeatsOnShiftClick; private boolean maxRepeatsOnShiftClick;
@Persist @Persist
private final List<NPCShopAction> result = Lists.newArrayList(); private final List<NPCShopAction> result = Lists.newArrayList();
@Persist
private String resultMessage;
public List<Transaction> apply(List<NPCShopAction> actions, Function<NPCShopAction, Transaction> func) { public List<Transaction> apply(List<NPCShopAction> actions, Function<NPCShopAction, Transaction> func) {
List<Transaction> pending = Lists.newArrayList(); List<Transaction> pending = Lists.newArrayList();
@ -338,12 +341,16 @@ public class ShopTrait extends Trait {
@Override @Override
public void load(DataKey key) { public void load(DataKey key) {
if (key.keyExists("message")) { if (key.keyExists("message")) {
clickMessage = key.getString("message"); resultMessage = key.getString("message");
key.removeKey("message"); key.removeKey("message");
} }
if (key.keyExists("clickMessage")) {
resultMessage = key.getString("clickMessage");
key.removeKey("clickMessage");
}
} }
public void onClick(NPCShop shop, CitizensInventoryClickEvent event, boolean secondClick) { public void onClick(NPCShop shop, InventoryClickEvent event, boolean secondClick) {
if (clickToConfirmMessage != null && !secondClick) { if (clickToConfirmMessage != null && !secondClick) {
Messaging.sendColorless(event.getWhoClicked(), Messaging.sendColorless(event.getWhoClicked(),
placeholders(clickToConfirmMessage, (Player) event.getWhoClicked())); placeholders(clickToConfirmMessage, (Player) event.getWhoClicked()));
@ -362,15 +369,20 @@ public class ShopTrait extends Trait {
} }
final int repeats = max == Integer.MAX_VALUE ? 1 : max; final int repeats = max == Integer.MAX_VALUE ? 1 : max;
List<Transaction> take = apply(cost, action -> action.take(event.getWhoClicked(), repeats)); List<Transaction> take = apply(cost, action -> action.take(event.getWhoClicked(), repeats));
if (take == null) if (take == null) {
if (costMessage != null) {
Messaging.sendColorless(event.getWhoClicked(),
placeholders(costMessage, (Player) event.getWhoClicked()));
}
return; return;
}
if (apply(result, action -> action.grant(event.getWhoClicked(), repeats)) == null) { if (apply(result, action -> action.grant(event.getWhoClicked(), repeats)) == null) {
take.forEach(a -> a.rollback()); take.forEach(a -> a.rollback());
return; return;
} }
if (clickMessage != null) { if (resultMessage != null) {
Messaging.sendColorless(event.getWhoClicked(), Messaging.sendColorless(event.getWhoClicked(),
Placeholders.replace(clickMessage, (Player) event.getWhoClicked())); placeholders(resultMessage, (Player) event.getWhoClicked()));
} }
} }
@ -423,16 +435,26 @@ public class ShopTrait extends Trait {
ctx.getSlot(9 * 4 + 4).setItemStack(modified.getDisplayItem(null)); ctx.getSlot(9 * 4 + 4).setItemStack(modified.getDisplayItem(null));
} }
ctx.getSlot(9 * 3 + 3).setItemStack(new ItemStack(Util.getFallbackMaterial("OAK_SIGN", "SIGN")), ctx.getSlot(9 * 3 + 3).setItemStack(new ItemStack(Util.getFallbackMaterial("OAK_SIGN", "SIGN")),
"Set message to send on click, currently:\n", "Set message to send on successful click, currently:\n",
modified.clickMessage == null ? "Unset" : modified.clickMessage); modified.resultMessage == null ? "Unset" : modified.resultMessage);
ctx.getSlot(9 * 3 + 3).setClickHandler( ctx.getSlot(9 * 3 + 3).setClickHandler(
e -> ctx.getMenu().transition(InputMenus.stringSetter(() -> modified.clickMessage, s -> { e -> ctx.getMenu().transition(InputMenus.stringSetter(() -> modified.resultMessage, s -> {
modified.clickMessage = s; modified.resultMessage = s;
ctx.getSlot(9 * 3 + 3).setDescription(modified.clickMessage); ctx.getSlot(9 * 3 + 3).setDescription(modified.resultMessage);
})));
ctx.getSlot(9 * 3 + 6).setItemStack(new ItemStack(Util.getFallbackMaterial("BARRIER", "FIRE")),
"Set message to send on unsuccessful click, currently:\n",
modified.costMessage == null ? "Unset" : modified.costMessage);
ctx.getSlot(9 * 3 + 6).setClickHandler(
e -> ctx.getMenu().transition(InputMenus.stringSetter(() -> modified.costMessage, s -> {
modified.costMessage = s;
ctx.getSlot(9 * 3 + 6).setDescription(modified.costMessage);
}))); })));
ctx.getSlot(9 * 3 + 5).setItemStack(new ItemStack(Util.getFallbackMaterial("FEATHER", "SIGN")), ctx.getSlot(9 * 3 + 5).setItemStack(new ItemStack(Util.getFallbackMaterial("FEATHER", "SIGN")),
"Set click to confirm message.", "You can use <cost> or <result> placeholders.\nCurrently:\n" "Set click to confirm message.",
"For example, 'click again to buy this item'\nYou can use <cost> or <result> placeholders.\nCurrently:\n"
+ (modified.clickToConfirmMessage == null ? "Unset" : modified.clickToConfirmMessage)); + (modified.clickToConfirmMessage == null ? "Unset" : modified.clickToConfirmMessage));
ctx.getSlot(9 * 3 + 5).setClickHandler( ctx.getSlot(9 * 3 + 5).setClickHandler(
e -> ctx.getMenu().transition(InputMenus.stringSetter(() -> modified.clickToConfirmMessage, s -> { e -> ctx.getMenu().transition(InputMenus.stringSetter(() -> modified.clickToConfirmMessage, s -> {