From 6744df2a572de7e39c7cf1da6c26a942d6c687d2 Mon Sep 17 00:00:00 2001 From: atomicint Date: Wed, 17 Feb 2016 20:45:43 -0500 Subject: [PATCH] Fix remove(slot) method - Add Remove/AddItemTransactionBlock --- .../org/apollo/game/model/inv/Inventory.java | 5 ++- .../block/AddItemTransactionBlock.java | 43 +++++++++++++++++++ .../block/RemoveItemTransactionBlock.java | 35 +++++++++++++++ 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 game/src/main/org/apollo/game/model/inv/transaction/block/AddItemTransactionBlock.java create mode 100644 game/src/main/org/apollo/game/model/inv/transaction/block/RemoveItemTransactionBlock.java diff --git a/game/src/main/org/apollo/game/model/inv/Inventory.java b/game/src/main/org/apollo/game/model/inv/Inventory.java index a3850ea4..6eebf540 100644 --- a/game/src/main/org/apollo/game/model/inv/Inventory.java +++ b/game/src/main/org/apollo/game/model/inv/Inventory.java @@ -421,7 +421,10 @@ public final class Inventory { public Optional remove(int slot) { Item item = get(slot); if (item != null) { - remove(item); + int amount = remove(item); + if (amount != item.getAmount()) { + return Optional.of(new Item(item.getId(), amount)); + } return Optional.of(item); } return Optional.empty(); diff --git a/game/src/main/org/apollo/game/model/inv/transaction/block/AddItemTransactionBlock.java b/game/src/main/org/apollo/game/model/inv/transaction/block/AddItemTransactionBlock.java new file mode 100644 index 00000000..5b34a5a6 --- /dev/null +++ b/game/src/main/org/apollo/game/model/inv/transaction/block/AddItemTransactionBlock.java @@ -0,0 +1,43 @@ +package org.apollo.game.model.inv.transaction.block; + +import org.apollo.game.model.Item; +import org.apollo.game.model.inv.Inventory; +import org.apollo.game.model.inv.transaction.TransactionBlock; + +/** + * A TransactionBlock which adds an Item to an Inventory. + */ +public final class AddItemTransactionBlock implements TransactionBlock { + + /** + * The Inventory the Item is being added to. + */ + private final Inventory inventory; + + /** + * The Item we are adding to the Inventory. + */ + private final Item item; + + /** + * Creates a new {@link AddItemTransactionBlock}. + * + * @param inventory The Inventory we are adding the specified Item to. + * @param item The Item to add to the Inventory. + */ + public AddItemTransactionBlock(Inventory inventory, Item item) { + this.inventory = inventory; + this.item = item; + } + + @Override + public boolean commit() { + return !inventory.add(item).isPresent(); + } + + @Override + public void revert() { + inventory.remove(item); + } + +} \ No newline at end of file diff --git a/game/src/main/org/apollo/game/model/inv/transaction/block/RemoveItemTransactionBlock.java b/game/src/main/org/apollo/game/model/inv/transaction/block/RemoveItemTransactionBlock.java new file mode 100644 index 00000000..b3b5a7fe --- /dev/null +++ b/game/src/main/org/apollo/game/model/inv/transaction/block/RemoveItemTransactionBlock.java @@ -0,0 +1,35 @@ +package org.apollo.game.model.inv.transaction.block; + +import java.util.Optional; + +import org.apollo.game.model.Item; +import org.apollo.game.model.inv.Inventory; +import org.apollo.game.model.inv.transaction.TransactionBlock; + +public final class RemoveItemTransactionBlock implements TransactionBlock { + + private final Inventory inventory; + + private final Item item; + + private Optional removed = Optional.empty(); + + public RemoveItemTransactionBlock(Inventory inventory, Item item) { + this.inventory = inventory; + this.item = item; + } + + @Override + public boolean commit() { + int slot = inventory.slotOf(item.getId()); + removed = inventory.remove(slot); + return removed.get().equals(item); + } + + @Override + public void revert() { + removed.ifPresent(inventory::add); + removed = Optional.empty(); + } + +} \ No newline at end of file