Add IStorage#getCacheDelta, makes "void excess items" feature work again on 1.11

This commit is contained in:
Raoul Van den Berge
2016-11-30 13:57:05 +01:00
parent d8f6f98960
commit 3cf6c8b791
6 changed files with 99 additions and 59 deletions

View File

@@ -2,6 +2,8 @@ package com.raoulvdberge.refinedstorage.api.storage;
import net.minecraft.util.NonNullList; import net.minecraft.util.NonNullList;
import javax.annotation.Nullable;
public interface IStorage<T> { public interface IStorage<T> {
/** /**
* @return stacks stored in this storage * @return stacks stored in this storage
@@ -26,7 +28,12 @@ public interface IStorage<T> {
} }
/** /**
* @return true if this storage voids incoming items or fluids * Returns the delta that needs to be added to the item or fluid storage cache AFTER insertion of the stack.
*
* @param storedPreInsertion the amount stored pre insertion
* @param size the size of the stack being inserted
* @param remainder the remainder that we got back, or null if no remainder was there
* @return the amount to increase the cache with
*/ */
boolean isVoiding(); int getCacheDelta(int storedPreInsertion, int size, @Nullable T remainder);
} }

View File

@@ -1,6 +1,7 @@
package com.raoulvdberge.refinedstorage.apiimpl.storage.fluid; package com.raoulvdberge.refinedstorage.apiimpl.storage.fluid;
import com.raoulvdberge.refinedstorage.RSUtils; import com.raoulvdberge.refinedstorage.RSUtils;
import com.raoulvdberge.refinedstorage.api.storage.AccessType;
import com.raoulvdberge.refinedstorage.api.storage.fluid.IFluidStorage; import com.raoulvdberge.refinedstorage.api.storage.fluid.IFluidStorage;
import com.raoulvdberge.refinedstorage.apiimpl.API; import com.raoulvdberge.refinedstorage.apiimpl.API;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
@@ -85,6 +86,10 @@ public abstract class FluidStorageNBT implements IFluidStorage {
int remainingSpace = getCapacity() - getStored(); int remainingSpace = getCapacity() - getStored();
if (remainingSpace <= 0) { if (remainingSpace <= 0) {
if (isVoiding()) {
return null;
}
return RSUtils.copyStackWithSize(stack, size); return RSUtils.copyStackWithSize(stack, size);
} }
@@ -96,7 +101,7 @@ public abstract class FluidStorageNBT implements IFluidStorage {
onStorageChanged(); onStorageChanged();
} }
return 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); tag.setInteger(NBT_STORED, getStored() + size);
@@ -115,6 +120,10 @@ public abstract class FluidStorageNBT implements IFluidStorage {
int remainingSpace = getCapacity() - getStored(); int remainingSpace = getCapacity() - getStored();
if (remainingSpace <= 0) { if (remainingSpace <= 0) {
if (isVoiding()) {
return null;
}
return RSUtils.copyStackWithSize(stack, size); return RSUtils.copyStackWithSize(stack, size);
} }
@@ -126,7 +135,7 @@ public abstract class FluidStorageNBT implements IFluidStorage {
onStorageChanged(); onStorageChanged();
} }
return 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); tag.setInteger(NBT_STORED, getStored() + size);
@@ -182,8 +191,23 @@ public abstract class FluidStorageNBT implements IFluidStorage {
return capacity; return capacity;
} }
public boolean isFull() { protected boolean isVoiding() {
return getStored() == getCapacity(); return false;
}
@Override
public int getCacheDelta(int storedPreInsertion, int size, @Nullable FluidStack remainder) {
if (getAccessType() == AccessType.INSERT) {
return 0;
}
int inserted = remainder == null ? size : (size - remainder.amount);
if (isVoiding() && storedPreInsertion + inserted > getCapacity()) {
inserted = getCapacity() - storedPreInsertion;
}
return inserted;
} }
public NBTTagCompound getTag() { public NBTTagCompound getTag() {

View File

@@ -1,5 +1,6 @@
package com.raoulvdberge.refinedstorage.apiimpl.storage.item; package com.raoulvdberge.refinedstorage.apiimpl.storage.item;
import com.raoulvdberge.refinedstorage.api.storage.AccessType;
import com.raoulvdberge.refinedstorage.api.storage.item.IItemStorage; import com.raoulvdberge.refinedstorage.api.storage.item.IItemStorage;
import com.raoulvdberge.refinedstorage.apiimpl.API; import com.raoulvdberge.refinedstorage.apiimpl.API;
import net.minecraft.item.Item; import net.minecraft.item.Item;
@@ -122,6 +123,10 @@ public abstract class ItemStorageNBT implements IItemStorage {
int remainingSpace = getCapacity() - getStored(); int remainingSpace = getCapacity() - getStored();
if (remainingSpace <= 0) { if (remainingSpace <= 0) {
if (isVoiding()) {
return null;
}
return ItemHandlerHelper.copyStackWithSize(stack, size); return ItemHandlerHelper.copyStackWithSize(stack, size);
} }
@@ -133,7 +138,7 @@ public abstract class ItemStorageNBT implements IItemStorage {
onStorageChanged(); onStorageChanged();
} }
return 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); tag.setInteger(NBT_STORED, getStored() + size);
@@ -152,6 +157,10 @@ public abstract class ItemStorageNBT implements IItemStorage {
int remainingSpace = getCapacity() - getStored(); int remainingSpace = getCapacity() - getStored();
if (remainingSpace <= 0) { if (remainingSpace <= 0) {
if (isVoiding()) {
return null;
}
return ItemHandlerHelper.copyStackWithSize(stack, size); return ItemHandlerHelper.copyStackWithSize(stack, size);
} }
@@ -163,7 +172,7 @@ public abstract class ItemStorageNBT implements IItemStorage {
onStorageChanged(); onStorageChanged();
} }
return 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); tag.setInteger(NBT_STORED, getStored() + size);
@@ -219,8 +228,23 @@ public abstract class ItemStorageNBT implements IItemStorage {
return capacity; return capacity;
} }
public boolean isFull() { protected boolean isVoiding() {
return getStored() == getCapacity(); return false;
}
@Override
public int getCacheDelta(int storedPreInsertion, int size, @Nullable ItemStack remainder) {
if (getAccessType() == AccessType.INSERT) {
return 0;
}
int inserted = remainder == null ? size : (size - remainder.getCount());
if (isVoiding() && storedPreInsertion + inserted > getCapacity()) {
inserted = getCapacity() - storedPreInsertion;
}
return inserted;
} }
public NBTTagCompound getTag() { public NBTTagCompound getTag() {

View File

@@ -580,40 +580,34 @@ public class TileController extends TileBase implements INetworkMaster, IRedston
return ItemHandlerHelper.copyStackWithSize(stack, size); return ItemHandlerHelper.copyStackWithSize(stack, size);
} }
int originalSize = size;
ItemStack remainder = stack; ItemStack remainder = stack;
int inserted = 0;
int insertedExternally = 0; int insertedExternally = 0;
int insertedToIgnore = 0;
for (IItemStorage storage : this.itemStorage.getStorages()) { for (IItemStorage storage : this.itemStorage.getStorages()) {
if (storage.getAccessType() != AccessType.EXTRACT) { int storedPre = storage.getStored();
remainder = storage.insertItem(remainder, size, simulate);
if (!simulate) { remainder = storage.insertItem(remainder, size, simulate);
if (remainder != null && storage.isVoiding()) {
insertedToIgnore += remainder.getCount();
remainder = null; if (!simulate) {
} else if (storage.getAccessType() == AccessType.INSERT) { inserted += storage.getCacheDelta(storedPre, size, remainder);
insertedToIgnore += size - (remainder != null ? remainder.getCount() : 0);
}
}
} }
if (remainder == null) { if (remainder == null) {
// The external storage is responsible for sending changes, we don't need to anymore
if (storage instanceof ItemStorageExternal && !simulate) { if (storage instanceof ItemStorageExternal && !simulate) {
((ItemStorageExternal) storage).detectChanges(this); ((ItemStorageExternal) storage).detectChanges(this);
// the external storage will send the change, we don't need to anymore
insertedExternally += size; insertedExternally += size;
} }
break; break;
} else { } else {
// The external storage is responsible for sending changes, we don't need to anymore
if (size != remainder.getCount() && storage instanceof ItemStorageExternal && !simulate) { if (size != remainder.getCount() && storage instanceof ItemStorageExternal && !simulate) {
((ItemStorageExternal) storage).detectChanges(this); ((ItemStorageExternal) storage).detectChanges(this);
// the external storage will send the change, we don't need to anymore
insertedExternally += size - remainder.getCount(); insertedExternally += size - remainder.getCount();
} }
@@ -622,14 +616,12 @@ public class TileController extends TileBase implements INetworkMaster, IRedston
} }
if (!simulate) { if (!simulate) {
int inserted = remainder == null ? originalSize : (originalSize - remainder.getCount()); if (inserted - insertedExternally > 0) {
itemStorage.add(stack, inserted - insertedExternally, false);
if (inserted - insertedExternally - insertedToIgnore > 0) {
itemStorage.add(stack, inserted - insertedExternally - insertedToIgnore, false);
} }
if (inserted - insertedToIgnore > 0) { if (inserted > 0) {
ItemStack checkSteps = ItemHandlerHelper.copyStackWithSize(stack, inserted - insertedToIgnore); ItemStack checkSteps = ItemHandlerHelper.copyStackWithSize(stack, inserted);
for (ICraftingTask task : craftingTasks) { for (ICraftingTask task : craftingTasks) {
for (ICraftingStep processable : task.getSteps()) { for (ICraftingStep processable : task.getSteps()) {
@@ -648,7 +640,9 @@ public class TileController extends TileBase implements INetworkMaster, IRedston
public ItemStack extractItem(@Nonnull ItemStack stack, int size, int flags, boolean simulate) { public ItemStack extractItem(@Nonnull ItemStack stack, int size, int flags, boolean simulate) {
int requested = size; int requested = size;
int received = 0; int received = 0;
int externalStorageExtracted = 0;
int extractedExternally = 0;
ItemStack newStack = null; ItemStack newStack = null;
for (IItemStorage storage : this.itemStorage.getStorages()) { for (IItemStorage storage : this.itemStorage.getStorages()) {
@@ -659,10 +653,11 @@ public class TileController extends TileBase implements INetworkMaster, IRedston
} }
if (took != null) { if (took != null) {
// The external storage is responsible for sending changes, we don't need to anymore
if (storage instanceof ItemStorageExternal && !simulate) { if (storage instanceof ItemStorageExternal && !simulate) {
((ItemStorageExternal) storage).detectChanges(this); ((ItemStorageExternal) storage).detectChanges(this);
// the external storage will send the change, we don't need to anymore
externalStorageExtracted += took.getCount(); extractedExternally += took.getCount();
} }
if (newStack == null) { if (newStack == null) {
@@ -679,8 +674,8 @@ public class TileController extends TileBase implements INetworkMaster, IRedston
} }
} }
if (newStack != null && newStack.getCount() - externalStorageExtracted > 0 && !simulate) { if (newStack != null && newStack.getCount() - extractedExternally > 0 && !simulate) {
itemStorage.remove(newStack, newStack.getCount() - externalStorageExtracted); itemStorage.remove(newStack, newStack.getCount() - extractedExternally);
} }
return newStack; return newStack;
@@ -693,23 +688,17 @@ public class TileController extends TileBase implements INetworkMaster, IRedston
return RSUtils.copyStackWithSize(stack, size); return RSUtils.copyStackWithSize(stack, size);
} }
int originalSize = size;
AccessType accessType = AccessType.INSERT_EXTRACT;
FluidStack remainder = stack; FluidStack remainder = stack;
int insertedToIgnore = 0; int inserted = 0;
for (IFluidStorage storage : this.fluidStorage.getStorages()) { for (IFluidStorage storage : this.fluidStorage.getStorages()) {
accessType = storage.getAccessType(); int storedPre = storage.getStored();
if (accessType != AccessType.EXTRACT) { remainder = storage.insertFluid(remainder, size, simulate);
remainder = storage.insertFluid(remainder, size, simulate);
if (remainder != null && storage.isVoiding() && !simulate) { if (!simulate) {
insertedToIgnore += remainder.amount; inserted += storage.getCacheDelta(storedPre, size, remainder);
remainder = null;
}
} }
if (storage instanceof FluidStorageExternal && !simulate) { if (storage instanceof FluidStorageExternal && !simulate) {
@@ -723,14 +712,8 @@ public class TileController extends TileBase implements INetworkMaster, IRedston
} }
} }
if (!simulate) { if (inserted > 0) {
int inserted = remainder == null ? originalSize : (originalSize - remainder.amount); fluidStorage.add(RSUtils.copyStackWithSize(stack, inserted), false);
inserted -= insertedToIgnore;
if (inserted > 0 && accessType != AccessType.INSERT) {
fluidStorage.add(RSUtils.copyStackWithSize(stack, inserted), false);
}
} }
return remainder; return remainder;
@@ -741,6 +724,7 @@ public class TileController extends TileBase implements INetworkMaster, IRedston
public FluidStack extractFluid(@Nonnull FluidStack stack, int size, int flags, boolean simulate) { public FluidStack extractFluid(@Nonnull FluidStack stack, int size, int flags, boolean simulate) {
int requested = size; int requested = size;
int received = 0; int received = 0;
FluidStack newStack = null; FluidStack newStack = null;
for (IFluidStorage storage : this.fluidStorage.getStorages()) { for (IFluidStorage storage : this.fluidStorage.getStorages()) {

View File

@@ -85,8 +85,8 @@ public class FluidStorageExternal implements IFluidStorage {
} }
@Override @Override
public boolean isVoiding() { public int getCacheDelta(int storedPreInsertion, int size, @Nullable FluidStack remainder) {
return false; return remainder == null ? size : (size - remainder.amount);
} }
public boolean updateCache() { public boolean updateCache() {

View File

@@ -6,6 +6,7 @@ import com.raoulvdberge.refinedstorage.api.storage.item.IItemStorage;
import com.raoulvdberge.refinedstorage.apiimpl.API; import com.raoulvdberge.refinedstorage.apiimpl.API;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import javax.annotation.Nullable;
import java.util.List; import java.util.List;
public abstract class ItemStorageExternal implements IItemStorage { public abstract class ItemStorageExternal implements IItemStorage {
@@ -78,7 +79,7 @@ public abstract class ItemStorageExternal implements IItemStorage {
} }
@Override @Override
public boolean isVoiding() { public int getCacheDelta(int storedPreInsertion, int size, @Nullable ItemStack remainder) {
return false; return remainder == null ? size : (size - remainder.getCount());
} }
} }