Make INetwork insertFluid/extractFluid return a NonNull FluidStack

This commit is contained in:
raoulvdberge
2019-10-16 19:07:31 +02:00
parent b9761e2539
commit 73ccc830c3
11 changed files with 56 additions and 75 deletions

View File

@@ -119,16 +119,16 @@ public interface ICraftingManager {
/**
* Tracks an incoming stack.
*
* @param stack the stack
* @param stack the stack, can be empty
*/
int track(@Nonnull ItemStack stack, int size);
/**
* Tracks an incoming stack.
*
* @param stack the stack
* @param stack the stack, can be empty
*/
int track(FluidStack stack, int size);
int track(@Nonnull FluidStack stack, int size);
/**
* @return a list of crafting patterns in this network, do NOT modify this list

View File

@@ -17,7 +17,6 @@ import net.minecraft.world.World;
import net.minecraftforge.fluids.FluidStack;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.function.Predicate;
/**
@@ -159,9 +158,9 @@ public interface INetwork {
* @param stack the stack prototype to insert, 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
FluidStack insertFluid(@Nonnull FluidStack stack, int size, Action action);
/**
@@ -169,14 +168,14 @@ public interface INetwork {
*
* @param stack the stack prototype to insert, do NOT modify
* @param size the amount of that prototype that has to be inserted
* @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
default FluidStack insertFluidTracked(@Nonnull FluidStack stack, int size) {
int remainder = getCraftingManager().track(stack, size);
if (remainder == 0) {
return null;
return FluidStack.EMPTY;
}
return insertFluid(stack, remainder, Action.PERFORM);
@@ -189,9 +188,9 @@ public interface INetwork {
* @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 a stack with the result
*/
@Nullable
@Nonnull
FluidStack extractFluid(@Nonnull FluidStack stack, int size, int flags, Action action, Predicate<IStorage<FluidStack>> filter);
/**
@@ -201,9 +200,9 @@ public interface INetwork {
* @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 a stack with the result
*/
@Nullable
@Nonnull
default FluidStack extractFluid(FluidStack stack, int size, int flags, Action action) {
return extractFluid(stack, size, flags, action, s -> true);
}
@@ -214,9 +213,9 @@ public interface INetwork {
* @param stack the prototype of the stack to extract, do NOT modify
* @param size the amount of that prototype that has to be extracted
* @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 a stack with the result
*/
@Nullable
@Nonnull
default FluidStack extractFluid(FluidStack stack, int size, Action action) {
return extractFluid(stack, size, IComparer.COMPARE_NBT, action);
}

View File

@@ -333,7 +333,11 @@ public class CraftingManager implements ICraftingManager {
}
@Override
public int track(FluidStack stack, int size) {
public int track(@Nonnull FluidStack stack, int size) {
if (stack.isEmpty()) {
return 0;
}
for (ICraftingTask task : tasks.values()) {
size = task.onTrackedInsert(stack, size);

View File

@@ -568,7 +568,7 @@ public class CraftingTask implements ICraftingTask {
for (StackListEntry<FluidStack> toExtract : toExtractInitialFluids.getStacks()) {
FluidStack result = network.extractFluid(toExtract.getStack(), toExtract.getStack().getAmount(), Action.PERFORM);
if (result != null) {
if (!result.isEmpty()) {
internalFluidStorage.insert(toExtract.getStack(), result.getAmount(), Action.PERFORM);
toRemove.add(result);
@@ -856,13 +856,7 @@ public class CraftingTask implements ICraftingTask {
for (FluidStack stack : internalFluidStorage.getStacks()) {
FluidStack remainder = network.insertFluid(stack, stack.getAmount(), Action.PERFORM);
toPerform.add(() -> {
if (remainder == null) {
internalFluidStorage.extract(stack, stack.getAmount(), IComparer.COMPARE_NBT, Action.PERFORM);
} else {
internalFluidStorage.extract(stack, stack.getAmount() - remainder.getAmount(), IComparer.COMPARE_NBT, Action.PERFORM);
}
});
toPerform.add(() -> internalFluidStorage.extract(stack, stack.getAmount() - remainder.getAmount(), IComparer.COMPARE_NBT, Action.PERFORM));
}
// Prevent CME.
@@ -993,9 +987,7 @@ public class CraftingTask implements ICraftingTask {
} else {
FluidStack remainder = network.insertFluid(stack, needed, Action.PERFORM);
if (remainder != null) {
internalFluidStorage.insert(stack, needed, Action.PERFORM);
}
internalFluidStorage.insert(remainder, remainder.getAmount(), Action.PERFORM);
}
if (size == 0) {

View File

@@ -61,9 +61,7 @@ public class FluidGridHandler implements IFluidGridHandler {
FluidStack extracted = network.extractFluid(stack, FluidAttributes.BUCKET_VOLUME, Action.PERFORM);
if (extracted != null) {
fluidHandler.fill(extracted, IFluidHandler.FluidAction.EXECUTE);
}
fluidHandler.fill(extracted, IFluidHandler.FluidAction.EXECUTE);
if (shift) {
if (!player.inventory.addItemStackToInventory(fluidHandler.getContainer().copy())) {
@@ -88,7 +86,7 @@ public class FluidGridHandler implements IFluidGridHandler {
Pair<ItemStack, FluidStack> result = StackUtils.getFluid(container, true);
if (!result.getValue().isEmpty() && network.insertFluid(result.getValue(), result.getValue().getAmount(), Action.SIMULATE) == null) {
if (!result.getValue().isEmpty() && network.insertFluid(result.getValue(), result.getValue().getAmount(), Action.SIMULATE).isEmpty()) {
network.getFluidStorageTracker().changed(player, result.getValue().copy());
result = StackUtils.getFluid(container, false);

View File

@@ -122,14 +122,12 @@ public class NetworkNodeExporter extends NetworkNode implements IComparable, ITy
FluidStack took = network.extractFluid(stack, toExtract, compare, Action.SIMULATE);
if (took != null) {
int filled = handler.fill(took, IFluidHandler.FluidAction.SIMULATE);
int filled = handler.fill(took, IFluidHandler.FluidAction.SIMULATE);
if (filled > 0) {
took = network.extractFluid(stack, filled, compare, Action.PERFORM);
if (filled > 0) {
took = network.extractFluid(stack, filled, compare, Action.PERFORM);
handler.fill(took, IFluidHandler.FluidAction.EXECUTE);
}
handler.fill(took, IFluidHandler.FluidAction.EXECUTE);
}
} else if (upgrades.hasUpgrade(UpgradeItem.Type.CRAFTING)) {
network.getCraftingManager().request(this, stack, toExtract);

View File

@@ -87,12 +87,10 @@ public class NetworkNodeFluidInterface extends NetworkNode {
FluidStack drained = tankIn.drain(FluidAttributes.BUCKET_VOLUME * upgrades.getItemInteractCount(), IFluidHandler.FluidAction.EXECUTE);
// Drain in tank
if (drained != null) {
if (!drained.isEmpty()) {
FluidStack remainder = network.insertFluidTracked(drained, drained.getAmount());
if (remainder != null) {
tankIn.fill(remainder, IFluidHandler.FluidAction.EXECUTE);
}
tankIn.fill(remainder, IFluidHandler.FluidAction.EXECUTE);
}
}
@@ -126,8 +124,8 @@ public class NetworkNodeFluidInterface extends NetworkNode {
return !(s instanceof FluidExternalStorage) || !((FluidExternalStorage) s).isConnectedToInterface();
});
if (result != null) {
if (tankOut.getFluid() == null) {
if (!result.isEmpty()) {
if (tankOut.getFluid().isEmpty()) {
tankOut.setFluid(result);
} else {
tankOut.getFluid().grow(result.getAmount());
@@ -138,7 +136,7 @@ public class NetworkNodeFluidInterface extends NetworkNode {
// Example: our delta is 5, we extracted 3 fluids.
// That means we still have to autocraft 2 fluids.
delta -= result == null ? 0 : result.getAmount();
delta -= result.getAmount();
if (delta > 0 && upgrades.hasUpgrade(UpgradeItem.Type.CRAFTING)) {
network.getCraftingManager().request(this, wanted, delta);
@@ -146,11 +144,7 @@ public class NetworkNodeFluidInterface extends NetworkNode {
} else if (delta < 0) {
FluidStack remainder = network.insertFluidTracked(got, Math.abs(delta));
if (remainder == null) {
tankOut.getFluid().shrink(Math.abs(delta));
} else {
tankOut.getFluid().shrink(Math.abs(delta) - remainder.getAmount());
}
tankOut.getFluid().shrink(Math.abs(delta) - remainder.getAmount());
onTankOutChanged();
}

View File

@@ -101,15 +101,15 @@ public class NetworkNodeImporter extends NetworkNode implements IComparable, IWh
if (handler != null) {
FluidStack stack = handler.drain(FluidAttributes.BUCKET_VOLUME, IFluidHandler.FluidAction.SIMULATE);
if (stack != null && IWhitelistBlacklist.acceptsFluid(fluidFilters, mode, compare, stack) && network.insertFluid(stack, stack.getAmount(), Action.SIMULATE) == null) {
if (!stack.isEmpty() &&
IWhitelistBlacklist.acceptsFluid(fluidFilters, mode, compare, stack) &&
network.insertFluid(stack, stack.getAmount(), Action.SIMULATE).isEmpty()) {
FluidStack toDrain = handler.drain(FluidAttributes.BUCKET_VOLUME * upgrades.getItemInteractCount(), IFluidHandler.FluidAction.EXECUTE); // TODO: is this execute?
if (toDrain != null) {
if (!toDrain.isEmpty()) {
FluidStack remainder = network.insertFluidTracked(toDrain, toDrain.getAmount());
if (remainder != null) {
toDrain.shrink(remainder.getAmount());
}
toDrain.shrink(remainder.getAmount());
handler.drain(toDrain, IFluidHandler.FluidAction.EXECUTE);
}

View File

@@ -114,7 +114,7 @@ public class NetworkNodeInterface extends NetworkNode implements IComparable {
// Example: our delta is 5, we extracted 3 items.
// That means we still have to autocraft 2 items.
delta -= result.isEmpty() ? 0 : result.getCount();
delta -= result.getCount();
if (delta > 0 && upgrades.hasUpgrade(UpgradeItem.Type.CRAFTING)) {
network.getCraftingManager().request(new SlottedCraftingRequest(this, i), wanted, delta);

View File

@@ -289,9 +289,7 @@ public class NetworkNodeDiskManipulator extends NetworkNode implements IComparab
FluidStack remainder = network.insertFluid(extracted, extracted.getAmount(), Action.PERFORM);
if (remainder != null) {
storage.insert(remainder, remainder.getAmount(), Action.PERFORM);
}
storage.insert(remainder, remainder.getAmount(), Action.PERFORM);
}
private boolean isFluidDiskDone(IStorageDisk<FluidStack> storage, int slot) {
@@ -319,7 +317,7 @@ public class NetworkNodeDiskManipulator extends NetworkNode implements IComparab
}
FluidStack remainder = network.insertFluid(extracted, extracted.getAmount(), Action.SIMULATE);
if (remainder == null) { // A fluid could be inserted (no remainders when trying to). This disk isn't done.
if (remainder.isEmpty()) { // A fluid could be inserted (no remainders when trying to). This disk isn't done.
return false;
}
}
@@ -327,7 +325,7 @@ public class NetworkNodeDiskManipulator extends NetworkNode implements IComparab
}
private void extractFluidFromNetwork(IStorageDisk<FluidStack> storage, int slot) {
FluidStack extracted = null;
FluidStack extracted = FluidStack.EMPTY;
int i = 0;
if (fluidFilters.isEmpty()) {
@@ -357,16 +355,14 @@ public class NetworkNodeDiskManipulator extends NetworkNode implements IComparab
}
}
if (extracted == null) {
if (extracted.isEmpty()) {
moveDriveToOutput(slot);
return;
}
FluidStack remainder = storage.insert(extracted, extracted.getAmount(), Action.PERFORM);
if (!remainder.isEmpty()) {
network.insertFluid(remainder, remainder.getAmount(), Action.PERFORM);
}
network.insertFluid(remainder, remainder.getAmount(), Action.PERFORM);
}
private void moveDriveToOutput(int slot) {

View File

@@ -400,8 +400,13 @@ public class ControllerTile extends BaseTile implements ITickableTileEntity, INe
@Override
@Nonnull
public FluidStack insertFluid(@Nonnull FluidStack stack, int size, Action action) {
if (stack == null || fluidStorage.getStorages().isEmpty()) {
if (stack.isEmpty()) {
return stack;
}
if (fluidStorage.getStorages().isEmpty()) {
return StackUtils.copy(stack, size);
}
@@ -448,16 +453,16 @@ public class ControllerTile extends BaseTile implements ITickableTileEntity, INe
fluidStorage.add(stack, inserted - insertedExternally, false, false);
}
// TODO Remove.
if (remainder.isEmpty()) {
remainder = null;
}
return remainder;
}
@Override
@Nonnull
public FluidStack extractFluid(@Nonnull FluidStack stack, int size, int flags, Action action, Predicate<IStorage<FluidStack>> filter) {
if (stack.isEmpty()) {
return stack;
}
int requested = size;
int received = 0;
@@ -498,11 +503,6 @@ public class ControllerTile extends BaseTile implements ITickableTileEntity, INe
fluidStorage.remove(newStack, newStack.getAmount() - extractedExternally, false);
}
// TODO Remove.
if (newStack.isEmpty()) {
newStack = null;
}
return newStack;
}