From 81b606272d022f2df2cd5d824bf70c6ac0fbdfee Mon Sep 17 00:00:00 2001 From: ExE Boss Date: Thu, 26 Oct 2017 00:00:00 +0200 Subject: [PATCH 1/4] Work on Inventory code # List of changes: - Completed InventoryIterator to match the spec - Improved the Inventory interface - Completed JavaDoc - Now uses `java.util.Optional` instead of `null` - Methods now throw `java.lang.IndexOutOfBoundsException` if the requested slot is out of bounds --- .../v17/wrapper/inventory/BWInventory.java | 28 ++++++-- .../v17/wrapper/inventory/FWInventory.java | 26 +++---- .../v18/wrapper/inventory/BWInventory.java | 28 ++++++-- .../v18/wrapper/inventory/FWInventory.java | 32 +++++---- .../core/component/inventory/Inventory.java | 68 +++++++++++-------- .../inventory/InventoryIterator.java | 12 +++- .../component/inventory/InventorySimple.java | 27 +++++--- .../component/inventory/InventoryView.java | 16 +++-- src/main/java/nova/core/item/Item.java | 8 ++- 9 files changed, 159 insertions(+), 86 deletions(-) diff --git a/minecraft/1.7/src/main/java/nova/core/wrapper/mc/forge/v17/wrapper/inventory/BWInventory.java b/minecraft/1.7/src/main/java/nova/core/wrapper/mc/forge/v17/wrapper/inventory/BWInventory.java index 50fd3db23..68c8ee5a8 100644 --- a/minecraft/1.7/src/main/java/nova/core/wrapper/mc/forge/v17/wrapper/inventory/BWInventory.java +++ b/minecraft/1.7/src/main/java/nova/core/wrapper/mc/forge/v17/wrapper/inventory/BWInventory.java @@ -25,7 +25,6 @@ import nova.core.component.inventory.Inventory; import nova.core.item.Item; import nova.core.wrapper.mc.forge.v17.wrapper.item.ItemConverter; -import nova.internal.core.Game; import java.util.Optional; @@ -37,14 +36,15 @@ public BWInventory(IInventory mcInventory) { } @Override - public Optional get(int i) { - return Optional.ofNullable(wrapped.getStackInSlot(i)).map(ItemConverter.instance()::toNova); + public Optional get(int slot) { + return Optional.ofNullable(wrapped.getStackInSlot(slot)).map(ItemConverter.instance()::toNova); } @Override - public boolean set(int i, Item item) { - wrapped.setInventorySlotContents(i, ItemConverter.instance().toNative(item)); - return true; + public boolean set(int slot, Optional item) { + Optional orig = get(slot); + wrapped.setInventorySlotContents(slot, item.map(ItemConverter.instance()::toNative).orElse(null)); + return !orig.equals(get(slot)); } @Override @@ -54,6 +54,22 @@ public Optional remove(int slot) { return item; } + @Override + public Optional remove(int slot, int amount) { + Optional itemStack = Optional.ofNullable(wrapped.getStackInSlot(slot)); + Optional o = itemStack.map(ItemConverter.instance()::toNova); + if (o.isPresent()) { + Item item = o.get(); + item.setCount(item.count() - amount); + if (item.count() <= 0) { + return remove(slot); + } + ItemConverter.instance().updateMCItemStack(itemStack.get(), item); + return Optional.of(item.withAmount(amount)); + } + return Optional.empty(); + } + @Override public int size() { return wrapped.getSizeInventory(); diff --git a/minecraft/1.7/src/main/java/nova/core/wrapper/mc/forge/v17/wrapper/inventory/FWInventory.java b/minecraft/1.7/src/main/java/nova/core/wrapper/mc/forge/v17/wrapper/inventory/FWInventory.java index 5f5034941..1b762fac0 100644 --- a/minecraft/1.7/src/main/java/nova/core/wrapper/mc/forge/v17/wrapper/inventory/FWInventory.java +++ b/minecraft/1.7/src/main/java/nova/core/wrapper/mc/forge/v17/wrapper/inventory/FWInventory.java @@ -24,8 +24,11 @@ import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; import nova.core.component.inventory.Inventory; +import nova.core.component.inventory.InventorySimple; import nova.core.wrapper.mc.forge.v17.wrapper.item.ItemConverter; +import java.util.Optional; + public class FWInventory implements IInventory { public final Inventory wrapped; @@ -41,7 +44,8 @@ public int getSizeInventory() { @Override public ItemStack getStackInSlot(int slot) { - return ItemConverter.instance().toNative(wrapped.get(slot).orElse(null)); + if (slot < 0 || slot >= wrapped.size()) return null; + return wrapped.get(slot).map(ItemConverter.instance()::toNative).orElse(null); } @Override @@ -64,8 +68,9 @@ public ItemStack getStackInSlotOnClosing(int slot) { } @Override - public void setInventorySlotContents(int slot, ItemStack stack) { - wrapped.set(slot, stack != null ? ItemConverter.instance().getNovaItem(stack) : null); + public void setInventorySlotContents(int slot, ItemStack item) { + if (slot < 0 || slot >= wrapped.size()) return; + wrapped.set(slot, Optional.ofNullable(item).map(ItemConverter.instance()::toNova)); } @Override @@ -91,23 +96,20 @@ public void markDirty() { @Override public boolean isUseableByPlayer(EntityPlayer player) { - // TODO Auto-generated method stub return true; } @Override - public void openInventory() { - - } + public void openInventory() {} @Override - public void closeInventory() { - - } + public void closeInventory() {} @Override - public boolean isItemValidForSlot(int slot, ItemStack stack) { - // TODO Auto-generated method stub + public boolean isItemValidForSlot(int slot, ItemStack item) { + if (item != null && wrapped instanceof InventorySimple) { + ((InventorySimple) wrapped).isItemValidForSlot.apply(slot, ItemConverter.instance().toNova(item)); + } return true; } diff --git a/minecraft/1.8/src/main/java/nova/core/wrapper/mc/forge/v18/wrapper/inventory/BWInventory.java b/minecraft/1.8/src/main/java/nova/core/wrapper/mc/forge/v18/wrapper/inventory/BWInventory.java index 53a57f887..0d8eb2f86 100644 --- a/minecraft/1.8/src/main/java/nova/core/wrapper/mc/forge/v18/wrapper/inventory/BWInventory.java +++ b/minecraft/1.8/src/main/java/nova/core/wrapper/mc/forge/v18/wrapper/inventory/BWInventory.java @@ -25,7 +25,6 @@ import nova.core.component.inventory.Inventory; import nova.core.item.Item; import nova.core.wrapper.mc.forge.v18.wrapper.item.ItemConverter; -import nova.internal.core.Game; import java.util.Optional; @@ -37,14 +36,15 @@ public BWInventory(IInventory mcInventory) { } @Override - public Optional get(int i) { - return Optional.ofNullable(wrapped.getStackInSlot(i)).map(ItemConverter.instance()::toNova); + public Optional get(int slot) { + return Optional.ofNullable(wrapped.getStackInSlot(slot)).map(ItemConverter.instance()::toNova); } @Override - public boolean set(int i, Item item) { - wrapped.setInventorySlotContents(i, ItemConverter.instance().toNative(item)); - return true; + public boolean set(int slot, Optional item) { + Optional orig = get(slot); + wrapped.setInventorySlotContents(slot, item.map(ItemConverter.instance()::toNative).orElse(null)); + return !orig.equals(get(slot)); } @Override @@ -54,6 +54,22 @@ public Optional remove(int slot) { return item; } + @Override + public Optional remove(int slot, int amount) { + Optional itemStack = Optional.ofNullable(wrapped.getStackInSlot(slot)); + Optional o = itemStack.map(ItemConverter.instance()::toNova); + if (o.isPresent()) { + Item item = o.get(); + item.setCount(item.count() - amount); + if (item.count() <= 0) { + return remove(slot); + } + ItemConverter.instance().updateMCItemStack(itemStack.get(), item); + return Optional.of(item.withAmount(amount)); + } + return Optional.empty(); + } + @Override public int size() { return wrapped.getSizeInventory(); diff --git a/minecraft/1.8/src/main/java/nova/core/wrapper/mc/forge/v18/wrapper/inventory/FWInventory.java b/minecraft/1.8/src/main/java/nova/core/wrapper/mc/forge/v18/wrapper/inventory/FWInventory.java index da7af08ad..c3f6dcf54 100644 --- a/minecraft/1.8/src/main/java/nova/core/wrapper/mc/forge/v18/wrapper/inventory/FWInventory.java +++ b/minecraft/1.8/src/main/java/nova/core/wrapper/mc/forge/v18/wrapper/inventory/FWInventory.java @@ -25,8 +25,11 @@ import net.minecraft.item.ItemStack; import net.minecraft.util.IChatComponent; import nova.core.component.inventory.Inventory; +import nova.core.component.inventory.InventorySimple; import nova.core.wrapper.mc.forge.v18.wrapper.item.ItemConverter; +import java.util.Optional; + public class FWInventory implements IInventory { public Inventory wrapped; @@ -42,11 +45,13 @@ public int getSizeInventory() { @Override public ItemStack getStackInSlot(int slot) { - return ItemConverter.instance().toNative(wrapped.get(slot).orElse(null)); + if (slot < 0 || slot >= wrapped.size()) return null; + return wrapped.get(slot).map(ItemConverter.instance()::toNative).orElse(null); } @Override public ItemStack decrStackSize(int slot, int amount) { + if (slot < 0 || slot >= wrapped.size()) return null; ItemStack stack = getStackInSlot(slot); ItemStack ret = stack.copy(); ret.stackSize = Math.min(ret.stackSize, amount); @@ -61,12 +66,14 @@ public ItemStack decrStackSize(int slot, int amount) { @Override public ItemStack getStackInSlotOnClosing(int slot) { + if (slot < 0 || slot >= wrapped.size()) return null; return getStackInSlot(slot); } @Override - public void setInventorySlotContents(int slot, ItemStack stack) { - wrapped.set(slot, stack != null ? ItemConverter.instance().getNovaItem(stack) : null); + public void setInventorySlotContents(int slot, ItemStack item) { + if (slot < 0 || slot >= wrapped.size()) return; + wrapped.set(slot, Optional.ofNullable(item).map(ItemConverter.instance()::toNova)); } @Override @@ -97,23 +104,20 @@ public void markDirty() { @Override public boolean isUseableByPlayer(EntityPlayer player) { - // TODO Auto-generated method stub return true; } @Override - public void openInventory(EntityPlayer playerIn) { - - } + public void openInventory(EntityPlayer playerIn) {} @Override - public void closeInventory(EntityPlayer playerIn) { - - } + public void closeInventory(EntityPlayer playerIn) {} @Override - public boolean isItemValidForSlot(int slot, ItemStack stack) { - // TODO Auto-generated method stub + public boolean isItemValidForSlot(int slot, ItemStack item) { + if (item != null && wrapped instanceof InventorySimple) { + ((InventorySimple) wrapped).isItemValidForSlot.apply(slot, ItemConverter.instance().toNova(item)); + } return true; } @@ -123,9 +127,7 @@ public int getField(int id) { } @Override - public void setField(int id, int value) { - - } + public void setField(int id, int value) {} @Override public int getFieldCount() { diff --git a/src/main/java/nova/core/component/inventory/Inventory.java b/src/main/java/nova/core/component/inventory/Inventory.java index bd524be6f..e70dfb10b 100644 --- a/src/main/java/nova/core/component/inventory/Inventory.java +++ b/src/main/java/nova/core/component/inventory/Inventory.java @@ -39,15 +39,25 @@ * @see InventoryView */ public interface Inventory extends Iterable { + + /** + * @param slot Slot number + * @return The item in the slot or an {@link Optional#empty empty optional} + * if there is no item in the slot + * @throws IndexOutOfBoundsException if the slot is out of range + * (index < 0 || index >= size()) + */ Optional get(int slot); /** * Sets {@link Item} in slot * @param slot Slot number - * @param stack Stack to insert - * @return Whether succeed + * @param item Item to insert + * @return Whether the operation succeeded or not + * @throws IndexOutOfBoundsException if the slot is out of range + * (index < 0 || index >= size()) */ - boolean set(int slot, Item stack); + boolean set(int slot, Optional item); /** * Gets count of slots @@ -63,47 +73,54 @@ public interface Inventory extends Iterable { /** * Adds items to this inventory at specified slot * @param slot Slot to add items into - * @param stack {@link Item} containing items + * @param item Item to add * @return Amount of items left(did not fit inside this inventory) + * @throws IndexOutOfBoundsException if the slot is out of range + * (index < 0 || index >= size()) */ - default int add(int slot, Item stack) { + default Optional add(int slot, Item item) { Optional o = get(slot); if (o.isPresent()) { - if (stack.sameItemType(o.get())) { - return stack.count() - o.get().addCount(stack.count()); + if (item.sameItemType(o.get())) { + return Optional.of(item.withAmount(item.count() - o.get().addCount(item.count()))); } else { - return stack.count(); + return Optional.of(item); } } else { - set(slot, stack); - return 0; + set(slot, Optional.of(item)); + return Optional.empty(); } } /** * Adds items to this inventory - * @param stack {@link Item} containing items + * @param item {@link Item} containing items * @return Amount of items left(did not fit inside this inventory) */ - default int add(Item stack) { - int itemsLeft = stack.count(); + default Optional add(Item item) { + int itemsLeft = item.count(); for (int i = 0; i < size(); i++) { - itemsLeft = add(i, stack.withAmount(itemsLeft)); + itemsLeft = add(i, item.withAmount(itemsLeft)).map(Item::count).orElse(0); if (itemsLeft == 0) break; } - if (itemsLeft != stack.count()) { + if (itemsLeft != item.count()) { markChanged(); } - return itemsLeft; + final int il = itemsLeft; + return Optional.of(item) + .filter(i -> il > 0) + .map(i -> i.withAmount(il)); } /** * Removes a one count of the item from a slot. * @param slot The slot index to remove * @return The items removed + * @throws IndexOutOfBoundsException if the slot is out of range + * (index < 0 || index >= size()) */ Optional remove(int slot); @@ -112,6 +129,8 @@ default int add(Item stack) { * @param slot The slot index to remove * @param amount The amount of items to remove * @return The items removed + * @throws IndexOutOfBoundsException if the slot is out of range + * (index < 0 || index >= size()) */ default Optional remove(int slot, int amount) { Optional o = get(slot); @@ -120,9 +139,8 @@ default Optional remove(int slot, int amount) { item.setCount(item.count() - amount); if (item.count() <= 0) { - remove(slot); + return remove(slot); } - return Optional.of(item.withAmount(amount)); } return Optional.empty(); @@ -134,14 +152,12 @@ default Optional remove(int slot, int amount) { * @return The items removed */ default Optional remove(Item check) { - int left = check.count(); - for (int i = 0; i < size(); i++) { Optional opItem = get(i); - if (opItem.isPresent()) { - Optional removed = remove(i, check.count()); + if (opItem.isPresent() && check.sameItemType(opItem.get())) { + Optional removed = remove(i, left); if (removed.isPresent()) { left -= removed.get().count(); @@ -150,11 +166,9 @@ default Optional remove(Item check) { } int removed = check.count() - left; - if (removed > 0) { return Optional.of(check.withAmount(removed)); } - return Optional.empty(); } @@ -163,7 +177,7 @@ default Optional remove(Item check) { * @return This inventory as list of {@link Item Items} */ default List toList() { - ArrayList list = new ArrayList<>(); + List list = new ArrayList<>(); for (Item i : this) { list.add(i); } @@ -189,8 +203,8 @@ default Spliterator spliterator() { } /** - * Represents this inventory as {@link Item} {@link Stream} - * @return This inventory as {@link Item} {@link Stream} + * Represents this inventory as an {@link Item} {@link Stream} + * @return This inventory as an {@link Item} {@link Stream} */ default Stream stream() { return StreamSupport.stream(spliterator(), false); diff --git a/src/main/java/nova/core/component/inventory/InventoryIterator.java b/src/main/java/nova/core/component/inventory/InventoryIterator.java index 177cd2ecc..7f54383e8 100644 --- a/src/main/java/nova/core/component/inventory/InventoryIterator.java +++ b/src/main/java/nova/core/component/inventory/InventoryIterator.java @@ -23,6 +23,7 @@ import nova.core.item.Item; import java.util.Iterator; +import java.util.NoSuchElementException; import java.util.Optional; class InventoryIterator implements Iterator { @@ -30,6 +31,7 @@ class InventoryIterator implements Iterator { private int nextPos; private int currentPos; private Item next = null; + private boolean initialized = false; InventoryIterator(Inventory inv) { this.inv = inv; @@ -54,7 +56,11 @@ public boolean hasNext() { } @Override - public Item next() { + public Item next() throws NoSuchElementException { + if (next == null) { + throw new NoSuchElementException(); + } + initialized = true; Item current = next; findNext(); return current; @@ -62,6 +68,8 @@ public Item next() { @Override public void remove() { - inv.remove(currentPos); + if (!initialized || !inv.remove(currentPos).isPresent()) { + throw new IllegalStateException(); + } } } diff --git a/src/main/java/nova/core/component/inventory/InventorySimple.java b/src/main/java/nova/core/component/inventory/InventorySimple.java index 9a6e51bff..f45deb33b 100644 --- a/src/main/java/nova/core/component/inventory/InventorySimple.java +++ b/src/main/java/nova/core/component/inventory/InventorySimple.java @@ -77,33 +77,42 @@ public int size() { } @Override - public Optional get(int slot) { + public Optional get(int slot) throws IndexOutOfBoundsException { if (slot < 0 || slot >= items.length) { - return Optional.empty(); + throw new IndexOutOfBoundsException(); } else { return Optional.ofNullable(items[slot]); } } @Override - public boolean set(int slot, Item item) { - if (slot < 0 || slot >= items.length || !isItemValidForSlot.apply(slot, item)) { + public boolean set(int slot, Optional item) throws IndexOutOfBoundsException { + if (slot < 0 || slot >= items.length) { + throw new IndexOutOfBoundsException(); + } else if (!item.filter(i -> isItemValidForSlot.apply(slot, i)).isPresent()) { return false; } else { - items[slot] = item; - changed = true; - return true; + boolean result = (items[slot] != item.orElse(null)); + changed |= result; + items[slot] = item.get(); + return result; } } @Override - public Optional remove(int slot) { + public Optional remove(int slot) throws IndexOutOfBoundsException { + if (slot < 0 || slot >= items.length) { + throw new IndexOutOfBoundsException(); + } Item item = items[slot]; items[slot] = null; return Optional.ofNullable(item); } - public Optional swap(int slot, Item item) { + public Optional swap(int slot, Optional item) throws IndexOutOfBoundsException { + if (slot < 0 || slot >= items.length) { + throw new IndexOutOfBoundsException(); + } Optional current = get(slot); set(slot, item); return current; diff --git a/src/main/java/nova/core/component/inventory/InventoryView.java b/src/main/java/nova/core/component/inventory/InventoryView.java index 9b42f37cb..9008d7e00 100644 --- a/src/main/java/nova/core/component/inventory/InventoryView.java +++ b/src/main/java/nova/core/component/inventory/InventoryView.java @@ -41,26 +41,30 @@ public InventoryView(Inventory parent, int[] slots) { } @Override - public Optional get(int slot) { + public Optional get(int slot) throws IndexOutOfBoundsException { if (slot < 0 || slot >= slots.length) { - return null; + throw new IndexOutOfBoundsException(); } else { return parent.get(slots[slot]); } } @Override - public boolean set(int slot, Item stack) { + public boolean set(int slot, Optional stack) throws IndexOutOfBoundsException { if (slot < 0 || slot >= slots.length) { - return false; + throw new IndexOutOfBoundsException(); } else { return parent.set(slots[slot], stack); } } @Override - public Optional remove(int slot) { - return parent.remove(slot); + public Optional remove(int slot) throws IndexOutOfBoundsException { + if (slot < 0 || slot >= slots.length) { + throw new IndexOutOfBoundsException(); + } else { + return parent.remove(slots[slot]); + } } @Override diff --git a/src/main/java/nova/core/item/Item.java b/src/main/java/nova/core/item/Item.java index f3770a9a8..fd5ac2301 100644 --- a/src/main/java/nova/core/item/Item.java +++ b/src/main/java/nova/core/item/Item.java @@ -31,6 +31,7 @@ import nova.core.retention.Storable; import nova.core.util.Direction; import nova.core.util.Identifiable; +import nova.core.util.math.MathUtil; import org.apache.commons.math3.geometry.euclidean.threed.Vector3D; import java.util.List; @@ -45,8 +46,8 @@ public class Item extends ComponentProvider implements Identifiabl private int count = 1; /** - * Called to get the ItemFactory that refers to this Block class. - * @return The {@link nova.core.item.ItemFactory} that refers to this Block class. + * Called to get the ItemFactory that refers to this Item class. + * @return The {@link ItemFactory} that refers to this Item class. */ public final ItemFactory getFactory() { return (ItemFactory) components.get(FactoryProvider.class).factory; @@ -81,9 +82,10 @@ public int count() { /** * Sets new size of this ItemStack * @param size New size + * @return this instance */ public Item setCount(int size) { - count = Math.max(Math.min(getMaxCount(), size), 0); + count = MathUtil.clamp(size, 0, getMaxCount()); return this; } From ff59b33919b2d1f64859bca91d948f12e5260bc4 Mon Sep 17 00:00:00 2001 From: ExE Boss <3889017+ExE-Boss@users.noreply.github.com> Date: Thu, 21 Feb 2019 20:20:00 +0100 Subject: [PATCH 2/4] =?UTF-8?q?Allow=20`null`=20as=C2=A0parameter=20to?= =?UTF-8?q?=C2=A0Inventory=20methods?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wrapper/inventory/BWInventory.java | 4 +-- .../wrapper/inventory/FWInventory.java | 4 +-- .../v1_8/wrapper/inventory/BWInventory.java | 4 +-- .../v1_8/wrapper/inventory/FWInventory.java | 4 +-- .../core/component/inventory/Inventory.java | 33 ++++++++++++------- .../component/inventory/InventorySimple.java | 10 +++--- .../component/inventory/InventoryView.java | 2 +- 7 files changed, 33 insertions(+), 28 deletions(-) diff --git a/minecraft/1.7.10/src/main/java/nova/core/wrapper/mc/forge/v1_7_10/wrapper/inventory/BWInventory.java b/minecraft/1.7.10/src/main/java/nova/core/wrapper/mc/forge/v1_7_10/wrapper/inventory/BWInventory.java index 84cd9e0ba..9c390f533 100644 --- a/minecraft/1.7.10/src/main/java/nova/core/wrapper/mc/forge/v1_7_10/wrapper/inventory/BWInventory.java +++ b/minecraft/1.7.10/src/main/java/nova/core/wrapper/mc/forge/v1_7_10/wrapper/inventory/BWInventory.java @@ -41,9 +41,9 @@ public Optional get(int slot) { } @Override - public boolean set(int slot, Optional item) { + public boolean set(int slot, Item item) { Optional orig = get(slot); - wrapped.setInventorySlotContents(slot, item.map(ItemConverter.instance()::toNative).orElse(null)); + wrapped.setInventorySlotContents(slot, ItemConverter.instance().toNative(item)); return !orig.equals(get(slot)); } diff --git a/minecraft/1.7.10/src/main/java/nova/core/wrapper/mc/forge/v1_7_10/wrapper/inventory/FWInventory.java b/minecraft/1.7.10/src/main/java/nova/core/wrapper/mc/forge/v1_7_10/wrapper/inventory/FWInventory.java index 194051d3c..42412c013 100644 --- a/minecraft/1.7.10/src/main/java/nova/core/wrapper/mc/forge/v1_7_10/wrapper/inventory/FWInventory.java +++ b/minecraft/1.7.10/src/main/java/nova/core/wrapper/mc/forge/v1_7_10/wrapper/inventory/FWInventory.java @@ -27,8 +27,6 @@ import nova.core.component.inventory.InventorySimple; import nova.core.wrapper.mc.forge.v1_7_10.wrapper.item.ItemConverter; -import java.util.Optional; - public class FWInventory implements IInventory { public final Inventory wrapped; @@ -70,7 +68,7 @@ public ItemStack getStackInSlotOnClosing(int slot) { @Override public void setInventorySlotContents(int slot, ItemStack item) { if (slot < 0 || slot >= wrapped.size()) return; - wrapped.set(slot, Optional.ofNullable(item).map(ItemConverter.instance()::toNova)); + wrapped.set(slot, item == null ? null : ItemConverter.instance().toNova(item)); } @Override diff --git a/minecraft/1.8/src/main/java/nova/core/wrapper/mc/forge/v1_8/wrapper/inventory/BWInventory.java b/minecraft/1.8/src/main/java/nova/core/wrapper/mc/forge/v1_8/wrapper/inventory/BWInventory.java index ac3922731..575faa81e 100644 --- a/minecraft/1.8/src/main/java/nova/core/wrapper/mc/forge/v1_8/wrapper/inventory/BWInventory.java +++ b/minecraft/1.8/src/main/java/nova/core/wrapper/mc/forge/v1_8/wrapper/inventory/BWInventory.java @@ -41,9 +41,9 @@ public Optional get(int slot) { } @Override - public boolean set(int slot, Optional item) { + public boolean set(int slot, Item item) { Optional orig = get(slot); - wrapped.setInventorySlotContents(slot, item.map(ItemConverter.instance()::toNative).orElse(null)); + wrapped.setInventorySlotContents(slot, ItemConverter.instance().toNative(item)); return !orig.equals(get(slot)); } diff --git a/minecraft/1.8/src/main/java/nova/core/wrapper/mc/forge/v1_8/wrapper/inventory/FWInventory.java b/minecraft/1.8/src/main/java/nova/core/wrapper/mc/forge/v1_8/wrapper/inventory/FWInventory.java index 3377365f0..1e3e87314 100644 --- a/minecraft/1.8/src/main/java/nova/core/wrapper/mc/forge/v1_8/wrapper/inventory/FWInventory.java +++ b/minecraft/1.8/src/main/java/nova/core/wrapper/mc/forge/v1_8/wrapper/inventory/FWInventory.java @@ -28,8 +28,6 @@ import nova.core.component.inventory.InventorySimple; import nova.core.wrapper.mc.forge.v1_8.wrapper.item.ItemConverter; -import java.util.Optional; - public class FWInventory implements IInventory { public Inventory wrapped; @@ -73,7 +71,7 @@ public ItemStack getStackInSlotOnClosing(int slot) { @Override public void setInventorySlotContents(int slot, ItemStack item) { if (slot < 0 || slot >= wrapped.size()) return; - wrapped.set(slot, Optional.ofNullable(item).map(ItemConverter.instance()::toNova)); + wrapped.set(slot, item == null ? null : ItemConverter.instance().toNova(item)); } @Override diff --git a/src/main/java/nova/core/component/inventory/Inventory.java b/src/main/java/nova/core/component/inventory/Inventory.java index e70dfb10b..71fdebfaa 100644 --- a/src/main/java/nova/core/component/inventory/Inventory.java +++ b/src/main/java/nova/core/component/inventory/Inventory.java @@ -47,7 +47,7 @@ public interface Inventory extends Iterable { * @throws IndexOutOfBoundsException if the slot is out of range * (index < 0 || index >= size()) */ - Optional get(int slot); + Optional get(int slot) throws IndexOutOfBoundsException; /** * Sets {@link Item} in slot @@ -57,7 +57,7 @@ public interface Inventory extends Iterable { * @throws IndexOutOfBoundsException if the slot is out of range * (index < 0 || index >= size()) */ - boolean set(int slot, Optional item); + boolean set(int slot, Item item) throws IndexOutOfBoundsException; /** * Gets count of slots @@ -78,8 +78,11 @@ public interface Inventory extends Iterable { * @throws IndexOutOfBoundsException if the slot is out of range * (index < 0 || index >= size()) */ - default Optional add(int slot, Item item) { + default Optional add(int slot, Item item) throws IndexOutOfBoundsException { Optional o = get(slot); + if (item == null) { + return Optional.empty(); + } if (o.isPresent()) { if (item.sameItemType(o.get())) { return Optional.of(item.withAmount(item.count() - o.get().addCount(item.count()))); @@ -87,7 +90,7 @@ default Optional add(int slot, Item item) { return Optional.of(item); } } else { - set(slot, Optional.of(item)); + set(slot, item); return Optional.empty(); } } @@ -98,6 +101,9 @@ default Optional add(int slot, Item item) { * @return Amount of items left(did not fit inside this inventory) */ default Optional add(Item item) { + if (item == null) { + return Optional.empty(); + } int itemsLeft = item.count(); for (int i = 0; i < size(); i++) { itemsLeft = add(i, item.withAmount(itemsLeft)).map(Item::count).orElse(0); @@ -122,7 +128,7 @@ default Optional add(Item item) { * @throws IndexOutOfBoundsException if the slot is out of range * (index < 0 || index >= size()) */ - Optional remove(int slot); + Optional remove(int slot) throws IndexOutOfBoundsException; /** * Removes a certain amount of items from a slot. @@ -132,7 +138,7 @@ default Optional add(Item item) { * @throws IndexOutOfBoundsException if the slot is out of range * (index < 0 || index >= size()) */ - default Optional remove(int slot, int amount) { + default Optional remove(int slot, int amount) throws IndexOutOfBoundsException { Optional o = get(slot); if (o.isPresent()) { Item item = o.get(); @@ -148,15 +154,18 @@ default Optional remove(int slot, int amount) { /** * Removes a certain item from a slot. - * @param check The item type to check with + * @param item The item type to check with * @return The items removed */ - default Optional remove(Item check) { - int left = check.count(); + default Optional remove(Item item) { + if (item == null) { + return Optional.empty(); + } + int left = item.count(); for (int i = 0; i < size(); i++) { Optional opItem = get(i); - if (opItem.isPresent() && check.sameItemType(opItem.get())) { + if (opItem.isPresent() && item.sameItemType(opItem.get())) { Optional removed = remove(i, left); if (removed.isPresent()) { @@ -165,9 +174,9 @@ default Optional remove(Item check) { } } - int removed = check.count() - left; + int removed = item.count() - left; if (removed > 0) { - return Optional.of(check.withAmount(removed)); + return Optional.of(item.withAmount(removed)); } return Optional.empty(); } diff --git a/src/main/java/nova/core/component/inventory/InventorySimple.java b/src/main/java/nova/core/component/inventory/InventorySimple.java index f45deb33b..6121ba084 100644 --- a/src/main/java/nova/core/component/inventory/InventorySimple.java +++ b/src/main/java/nova/core/component/inventory/InventorySimple.java @@ -86,15 +86,15 @@ public Optional get(int slot) throws IndexOutOfBoundsException { } @Override - public boolean set(int slot, Optional item) throws IndexOutOfBoundsException { + public boolean set(int slot, Item item) throws IndexOutOfBoundsException { if (slot < 0 || slot >= items.length) { throw new IndexOutOfBoundsException(); - } else if (!item.filter(i -> isItemValidForSlot.apply(slot, i)).isPresent()) { + } else if (isItemValidForSlot.apply(slot, item)) { return false; } else { - boolean result = (items[slot] != item.orElse(null)); + boolean result = (items[slot] != item); changed |= result; - items[slot] = item.get(); + items[slot] = item; return result; } } @@ -109,7 +109,7 @@ public Optional remove(int slot) throws IndexOutOfBoundsException { return Optional.ofNullable(item); } - public Optional swap(int slot, Optional item) throws IndexOutOfBoundsException { + public Optional swap(int slot, Item item) throws IndexOutOfBoundsException { if (slot < 0 || slot >= items.length) { throw new IndexOutOfBoundsException(); } diff --git a/src/main/java/nova/core/component/inventory/InventoryView.java b/src/main/java/nova/core/component/inventory/InventoryView.java index 9008d7e00..e32947c85 100644 --- a/src/main/java/nova/core/component/inventory/InventoryView.java +++ b/src/main/java/nova/core/component/inventory/InventoryView.java @@ -50,7 +50,7 @@ public Optional get(int slot) throws IndexOutOfBoundsException { } @Override - public boolean set(int slot, Optional stack) throws IndexOutOfBoundsException { + public boolean set(int slot, Item stack) throws IndexOutOfBoundsException { if (slot < 0 || slot >= slots.length) { throw new IndexOutOfBoundsException(); } else { From 10ad2748ef9af4e7405bbd13d95b4529b20ef74d Mon Sep 17 00:00:00 2001 From: ExE Boss <3889017+ExE-Boss@users.noreply.github.com> Date: Fri, 22 Feb 2019 09:45:00 +0100 Subject: [PATCH 3/4] =?UTF-8?q?Apply=20inventory=20changes=20to=C2=A0Minec?= =?UTF-8?q?raft=C2=A01.11.2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wrapper/inventory/BWInventory.java | 19 +++++++++++++++++-- .../wrapper/inventory/FWInventory.java | 10 ++++------ .../wrapper/inventory/BWInventory.java | 9 ++++----- .../wrapper/inventory/FWInventory.java | 10 +++------- .../v1_8/wrapper/inventory/BWInventory.java | 9 ++++----- .../v1_8/wrapper/inventory/FWInventory.java | 10 +++------- 6 files changed, 35 insertions(+), 32 deletions(-) diff --git a/minecraft/1.11.2/src/main/java/nova/core/wrapper/mc/forge/v1_11_2/wrapper/inventory/BWInventory.java b/minecraft/1.11.2/src/main/java/nova/core/wrapper/mc/forge/v1_11_2/wrapper/inventory/BWInventory.java index ee9e28e69..cc912866f 100644 --- a/minecraft/1.11.2/src/main/java/nova/core/wrapper/mc/forge/v1_11_2/wrapper/inventory/BWInventory.java +++ b/minecraft/1.11.2/src/main/java/nova/core/wrapper/mc/forge/v1_11_2/wrapper/inventory/BWInventory.java @@ -25,7 +25,6 @@ import nova.core.component.inventory.Inventory; import nova.core.item.Item; import nova.core.wrapper.mc.forge.v1_11_2.wrapper.item.ItemConverter; -import nova.internal.core.Game; import java.util.Optional; @@ -43,8 +42,9 @@ public Optional get(int slot) { @Override public boolean set(int slot, Item item) { + Optional orig = get(slot); wrapped.setInventorySlotContents(slot, ItemConverter.instance().toNative(item)); - return true; + return !orig.equals(get(slot)); } @Override @@ -54,6 +54,21 @@ public Optional remove(int slot) { return item; } + @Override + public Optional remove(int slot, int amount) { + ItemStack stack = wrapped.getStackInSlot(slot); + if (stack != null) { + Item item = ItemConverter.instance().toNova(stack); + item.setCount(item.count() - amount); + if (item.count() <= 0) { + return remove(slot); + } + ItemConverter.instance().updateMCItemStack(stack, item); + return Optional.of(item.withAmount(amount)); + } + return Optional.empty(); + } + @Override public int size() { return wrapped.getSizeInventory(); diff --git a/minecraft/1.11.2/src/main/java/nova/core/wrapper/mc/forge/v1_11_2/wrapper/inventory/FWInventory.java b/minecraft/1.11.2/src/main/java/nova/core/wrapper/mc/forge/v1_11_2/wrapper/inventory/FWInventory.java index 5dc81b61d..1e3e08194 100644 --- a/minecraft/1.11.2/src/main/java/nova/core/wrapper/mc/forge/v1_11_2/wrapper/inventory/FWInventory.java +++ b/minecraft/1.11.2/src/main/java/nova/core/wrapper/mc/forge/v1_11_2/wrapper/inventory/FWInventory.java @@ -52,7 +52,8 @@ public boolean isEmpty() { @Override public ItemStack getStackInSlot(int slot) { - return ItemConverter.instance().toNative(wrapped.get(slot).orElse(null)); + if (slot < 0 || slot >= wrapped.size()) return null; + return wrapped.get(slot).map(ItemConverter.instance()::toNative).orElse(null); } @Override @@ -82,7 +83,8 @@ public ItemStack removeStackFromSlot(int slot) { @Override public void setInventorySlotContents(int slot, ItemStack stack) { - wrapped.set(slot, stack != null ? ItemConverter.instance().getNovaItem(stack) : null); + if (slot < 0 || slot >= wrapped.size()) return; + wrapped.set(slot, stack != null ? ItemConverter.instance().toNova(stack) : null); } @Override @@ -113,23 +115,19 @@ public void markDirty() { @Override public boolean isUsableByPlayer(EntityPlayer player) { - // TODO Auto-generated method stub return true; } @Override public void openInventory(EntityPlayer playerIn) { - } @Override public void closeInventory(EntityPlayer playerIn) { - } @Override public boolean isItemValidForSlot(int slot, ItemStack stack) { - // TODO Auto-generated method stub return true; } diff --git a/minecraft/1.7.10/src/main/java/nova/core/wrapper/mc/forge/v1_7_10/wrapper/inventory/BWInventory.java b/minecraft/1.7.10/src/main/java/nova/core/wrapper/mc/forge/v1_7_10/wrapper/inventory/BWInventory.java index 9c390f533..e5464250c 100644 --- a/minecraft/1.7.10/src/main/java/nova/core/wrapper/mc/forge/v1_7_10/wrapper/inventory/BWInventory.java +++ b/minecraft/1.7.10/src/main/java/nova/core/wrapper/mc/forge/v1_7_10/wrapper/inventory/BWInventory.java @@ -56,15 +56,14 @@ public Optional remove(int slot) { @Override public Optional remove(int slot, int amount) { - Optional itemStack = Optional.ofNullable(wrapped.getStackInSlot(slot)); - Optional o = itemStack.map(ItemConverter.instance()::toNova); - if (o.isPresent()) { - Item item = o.get(); + ItemStack stack = wrapped.getStackInSlot(slot); + if (stack != null) { + Item item = ItemConverter.instance().toNova(stack); item.setCount(item.count() - amount); if (item.count() <= 0) { return remove(slot); } - ItemConverter.instance().updateMCItemStack(itemStack.get(), item); + ItemConverter.instance().updateMCItemStack(stack, item); return Optional.of(item.withAmount(amount)); } return Optional.empty(); diff --git a/minecraft/1.7.10/src/main/java/nova/core/wrapper/mc/forge/v1_7_10/wrapper/inventory/FWInventory.java b/minecraft/1.7.10/src/main/java/nova/core/wrapper/mc/forge/v1_7_10/wrapper/inventory/FWInventory.java index 42412c013..979e44a46 100644 --- a/minecraft/1.7.10/src/main/java/nova/core/wrapper/mc/forge/v1_7_10/wrapper/inventory/FWInventory.java +++ b/minecraft/1.7.10/src/main/java/nova/core/wrapper/mc/forge/v1_7_10/wrapper/inventory/FWInventory.java @@ -24,7 +24,6 @@ import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; import nova.core.component.inventory.Inventory; -import nova.core.component.inventory.InventorySimple; import nova.core.wrapper.mc.forge.v1_7_10.wrapper.item.ItemConverter; public class FWInventory implements IInventory { @@ -66,9 +65,9 @@ public ItemStack getStackInSlotOnClosing(int slot) { } @Override - public void setInventorySlotContents(int slot, ItemStack item) { + public void setInventorySlotContents(int slot, ItemStack stack) { if (slot < 0 || slot >= wrapped.size()) return; - wrapped.set(slot, item == null ? null : ItemConverter.instance().toNova(item)); + wrapped.set(slot, stack != null ? ItemConverter.instance().toNova(stack) : null); } @Override @@ -106,10 +105,7 @@ public void closeInventory() { } @Override - public boolean isItemValidForSlot(int slot, ItemStack item) { - if (item != null && wrapped instanceof InventorySimple) { - ((InventorySimple) wrapped).isItemValidForSlot.apply(slot, ItemConverter.instance().toNova(item)); - } + public boolean isItemValidForSlot(int slot, ItemStack stack) { return true; } diff --git a/minecraft/1.8/src/main/java/nova/core/wrapper/mc/forge/v1_8/wrapper/inventory/BWInventory.java b/minecraft/1.8/src/main/java/nova/core/wrapper/mc/forge/v1_8/wrapper/inventory/BWInventory.java index 575faa81e..f34cb3075 100644 --- a/minecraft/1.8/src/main/java/nova/core/wrapper/mc/forge/v1_8/wrapper/inventory/BWInventory.java +++ b/minecraft/1.8/src/main/java/nova/core/wrapper/mc/forge/v1_8/wrapper/inventory/BWInventory.java @@ -56,15 +56,14 @@ public Optional remove(int slot) { @Override public Optional remove(int slot, int amount) { - Optional itemStack = Optional.ofNullable(wrapped.getStackInSlot(slot)); - Optional o = itemStack.map(ItemConverter.instance()::toNova); - if (o.isPresent()) { - Item item = o.get(); + ItemStack stack = wrapped.getStackInSlot(slot); + if (stack != null) { + Item item = ItemConverter.instance().toNova(stack); item.setCount(item.count() - amount); if (item.count() <= 0) { return remove(slot); } - ItemConverter.instance().updateMCItemStack(itemStack.get(), item); + ItemConverter.instance().updateMCItemStack(stack, item); return Optional.of(item.withAmount(amount)); } return Optional.empty(); diff --git a/minecraft/1.8/src/main/java/nova/core/wrapper/mc/forge/v1_8/wrapper/inventory/FWInventory.java b/minecraft/1.8/src/main/java/nova/core/wrapper/mc/forge/v1_8/wrapper/inventory/FWInventory.java index 1e3e87314..469aae0ef 100644 --- a/minecraft/1.8/src/main/java/nova/core/wrapper/mc/forge/v1_8/wrapper/inventory/FWInventory.java +++ b/minecraft/1.8/src/main/java/nova/core/wrapper/mc/forge/v1_8/wrapper/inventory/FWInventory.java @@ -25,7 +25,6 @@ import net.minecraft.item.ItemStack; import net.minecraft.util.IChatComponent; import nova.core.component.inventory.Inventory; -import nova.core.component.inventory.InventorySimple; import nova.core.wrapper.mc.forge.v1_8.wrapper.item.ItemConverter; public class FWInventory implements IInventory { @@ -69,9 +68,9 @@ public ItemStack getStackInSlotOnClosing(int slot) { } @Override - public void setInventorySlotContents(int slot, ItemStack item) { + public void setInventorySlotContents(int slot, ItemStack stack) { if (slot < 0 || slot >= wrapped.size()) return; - wrapped.set(slot, item == null ? null : ItemConverter.instance().toNova(item)); + wrapped.set(slot, stack != null ? ItemConverter.instance().toNova(stack) : null); } @Override @@ -114,10 +113,7 @@ public void closeInventory(EntityPlayer playerIn) { } @Override - public boolean isItemValidForSlot(int slot, ItemStack item) { - if (item != null && wrapped instanceof InventorySimple) { - ((InventorySimple) wrapped).isItemValidForSlot.apply(slot, ItemConverter.instance().toNova(item)); - } + public boolean isItemValidForSlot(int slot, ItemStack stack) { return true; } From 50b379e250f64e047a9c352bdddf53879f229f3c Mon Sep 17 00:00:00 2001 From: ExE Boss <3889017+ExE-Boss@users.noreply.github.com> Date: Fri, 22 Feb 2019 10:50:00 +0100 Subject: [PATCH 4/4] =?UTF-8?q?Create=20test=20stub=20for=C2=A0`InventoryI?= =?UTF-8?q?terator`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../inventory/InventoryIterator.java | 3 +- .../inventory/InventoryIteratorTest.java | 40 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 src/test/java/nova/core/component/inventory/InventoryIteratorTest.java diff --git a/src/main/java/nova/core/component/inventory/InventoryIterator.java b/src/main/java/nova/core/component/inventory/InventoryIterator.java index 7f54383e8..d4ff2cdb8 100644 --- a/src/main/java/nova/core/component/inventory/InventoryIterator.java +++ b/src/main/java/nova/core/component/inventory/InventoryIterator.java @@ -24,6 +24,7 @@ import java.util.Iterator; import java.util.NoSuchElementException; +import java.util.Objects; import java.util.Optional; class InventoryIterator implements Iterator { @@ -34,7 +35,7 @@ class InventoryIterator implements Iterator { private boolean initialized = false; InventoryIterator(Inventory inv) { - this.inv = inv; + this.inv = Objects.requireNonNull(inv, "`inv` can't be null"); findNext(); } diff --git a/src/test/java/nova/core/component/inventory/InventoryIteratorTest.java b/src/test/java/nova/core/component/inventory/InventoryIteratorTest.java new file mode 100644 index 000000000..72c00ef68 --- /dev/null +++ b/src/test/java/nova/core/component/inventory/InventoryIteratorTest.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2019 NOVA, All rights reserved. + * This library is free software, licensed under GNU Lesser General Public License version 3 + * + * This file is part of NOVA. + * + * NOVA is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * + * NOVA is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with NOVA. If not, see . + */ + +package nova.core.component.inventory; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.*; + +/** + * @author ExE Boss + */ +public class InventoryIteratorTest { + + public InventoryIteratorTest() { + } + + @Test + public void testConstructorThrowsNPE() { + assertThatCode(() -> new InventoryIterator(null)) + .isInstanceOf(NullPointerException.class); + } +}