Fixed External Storage not updating correctly

This commit is contained in:
Raoul Van den Berge
2016-07-07 19:50:03 +02:00
parent e2b6913bbd
commit 6c5a5fb45c
9 changed files with 89 additions and 14 deletions

View File

@@ -327,4 +327,16 @@ public final class RefinedStorageUtils {
public static boolean hasPattern(INetworkMaster network, ItemStack stack) {
return RefinedStorageUtils.getPattern(network, stack) != null;
}
public static int getItemStackHashCode(ItemStack stack) {
return getItemStackHashCode(stack, stack == null ? 0 : stack.stackSize);
}
public static int getItemStackHashCode(ItemStack stack, int stackSize) {
if (stack == null) {
return 0;
}
return stack.getItem().hashCode() + Math.max(1, stackSize) * (stack.hasTagCompound() ? stack.getTagCompound().hashCode() : 1);
}
}

View File

@@ -36,8 +36,12 @@ public class BlockExternalStorage extends BlockNode {
public void neighborChanged(IBlockState state, World world, BlockPos pos, Block block) {
super.neighborChanged(state, world, pos, block);
if (!world.isRemote && ((TileExternalStorage) world.getTileEntity(pos)).isConnected()) {
((TileExternalStorage) world.getTileEntity(pos)).updateStorage();
if (!world.isRemote) {
TileExternalStorage externalStorage = (TileExternalStorage) world.getTileEntity(pos);
if (externalStorage.getNetwork() != null) {
externalStorage.updateStorage(externalStorage.getNetwork());
}
}
}

View File

@@ -50,6 +50,7 @@ import refinedstorage.tile.TileBase;
import refinedstorage.tile.TileCrafter;
import refinedstorage.tile.config.IRedstoneModeConfig;
import refinedstorage.tile.config.RedstoneMode;
import refinedstorage.tile.externalstorage.ExternalStorage;
import java.util.*;
@@ -488,6 +489,10 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR
for (IStorage storage : this.storage.getStorages()) {
remainder = storage.insertItem(remainder, size, simulate);
if (storage instanceof ExternalStorage && !simulate) {
((ExternalStorage) storage).setHash();
}
if (remainder == null) {
break;
} else {
@@ -524,6 +529,10 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR
for (IStorage storage : this.storage.getStorages()) {
ItemStack took = storage.extractItem(stack, requested - received, flags);
if (storage instanceof ExternalStorage) {
((ExternalStorage) storage).setHash();
}
if (took != null) {
if (newStack == null) {
newStack = took;

View File

@@ -17,6 +17,8 @@ public class DeepStorageUnitStorage extends ExternalStorage {
public DeepStorageUnitStorage(TileExternalStorage externalStorage, IDeepStorageUnit unit) {
this.externalStorage = externalStorage;
this.unit = unit;
setHash();
}
@Override
@@ -24,6 +26,11 @@ public class DeepStorageUnitStorage extends ExternalStorage {
return unit.getMaxStoredCount();
}
@Override
public int getHash() {
return RefinedStorageUtils.getItemStackHashCode(unit.getStoredItemType());
}
@Override
public List<ItemStack> getItems() {
if (unit.getStoredItemType() != null && unit.getStoredItemType().stackSize > 0) {

View File

@@ -17,6 +17,8 @@ public class DrawerStorage extends ExternalStorage {
public DrawerStorage(TileExternalStorage externalStorage, IDrawer drawer) {
this.externalStorage = externalStorage;
this.drawer = drawer;
setHash();
}
@Override
@@ -24,6 +26,11 @@ public class DrawerStorage extends ExternalStorage {
return drawer.getMaxCapacity();
}
@Override
public int getHash() {
return RefinedStorageUtils.getItemStackHashCode(drawer.getStoredItemPrototype(), drawer.getStoredItemCount());
}
@Override
public List<ItemStack> getItems() {
if (!drawer.isEmpty() && drawer.getStoredItemCount() > 0) {

View File

@@ -3,5 +3,23 @@ package refinedstorage.tile.externalstorage;
import refinedstorage.api.storage.IStorage;
public abstract class ExternalStorage implements IStorage {
private int hash = -1;
public abstract int getCapacity();
public void setHash() {
this.hash = getHash();
}
public abstract int getHash();
public boolean isDirty() {
if (hash != -1 && hash != getHash()) {
hash = getHash();
return true;
}
return false;
}
}

View File

@@ -16,6 +16,8 @@ public class ItemHandlerStorage extends ExternalStorage {
public ItemHandlerStorage(TileExternalStorage externalStorage, IItemHandler handler) {
this.externalStorage = externalStorage;
this.handler = handler;
setHash();
}
@Override
@@ -23,6 +25,19 @@ public class ItemHandlerStorage extends ExternalStorage {
return handler.getSlots() * 64;
}
@Override
public int getHash() {
int code = 0;
for (int i = 0; i < handler.getSlots(); ++i) {
if (handler.getStackInSlot(i) != null && handler.getStackInSlot(i).getItem() != null) {
code += RefinedStorageUtils.getItemStackHashCode(handler.getStackInSlot(i));
}
}
return code;
}
@Override
public List<ItemStack> getItems() {
List<ItemStack> items = new ArrayList<ItemStack>();

View File

@@ -42,7 +42,6 @@ public class TileExternalStorage extends TileNode implements IStorageProvider, I
private List<ExternalStorage> storages = new ArrayList<ExternalStorage>();
private int lastDrawerCount;
private boolean updatedOnce;
@Override
public int getEnergyUsage() {
@@ -58,21 +57,23 @@ public class TileExternalStorage extends TileNode implements IStorageProvider, I
super.onConnectionChange(network, state);
network.getStorage().rebuild();
updatedOnce = false;
}
@Override
public void update() {
if (isConnected()) {
if (!updatedOnce) {
updateStorage();
if (!worldObj.isRemote && network != null) {
for (ExternalStorage storage : storages) {
if (storage.isDirty()) {
updateStorage(network);
updatedOnce = true;
} else if (getFacingTile() instanceof IDrawerGroup && lastDrawerCount != ((IDrawerGroup) getFacingTile()).getDrawerCount()) {
break;
}
}
if (getFacingTile() instanceof IDrawerGroup && lastDrawerCount != ((IDrawerGroup) getFacingTile()).getDrawerCount()) {
lastDrawerCount = ((IDrawerGroup) getFacingTile()).getDrawerCount();
updateStorage();
updateStorage(network);
}
}
@@ -173,8 +174,9 @@ public class TileExternalStorage extends TileNode implements IStorageProvider, I
markDirty();
}
// Called when the neighbor block changes or when a drawer is added or removed to a drawer group
public void updateStorage() {
public void updateStorage(INetworkMaster network) {
System.out.println("Updating storages");
storages.clear();
TileEntity facing = getFacingTile();