Skip to content

Commit

Permalink
Add a helper method to NPCShopAction to save the inventory properly
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Mar 11, 2024
1 parent c683b75 commit 3c314c1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
14 changes: 7 additions & 7 deletions main/src/main/java/net/citizensnpcs/trait/CommandTrait.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,46 +101,46 @@ private Transaction chargeCommandCosts(Player player, Hand hand, NPCCommand comm
return Transaction.success();
if (nonZeroOrNegativeOne(cost) && !player.hasPermission("citizens.npc.command.ignoreerrors.cost")) {
action = new MoneyAction(cost);
if (!action.take(player, null, 1).isPossible()) {
if (!action.take(player, 1).isPossible()) {
sendErrorMessage(player, CommandTraitError.MISSING_MONEY, null, cost);
}
}
if (experienceCost > 0 && !player.hasPermission("citizens.npc.command.ignoreerrors.expcost")) {
action = new ExperienceAction(experienceCost);
if (!action.take(player, null, 1).isPossible()) {
if (!action.take(player, 1).isPossible()) {
sendErrorMessage(player, CommandTraitError.MISSING_EXPERIENCE, null, experienceCost);
}
}
if (itemRequirements.size() > 0 && !player.hasPermission("citizens.npc.command.ignoreerrors.itemcost")) {
action = new ItemAction(itemRequirements);
if (!action.take(player, null, 1).isPossible()) {
if (!action.take(player, 1).isPossible()) {
ItemStack stack = itemRequirements.get(0);
sendErrorMessage(player, CommandTraitError.MISSING_ITEM, null, Util.prettyEnum(stack.getType()),
stack.getAmount());
}
}
if (nonZeroOrNegativeOne(command.cost) && !player.hasPermission("citizens.npc.command.ignoreerrors.cost")) {
action = new MoneyAction(command.cost);
if (!action.take(player, null, 1).isPossible()) {
if (!action.take(player, 1).isPossible()) {
sendErrorMessage(player, CommandTraitError.MISSING_MONEY, null, command.cost);
}
}
if (command.experienceCost != -1 && !player.hasPermission("citizens.npc.command.ignoreerrors.expcost")) {
action = new ExperienceAction(command.experienceCost);
if (!action.take(player, null, 1).isPossible()) {
if (!action.take(player, 1).isPossible()) {
sendErrorMessage(player, CommandTraitError.MISSING_EXPERIENCE, null, command.experienceCost);
}
}
if (command.itemCost != null && command.itemCost.size() > 0
&& !player.hasPermission("citizens.npc.command.ignoreerrors.itemcost")) {
action = new ItemAction(command.itemCost);
if (!action.take(player, null, 1).isPossible()) {
if (!action.take(player, 1).isPossible()) {
ItemStack stack = command.itemCost.get(0);
sendErrorMessage(player, CommandTraitError.MISSING_ITEM, null, Util.prettyEnum(stack.getType()),
stack.getAmount());
}
}
return action == null ? Transaction.success() : action.take(player, null, 1);
return action == null ? Transaction.success() : action.take(player, 1);
}

public void clear() {
Expand Down
26 changes: 26 additions & 0 deletions main/src/main/java/net/citizensnpcs/trait/shop/NPCShopAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import java.util.function.Supplier;

import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import com.google.common.collect.Lists;

import net.citizensnpcs.api.gui.InventoryMenuPage;
import net.citizensnpcs.api.persistence.PersistenceLoader;
import net.citizensnpcs.api.persistence.PersisterRegistry;
import net.citizensnpcs.util.InventoryMultiplexer;

public abstract class NPCShopAction implements Cloneable {
@Override
Expand All @@ -29,8 +31,32 @@ public NPCShopAction clone() {

public abstract Transaction grant(Entity entity, ItemStack[] inventory, int repeats);

public Transaction grant(Player entity, int repeats) {
InventoryMultiplexer im = new InventoryMultiplexer(entity.getInventory());
Transaction tx = grant(entity, im.getInventory(), repeats);
return Transaction.create(tx::isPossible, () -> {
tx.run();
im.save();
}, () -> {
tx.rollback();
im.save();
});
}

public abstract Transaction take(Entity entity, ItemStack[] inventory, int repeats);

public Transaction take(Player entity, int repeats) {
InventoryMultiplexer im = new InventoryMultiplexer(entity.getInventory());
Transaction tx = take(entity, im.getInventory(), repeats);
return Transaction.create(tx::isPossible, () -> {
tx.run();
im.save();
}, () -> {
tx.rollback();
im.save();
});
}

public static interface GUI {
public InventoryMenuPage createEditor(NPCShopAction previous, Consumer<NPCShopAction> callback);

Expand Down

0 comments on commit 3c314c1

Please sign in to comment.