Add option to have players purchase items only once (#3037)

* Add option to have players purchase items only once

* In the process of updating `purchases` to be a map

NOT FINISHED!

* Fix bug in previous commit. Thanks for the help fullwall!
This commit is contained in:
Brando! 2023-08-13 10:20:41 -07:00 committed by GitHub
parent 3e374b7289
commit e0fa527c25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,6 +2,7 @@ package net.citizensnpcs.trait;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.regex.Matcher;
@ -307,6 +308,12 @@ public class ShopTrait extends Trait {
private final List<NPCShopAction> result = Lists.newArrayList();
@Persist
private String resultMessage;
@Persist
private int timesPurchasable = 0;
@Persist
private String alreadyPurchasedMessage;
@Persist(keyType = UUID.class)
private final Map<UUID, Integer> purchases = Maps.newHashMap();
public List<Transaction> apply(List<NPCShopAction> actions, Function<NPCShopAction, Transaction> func) {
List<Transaction> pending = Lists.newArrayList();
@ -369,6 +376,10 @@ public class ShopTrait extends Trait {
List<String> lore = Lists.newArrayList();
cost.forEach(a -> lore.add(a.describe()));
result.forEach(a -> lore.add(a.describe()));
if (timesPurchasable > 0) {
lore.add("Times purchasable: " + timesPurchasable);
}
meta.setLore(lore);
}
if (meta.hasLore()) {
meta.setLore(Lists.transform(meta.getLore(), line -> placeholders(line, player)));
@ -390,9 +401,17 @@ public class ShopTrait extends Trait {
}
public void onClick(NPCShop shop, InventoryClickEvent event, boolean secondClick) {
Player player = (Player) event.getWhoClicked();
if (purchases.containsKey(player.getUniqueId()) && timesPurchasable > 0 && purchases.get(player.getUniqueId()) == timesPurchasable) {
if (alreadyPurchasedMessage != null) {
Messaging.sendColorless(event.getWhoClicked(),
placeholders(alreadyPurchasedMessage, player));
}
return;
}
if (clickToConfirmMessage != null && !secondClick) {
Messaging.sendColorless(event.getWhoClicked(),
placeholders(clickToConfirmMessage, (Player) event.getWhoClicked()));
placeholders(clickToConfirmMessage, player));
return;
}
int max = Integer.MAX_VALUE;
@ -411,7 +430,7 @@ public class ShopTrait extends Trait {
if (take == null) {
if (costMessage != null) {
Messaging.sendColorless(event.getWhoClicked(),
placeholders(costMessage, (Player) event.getWhoClicked()));
placeholders(costMessage, player));
}
return;
}
@ -421,7 +440,11 @@ public class ShopTrait extends Trait {
}
if (resultMessage != null) {
Messaging.sendColorless(event.getWhoClicked(),
placeholders(resultMessage, (Player) event.getWhoClicked()));
placeholders(resultMessage, player));
}
if (timesPurchasable > 0) {
int timesPurchasedAlready = purchases.get(player.getUniqueId()) == null ? 0 : purchases.get(player.getUniqueId());
purchases.put(player.getUniqueId(), ++timesPurchasedAlready);
}
}
@ -473,6 +496,24 @@ public class ShopTrait extends Trait {
if (modified.display != null) {
ctx.getSlot(9 * 4 + 4).setItemStack(modified.getDisplayItem(null));
}
ctx.getSlot(9 * 3 + 2).setItemStack(new ItemStack(Material.EGG),
"Only purchasable once per player",
"Times purchasable: " + modified.timesPurchasable + (modified.timesPurchasable == 0 ? " (no limit)" : ""));
ctx.getSlot(9 * 3 + 2).setClickHandler(
e -> ctx.getMenu().transition(InputMenus.stringSetter(() -> String.valueOf(modified.timesPurchasable), s -> {
modified.timesPurchasable = Integer.parseInt(s);
ctx.getSlot(9 * 4 + 2).setDescription("Times purchasable: " + modified.timesPurchasable + (modified.timesPurchasable == 0 ? " (no limit)" : ""));
})));
ctx.getSlot(9 * 4 + 2).setItemStack(new ItemStack(Util.getFallbackMaterial("OAK_SIGN", "SIGN")),
"Set message to send on already purchased, currently:\n",
modified.alreadyPurchasedMessage == null ? "Unset" : modified.alreadyPurchasedMessage);
ctx.getSlot(9 * 4 + 2).setClickHandler(
e -> ctx.getMenu().transition(InputMenus.stringSetter(() -> modified.alreadyPurchasedMessage, s -> {
modified.alreadyPurchasedMessage = s;
ctx.getSlot(9 * 4 + 2).setDescription(modified.alreadyPurchasedMessage);
})));
ctx.getSlot(9 * 3 + 3).setItemStack(new ItemStack(Util.getFallbackMaterial("OAK_SIGN", "SIGN")),
"Set message to send on successful click, currently:\n",
modified.resultMessage == null ? "Unset" : modified.resultMessage);
@ -793,4 +834,4 @@ public class ShopTrait extends Trait {
NPCShopAction.register(CommandAction.class, "command", new CommandActionGUI());
NPCShopAction.register(ExperienceAction.class, "experience", new ExperienceActionGUI());
}
}
}