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

@@ -3,8 +3,9 @@
### 0.8.6 ### 0.8.6
**Bugfixes** **Bugfixes**
- Fixed External Storage disconnecting on world reload - Fixed External Storage disconnecting on world reload
- Fixed External Storage not updating correctly
- Fixed wireless signal starting from Controller instead of per Wireless Transmitter individually - Fixed wireless signal starting from Controller instead of per Wireless Transmitter individually
- Huge performance improvements to large networks - Huge performance improvements to large storage networks
**Features** **Features**
- Re-added Controllers exploding when two of them are connected to the same network - Re-added Controllers exploding when two of them are connected to the same network

View File

@@ -327,4 +327,16 @@ public final class RefinedStorageUtils {
public static boolean hasPattern(INetworkMaster network, ItemStack stack) { public static boolean hasPattern(INetworkMaster network, ItemStack stack) {
return RefinedStorageUtils.getPattern(network, stack) != null; 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) { public void neighborChanged(IBlockState state, World world, BlockPos pos, Block block) {
super.neighborChanged(state, world, pos, block); super.neighborChanged(state, world, pos, block);
if (!world.isRemote && ((TileExternalStorage) world.getTileEntity(pos)).isConnected()) { if (!world.isRemote) {
((TileExternalStorage) world.getTileEntity(pos)).updateStorage(); 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.TileCrafter;
import refinedstorage.tile.config.IRedstoneModeConfig; import refinedstorage.tile.config.IRedstoneModeConfig;
import refinedstorage.tile.config.RedstoneMode; import refinedstorage.tile.config.RedstoneMode;
import refinedstorage.tile.externalstorage.ExternalStorage;
import java.util.*; import java.util.*;
@@ -488,6 +489,10 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR
for (IStorage storage : this.storage.getStorages()) { for (IStorage storage : this.storage.getStorages()) {
remainder = storage.insertItem(remainder, size, simulate); remainder = storage.insertItem(remainder, size, simulate);
if (storage instanceof ExternalStorage && !simulate) {
((ExternalStorage) storage).setHash();
}
if (remainder == null) { if (remainder == null) {
break; break;
} else { } else {
@@ -524,6 +529,10 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR
for (IStorage storage : this.storage.getStorages()) { for (IStorage storage : this.storage.getStorages()) {
ItemStack took = storage.extractItem(stack, requested - received, flags); ItemStack took = storage.extractItem(stack, requested - received, flags);
if (storage instanceof ExternalStorage) {
((ExternalStorage) storage).setHash();
}
if (took != null) { if (took != null) {
if (newStack == null) { if (newStack == null) {
newStack = took; newStack = took;

View File

@@ -17,6 +17,8 @@ public class DeepStorageUnitStorage extends ExternalStorage {
public DeepStorageUnitStorage(TileExternalStorage externalStorage, IDeepStorageUnit unit) { public DeepStorageUnitStorage(TileExternalStorage externalStorage, IDeepStorageUnit unit) {
this.externalStorage = externalStorage; this.externalStorage = externalStorage;
this.unit = unit; this.unit = unit;
setHash();
} }
@Override @Override
@@ -24,6 +26,11 @@ public class DeepStorageUnitStorage extends ExternalStorage {
return unit.getMaxStoredCount(); return unit.getMaxStoredCount();
} }
@Override
public int getHash() {
return RefinedStorageUtils.getItemStackHashCode(unit.getStoredItemType());
}
@Override @Override
public List<ItemStack> getItems() { public List<ItemStack> getItems() {
if (unit.getStoredItemType() != null && unit.getStoredItemType().stackSize > 0) { 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) { public DrawerStorage(TileExternalStorage externalStorage, IDrawer drawer) {
this.externalStorage = externalStorage; this.externalStorage = externalStorage;
this.drawer = drawer; this.drawer = drawer;
setHash();
} }
@Override @Override
@@ -24,6 +26,11 @@ public class DrawerStorage extends ExternalStorage {
return drawer.getMaxCapacity(); return drawer.getMaxCapacity();
} }
@Override
public int getHash() {
return RefinedStorageUtils.getItemStackHashCode(drawer.getStoredItemPrototype(), drawer.getStoredItemCount());
}
@Override @Override
public List<ItemStack> getItems() { public List<ItemStack> getItems() {
if (!drawer.isEmpty() && drawer.getStoredItemCount() > 0) { if (!drawer.isEmpty() && drawer.getStoredItemCount() > 0) {

View File

@@ -3,5 +3,23 @@ package refinedstorage.tile.externalstorage;
import refinedstorage.api.storage.IStorage; import refinedstorage.api.storage.IStorage;
public abstract class ExternalStorage implements IStorage { public abstract class ExternalStorage implements IStorage {
private int hash = -1;
public abstract int getCapacity(); 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) { public ItemHandlerStorage(TileExternalStorage externalStorage, IItemHandler handler) {
this.externalStorage = externalStorage; this.externalStorage = externalStorage;
this.handler = handler; this.handler = handler;
setHash();
} }
@Override @Override
@@ -23,6 +25,19 @@ public class ItemHandlerStorage extends ExternalStorage {
return handler.getSlots() * 64; 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 @Override
public List<ItemStack> getItems() { public List<ItemStack> getItems() {
List<ItemStack> items = new ArrayList<ItemStack>(); 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 List<ExternalStorage> storages = new ArrayList<ExternalStorage>();
private int lastDrawerCount; private int lastDrawerCount;
private boolean updatedOnce;
@Override @Override
public int getEnergyUsage() { public int getEnergyUsage() {
@@ -58,21 +57,23 @@ public class TileExternalStorage extends TileNode implements IStorageProvider, I
super.onConnectionChange(network, state); super.onConnectionChange(network, state);
network.getStorage().rebuild(); network.getStorage().rebuild();
updatedOnce = false;
} }
@Override @Override
public void update() { public void update() {
if (isConnected()) { if (!worldObj.isRemote && network != null) {
if (!updatedOnce) { for (ExternalStorage storage : storages) {
updateStorage(); if (storage.isDirty()) {
updateStorage(network);
updatedOnce = true; break;
} else if (getFacingTile() instanceof IDrawerGroup && lastDrawerCount != ((IDrawerGroup) getFacingTile()).getDrawerCount()) { }
}
if (getFacingTile() instanceof IDrawerGroup && lastDrawerCount != ((IDrawerGroup) getFacingTile()).getDrawerCount()) {
lastDrawerCount = ((IDrawerGroup) getFacingTile()).getDrawerCount(); lastDrawerCount = ((IDrawerGroup) getFacingTile()).getDrawerCount();
updateStorage(); updateStorage(network);
} }
} }
@@ -173,8 +174,9 @@ public class TileExternalStorage extends TileNode implements IStorageProvider, I
markDirty(); markDirty();
} }
// Called when the neighbor block changes or when a drawer is added or removed to a drawer group public void updateStorage(INetworkMaster network) {
public void updateStorage() { System.out.println("Updating storages");
storages.clear(); storages.clear();
TileEntity facing = getFacingTile(); TileEntity facing = getFacingTile();