Fixes storages not saving properly?

This commit is contained in:
Raoul Van den Berge
2016-06-11 22:21:28 +02:00
parent 3b695a6ec6
commit 680395dcf2
4 changed files with 44 additions and 76 deletions

View File

@@ -4,9 +4,11 @@ import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList; import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.items.ItemHandlerHelper; import net.minecraftforge.items.ItemHandlerHelper;
import refinedstorage.RefinedStorageUtils; import refinedstorage.RefinedStorageUtils;
import javax.annotation.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -33,24 +35,24 @@ public abstract class NBTStorage implements IStorage {
private NBTTagCompound tag; private NBTTagCompound tag;
private int capacity; private int capacity;
private TileEntity tile;
private boolean dirty;
private List<ItemStack> stacks = new ArrayList<ItemStack>(); private List<ItemStack> stacks = new ArrayList<ItemStack>();
/** /**
* @param tag The NBT tag we are reading from and writing the amount stored to, has to be initialized with {@link NBTStorage#createNBT()} * @param tag The NBT tag we are reading from and writing the amount stored to, has to be initialized with {@link NBTStorage#createNBT()}
* @param capacity The capacity of this storage * @param capacity The capacity of this storage
* @param tile A tile that the NBT storage is in, will be marked dirty when storage changes
*/ */
public NBTStorage(NBTTagCompound tag, int capacity) { public NBTStorage(NBTTagCompound tag, int capacity, @Nullable TileEntity tile) {
this.tag = tag; this.tag = tag;
this.capacity = capacity; this.capacity = capacity;
this.tile = tile;
readFromNBT(); readFromNBT();
} }
public void readFromNBT() { public void readFromNBT() {
System.out.println("[REFINED STORAGE DEBUG] Reading from storage, protocol " + tag.getInteger(NBT_PROTOCOL) + ".");
NBTTagList list = (NBTTagList) tag.getTag(NBT_ITEMS); NBTTagList list = (NBTTagList) tag.getTag(NBT_ITEMS);
for (int i = 0; i < list.tagCount(); ++i) { for (int i = 0; i < list.tagCount(); ++i) {
@@ -66,18 +68,15 @@ public abstract class NBTStorage implements IStorage {
stack.setTagCompound(tag.hasKey(NBT_ITEM_NBT) ? tag.getCompoundTag(NBT_ITEM_NBT) : null); stack.setTagCompound(tag.hasKey(NBT_ITEM_NBT) ? tag.getCompoundTag(NBT_ITEM_NBT) : null);
if (stack.getItem() != null) { if (stack.getItem() != null) {
System.out.println("[REFINED STORAGE DEBUG] Read " + stack);
stacks.add(stack); stacks.add(stack);
} }
} }
} }
/** /**
* Writes the items to the NBT tag, check for {@link NBTStorage#isDirty()} before doing this to be efficient. * Writes the items to the NBT tag.
*
* @param tag The tag to write to
*/ */
public void writeToNBT(NBTTagCompound tag) { public void writeToNBT() {
NBTTagList list = new NBTTagList(); NBTTagList list = new NBTTagList();
// Dummy value for extracting ForgeCaps // Dummy value for extracting ForgeCaps
@@ -118,10 +117,6 @@ public abstract class NBTStorage implements IStorage {
public ItemStack push(ItemStack stack, int size, boolean simulate) { public ItemStack push(ItemStack stack, int size, boolean simulate) {
for (ItemStack otherStack : stacks) { for (ItemStack otherStack : stacks) {
if (RefinedStorageUtils.compareStackNoQuantity(otherStack, stack)) { if (RefinedStorageUtils.compareStackNoQuantity(otherStack, stack)) {
if (!simulate) {
markDirty();
}
if (getStored() + size > getCapacity()) { if (getStored() + size > getCapacity()) {
int remainingSpace = getCapacity() - getStored(); int remainingSpace = getCapacity() - getStored();
@@ -133,6 +128,8 @@ public abstract class NBTStorage implements IStorage {
tag.setInteger(NBT_STORED, getStored() + remainingSpace); tag.setInteger(NBT_STORED, getStored() + remainingSpace);
otherStack.stackSize += remainingSpace; otherStack.stackSize += remainingSpace;
onStorageChanged();
} }
return ItemHandlerHelper.copyStackWithSize(otherStack, size - remainingSpace); return ItemHandlerHelper.copyStackWithSize(otherStack, size - remainingSpace);
@@ -141,6 +138,8 @@ public abstract class NBTStorage implements IStorage {
tag.setInteger(NBT_STORED, getStored() + size); tag.setInteger(NBT_STORED, getStored() + size);
otherStack.stackSize += size; otherStack.stackSize += size;
onStorageChanged();
} }
return null; return null;
@@ -148,10 +147,6 @@ public abstract class NBTStorage implements IStorage {
} }
} }
if (!simulate) {
markDirty();
}
if (getStored() + size > getCapacity()) { if (getStored() + size > getCapacity()) {
int remainingSpace = getCapacity() - getStored(); int remainingSpace = getCapacity() - getStored();
@@ -163,6 +158,8 @@ public abstract class NBTStorage implements IStorage {
tag.setInteger(NBT_STORED, getStored() + remainingSpace); tag.setInteger(NBT_STORED, getStored() + remainingSpace);
stacks.add(ItemHandlerHelper.copyStackWithSize(stack, remainingSpace)); stacks.add(ItemHandlerHelper.copyStackWithSize(stack, remainingSpace));
onStorageChanged();
} }
return ItemHandlerHelper.copyStackWithSize(stack, size - remainingSpace); return ItemHandlerHelper.copyStackWithSize(stack, size - remainingSpace);
@@ -171,6 +168,8 @@ public abstract class NBTStorage implements IStorage {
tag.setInteger(NBT_STORED, getStored() + size); tag.setInteger(NBT_STORED, getStored() + size);
stacks.add(ItemHandlerHelper.copyStackWithSize(stack, size)); stacks.add(ItemHandlerHelper.copyStackWithSize(stack, size));
onStorageChanged();
} }
return null; return null;
@@ -193,7 +192,7 @@ public abstract class NBTStorage implements IStorage {
tag.setInteger(NBT_STORED, getStored() - size); tag.setInteger(NBT_STORED, getStored() - size);
markDirty(); onStorageChanged();
return ItemHandlerHelper.copyStackWithSize(otherStack, size); return ItemHandlerHelper.copyStackWithSize(otherStack, size);
} }
@@ -202,6 +201,14 @@ public abstract class NBTStorage implements IStorage {
return null; return null;
} }
public void onStorageChanged() {
writeToNBT();
if (tile != null) {
tile.markDirty();
}
}
@Override @Override
public int getStored() { public int getStored() {
return getStoredFromNBT(tag); return getStoredFromNBT(tag);
@@ -215,18 +222,6 @@ public abstract class NBTStorage implements IStorage {
return tag; return tag;
} }
public void markDirty() {
this.dirty = true;
}
public boolean isDirty() {
return dirty;
}
public void markClean() {
this.dirty = false;
}
public static int getStoredFromNBT(NBTTagCompound tag) { public static int getStoredFromNBT(NBTTagCompound tag) {
return tag.getInteger(NBT_STORED); return tag.getInteger(NBT_STORED);
} }

View File

@@ -6,7 +6,6 @@ import refinedstorage.container.slot.SlotSpecimen;
import refinedstorage.tile.TileConstructor; import refinedstorage.tile.TileConstructor;
public class ContainerConstructor extends ContainerBase { public class ContainerConstructor extends ContainerBase {
public ContainerConstructor(EntityPlayer player, TileConstructor constructor) { public ContainerConstructor(EntityPlayer player, TileConstructor constructor) {
super(player); super(player);

View File

@@ -27,7 +27,7 @@ import java.util.List;
public class TileDiskDrive extends TileMachine implements IStorageProvider, IStorageGui, ICompareConfig, IModeConfig { public class TileDiskDrive extends TileMachine implements IStorageProvider, IStorageGui, ICompareConfig, IModeConfig {
public class Storage extends NBTStorage { public class Storage extends NBTStorage {
public Storage(ItemStack disk) { public Storage(ItemStack disk) {
super(disk.getTagCompound(), EnumStorageType.getById(disk.getItemDamage()).getCapacity()); super(disk.getTagCompound(), EnumStorageType.getById(disk.getItemDamage()).getCapacity(), TileDiskDrive.this);
} }
@Override @Override
@@ -49,7 +49,22 @@ public class TileDiskDrive extends TileMachine implements IStorageProvider, ISto
public static final String NBT_COMPARE = "Compare"; public static final String NBT_COMPARE = "Compare";
public static final String NBT_MODE = "Mode"; public static final String NBT_MODE = "Mode";
private BasicItemHandler disks = new BasicItemHandler(8, this, new BasicItemValidator(RefinedStorageItems.STORAGE_DISK)); private BasicItemHandler disks = new BasicItemHandler(8, this, new BasicItemValidator(RefinedStorageItems.STORAGE_DISK)) {
@Override
protected void onContentsChanged(int slot) {
super.onContentsChanged(slot);
ItemStack disk = getStackInSlot(slot);
System.out.println("#" + slot + ": " + disk);
if (disk == null) {
storages[slot] = null;
} else {
storages[slot] = new Storage(disk);
}
}
};
private BasicItemHandler filters = new BasicItemHandler(9, this); private BasicItemHandler filters = new BasicItemHandler(9, this);
private Storage storages[] = new Storage[8]; private Storage storages[] = new Storage[8];
@@ -58,17 +73,6 @@ public class TileDiskDrive extends TileMachine implements IStorageProvider, ISto
private int compare = 0; private int compare = 0;
private int mode = ModeConstants.WHITELIST; private int mode = ModeConstants.WHITELIST;
public Storage getStorage(int slot) {
if (disks.getStackInSlot(slot) == null) {
storages[slot] = null;
} else if (storages[slot] == null) {
System.out.println("[REFINED STORAGE DEBUG] Initialized storage " + slot + " in disk drive, this should only happen ONCE when loading world/ entering a unloaded chunk. If it happens while you're in the same chunk, something is wrong !!!");
storages[slot] = new Storage(disks.getStackInSlot(slot));
}
return storages[slot];
}
@Override @Override
public int getEnergyUsage() { public int getEnergyUsage() {
int base = 5; int base = 5;
@@ -86,31 +90,9 @@ public class TileDiskDrive extends TileMachine implements IStorageProvider, ISto
public void updateMachine() { public void updateMachine() {
} }
@Override
public void update() {
super.update();
if (!worldObj.isRemote) {
for (int i = 0; i < disks.getSlots(); ++i) {
Storage storage = getStorage(i);
if (storage != null && storage.isDirty()) {
storage.writeToNBT(disks.getStackInSlot(i).getTagCompound());
storage.markClean();
System.out.println("[REFINED STORAGE DEBUG] Disk Drive slot " + i + " is MARKED DIRTY, thus it should save when you leave the area.");
markDirty();
}
}
}
}
@Override @Override
public void provide(List<IStorage> storages) { public void provide(List<IStorage> storages) {
for (int i = 0; i < disks.getSlots(); ++i) { for (IStorage storage : this.storages) {
Storage storage = getStorage(i);
if (storage != null) { if (storage != null) {
storages.add(storage); storages.add(storage);
} }
@@ -121,7 +103,6 @@ public class TileDiskDrive extends TileMachine implements IStorageProvider, ISto
public void read(NBTTagCompound nbt) { public void read(NBTTagCompound nbt) {
super.read(nbt); super.read(nbt);
System.out.println("[REFINED STORAGE DEBUG] Reading from storage now.");
RefinedStorageUtils.readItems(disks, 0, nbt); RefinedStorageUtils.readItems(disks, 0, nbt);
RefinedStorageUtils.readItems(filters, 1, nbt); RefinedStorageUtils.readItems(filters, 1, nbt);

View File

@@ -26,7 +26,7 @@ import java.util.List;
public class TileStorage extends TileMachine implements IStorageProvider, IStorageGui, ICompareConfig, IModeConfig { public class TileStorage extends TileMachine implements IStorageProvider, IStorageGui, ICompareConfig, IModeConfig {
class Storage extends NBTStorage { class Storage extends NBTStorage {
public Storage() { public Storage() {
super(TileStorage.this.getStorageTag(), TileStorage.this.getCapacity()); super(TileStorage.this.getStorageTag(), TileStorage.this.getCapacity(), TileStorage.this);
} }
@Override @Override
@@ -78,13 +78,6 @@ public class TileStorage extends TileMachine implements IStorageProvider, IStora
if (storage == null && storageTag != null) { if (storage == null && storageTag != null) {
storage = new Storage(); storage = new Storage();
} }
if (storage != null && storage.isDirty()) {
storage.writeToNBT(storageTag);
storage.markClean();
markDirty();
}
} }
@Override @Override