Make IStorage return NonNull ItemStack/FluidStack

This commit is contained in:
raoulvdberge
2019-10-16 18:02:05 +02:00
parent ad44107c39
commit 7c50c49aaf
24 changed files with 161 additions and 98 deletions

View File

@@ -3,6 +3,7 @@ package com.raoulvdberge.refinedstorage.api.network.grid.handler;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.ItemStack;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.UUID;
@@ -30,7 +31,7 @@ public interface IItemGridHandler {
* @param stack the item we're trying to insert
* @return the remainder, or null if there is no remainder
*/
@Nullable
@Nonnull
ItemStack onInsert(ServerPlayerEntity player, ItemStack stack);
/**
@@ -48,6 +49,8 @@ public interface IItemGridHandler {
* @param stack the stack
* @return the remainder stack
*/
// TODO Maybe remove?
@Nonnull
ItemStack onShiftClick(ServerPlayerEntity player, ItemStack stack);
/**

View File

@@ -28,12 +28,12 @@ public interface IStorage<T> {
/**
* Inserts a stack to this storage.
*
* @param stack the stack prototype to insert, do NOT modify
* @param stack the stack prototype to insert, can be empty, do NOT modify
* @param size the amount of that prototype that has to be inserted
* @param action the action
* @return null if the insert was successful, or a stack with the remainder
* @return an empty stack if the insert was successful, or a stack with the remainder
*/
@Nullable
@Nonnull
T insert(@Nonnull T stack, int size, Action action);
/**
@@ -41,13 +41,13 @@ public interface IStorage<T> {
* <p>
* If the stack we found in the system is smaller than the requested size, return that stack anyway.
*
* @param stack a prototype of the stack to extract, do NOT modify
* @param stack a prototype of the stack to extract, can be empty, do NOT modify
* @param size the amount of that prototype that has to be extracted
* @param flags the flags to compare on, see {@link IComparer}
* @param action the action
* @return null if we didn't extract anything, or a stack with the result
* @return an empty stack if nothing was extracted, or an extracted stack
*/
@Nullable
@Nonnull
T extract(@Nonnull T stack, int size, int flags, Action action);
/**

View File

@@ -611,7 +611,7 @@ public class CraftingTask implements ICraftingTask {
for (StackListEntry<ItemStack> need : c.getToExtract().getStacks()) {
ItemStack result = this.internalStorage.extract(need.getStack(), need.getStack().getCount(), DEFAULT_EXTRACT_FLAGS, Action.SIMULATE);
if (result == null || result.getCount() != need.getStack().getCount()) {
if (result.getCount() != need.getStack().getCount()) {
hasAll = false;
break;
@@ -622,7 +622,7 @@ public class CraftingTask implements ICraftingTask {
for (StackListEntry<ItemStack> need : c.getToExtract().getStacks()) {
ItemStack result = this.internalStorage.extract(need.getStack(), need.getStack().getCount(), DEFAULT_EXTRACT_FLAGS, Action.PERFORM);
if (result == null || result.getCount() != need.getStack().getCount()) {
if (result.getCount() != need.getStack().getCount()) {
throw new IllegalStateException("Extractor check lied");
}
}
@@ -701,7 +701,7 @@ public class CraftingTask implements ICraftingTask {
} else {
ItemStack result = this.internalStorage.extract(need.getStack(), need.getStack().getCount(), DEFAULT_EXTRACT_FLAGS, Action.SIMULATE);
if (result == null || result.getCount() != need.getStack().getCount()) {
if (result.getCount() != need.getStack().getCount()) {
hasAll = false;
break;
@@ -721,7 +721,7 @@ public class CraftingTask implements ICraftingTask {
} else {
FluidStack result = this.internalFluidStorage.extract(need.getStack(), need.getStack().getAmount(), IComparer.COMPARE_NBT, Action.SIMULATE);
if (result == null || result.getAmount() != need.getStack().getAmount()) {
if (result.getAmount() != need.getStack().getAmount()) {
hasAll = false;
break;
@@ -740,7 +740,7 @@ public class CraftingTask implements ICraftingTask {
for (StackListEntry<ItemStack> need : p.getItemsToPut().getStacks()) {
ItemStack result = this.internalStorage.extract(need.getStack(), need.getStack().getCount(), DEFAULT_EXTRACT_FLAGS, Action.PERFORM);
if (result == null || result.getCount() != need.getStack().getCount()) {
if (result.getCount() != need.getStack().getCount()) {
throw new IllegalStateException("The internal crafting inventory reported that " + need.getStack() + " was available but we got " + result);
}
@@ -753,7 +753,7 @@ public class CraftingTask implements ICraftingTask {
for (StackListEntry<FluidStack> need : p.getFluidsToPut().getStacks()) {
FluidStack result = this.internalFluidStorage.extract(need.getStack(), need.getStack().getAmount(), IComparer.COMPARE_NBT, Action.PERFORM);
if (result == null || result.getAmount() != need.getStack().getAmount()) {
if (result.getAmount() != need.getStack().getAmount()) {
throw new IllegalStateException("The internal crafting inventory reported that " + need + " was available but we got " + result);
}

View File

@@ -59,7 +59,11 @@ public class FluidGridHandler implements IFluidGridHandler {
bucket.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null).ifPresent(fluidHandler -> {
network.getFluidStorageTracker().changed(player, stack.copy());
fluidHandler.fill(network.extractFluid(stack, FluidAttributes.BUCKET_VOLUME, Action.PERFORM), IFluidHandler.FluidAction.EXECUTE);
FluidStack extracted = network.extractFluid(stack, FluidAttributes.BUCKET_VOLUME, Action.PERFORM);
if (extracted != null) {
fluidHandler.fill(extracted, IFluidHandler.FluidAction.EXECUTE);
}
if (shift) {
if (!player.inventory.addItemStackToInventory(fluidHandler.getContainer().copy())) {

View File

@@ -18,6 +18,7 @@ import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemHandlerHelper;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.UUID;
@@ -107,6 +108,7 @@ public class ItemGridHandler implements IItemGridHandler {
}
@Override
@Nonnull
public ItemStack onInsert(ServerPlayerEntity player, ItemStack stack) {
if (!network.getSecurityManager().hasPermission(Permission.INSERT, player)) {
return stack;
@@ -114,7 +116,7 @@ public class ItemGridHandler implements IItemGridHandler {
network.getItemStorageTracker().changed(player, stack.copy());
ItemStack remainder = network.insertItem(stack, stack.getCount(), Action.PERFORM);
ItemStack remainder = StackUtils.nullToEmpty(network.insertItem(stack, stack.getCount(), Action.PERFORM));
// TODO network.getNetworkItemHandler().drainEnergy(player, RS.INSTANCE.config.wirelessGridInsertUsage);
@@ -152,8 +154,9 @@ public class ItemGridHandler implements IItemGridHandler {
}
@Override
@Nonnull
public ItemStack onShiftClick(ServerPlayerEntity player, ItemStack stack) {
return StackUtils.nullToEmpty(onInsert(player, stack));
return onInsert(player, stack);
}
@Override

View File

@@ -73,7 +73,7 @@ public class PortableFluidGridHandler implements IFluidGridHandler {
public ItemStack onInsert(ServerPlayerEntity player, ItemStack container) {
Pair<ItemStack, FluidStack> result = StackUtils.getFluid(container, true);
if (!result.getValue().isEmpty() && portableGrid.getFluidStorage().insert(result.getValue(), result.getValue().getAmount(), Action.SIMULATE) == null) {
if (!result.getValue().isEmpty() && portableGrid.getFluidStorage().insert(result.getValue(), result.getValue().getAmount(), Action.SIMULATE).isEmpty()) {
portableGrid.getFluidStorageTracker().changed(player, result.getValue().copy());
result = StackUtils.getFluid(container, false);

View File

@@ -7,7 +7,6 @@ import com.raoulvdberge.refinedstorage.api.util.Action;
import com.raoulvdberge.refinedstorage.api.util.IComparer;
import com.raoulvdberge.refinedstorage.apiimpl.API;
import com.raoulvdberge.refinedstorage.tile.grid.portable.IPortableGrid;
import com.raoulvdberge.refinedstorage.util.StackUtils;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Direction;
@@ -15,6 +14,7 @@ import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemHandlerHelper;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.UUID;
@@ -80,7 +80,7 @@ public class PortableItemGridHandler implements IItemGridHandler {
ItemStack took = portableGrid.getItemStorage().extract(item, size, IComparer.COMPARE_NBT, Action.SIMULATE);
if (took != null) {
if (!took.isEmpty()) {
if ((flags & EXTRACT_SHIFT) == EXTRACT_SHIFT) {
IItemHandler playerInventory = player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, Direction.UP).orElse(null);
@@ -105,8 +105,8 @@ public class PortableItemGridHandler implements IItemGridHandler {
}
}
@Nullable
@Override
@Nonnull
public ItemStack onInsert(ServerPlayerEntity player, ItemStack stack) {
if (portableGrid.getStorage() == null || !grid.isActive()) {
return stack;
@@ -133,17 +133,18 @@ public class PortableItemGridHandler implements IItemGridHandler {
portableGrid.getItemStorageTracker().changed(player, stack.copy());
if (single) {
if (portableGrid.getItemStorage().insert(stack, size, Action.SIMULATE) == null) {
if (portableGrid.getItemStorage().insert(stack, size, Action.SIMULATE).isEmpty()) {
portableGrid.getItemStorage().insert(stack, size, Action.PERFORM);
stack.shrink(size);
// TODO ???
if (stack.getCount() == 0) {
player.inventory.setItemStack(ItemStack.EMPTY);
}
}
} else {
player.inventory.setItemStack(StackUtils.nullToEmpty(portableGrid.getItemStorage().insert(stack, size, Action.PERFORM)));
player.inventory.setItemStack(portableGrid.getItemStorage().insert(stack, size, Action.PERFORM));
}
player.updateHeldItem();
@@ -152,8 +153,9 @@ public class PortableItemGridHandler implements IItemGridHandler {
}
@Override
@Nonnull
public ItemStack onShiftClick(ServerPlayerEntity player, ItemStack stack) {
return StackUtils.nullToEmpty(onInsert(player, stack));
return onInsert(player, stack);
}
@Override

View File

@@ -81,8 +81,8 @@ public class DiskDriveNetworkNode extends NetworkNode implements IStorageProvide
slot,
itemDisks,
fluidDisks,
s -> new StorageDiskItemDriveWrapper(DiskDriveNetworkNode.this, s),
s -> new StorageDiskFluidDriveWrapper(DiskDriveNetworkNode.this, s)
s -> new ItemDriveWrapperStorageDisk(DiskDriveNetworkNode.this, s),
s -> new FluidDriveWrapperStorageDisk(DiskDriveNetworkNode.this, s)
);
if (network != null) {

View File

@@ -15,12 +15,12 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collection;
public class StorageDiskFluidDriveWrapper implements IStorageDisk<FluidStack> {
public class FluidDriveWrapperStorageDisk implements IStorageDisk<FluidStack> {
private DiskDriveNetworkNode diskDrive;
private IStorageDisk<FluidStack> parent;
private DiskDriveNetworkNode.DiskState lastState;
public StorageDiskFluidDriveWrapper(DiskDriveNetworkNode diskDrive, IStorageDisk<FluidStack> parent) {
public FluidDriveWrapperStorageDisk(DiskDriveNetworkNode diskDrive, IStorageDisk<FluidStack> parent) {
this.diskDrive = diskDrive;
this.parent = parent;
this.setSettings(
@@ -54,7 +54,7 @@ public class StorageDiskFluidDriveWrapper implements IStorageDisk<FluidStack> {
}
@Override
@Nullable
@Nonnull
public FluidStack insert(@Nonnull FluidStack stack, int size, Action action) {
if (!IWhitelistBlacklist.acceptsFluid(diskDrive.getFluidFilters(), diskDrive.getWhitelistBlacklistMode(), diskDrive.getCompare(), stack)) {
return StackUtils.copy(stack, size);
@@ -63,8 +63,8 @@ public class StorageDiskFluidDriveWrapper implements IStorageDisk<FluidStack> {
return parent.insert(stack, size, action);
}
@Nullable
@Override
@Nonnull
public FluidStack extract(@Nonnull FluidStack stack, int size, int flags, Action action) {
return parent.extract(stack, size, flags, action);
}

View File

@@ -15,12 +15,12 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collection;
public class StorageDiskItemDriveWrapper implements IStorageDisk<ItemStack> {
public class ItemDriveWrapperStorageDisk implements IStorageDisk<ItemStack> {
private DiskDriveNetworkNode diskDrive;
private IStorageDisk<ItemStack> parent;
private DiskDriveNetworkNode.DiskState lastState;
public StorageDiskItemDriveWrapper(DiskDriveNetworkNode diskDrive, IStorageDisk<ItemStack> parent) {
public ItemDriveWrapperStorageDisk(DiskDriveNetworkNode diskDrive, IStorageDisk<ItemStack> parent) {
this.diskDrive = diskDrive;
this.parent = parent;
this.setSettings(
@@ -54,7 +54,7 @@ public class StorageDiskItemDriveWrapper implements IStorageDisk<ItemStack> {
}
@Override
@Nullable
@Nonnull
public ItemStack insert(@Nonnull ItemStack stack, int size, Action action) {
if (!IWhitelistBlacklist.acceptsItem(diskDrive.getItemFilters(), diskDrive.getWhitelistBlacklistMode(), diskDrive.getCompare(), stack)) {
return ItemHandlerHelper.copyStackWithSize(stack, size);
@@ -63,8 +63,8 @@ public class StorageDiskItemDriveWrapper implements IStorageDisk<ItemStack> {
return parent.insert(stack, size, action);
}
@Nullable
@Override
@Nonnull
public ItemStack extract(@Nonnull ItemStack stack, int size, int flags, Action action) {
return parent.extract(stack, size, flags, action);
}

View File

@@ -182,7 +182,7 @@ public class NetworkNodeDiskManipulator extends NetworkNode implements IComparab
ItemStack stack = stacks.get(i);
ItemStack extracted = storage.extract(stack, upgrades.getItemInteractCount(), compare, Action.PERFORM);
if (extracted == null) {
if (extracted.isEmpty()) {
continue;
}
@@ -217,7 +217,7 @@ public class NetworkNodeDiskManipulator extends NetworkNode implements IComparab
ItemStack stack = stacks.get(i);
ItemStack extracted = storage.extract(stack, upgrades.getItemInteractCount(), compare, Action.SIMULATE);
if (extracted == null) {
if (extracted.isEmpty()) {
continue;
}
@@ -267,7 +267,7 @@ public class NetworkNodeDiskManipulator extends NetworkNode implements IComparab
ItemStack remainder = storage.insert(extracted, extracted.getCount(), Action.PERFORM);
if (remainder != null) {
if (!remainder.isEmpty()) {
network.insertItem(remainder, remainder.getCount(), Action.PERFORM);
}
}
@@ -275,16 +275,16 @@ public class NetworkNodeDiskManipulator extends NetworkNode implements IComparab
private void insertFluidIntoNetwork(IStorageDisk<FluidStack> storage, int slot) {
List<FluidStack> stacks = new ArrayList<>(storage.getStacks());
FluidStack extracted = null;
FluidStack extracted = FluidStack.EMPTY;
int i = 0;
while (extracted == null && stacks.size() > i) {
while (extracted.isEmpty() && stacks.size() > i) {
FluidStack stack = stacks.get(i++);
extracted = storage.extract(stack, upgrades.getItemInteractCount(), compare, Action.PERFORM);
}
if (extracted == null) {
if (extracted.isEmpty()) {
moveDriveToOutput(slot);
return;
}
@@ -316,7 +316,7 @@ public class NetworkNodeDiskManipulator extends NetworkNode implements IComparab
FluidStack stack = stacks.get(i);
FluidStack extracted = storage.extract(stack, upgrades.getItemInteractCount(), compare, Action.SIMULATE);
if (extracted == null) {
if (extracted.isEmpty()) {
continue;
}
@@ -366,7 +366,7 @@ public class NetworkNodeDiskManipulator extends NetworkNode implements IComparab
FluidStack remainder = storage.insert(extracted, extracted.getAmount(), Action.PERFORM);
if (remainder != null) {
if (!remainder.isEmpty()) {
network.insertFluid(remainder, remainder.getAmount(), Action.PERFORM);
}
}

View File

@@ -66,7 +66,7 @@ public class StorageDiskFluidManipulatorWrapper implements IStorageDisk<FluidSta
}
@Override
@Nullable
@Nonnull
public FluidStack insert(@Nonnull FluidStack stack, int size, Action action) {
if (!IWhitelistBlacklist.acceptsFluid(diskManipulator.getFluidFilters(), diskManipulator.getWhitelistBlacklistMode(), diskManipulator.getCompare(), stack)) {
return StackUtils.copy(stack, size);
@@ -76,10 +76,10 @@ public class StorageDiskFluidManipulatorWrapper implements IStorageDisk<FluidSta
}
@Override
@Nullable
@Nonnull
public FluidStack extract(@Nonnull FluidStack stack, int size, int flags, Action action) {
if (!IWhitelistBlacklist.acceptsFluid(diskManipulator.getFluidFilters(), diskManipulator.getWhitelistBlacklistMode(), diskManipulator.getCompare(), stack)) {
return null;
return FluidStack.EMPTY;
}
return parent.extract(stack, size, flags, action);

View File

@@ -61,7 +61,7 @@ public class StorageDiskItemManipulatorWrapper implements IStorageDisk<ItemStack
}
@Override
@Nullable
@Nonnull
public ItemStack insert(@Nonnull ItemStack stack, int size, Action action) {
if (!IWhitelistBlacklist.acceptsItem(diskManipulator.getItemFilters(), diskManipulator.getWhitelistBlacklistMode(), diskManipulator.getCompare(), stack)) {
return ItemHandlerHelper.copyStackWithSize(stack, size);
@@ -71,10 +71,10 @@ public class StorageDiskItemManipulatorWrapper implements IStorageDisk<ItemStack
}
@Override
@Nullable
@Nonnull
public ItemStack extract(@Nonnull ItemStack stack, int size, int flags, Action action) {
if (!IWhitelistBlacklist.acceptsItem(diskManipulator.getItemFilters(), diskManipulator.getWhitelistBlacklistMode(), diskManipulator.getCompare(), stack)) {
return null;
return ItemStack.EMPTY;
}
return parent.extract(stack, size, flags, action);

View File

@@ -139,7 +139,7 @@ public class FluidStorageNetworkNode extends NetworkNode implements IStorageScre
API.instance().getStorageDiskManager((ServerWorld) world).markForSaving();
}
this.storage = new StorageDiskFluidStorageWrapper(this, disk);
this.storage = new FluidStorageWrapperStorageDisk(this, disk);
}
public void setStorageId(UUID id) {

View File

@@ -15,11 +15,11 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collection;
public class StorageDiskFluidStorageWrapper implements IStorageDisk<FluidStack> {
public class FluidStorageWrapperStorageDisk implements IStorageDisk<FluidStack> {
private FluidStorageNetworkNode storage;
private IStorageDisk<FluidStack> parent;
public StorageDiskFluidStorageWrapper(FluidStorageNetworkNode storage, IStorageDisk<FluidStack> parent) {
public FluidStorageWrapperStorageDisk(FluidStorageNetworkNode storage, IStorageDisk<FluidStack> parent) {
this.storage = storage;
this.parent = parent;
this.setSettings(null, storage);
@@ -41,7 +41,7 @@ public class StorageDiskFluidStorageWrapper implements IStorageDisk<FluidStack>
}
@Override
@Nullable
@Nonnull
public FluidStack insert(@Nonnull FluidStack stack, int size, Action action) {
if (!IWhitelistBlacklist.acceptsFluid(storage.getFilters(), storage.getWhitelistBlacklistMode(), storage.getCompare(), stack)) {
return StackUtils.copy(stack, size);
@@ -50,8 +50,8 @@ public class StorageDiskFluidStorageWrapper implements IStorageDisk<FluidStack>
return parent.insert(stack, size, action);
}
@Nullable
@Override
@Nonnull
public FluidStack extract(@Nonnull FluidStack stack, int size, int flags, Action action) {
return parent.extract(stack, size, flags, action);
}

View File

@@ -15,11 +15,11 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collection;
public class StorageDiskItemStorageWrapper implements IStorageDisk<ItemStack> {
public class ItemStorageWrapperStorageDisk implements IStorageDisk<ItemStack> {
private StorageNetworkNode storage;
private IStorageDisk<ItemStack> parent;
public StorageDiskItemStorageWrapper(StorageNetworkNode storage, IStorageDisk<ItemStack> parent) {
public ItemStorageWrapperStorageDisk(StorageNetworkNode storage, IStorageDisk<ItemStack> parent) {
this.storage = storage;
this.parent = parent;
this.setSettings(null, storage);
@@ -41,7 +41,7 @@ public class StorageDiskItemStorageWrapper implements IStorageDisk<ItemStack> {
}
@Override
@Nullable
@Nonnull
public ItemStack insert(@Nonnull ItemStack stack, int size, Action action) {
if (!IWhitelistBlacklist.acceptsItem(storage.getFilters(), storage.getWhitelistBlacklistMode(), storage.getCompare(), stack)) {
return ItemHandlerHelper.copyStackWithSize(stack, size);
@@ -50,8 +50,8 @@ public class StorageDiskItemStorageWrapper implements IStorageDisk<ItemStack> {
return parent.insert(stack, size, action);
}
@Nullable
@Override
@Nonnull
public ItemStack extract(@Nonnull ItemStack stack, int size, int flags, Action action) {
return parent.extract(stack, size, flags, action);
}

View File

@@ -139,7 +139,7 @@ public class StorageNetworkNode extends NetworkNode implements IStorageScreen, I
API.instance().getStorageDiskManager((ServerWorld) world).markForSaving();
}
this.storage = new StorageDiskItemStorageWrapper(this, disk);
this.storage = new ItemStorageWrapperStorageDisk(this, disk);
}
public void setStorageId(UUID id) {

View File

@@ -67,8 +67,12 @@ public class FluidStorageDisk implements IStorageDisk<FluidStack> {
}
@Override
@Nullable
@Nonnull
public FluidStack insert(@Nonnull FluidStack stack, int size, Action action) {
if (stack.isEmpty()) {
return stack;
}
for (FluidStack otherStack : stacks.get(stack.getFluid())) {
if (otherStack.isFluidEqual(stack)) {
if (getCapacity() != -1 && getStored() + size > getCapacity()) {
@@ -92,7 +96,7 @@ public class FluidStorageDisk implements IStorageDisk<FluidStack> {
onChanged();
}
return null;
return FluidStack.EMPTY;
}
}
}
@@ -118,13 +122,17 @@ public class FluidStorageDisk implements IStorageDisk<FluidStack> {
onChanged();
}
return null;
return FluidStack.EMPTY;
}
}
@Override
@Nullable
@Nonnull
public FluidStack extract(@Nonnull FluidStack stack, int size, int flags, Action action) {
if (stack.isEmpty()) {
return stack;
}
for (FluidStack otherStack : stacks.get(stack.getFluid())) {
if (API.instance().getComparer().isEqual(otherStack, stack, flags)) {
if (size > otherStack.getAmount()) {
@@ -145,7 +153,7 @@ public class FluidStorageDisk implements IStorageDisk<FluidStack> {
}
}
return null;
return FluidStack.EMPTY;
}
@Override

View File

@@ -73,8 +73,12 @@ public class ItemStorageDisk implements IStorageDisk<ItemStack> {
}
@Override
@Nullable
@Nonnull
public ItemStack insert(@Nonnull ItemStack stack, int size, Action action) {
if (stack.isEmpty()) {
return stack;
}
for (ItemStack otherStack : stacks.get(stack.getItem())) {
if (API.instance().getComparer().isEqualNoQuantity(otherStack, stack)) {
if (getCapacity() != -1 && getStored() + size > getCapacity()) {
@@ -98,7 +102,7 @@ public class ItemStorageDisk implements IStorageDisk<ItemStack> {
onChanged();
}
return null;
return ItemStack.EMPTY;
}
}
}
@@ -124,13 +128,17 @@ public class ItemStorageDisk implements IStorageDisk<ItemStack> {
onChanged();
}
return null;
return ItemStack.EMPTY;
}
}
@Override
@Nullable
@Nonnull
public ItemStack extract(@Nonnull ItemStack stack, int size, int flags, Action action) {
if (stack.isEmpty()) {
return stack;
}
for (ItemStack otherStack : stacks.get(stack.getItem())) {
if (API.instance().getComparer().isEqual(otherStack, stack, flags)) {
if (size > otherStack.getCount()) {
@@ -151,7 +159,7 @@ public class ItemStorageDisk implements IStorageDisk<ItemStack> {
}
}
return null;
return ItemStack.EMPTY;
}
@Override

View File

@@ -43,8 +43,8 @@ public class PortableFluidStorageDisk implements IStorageDisk<FluidStack> {
return parent.getStacks();
}
@Nullable
@Override
@Nonnull
public FluidStack insert(@Nonnull FluidStack stack, int size, Action action) {
int storedPre = parent.getStored();
@@ -61,12 +61,12 @@ public class PortableFluidStorageDisk implements IStorageDisk<FluidStack> {
return remainder;
}
@Nullable
@Override
@Nonnull
public FluidStack extract(@Nonnull FluidStack stack, int size, int flags, Action action) {
FluidStack extracted = parent.extract(stack, size, flags, action);
if (action == Action.PERFORM && extracted != null) {
if (action == Action.PERFORM && !extracted.isEmpty()) {
portableGrid.getFluidCache().remove(extracted, extracted.getAmount(), false);
}

View File

@@ -43,8 +43,8 @@ public class PortableItemStorageDisk implements IStorageDisk<ItemStack> {
return parent.getStacks();
}
@Nullable
@Override
@Nonnull
public ItemStack insert(@Nonnull ItemStack stack, int size, Action action) {
int storedPre = parent.getStored();
@@ -61,12 +61,12 @@ public class PortableItemStorageDisk implements IStorageDisk<ItemStack> {
return remainder;
}
@Nullable
@Override
@Nonnull
public ItemStack extract(@Nonnull ItemStack stack, int size, int flags, Action action) {
ItemStack extracted = parent.extract(stack, size, flags, action);
if (action == Action.PERFORM && extracted != null) {
if (action == Action.PERFORM && !extracted.isEmpty()) {
portableGrid.getItemCache().remove(extracted, extracted.getCount(), false);
}

View File

@@ -76,14 +76,18 @@ public class FluidExternalStorage implements IExternalStorage<FluidStack> {
return Collections.emptyList();
}
@Nullable
@Override
@Nonnull
public FluidStack insert(@Nonnull FluidStack stack, int size, Action action) {
if (stack.isEmpty()) {
return stack;
}
if (context.acceptsFluid(stack)) {
int filled = handlerSupplier.get().fill(StackUtils.copy(stack, size), action == Action.PERFORM ? IFluidHandler.FluidAction.EXECUTE : IFluidHandler.FluidAction.SIMULATE);
if (filled == size) {
return null;
return FluidStack.EMPTY;
}
return StackUtils.copy(stack, size - filled);
@@ -92,13 +96,17 @@ public class FluidExternalStorage implements IExternalStorage<FluidStack> {
return StackUtils.copy(stack, size);
}
@Nullable
@Override
@Nonnull
public FluidStack extract(@Nonnull FluidStack stack, int size, int flags, Action action) {
if (stack.isEmpty()) {
return stack;
}
IFluidHandler handler = handlerSupplier.get();
if (handler == null) {
return null;
return FluidStack.EMPTY;
}
return handler.drain(StackUtils.copy(stack, size), action == Action.PERFORM ? IFluidHandler.FluidAction.EXECUTE : IFluidHandler.FluidAction.SIMULATE);

View File

@@ -6,7 +6,6 @@ import com.raoulvdberge.refinedstorage.api.storage.externalstorage.IExternalStor
import com.raoulvdberge.refinedstorage.api.storage.externalstorage.IExternalStorageContext;
import com.raoulvdberge.refinedstorage.api.util.Action;
import com.raoulvdberge.refinedstorage.apiimpl.API;
import com.raoulvdberge.refinedstorage.util.StackUtils;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemHandlerHelper;
@@ -78,31 +77,39 @@ public class ItemExternalStorage implements IExternalStorage<ItemStack> {
return stacks;
}
@Nullable
@Override
@Nonnull
public ItemStack insert(@Nonnull ItemStack stack, int size, Action action) {
if (stack.isEmpty()) {
return stack;
}
IItemHandler handler = handlerSupplier.get();
if (handler != null && context.acceptsItem(stack)) {
return StackUtils.emptyToNull(ItemHandlerHelper.insertItem(handler, ItemHandlerHelper.copyStackWithSize(stack, size), action == Action.SIMULATE));
return ItemHandlerHelper.insertItem(handler, ItemHandlerHelper.copyStackWithSize(stack, size), action == Action.SIMULATE);
}
return ItemHandlerHelper.copyStackWithSize(stack, size);
}
@Nullable
@Override
@Nonnull
public ItemStack extract(@Nonnull ItemStack stack, int size, int flags, Action action) {
int remaining = size;
ItemStack received = null;
if (stack.isEmpty()) {
return stack;
}
IItemHandler handler = handlerSupplier.get();
if (handler == null) {
return null;
return ItemStack.EMPTY;
}
int remaining = size;
ItemStack received = ItemStack.EMPTY;
for (int i = 0; i < handler.getSlots(); ++i) {
ItemStack slot = handler.getStackInSlot(i);
@@ -110,7 +117,7 @@ public class ItemExternalStorage implements IExternalStorage<ItemStack> {
ItemStack got = handler.extractItem(i, remaining, action == Action.SIMULATE);
if (!got.isEmpty()) {
if (received == null) {
if (received.isEmpty()) {
received = got.copy();
} else {
received.grow(got.getCount());

View File

@@ -315,7 +315,7 @@ public class ControllerTile extends BaseTile implements ITickableTileEntity, INe
inserted += storage.getCacheDelta(storedPre, size, remainder);
}
if (remainder == null) {
if (remainder.isEmpty()) {
// The external storage is responsible for sending changes, we don't need to anymore
if (storage instanceof IExternalStorage && action == Action.PERFORM) {
((IExternalStorage) storage).update(this);
@@ -340,6 +340,11 @@ public class ControllerTile extends BaseTile implements ITickableTileEntity, INe
itemStorage.add(stack, inserted - insertedExternally, false, false);
}
// TODO Remove.
if (remainder.isEmpty()) {
remainder = null;
}
return remainder;
}
@@ -350,16 +355,16 @@ public class ControllerTile extends BaseTile implements ITickableTileEntity, INe
int extractedExternally = 0;
ItemStack newStack = null;
ItemStack newStack = ItemStack.EMPTY;
for (IStorage<ItemStack> storage : this.itemStorage.getStorages()) {
ItemStack took = null;
ItemStack took = ItemStack.EMPTY;
if (filter.test(storage) && storage.getAccessType() != AccessType.INSERT) {
took = storage.extract(stack, requested - received, flags, action);
}
if (took != null) {
if (!took.isEmpty()) {
// The external storage is responsible for sending changes, we don't need to anymore
if (storage instanceof IExternalStorage && action == Action.PERFORM) {
((IExternalStorage) storage).update(this);
@@ -367,7 +372,7 @@ public class ControllerTile extends BaseTile implements ITickableTileEntity, INe
extractedExternally += took.getCount();
}
if (newStack == null) {
if (newStack.isEmpty()) {
newStack = took;
} else {
newStack.grow(took.getCount());
@@ -381,10 +386,15 @@ public class ControllerTile extends BaseTile implements ITickableTileEntity, INe
}
}
if (newStack != null && newStack.getCount() - extractedExternally > 0 && action == Action.PERFORM) {
if (newStack.getCount() - extractedExternally > 0 && action == Action.PERFORM) {
itemStorage.remove(newStack, newStack.getCount() - extractedExternally, false);
}
// TODO Remove.
if (newStack.isEmpty()) {
newStack = null;
}
return newStack;
}
@@ -413,7 +423,7 @@ public class ControllerTile extends BaseTile implements ITickableTileEntity, INe
inserted += storage.getCacheDelta(storedPre, size, remainder);
}
if (remainder == null) {
if (remainder.isEmpty()) {
// The external storage is responsible for sending changes, we don't need to anymore
if (storage instanceof IExternalStorage && action == Action.PERFORM) {
((IExternalStorage) storage).update(this);
@@ -438,6 +448,11 @@ public class ControllerTile extends BaseTile implements ITickableTileEntity, INe
fluidStorage.add(stack, inserted - insertedExternally, false, false);
}
// TODO Remove.
if (remainder.isEmpty()) {
remainder = null;
}
return remainder;
}
@@ -448,16 +463,16 @@ public class ControllerTile extends BaseTile implements ITickableTileEntity, INe
int extractedExternally = 0;
FluidStack newStack = null;
FluidStack newStack = FluidStack.EMPTY;
for (IStorage<FluidStack> storage : this.fluidStorage.getStorages()) {
FluidStack took = null;
FluidStack took = FluidStack.EMPTY;
if (filter.test(storage) && storage.getAccessType() != AccessType.INSERT) {
took = storage.extract(stack, requested - received, flags, action);
}
if (took != null) {
if (!took.isEmpty()) {
// The external storage is responsible for sending changes, we don't need to anymore
if (storage instanceof IExternalStorage && action == Action.PERFORM) {
((IExternalStorage) storage).update(this);
@@ -465,7 +480,7 @@ public class ControllerTile extends BaseTile implements ITickableTileEntity, INe
extractedExternally += took.getAmount();
}
if (newStack == null) {
if (newStack.isEmpty()) {
newStack = took;
} else {
newStack.grow(took.getAmount());
@@ -479,10 +494,15 @@ public class ControllerTile extends BaseTile implements ITickableTileEntity, INe
}
}
if (newStack != null && newStack.getAmount() - extractedExternally > 0 && action == Action.PERFORM) {
if (newStack.getAmount() - extractedExternally > 0 && action == Action.PERFORM) {
fluidStorage.remove(newStack, newStack.getAmount() - extractedExternally, false);
}
// TODO Remove.
if (newStack.isEmpty()) {
newStack = null;
}
return newStack;
}