Skip to content

Commit

Permalink
Fix /npc text remove clickable not working, make /npc shop item actio…
Browse files Browse the repository at this point in the history
…n work on material alone for now
  • Loading branch information
fullwall committed Oct 26, 2022
1 parent ebe1d27 commit a91f0cd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
14 changes: 7 additions & 7 deletions main/src/main/java/net/citizensnpcs/trait/ShopTrait.java
Expand Up @@ -296,13 +296,13 @@ public ItemStack getDisplayItem(Player player) {
}

public void onClick(NPCShop shop, CitizensInventoryClickEvent event) {
if (shop.type != ShopType.COMMAND) {
List<Transaction> take = execute(cost, action -> action.take(event.getWhoClicked()));
if (take == null)
return;
if (execute(result, action -> action.grant(event.getWhoClicked())) == null) {
take.forEach(a -> a.rollback());
}
if (shop.type == ShopType.COMMAND)
return;
List<Transaction> take = execute(cost, action -> action.take(event.getWhoClicked()));
if (take == null)
return;
if (execute(result, action -> action.grant(event.getWhoClicked())) == null) {
take.forEach(a -> a.rollback());
}
}
}
Expand Down
27 changes: 22 additions & 5 deletions main/src/main/java/net/citizensnpcs/trait/shop/ItemAction.java
Expand Up @@ -68,9 +68,7 @@ public Transaction take(Entity entity) {
.collect(Collectors.toMap(k -> k.getType(), v -> v.getAmount()));
boolean contains = true;
for (Map.Entry<Material, Integer> entry : required.entrySet()) {
if (!source.contains(entry.getKey(), entry.getValue())) {
contains = false;
}
contains &= source.contains(entry.getKey(), entry.getValue());
}
for (ItemStack item : items) {
if (item.hasItemMeta() && !source.contains(item)) {
Expand All @@ -79,7 +77,26 @@ public Transaction take(Entity entity) {
}
return contains;
}, () -> {
source.removeItem(items.toArray(new ItemStack[items.size()]));
Map<Material, Integer> required = items.stream()
.collect(Collectors.toMap(k -> k.getType(), v -> v.getAmount()));
ItemStack[] contents = source.getContents();
for (int i = 0; i < contents.length; i++) {
ItemStack stack = contents[i];
if (stack == null || stack.getType() == Material.AIR || !required.containsKey(stack.getType()))
continue;
Material type = stack.getType();
int remaining = required.remove(type);
int taken = stack.getAmount() > remaining ? remaining : stack.getAmount();
if (stack.getAmount() == taken) {
source.clear(i);
} else {
stack.setAmount(stack.getAmount() - taken);
source.setItem(i, stack);
}
if (remaining - taken > 0) {
required.put(type, remaining - taken);
}
}
}, () -> {
source.addItem(items.toArray(new ItemStack[items.size()]));
});
Expand All @@ -105,7 +122,7 @@ public void initialise(MenuContext ctx) {
for (int i = 0; i < 3 * 9; i++) {
InventoryMenuSlot slot = ctx.getSlot(i);
slot.clear();
if (i < base.items.size()) {
if (base != null && i < base.items.size()) {
slot.setItemStack(base.items.get(i).clone());
}
slot.setClickHandler(event -> {
Expand Down
4 changes: 2 additions & 2 deletions main/src/main/java/net/citizensnpcs/trait/text/Text.java
Expand Up @@ -234,8 +234,8 @@ boolean sendPage(CommandSender player, int page) {
Paginator paginator = new Paginator().header("Current Texts").enablePageSwitcher();
for (int i = 0; i < text.size(); i++) {
paginator.addLine(text.get(i) + " <green>(<click:suggest_command:edit " + i
+ " ><yellow>edit</click>) (<click:run_command:remove " + i
+ "><hover:show_text:Remove this text><red>-</hover></click>)");
+ " ><yellow>edit</click>) (<hover:show_text:Remove this text><click:run_command:/npc text remove "
+ i + "><red>-</click></hover>)");
}
return paginator.sendPage(player, page);
}
Expand Down

0 comments on commit a91f0cd

Please sign in to comment.