Skip to content

Commit

Permalink
Allow null as parameter to Inventory methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ExE-Boss committed Feb 21, 2019
1 parent 18ac33a commit ff59b33
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public Optional<Item> get(int slot) {
}

@Override
public boolean set(int slot, Optional<Item> item) {
public boolean set(int slot, Item item) {
Optional<Item> 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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public Optional<Item> get(int slot) {
}

@Override
public boolean set(int slot, Optional<Item> item) {
public boolean set(int slot, Item item) {
Optional<Item> 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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
33 changes: 21 additions & 12 deletions src/main/java/nova/core/component/inventory/Inventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public interface Inventory extends Iterable<Item> {
* @throws IndexOutOfBoundsException if the slot is out of range
* (<tt>index &lt; 0 || index &gt;= size()</tt>)
*/
Optional<Item> get(int slot);
Optional<Item> get(int slot) throws IndexOutOfBoundsException;

/**
* Sets {@link Item} in slot
Expand All @@ -57,7 +57,7 @@ public interface Inventory extends Iterable<Item> {
* @throws IndexOutOfBoundsException if the slot is out of range
* (<tt>index &lt; 0 || index &gt;= size()</tt>)
*/
boolean set(int slot, Optional<Item> item);
boolean set(int slot, Item item) throws IndexOutOfBoundsException;

/**
* Gets count of slots
Expand All @@ -78,16 +78,19 @@ public interface Inventory extends Iterable<Item> {
* @throws IndexOutOfBoundsException if the slot is out of range
* (<tt>index &lt; 0 || index &gt;= size()</tt>)
*/
default Optional<Item> add(int slot, Item item) {
default Optional<Item> add(int slot, Item item) throws IndexOutOfBoundsException {
Optional<Item> 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())));
} else {
return Optional.of(item);
}
} else {
set(slot, Optional.of(item));
set(slot, item);
return Optional.empty();
}
}
Expand All @@ -98,6 +101,9 @@ default Optional<Item> add(int slot, Item item) {
* @return Amount of items left(did not fit inside this inventory)
*/
default Optional<Item> 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);
Expand All @@ -122,7 +128,7 @@ default Optional<Item> add(Item item) {
* @throws IndexOutOfBoundsException if the slot is out of range
* (<tt>index &lt; 0 || index &gt;= size()</tt>)
*/
Optional<Item> remove(int slot);
Optional<Item> remove(int slot) throws IndexOutOfBoundsException;

/**
* Removes a certain amount of items from a slot.
Expand All @@ -132,7 +138,7 @@ default Optional<Item> add(Item item) {
* @throws IndexOutOfBoundsException if the slot is out of range
* (<tt>index &lt; 0 || index &gt;= size()</tt>)
*/
default Optional<Item> remove(int slot, int amount) {
default Optional<Item> remove(int slot, int amount) throws IndexOutOfBoundsException {
Optional<Item> o = get(slot);
if (o.isPresent()) {
Item item = o.get();
Expand All @@ -148,15 +154,18 @@ default Optional<Item> 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<Item> remove(Item check) {
int left = check.count();
default Optional<Item> remove(Item item) {
if (item == null) {
return Optional.empty();
}
int left = item.count();
for (int i = 0; i < size(); i++) {
Optional<Item> opItem = get(i);

if (opItem.isPresent() && check.sameItemType(opItem.get())) {
if (opItem.isPresent() && item.sameItemType(opItem.get())) {
Optional<Item> removed = remove(i, left);

if (removed.isPresent()) {
Expand All @@ -165,9 +174,9 @@ default Optional<Item> 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();
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/nova/core/component/inventory/InventorySimple.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ public Optional<Item> get(int slot) throws IndexOutOfBoundsException {
}

@Override
public boolean set(int slot, Optional<Item> 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;
}
}
Expand All @@ -109,7 +109,7 @@ public Optional<Item> remove(int slot) throws IndexOutOfBoundsException {
return Optional.ofNullable(item);
}

public Optional<Item> swap(int slot, Optional<Item> item) throws IndexOutOfBoundsException {
public Optional<Item> swap(int slot, Item item) throws IndexOutOfBoundsException {
if (slot < 0 || slot >= items.length) {
throw new IndexOutOfBoundsException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public Optional<Item> get(int slot) throws IndexOutOfBoundsException {
}

@Override
public boolean set(int slot, Optional<Item> stack) throws IndexOutOfBoundsException {
public boolean set(int slot, Item stack) throws IndexOutOfBoundsException {
if (slot < 0 || slot >= slots.length) {
throw new IndexOutOfBoundsException();
} else {
Expand Down

0 comments on commit ff59b33

Please sign in to comment.