Skip to content

Commit

Permalink
Work on Inventory code
Browse files Browse the repository at this point in the history
# 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
  • Loading branch information
ExE-Boss committed Mar 19, 2018
1 parent 297857e commit 81b6062
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -37,14 +36,15 @@ public BWInventory(IInventory mcInventory) {
}

@Override
public Optional<Item> get(int i) {
return Optional.ofNullable(wrapped.getStackInSlot(i)).map(ItemConverter.instance()::toNova);
public Optional<Item> 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> item) {
Optional<Item> orig = get(slot);
wrapped.setInventorySlotContents(slot, item.map(ItemConverter.instance()::toNative).orElse(null));
return !orig.equals(get(slot));
}

@Override
Expand All @@ -54,6 +54,22 @@ public Optional<Item> remove(int slot) {
return item;
}

@Override
public Optional<Item> remove(int slot, int amount) {
Optional<ItemStack> itemStack = Optional.ofNullable(wrapped.getStackInSlot(slot));
Optional<Item> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -37,14 +36,15 @@ public BWInventory(IInventory mcInventory) {
}

@Override
public Optional<Item> get(int i) {
return Optional.ofNullable(wrapped.getStackInSlot(i)).map(ItemConverter.instance()::toNova);
public Optional<Item> 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> item) {
Optional<Item> orig = get(slot);
wrapped.setInventorySlotContents(slot, item.map(ItemConverter.instance()::toNative).orElse(null));
return !orig.equals(get(slot));
}

@Override
Expand All @@ -54,6 +54,22 @@ public Optional<Item> remove(int slot) {
return item;
}

@Override
public Optional<Item> remove(int slot, int amount) {
Optional<ItemStack> itemStack = Optional.ofNullable(wrapped.getStackInSlot(slot));
Optional<Item> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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;
}

Expand All @@ -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() {
Expand Down
Loading

0 comments on commit 81b6062

Please sign in to comment.