Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Refactor inventory #324

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.v1_11_2.wrapper.item.ItemConverter;
import nova.internal.core.Game;

import java.util.Optional;

Expand All @@ -43,8 +42,9 @@ public Optional<Item> get(int slot) {

@Override
public boolean set(int slot, Item item) {
Optional<Item> orig = get(slot);
wrapped.setInventorySlotContents(slot, ItemConverter.instance().toNative(item));
return true;
return !orig.equals(get(slot));
}

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

@Override
public Optional<Item> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

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.v1_7_10.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, Item item) {
Optional<Item> orig = get(slot);
wrapped.setInventorySlotContents(slot, ItemConverter.instance().toNative(item));
return !orig.equals(get(slot));
}

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

@Override
public Optional<Item> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,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 @@ -65,7 +66,8 @@ public ItemStack getStackInSlotOnClosing(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
Expand All @@ -91,23 +93,19 @@ public void markDirty() {

@Override
public boolean isUseableByPlayer(EntityPlayer player) {
// TODO Auto-generated method stub
return true;
}

@Override
public void openInventory() {

}

@Override
public void closeInventory() {

}

@Override
public boolean isItemValidForSlot(int slot, ItemStack stack) {
// TODO Auto-generated method stub
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.v1_8.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, Item item) {
Optional<Item> orig = get(slot);
wrapped.setInventorySlotContents(slot, ItemConverter.instance().toNative(item));
return !orig.equals(get(slot));
}

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

@Override
public Optional<Item> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,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 +63,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);
if (slot < 0 || slot >= wrapped.size()) return;
wrapped.set(slot, stack != null ? ItemConverter.instance().toNova(stack) : null);
}

@Override
Expand Down Expand Up @@ -97,23 +101,19 @@ public void markDirty() {

@Override
public boolean isUseableByPlayer(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;
}

Expand All @@ -124,7 +124,6 @@ public int getField(int id) {

@Override
public void setField(int id, int value) {

}

@Override
Expand Down
Loading