Add a helper method to NPCShopAction to save the inventory properly

This commit is contained in:
fullwall 2024-03-11 22:44:40 +08:00
parent c683b752a5
commit 3c314c1d3e
2 changed files with 33 additions and 7 deletions

View File

@ -101,19 +101,19 @@ public class CommandTrait extends Trait {
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());
@ -121,26 +121,26 @@ public class CommandTrait extends Trait {
}
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() {

View File

@ -5,6 +5,7 @@ import java.util.function.Consumer;
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;
@ -12,6 +13,7 @@ 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
@ -29,8 +31,32 @@ public abstract class NPCShopAction implements Cloneable {
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);