This commit is contained in:
raoulvdberge
2017-01-25 02:27:18 +01:00
parent b34d9b91de
commit cf78985bca
2 changed files with 30 additions and 48 deletions

View File

@@ -15,11 +15,17 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
public class StorageDiskFluid implements IStorageDisk<FluidStack> { public class StorageDiskFluid implements IStorageDisk<FluidStack> {
private static final int PROTOCOL = 1; /**
* Protocol history:
* - 1: Initial
* - 2: Removed "stored" tag
*/
private static final int PROTOCOL = 2;
private static final String NBT_PROTOCOL = "Protocol"; private static final String NBT_PROTOCOL = "Protocol";
private static final String NBT_FLUIDS = "Fluids"; private static final String NBT_FLUIDS = "Fluids";
// Used client side
private static final String NBT_STORED = "Stored"; private static final String NBT_STORED = "Stored";
private NBTTagCompound tag; private NBTTagCompound tag;
@@ -84,8 +90,6 @@ public class StorageDiskFluid implements IStorageDisk<FluidStack> {
} }
if (!simulate) { if (!simulate) {
tag.setInteger(NBT_STORED, getStored() + remainingSpace);
otherStack.amount += remainingSpace; otherStack.amount += remainingSpace;
onChanged(); onChanged();
@@ -94,8 +98,6 @@ public class StorageDiskFluid implements IStorageDisk<FluidStack> {
return isVoiding() ? null : RSUtils.copyStackWithSize(otherStack, size - remainingSpace); return isVoiding() ? null : RSUtils.copyStackWithSize(otherStack, size - remainingSpace);
} else { } else {
if (!simulate) { if (!simulate) {
tag.setInteger(NBT_STORED, getStored() + size);
otherStack.amount += size; otherStack.amount += size;
onChanged(); onChanged();
@@ -118,8 +120,6 @@ public class StorageDiskFluid implements IStorageDisk<FluidStack> {
} }
if (!simulate) { if (!simulate) {
tag.setInteger(NBT_STORED, getStored() + remainingSpace);
stacks.add(RSUtils.copyStackWithSize(stack, remainingSpace)); stacks.add(RSUtils.copyStackWithSize(stack, remainingSpace));
onChanged(); onChanged();
@@ -128,8 +128,6 @@ public class StorageDiskFluid implements IStorageDisk<FluidStack> {
return isVoiding() ? null : RSUtils.copyStackWithSize(stack, size - remainingSpace); return isVoiding() ? null : RSUtils.copyStackWithSize(stack, size - remainingSpace);
} else { } else {
if (!simulate) { if (!simulate) {
tag.setInteger(NBT_STORED, getStored() + size);
stacks.add(RSUtils.copyStackWithSize(stack, size)); stacks.add(RSUtils.copyStackWithSize(stack, size));
onChanged(); onChanged();
@@ -155,8 +153,6 @@ public class StorageDiskFluid implements IStorageDisk<FluidStack> {
otherStack.amount -= size; otherStack.amount -= size;
} }
tag.setInteger(NBT_STORED, getStored() - size);
onChanged(); onChanged();
} }
@@ -174,7 +170,7 @@ public class StorageDiskFluid implements IStorageDisk<FluidStack> {
@Override @Override
public int getStored() { public int getStored() {
return getStored(tag); return stacks.stream().mapToInt(s -> s.amount).sum();
} }
@Override @Override
@@ -209,28 +205,27 @@ public class StorageDiskFluid implements IStorageDisk<FluidStack> {
@Override @Override
public boolean isValid(ItemStack stack) { public boolean isValid(ItemStack stack) {
return stack.hasTagCompound() && stack.getTagCompound().hasKey(NBT_FLUIDS) && stack.getTagCompound().hasKey(NBT_STORED); return stack.hasTagCompound() && stack.getTagCompound().hasKey(NBT_FLUIDS);
} }
public static NBTTagCompound getShareTag(NBTTagCompound tag) { public static NBTTagCompound getShareTag(NBTTagCompound tag) {
StorageDiskFluid storage = new StorageDiskFluid(tag, 0);
storage.readFromNBT();
NBTTagCompound otherTag = new NBTTagCompound(); NBTTagCompound otherTag = new NBTTagCompound();
otherTag.setInteger(NBT_STORED, getStored(tag)); otherTag.setInteger(NBT_STORED, storage.getStored());
otherTag.setTag(NBT_FLUIDS, new NBTTagList()); // To circumvent not being able to insert disks in Disk Drives (see FluidStorageNBT#isValid(ItemStack)). otherTag.setTag(NBT_FLUIDS, new NBTTagList());
otherTag.setInteger(NBT_PROTOCOL, PROTOCOL); otherTag.setInteger(NBT_PROTOCOL, PROTOCOL);
return otherTag; return otherTag;
} }
public static int getStored(NBTTagCompound tag) {
return tag.getInteger(NBT_STORED);
}
public static NBTTagCompound getTag() { public static NBTTagCompound getTag() {
NBTTagCompound tag = new NBTTagCompound(); NBTTagCompound tag = new NBTTagCompound();
tag.setTag(NBT_FLUIDS, new NBTTagList()); tag.setTag(NBT_FLUIDS, new NBTTagList());
tag.setInteger(NBT_STORED, 0);
tag.setInteger(NBT_PROTOCOL, PROTOCOL); tag.setInteger(NBT_PROTOCOL, PROTOCOL);
return tag; return tag;

View File

@@ -1,7 +1,6 @@
package com.raoulvdberge.refinedstorage.apiimpl.storage; package com.raoulvdberge.refinedstorage.apiimpl.storage;
import com.raoulvdberge.refinedstorage.api.storage.AccessType; import com.raoulvdberge.refinedstorage.api.storage.AccessType;
import com.raoulvdberge.refinedstorage.api.storage.IStorage;
import com.raoulvdberge.refinedstorage.api.storage.IStorageDisk; import com.raoulvdberge.refinedstorage.api.storage.IStorageDisk;
import com.raoulvdberge.refinedstorage.api.storage.StorageDiskType; import com.raoulvdberge.refinedstorage.api.storage.StorageDiskType;
import com.raoulvdberge.refinedstorage.apiimpl.API; import com.raoulvdberge.refinedstorage.apiimpl.API;
@@ -15,15 +14,18 @@ import net.minecraftforge.items.ItemHandlerHelper;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
/**
* A implementation of {@link IStorage<ItemStack>} that stores storage items in NBT.
*/
public class StorageDiskItem implements IStorageDisk<ItemStack> { public class StorageDiskItem implements IStorageDisk<ItemStack> {
private static final int PROTOCOL = 1; /**
* Protocol history:
* - 1: Initial
* - 2: Removed "stored" tag
*/
private static final int PROTOCOL = 2;
private static final String NBT_PROTOCOL = "Protocol"; private static final String NBT_PROTOCOL = "Protocol";
private static final String NBT_ITEMS = "Items"; private static final String NBT_ITEMS = "Items";
// Only client side
private static final String NBT_STORED = "Stored"; private static final String NBT_STORED = "Stored";
private static final String NBT_ITEM_TYPE = "Type"; private static final String NBT_ITEM_TYPE = "Type";
@@ -37,10 +39,6 @@ public class StorageDiskItem implements IStorageDisk<ItemStack> {
private NonNullList<ItemStack> stacks = NonNullList.create(); private NonNullList<ItemStack> stacks = NonNullList.create();
/**
* @param tag The NBT tag we are reading from and writing the amount stored to, has to be initialized with {@link StorageDiskItem#getTag()} if it doesn't exist yet
* @param capacity The capacity of this storage, -1 for infinite capacity
*/
public StorageDiskItem(NBTTagCompound tag, int capacity) { public StorageDiskItem(NBTTagCompound tag, int capacity) {
this.tag = tag; this.tag = tag;
this.capacity = capacity; this.capacity = capacity;
@@ -128,8 +126,6 @@ public class StorageDiskItem implements IStorageDisk<ItemStack> {
} }
if (!simulate) { if (!simulate) {
tag.setInteger(NBT_STORED, getStored() + remainingSpace);
otherStack.grow(remainingSpace); otherStack.grow(remainingSpace);
onChanged(); onChanged();
@@ -138,8 +134,6 @@ public class StorageDiskItem implements IStorageDisk<ItemStack> {
return isVoiding() ? null : ItemHandlerHelper.copyStackWithSize(otherStack, size - remainingSpace); return isVoiding() ? null : ItemHandlerHelper.copyStackWithSize(otherStack, size - remainingSpace);
} else { } else {
if (!simulate) { if (!simulate) {
tag.setInteger(NBT_STORED, getStored() + size);
otherStack.grow(size); otherStack.grow(size);
onChanged(); onChanged();
@@ -162,8 +156,6 @@ public class StorageDiskItem implements IStorageDisk<ItemStack> {
} }
if (!simulate) { if (!simulate) {
tag.setInteger(NBT_STORED, getStored() + remainingSpace);
stacks.add(ItemHandlerHelper.copyStackWithSize(stack, remainingSpace)); stacks.add(ItemHandlerHelper.copyStackWithSize(stack, remainingSpace));
onChanged(); onChanged();
@@ -172,8 +164,6 @@ public class StorageDiskItem implements IStorageDisk<ItemStack> {
return isVoiding() ? null : ItemHandlerHelper.copyStackWithSize(stack, size - remainingSpace); return isVoiding() ? null : ItemHandlerHelper.copyStackWithSize(stack, size - remainingSpace);
} else { } else {
if (!simulate) { if (!simulate) {
tag.setInteger(NBT_STORED, getStored() + size);
stacks.add(ItemHandlerHelper.copyStackWithSize(stack, size)); stacks.add(ItemHandlerHelper.copyStackWithSize(stack, size));
onChanged(); onChanged();
@@ -199,8 +189,6 @@ public class StorageDiskItem implements IStorageDisk<ItemStack> {
otherStack.shrink(size); otherStack.shrink(size);
} }
tag.setInteger(NBT_STORED, getStored() - size);
onChanged(); onChanged();
} }
@@ -213,7 +201,7 @@ public class StorageDiskItem implements IStorageDisk<ItemStack> {
@Override @Override
public int getStored() { public int getStored() {
return getStored(tag); return stacks.stream().mapToInt(s -> s.getCount()).sum();
} }
@Override @Override
@@ -238,7 +226,7 @@ public class StorageDiskItem implements IStorageDisk<ItemStack> {
@Override @Override
public boolean isValid(ItemStack stack) { public boolean isValid(ItemStack stack) {
return stack.hasTagCompound() && stack.getTagCompound().hasKey(NBT_ITEMS) && stack.getTagCompound().hasKey(NBT_STORED); return stack.hasTagCompound() && stack.getTagCompound().hasKey(NBT_ITEMS);
} }
@Override @Override
@@ -257,24 +245,23 @@ public class StorageDiskItem implements IStorageDisk<ItemStack> {
} }
public static NBTTagCompound getShareTag(NBTTagCompound tag) { public static NBTTagCompound getShareTag(NBTTagCompound tag) {
StorageDiskItem storage = new StorageDiskItem(tag, 0);
storage.readFromNBT();
NBTTagCompound otherTag = new NBTTagCompound(); NBTTagCompound otherTag = new NBTTagCompound();
otherTag.setInteger(NBT_STORED, getStored(tag)); otherTag.setInteger(NBT_STORED, storage.getStored());
otherTag.setTag(NBT_ITEMS, new NBTTagList()); // To circumvent not being able to insert disks in Disk Drives (see ItemStorageNBT#isValid(ItemStack)). otherTag.setTag(NBT_ITEMS, new NBTTagList());
otherTag.setInteger(NBT_PROTOCOL, PROTOCOL); otherTag.setInteger(NBT_PROTOCOL, PROTOCOL);
return otherTag; return otherTag;
} }
public static int getStored(NBTTagCompound tag) {
return tag.getInteger(NBT_STORED);
}
public static NBTTagCompound getTag() { public static NBTTagCompound getTag() {
NBTTagCompound tag = new NBTTagCompound(); NBTTagCompound tag = new NBTTagCompound();
tag.setTag(NBT_ITEMS, new NBTTagList()); tag.setTag(NBT_ITEMS, new NBTTagList());
tag.setInteger(NBT_STORED, 0);
tag.setInteger(NBT_PROTOCOL, PROTOCOL); tag.setInteger(NBT_PROTOCOL, PROTOCOL);
return tag; return tag;