Fixes to world saved data impl, mark *actually* drives dirty

This commit is contained in:
raoulvdberge
2017-02-11 00:23:45 +01:00
parent 4e23823f35
commit 76b27b520d
12 changed files with 90 additions and 95 deletions

View File

@@ -28,9 +28,11 @@ public interface IStorageDisk<T> extends IStorage<T> {
boolean isValid(ItemStack stack); boolean isValid(ItemStack stack);
/** /**
* Called when the storage changes. * Sets a listener that is called when the storage changes.
*
* @param listener the listener
*/ */
void onChanged(); void setListener(Runnable listener);
/** /**
* Reads the storage from NBT. * Reads the storage from NBT.

View File

@@ -54,18 +54,11 @@ public class NetworkNodeListener {
} }
} }
@SubscribeEvent
public void onWorldSave(WorldEvent.Save e) {
WorldSavedDataNetworkNode.get(e.getWorld());
}
@SubscribeEvent @SubscribeEvent
public void onWorldLoad(WorldEvent.Load e) { public void onWorldLoad(WorldEvent.Load e) {
WorldSavedDataNetworkNode.get(e.getWorld()); // Clear all data from a possible previous load, so we start with a clean slate
}
@SubscribeEvent
public void onWorldUnload(WorldEvent.Unload e) {
API.instance().getNetworkNodeManager(e.getWorld().provider.getDimension()).clear(); API.instance().getNetworkNodeManager(e.getWorld().provider.getDimension()).clear();
WorldSavedDataNetworkNode.getOrLoadData(e.getWorld());
} }
} }

View File

@@ -42,6 +42,6 @@ public class NetworkNodeManager implements INetworkNodeManager {
@Override @Override
public void markDirty(World world) { public void markDirty(World world) {
WorldSavedDataNetworkNode.get(world).markDirty(); WorldSavedDataNetworkNode.getOrLoadData(world).markDirty();
} }
} }

View File

@@ -30,6 +30,8 @@ public class NetworkNodeFluidStorage extends NetworkNode implements IGuiStorage,
class StorageFluid extends StorageDiskFluid { class StorageFluid extends StorageDiskFluid {
public StorageFluid(NBTTagCompound tag) { public StorageFluid(NBTTagCompound tag) {
super(tag, NetworkNodeFluidStorage.this.getCapacity()); super(tag, NetworkNodeFluidStorage.this.getCapacity());
this.setListener(NetworkNodeFluidStorage.this::markDirty);
} }
@Override @Override
@@ -56,13 +58,6 @@ public class NetworkNodeFluidStorage extends NetworkNode implements IGuiStorage,
public boolean isVoiding() { public boolean isVoiding() {
return voidExcess; return voidExcess;
} }
@Override
public void onChanged() {
super.onChanged();
markDirty();
}
} }
public static final String NBT_STORAGE = "Storage"; public static final String NBT_STORAGE = "Storage";

View File

@@ -30,6 +30,8 @@ public class NetworkNodeStorage extends NetworkNode implements IGuiStorage, ISto
class StorageItem extends StorageDiskItem { class StorageItem extends StorageDiskItem {
public StorageItem(NBTTagCompound tag) { public StorageItem(NBTTagCompound tag) {
super(tag, NetworkNodeStorage.this.getCapacity()); super(tag, NetworkNodeStorage.this.getCapacity());
this.setListener(NetworkNodeStorage.this::markDirty);
} }
@Override @Override
@@ -55,13 +57,6 @@ public class NetworkNodeStorage extends NetworkNode implements IGuiStorage, ISto
public boolean isVoiding() { public boolean isVoiding() {
return voidExcess; return voidExcess;
} }
@Override
public void onChanged() {
super.onChanged();
markDirty();
}
} }
public static final String NBT_STORAGE = "Storage"; public static final String NBT_STORAGE = "Storage";

View File

@@ -77,7 +77,7 @@ public class WorldSavedDataNetworkNode extends WorldSavedData {
return tag; return tag;
} }
public static WorldSavedDataNetworkNode get(World world) { public static WorldSavedDataNetworkNode getOrLoadData(World world) {
MapStorage storage = world.getPerWorldStorage(); MapStorage storage = world.getPerWorldStorage();
WorldSavedDataNetworkNode instance = (WorldSavedDataNetworkNode) storage.getOrLoadData(WorldSavedDataNetworkNode.class, NAME); WorldSavedDataNetworkNode instance = (WorldSavedDataNetworkNode) storage.getOrLoadData(WorldSavedDataNetworkNode.class, NAME);

View File

@@ -21,6 +21,17 @@ public class StorageFluidDiskDrive implements IStorageDisk<FluidStack> {
public StorageFluidDiskDrive(NetworkNodeDiskDrive diskDrive, IStorageDisk<FluidStack> parent) { public StorageFluidDiskDrive(NetworkNodeDiskDrive diskDrive, IStorageDisk<FluidStack> parent) {
this.diskDrive = diskDrive; this.diskDrive = diskDrive;
this.parent = parent; this.parent = parent;
this.parent.setListener(() -> {
diskDrive.markDirty();
int currentState = TileDiskDrive.getDiskState(getStored(), getCapacity());
if (lastState != currentState) {
lastState = currentState;
RSUtils.updateBlock(diskDrive.getHolder().world(), diskDrive.getHolder().pos());
}
});
this.lastState = TileDiskDrive.getDiskState(getStored(), getCapacity()); this.lastState = TileDiskDrive.getDiskState(getStored(), getCapacity());
} }
@@ -81,18 +92,8 @@ public class StorageFluidDiskDrive implements IStorageDisk<FluidStack> {
} }
@Override @Override
public void onChanged() { public void setListener(Runnable listener) {
parent.onChanged(); // NO OP
diskDrive.markDirty();
int currentState = TileDiskDrive.getDiskState(getStored(), getCapacity());
if (lastState != currentState) {
lastState = currentState;
RSUtils.updateBlock(diskDrive.getHolder().world(), diskDrive.getHolder().pos());
}
} }
@Override @Override

View File

@@ -21,6 +21,17 @@ public class StorageItemDiskDrive implements IStorageDisk<ItemStack> {
public StorageItemDiskDrive(NetworkNodeDiskDrive diskDrive, IStorageDisk<ItemStack> parent) { public StorageItemDiskDrive(NetworkNodeDiskDrive diskDrive, IStorageDisk<ItemStack> parent) {
this.diskDrive = diskDrive; this.diskDrive = diskDrive;
this.parent = parent; this.parent = parent;
this.parent.setListener(() -> {
diskDrive.markDirty();
int currentState = TileDiskDrive.getDiskState(getStored(), getCapacity());
if (lastState != currentState) {
lastState = currentState;
RSUtils.updateBlock(diskDrive.getHolder().world(), diskDrive.getHolder().pos());
}
});
this.lastState = TileDiskDrive.getDiskState(getStored(), getCapacity()); this.lastState = TileDiskDrive.getDiskState(getStored(), getCapacity());
} }
@@ -81,18 +92,8 @@ public class StorageItemDiskDrive implements IStorageDisk<ItemStack> {
} }
@Override @Override
public void onChanged() { public void setListener(Runnable listener) {
parent.onChanged(); // NO OP
diskDrive.markDirty();
int currentState = TileDiskDrive.getDiskState(getStored(), getCapacity());
if (lastState != currentState) {
lastState = currentState;
RSUtils.updateBlock(diskDrive.getHolder().world(), diskDrive.getHolder().pos());
}
} }
@Override @Override

View File

@@ -20,6 +20,17 @@ public class StorageFluidDiskManipulator implements IStorageDisk<FluidStack> {
public StorageFluidDiskManipulator(NetworkNodeDiskManipulator diskManipulator, IStorageDisk<FluidStack> parent) { public StorageFluidDiskManipulator(NetworkNodeDiskManipulator diskManipulator, IStorageDisk<FluidStack> parent) {
this.diskManipulator = diskManipulator; this.diskManipulator = diskManipulator;
this.parent = parent; this.parent = parent;
this.parent.setListener(() -> {
diskManipulator.markDirty();
int currentState = TileDiskDrive.getDiskState(getStored(), getCapacity());
if (lastState != currentState) {
lastState = currentState;
RSUtils.updateBlock(diskManipulator.getHolder().world(), diskManipulator.getHolder().pos());
}
});
this.lastState = TileDiskDrive.getDiskState(getStored(), getCapacity()); this.lastState = TileDiskDrive.getDiskState(getStored(), getCapacity());
} }
@@ -39,18 +50,8 @@ public class StorageFluidDiskManipulator implements IStorageDisk<FluidStack> {
} }
@Override @Override
public void onChanged() { public void setListener(Runnable listener) {
parent.onChanged(); // NO OP
diskManipulator.markDirty();
int currentState = TileDiskDrive.getDiskState(getStored(), getCapacity());
if (lastState != currentState) {
lastState = currentState;
RSUtils.updateBlock(diskManipulator.getHolder().world(), diskManipulator.getHolder().pos());
}
} }
@Override @Override

View File

@@ -20,6 +20,17 @@ public class StorageItemDiskManipulator implements IStorageDisk<ItemStack> {
public StorageItemDiskManipulator(NetworkNodeDiskManipulator diskManipulator, IStorageDisk<ItemStack> parent) { public StorageItemDiskManipulator(NetworkNodeDiskManipulator diskManipulator, IStorageDisk<ItemStack> parent) {
this.diskManipulator = diskManipulator; this.diskManipulator = diskManipulator;
this.parent = parent; this.parent = parent;
this.parent.setListener(() -> {
diskManipulator.markDirty();
int currentState = TileDiskDrive.getDiskState(getStored(), getCapacity());
if (lastState != currentState) {
lastState = currentState;
RSUtils.updateBlock(diskManipulator.getHolder().world(), diskManipulator.getHolder().pos());
}
});
this.lastState = TileDiskDrive.getDiskState(getStored(), getCapacity()); this.lastState = TileDiskDrive.getDiskState(getStored(), getCapacity());
} }
@@ -39,18 +50,8 @@ public class StorageItemDiskManipulator implements IStorageDisk<ItemStack> {
} }
@Override @Override
public void onChanged() { public void setListener(Runnable listener) {
parent.onChanged(); // NO OP
diskManipulator.markDirty();
int currentState = TileDiskDrive.getDiskState(getStored(), getCapacity());
if (lastState != currentState) {
lastState = currentState;
RSUtils.updateBlock(diskManipulator.getHolder().world(), diskManipulator.getHolder().pos());
}
} }
@Override @Override

View File

@@ -27,6 +27,9 @@ public class StorageDiskFluid implements IStorageDisk<FluidStack> {
private NonNullList<FluidStack> stacks = NonNullList.create(); private NonNullList<FluidStack> stacks = NonNullList.create();
private Runnable listener = () -> {
};
public StorageDiskFluid(NBTTagCompound tag, int capacity) { public StorageDiskFluid(NBTTagCompound tag, int capacity) {
this.tag = tag; this.tag = tag;
this.capacity = capacity; this.capacity = capacity;
@@ -92,7 +95,7 @@ public class StorageDiskFluid implements IStorageDisk<FluidStack> {
otherStack.amount += remainingSpace; otherStack.amount += remainingSpace;
onChanged(); listener.run();
} }
return isVoiding() ? null : RSUtils.copyStackWithSize(otherStack, size - remainingSpace); return isVoiding() ? null : RSUtils.copyStackWithSize(otherStack, size - remainingSpace);
@@ -102,7 +105,7 @@ public class StorageDiskFluid implements IStorageDisk<FluidStack> {
otherStack.amount += size; otherStack.amount += size;
onChanged(); listener.run();
} }
return null; return null;
@@ -126,7 +129,7 @@ public class StorageDiskFluid implements IStorageDisk<FluidStack> {
stacks.add(RSUtils.copyStackWithSize(stack, remainingSpace)); stacks.add(RSUtils.copyStackWithSize(stack, remainingSpace));
onChanged(); listener.run();
} }
return isVoiding() ? null : RSUtils.copyStackWithSize(stack, size - remainingSpace); return isVoiding() ? null : RSUtils.copyStackWithSize(stack, size - remainingSpace);
@@ -136,7 +139,7 @@ public class StorageDiskFluid implements IStorageDisk<FluidStack> {
stacks.add(RSUtils.copyStackWithSize(stack, size)); stacks.add(RSUtils.copyStackWithSize(stack, size));
onChanged(); listener.run();
} }
return null; return null;
@@ -161,7 +164,7 @@ public class StorageDiskFluid implements IStorageDisk<FluidStack> {
tag.setInteger(NBT_STORED, getStored() - size); tag.setInteger(NBT_STORED, getStored() - size);
onChanged(); listener.run();
} }
return RSUtils.copyStackWithSize(otherStack, size); return RSUtils.copyStackWithSize(otherStack, size);
@@ -171,11 +174,6 @@ public class StorageDiskFluid implements IStorageDisk<FluidStack> {
return null; return null;
} }
@Override
public void onChanged() {
// NO OP
}
@Override @Override
public int getStored() { public int getStored() {
return getStored(tag); return getStored(tag);
@@ -216,6 +214,11 @@ public class StorageDiskFluid implements IStorageDisk<FluidStack> {
return stack.hasTagCompound() && stack.getTagCompound().hasKey(NBT_FLUIDS) && stack.getTagCompound().hasKey(NBT_STORED); return stack.hasTagCompound() && stack.getTagCompound().hasKey(NBT_FLUIDS) && stack.getTagCompound().hasKey(NBT_STORED);
} }
@Override
public void setListener(Runnable listener) {
this.listener = listener;
}
public static NBTTagCompound getShareTag(NBTTagCompound tag) { public static NBTTagCompound getShareTag(NBTTagCompound tag) {
NBTTagCompound otherTag = new NBTTagCompound(); NBTTagCompound otherTag = new NBTTagCompound();

View File

@@ -35,6 +35,9 @@ public class StorageDiskItem implements IStorageDisk<ItemStack> {
private NBTTagCompound tag; private NBTTagCompound tag;
private int capacity; private int capacity;
private Runnable listener = () -> {
};
private NonNullList<ItemStack> stacks = NonNullList.create(); private NonNullList<ItemStack> stacks = NonNullList.create();
/** /**
@@ -136,7 +139,7 @@ public class StorageDiskItem implements IStorageDisk<ItemStack> {
otherStack.grow(remainingSpace); otherStack.grow(remainingSpace);
onChanged(); listener.run();
} }
return isVoiding() ? null : ItemHandlerHelper.copyStackWithSize(otherStack, size - remainingSpace); return isVoiding() ? null : ItemHandlerHelper.copyStackWithSize(otherStack, size - remainingSpace);
@@ -146,7 +149,7 @@ public class StorageDiskItem implements IStorageDisk<ItemStack> {
otherStack.grow(size); otherStack.grow(size);
onChanged(); listener.run();
} }
return null; return null;
@@ -170,7 +173,7 @@ public class StorageDiskItem implements IStorageDisk<ItemStack> {
stacks.add(ItemHandlerHelper.copyStackWithSize(stack, remainingSpace)); stacks.add(ItemHandlerHelper.copyStackWithSize(stack, remainingSpace));
onChanged(); listener.run();
} }
return isVoiding() ? null : ItemHandlerHelper.copyStackWithSize(stack, size - remainingSpace); return isVoiding() ? null : ItemHandlerHelper.copyStackWithSize(stack, size - remainingSpace);
@@ -180,7 +183,7 @@ public class StorageDiskItem implements IStorageDisk<ItemStack> {
stacks.add(ItemHandlerHelper.copyStackWithSize(stack, size)); stacks.add(ItemHandlerHelper.copyStackWithSize(stack, size));
onChanged(); listener.run();
} }
return null; return null;
@@ -205,7 +208,7 @@ public class StorageDiskItem implements IStorageDisk<ItemStack> {
tag.setInteger(NBT_STORED, getStored() - size); tag.setInteger(NBT_STORED, getStored() - size);
onChanged(); listener.run();
} }
return ItemHandlerHelper.copyStackWithSize(otherStack, size); return ItemHandlerHelper.copyStackWithSize(otherStack, size);
@@ -236,13 +239,13 @@ public class StorageDiskItem implements IStorageDisk<ItemStack> {
} }
@Override @Override
public void onChanged() { public boolean isValid(ItemStack stack) {
// NO OP return stack.hasTagCompound() && stack.getTagCompound().hasKey(NBT_ITEMS) && stack.getTagCompound().hasKey(NBT_STORED);
} }
@Override @Override
public boolean isValid(ItemStack stack) { public void setListener(Runnable listener) {
return stack.hasTagCompound() && stack.getTagCompound().hasKey(NBT_ITEMS) && stack.getTagCompound().hasKey(NBT_STORED); this.listener = listener;
} }
@Override @Override