Fix external storage bug

This commit is contained in:
Raoul Van den Berge
2016-05-22 00:32:52 +02:00
parent 89084aea38
commit 398cc02924

View File

@@ -48,71 +48,55 @@ public class TileExternalStorage extends TileMachine implements IStorageProvider
@Override @Override
public void addItems(List<ItemGroup> items) { public void addItems(List<ItemGroup> items) {
IItemHandler handler = getItemHandler(); IDeepStorageUnit storageUnit = getStorageUnit();
if (handler != null) { if (storageUnit != null) {
for (int i = 0; i < handler.getSlots(); ++i) { if (storageUnit.getStoredItemType() != null) {
if (handler.getStackInSlot(i) != null) { items.add(new ItemGroup(storageUnit.getStoredItemType().copy()));
items.add(new ItemGroup(handler.getStackInSlot(i).copy()));
}
} }
} else { } else {
IDeepStorageUnit storageUnit = getStorageUnit(); IItemHandler handler = getItemHandler();
// @todo: doesn't work if (handler != null) {
if (storageUnit != null && storageUnit.getStoredItemType() != null) { for (int i = 0; i < handler.getSlots(); ++i) {
items.add(new ItemGroup(storageUnit.getStoredItemType().copy())); if (handler.getStackInSlot(i) != null) {
items.add(new ItemGroup(handler.getStackInSlot(i).copy()));
}
}
} }
} }
} }
@Override @Override
public void push(ItemStack stack) { public void push(ItemStack stack) {
IItemHandler handler = getItemHandler(); IDeepStorageUnit storageUnit = getStorageUnit();
if (handler != null) {
for (int i = 0; i < handler.getSlots(); ++i) {
if (handler.insertItem(i, stack, false) == null) {
break;
}
}
} else {
IDeepStorageUnit storageUnit = getStorageUnit();
if (storageUnit != null) {
if (storageUnit.getStoredItemType() == null) { if (storageUnit.getStoredItemType() == null) {
storageUnit.setStoredItemType(stack, stack.stackSize); storageUnit.setStoredItemType(stack, stack.stackSize);
} else { } else {
storageUnit.setStoredItemCount(storageUnit.getStoredItemType().stackSize + stack.stackSize); storageUnit.setStoredItemCount(storageUnit.getStoredItemType().stackSize + stack.stackSize);
} }
} else {
IItemHandler handler = getItemHandler();
if (handler != null) {
for (int i = 0; i < handler.getSlots(); ++i) {
if (handler.insertItem(i, stack, false) == null) {
break;
}
}
}
} }
} }
@Override @Override
public ItemStack take(ItemStack stack, int flags) { public ItemStack take(ItemStack stack, int flags) {
IItemHandler handler = getItemHandler();
int quantity = stack.stackSize; int quantity = stack.stackSize;
if (handler != null) { IDeepStorageUnit storageUnit = getStorageUnit();
for (int i = 0; i < handler.getSlots(); ++i) {
ItemStack slot = handler.getStackInSlot(i);
if (slot != null && RefinedStorageUtils.compareStack(slot, stack, flags)) {
if (quantity > slot.stackSize) {
quantity = slot.stackSize;
}
handler.extractItem(i, quantity, false);
ItemStack took = slot.copy();
took.stackSize = quantity;
return took;
}
}
} else {
IDeepStorageUnit storageUnit = getStorageUnit();
if (storageUnit != null) {
if (storageUnit.getStoredItemType() != null && RefinedStorageUtils.compareStackNoQuantity(storageUnit.getStoredItemType(), stack)) { if (storageUnit.getStoredItemType() != null && RefinedStorageUtils.compareStackNoQuantity(storageUnit.getStoredItemType(), stack)) {
if (quantity > storageUnit.getStoredItemType().stackSize) { if (quantity > storageUnit.getStoredItemType().stackSize) {
quantity = storageUnit.getStoredItemType().stackSize; quantity = storageUnit.getStoredItemType().stackSize;
@@ -125,6 +109,27 @@ public class TileExternalStorage extends TileMachine implements IStorageProvider
return took; return took;
} }
} else {
IItemHandler handler = getItemHandler();
if (handler != null) {
for (int i = 0; i < handler.getSlots(); ++i) {
ItemStack slot = handler.getStackInSlot(i);
if (slot != null && RefinedStorageUtils.compareStack(slot, stack, flags)) {
if (quantity > slot.stackSize) {
quantity = slot.stackSize;
}
handler.extractItem(i, quantity, false);
ItemStack took = slot.copy();
took.stackSize = quantity;
return took;
}
}
}
} }
return null; return null;
@@ -133,23 +138,23 @@ public class TileExternalStorage extends TileMachine implements IStorageProvider
@Override @Override
public boolean mayPush(ItemStack stack) { public boolean mayPush(ItemStack stack) {
if (ModeConfigUtils.doesNotViolateMode(inventory, this, compare, stack)) { if (ModeConfigUtils.doesNotViolateMode(inventory, this, compare, stack)) {
IItemHandler handler = getItemHandler(); IDeepStorageUnit storageUnit = getStorageUnit();
if (handler != null) { if (storageUnit != null) {
for (int i = 0; i < handler.getSlots(); ++i) { if (storageUnit.getStoredItemType() == null) {
if (handler.insertItem(i, stack, true) == null) { return stack.stackSize < storageUnit.getMaxStoredCount();
return true;
}
} }
return RefinedStorageUtils.compareStackNoQuantity(storageUnit.getStoredItemType(), stack) && (storageUnit.getStoredItemType().stackSize + stack.stackSize) < storageUnit.getMaxStoredCount();
} else { } else {
IDeepStorageUnit storageUnit = getStorageUnit(); IItemHandler handler = getItemHandler();
if (storageUnit != null) { if (handler != null) {
if (storageUnit.getStoredItemType() == null) { for (int i = 0; i < handler.getSlots(); ++i) {
return stack.stackSize < storageUnit.getMaxStoredCount(); if (handler.insertItem(i, stack, true) == null) {
return true;
}
} }
return RefinedStorageUtils.compareStackNoQuantity(storageUnit.getStoredItemType(), stack) && (storageUnit.getStoredItemType().stackSize + stack.stackSize) < storageUnit.getMaxStoredCount();
} }
} }
} }
@@ -157,14 +162,16 @@ public class TileExternalStorage extends TileMachine implements IStorageProvider
return false; return false;
} }
public IItemHandler getItemHandler() { public TileEntity getFront() {
return RefinedStorageUtils.getItemHandler(worldObj.getTileEntity(pos.offset(getDirection())), getDirection().getOpposite()); return worldObj.getTileEntity(pos.offset(getDirection()));
} }
public IDeepStorageUnit getStorageUnit() { public IDeepStorageUnit getStorageUnit() {
TileEntity front = worldObj.getTileEntity(pos.offset(getDirection())); return getFront() instanceof IDeepStorageUnit ? (IDeepStorageUnit) getFront() : null;
}
return front instanceof IDeepStorageUnit ? (IDeepStorageUnit) front : null; public IItemHandler getItemHandler() {
return RefinedStorageUtils.getItemHandler(getFront(), getDirection().getOpposite());
} }
@Override @Override
@@ -173,23 +180,23 @@ public class TileExternalStorage extends TileMachine implements IStorageProvider
buf.writeInt(priority); buf.writeInt(priority);
IItemHandler handler = getItemHandler(); IDeepStorageUnit storageUnit = getStorageUnit();
if (handler != null) { if (storageUnit != null) {
int amount = 0; buf.writeInt(storageUnit.getStoredItemType() == null ? 0 : storageUnit.getStoredItemType().stackSize);
for (int i = 0; i < handler.getSlots(); ++i) {
if (handler.getStackInSlot(i) != null) {
amount += handler.getStackInSlot(i).stackSize;
}
}
buf.writeInt(amount);
} else { } else {
IDeepStorageUnit storageUnit = getStorageUnit(); IItemHandler handler = getItemHandler();
if (storageUnit != null) { if (handler != null) {
buf.writeInt(storageUnit.getStoredItemType() == null ? 0 : storageUnit.getStoredItemType().stackSize); int amount = 0;
for (int i = 0; i < handler.getSlots(); ++i) {
if (handler.getStackInSlot(i) != null) {
amount += handler.getStackInSlot(i).stackSize;
}
}
buf.writeInt(amount);
} else { } else {
buf.writeInt(0); buf.writeInt(0);
} }
@@ -323,15 +330,15 @@ public class TileExternalStorage extends TileMachine implements IStorageProvider
@Override @Override
public int getCapacity() { public int getCapacity() {
IItemHandler handler = getItemHandler(); IDeepStorageUnit storageUnit = getStorageUnit();
if (handler != null) { if (storageUnit != null) {
return handler.getSlots() * 64; return storageUnit.getMaxStoredCount();
} else { } else {
IDeepStorageUnit storageUnit = getStorageUnit(); IItemHandler handler = getItemHandler();
if (storageUnit != null) { if (handler != null) {
return storageUnit.getMaxStoredCount(); return handler.getSlots() * 64;
} }
} }