Make INetwork insertItem/extractItem return a NonNull ItemStack

This commit is contained in:
raoulvdberge
2019-10-16 18:41:44 +02:00
parent 7c50c49aaf
commit b9761e2539
24 changed files with 210 additions and 242 deletions

View File

@@ -121,7 +121,7 @@ public interface ICraftingManager {
*
* @param stack the stack
*/
int track(ItemStack stack, int size);
int track(@Nonnull ItemStack stack, int size);
/**
* Tracks an incoming stack.

View File

@@ -87,27 +87,27 @@ public interface INetwork {
/**
* Inserts an item in this network.
*
* @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
ItemStack insertItem(@Nonnull ItemStack stack, int size, Action action);
/**
* Inserts an item and notifies the crafting manager of the incoming item.
*
* @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
* @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 ItemStack insertItemTracked(@Nonnull ItemStack stack, int size) {
int remainder = getCraftingManager().track(stack, size);
if (remainder == 0) {
return null;
return ItemStack.EMPTY;
}
return insertItem(stack, remainder, Action.PERFORM);
@@ -121,9 +121,9 @@ public interface INetwork {
* @param flags the flags to compare on, see {@link IComparer}
* @param action the action
* @param filter a filter for the storage
* @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
ItemStack extractItem(@Nonnull ItemStack stack, int size, int flags, Action action, Predicate<IStorage<ItemStack>> filter);
/**
@@ -133,9 +133,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 ItemStack extractItem(@Nonnull ItemStack stack, int size, int flags, Action action) {
return extractItem(stack, size, flags, action, s -> true);
}
@@ -146,9 +146,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 ItemStack extractItem(@Nonnull ItemStack stack, int size, Action action) {
return extractItem(stack, size, IComparer.COMPARE_NBT, action);
}

View File

@@ -1,6 +1,7 @@
package com.raoulvdberge.refinedstorage.api.network.grid;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.ICraftingRecipe;
/**
@@ -8,7 +9,7 @@ import net.minecraft.item.crafting.ICraftingRecipe;
*/
public interface ICraftingGridBehavior {
/**
* Default logic for regular crafting.
* Logic for regular crafting.
*
* @param grid the grid
* @param recipe the recipe
@@ -17,10 +18,19 @@ public interface ICraftingGridBehavior {
void onCrafted(IGridNetworkAware grid, ICraftingRecipe recipe, PlayerEntity player);
/**
* Default logic for crafting with shift click (mass crafting).
* Logic for crafting with shift click (mass crafting).
*
* @param grid the grid
* @param player the layer
* @param player the player
*/
void onCraftedShift(IGridNetworkAware grid, PlayerEntity player);
/**
* Logic for when a recipe is transferred to the grid.
*
* @param grid the grid
* @param player the player
* @param recipe the recipe
*/
void onRecipeTransfer(IGridNetworkAware grid, PlayerEntity player, ItemStack[][] recipe);
}

View File

@@ -3,7 +3,7 @@ package com.raoulvdberge.refinedstorage.api.network.grid.handler;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.ItemStack;
import javax.annotation.Nullable;
import javax.annotation.Nonnull;
import java.util.UUID;
/**
@@ -24,9 +24,9 @@ public interface IFluidGridHandler {
*
* @param player the player
* @param container a stack with a fluid container we're trying to insert
* @return the remainder, or null if there is no remainder
* @return the remainder, or an empty stack if there is no remainder
*/
@Nullable
@Nonnull
ItemStack onInsert(ServerPlayerEntity player, ItemStack container);
/**
@@ -43,6 +43,7 @@ public interface IFluidGridHandler {
* @param container the container
* @return the remainder container
*/
@Nonnull
ItemStack onShiftClick(ServerPlayerEntity player, ItemStack container);
/**

View File

@@ -29,7 +29,7 @@ public interface IItemGridHandler {
*
* @param player the player that is attempting the insert
* @param stack the item we're trying to insert
* @return the remainder, or null if there is no remainder
* @return the remainder, or an empty stack if there is no remainder
*/
@Nonnull
ItemStack onInsert(ServerPlayerEntity player, ItemStack stack);
@@ -47,7 +47,7 @@ public interface IItemGridHandler {
*
* @param player the player
* @param stack the stack
* @return the remainder stack
* @return the remainder, or an empty stack if there is no remainder
*/
// TODO Maybe remove?
@Nonnull

View File

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

View File

@@ -44,7 +44,7 @@ public class CraftingPattern implements ICraftingPattern {
for (int i = 0; i < 9; ++i) {
ItemStack input = PatternItem.getInputSlot(stack, i);
if (input == null) {
if (input.isEmpty()) {
inputs.add(NonNullList.create());
} else if (oredict) {
NonNullList<ItemStack> ores = NonNullList.create();
@@ -75,7 +75,7 @@ public class CraftingPattern implements ICraftingPattern {
}
ItemStack output = PatternItem.getOutputSlot(stack, i);
if (output != null) {
if (!output.isEmpty()) {
this.valid = true; // As soon as we have one output, we are valid.
outputs.add(output);
@@ -101,11 +101,9 @@ public class CraftingPattern implements ICraftingPattern {
for (int i = 0; i < 9; ++i) {
ItemStack input = PatternItem.getInputSlot(stack, i);
inputs.add(input == null ? NonNullList.create() : NonNullList.from(ItemStack.EMPTY, input));
inputs.add(input.isEmpty() ? NonNullList.create() : NonNullList.from(ItemStack.EMPTY, input));
if (input != null) {
inv.setInventorySlotContents(i, input);
}
inv.setInventorySlotContents(i, input);
}
Optional<ICraftingRecipe> potentialRecipe = world.getRecipeManager().getRecipe(IRecipeType.CRAFTING, inv, world);

View File

@@ -546,7 +546,7 @@ public class CraftingTask implements ICraftingTask {
for (StackListEntry<ItemStack> toExtract : toExtractInitial.getStacks()) {
ItemStack result = network.extractItem(toExtract.getStack(), toExtract.getStack().getCount(), Action.PERFORM);
if (result != null) {
if (!result.isEmpty()) {
internalStorage.insert(toExtract.getStack(), result.getCount(), Action.PERFORM);
toRemove.add(result);
@@ -634,9 +634,7 @@ public class CraftingTask implements ICraftingTask {
} else {
ItemStack remainder = this.network.insertItem(output, output.getCount(), Action.PERFORM);
if (remainder != null) {
this.internalStorage.insert(remainder, remainder.getCount(), Action.PERFORM);
}
this.internalStorage.insert(remainder, remainder.getCount(), Action.PERFORM);
}
// Byproducts need to always be inserted in the internal storage for later reuse further in the task.
@@ -852,13 +850,7 @@ public class CraftingTask implements ICraftingTask {
for (ItemStack stack : internalStorage.getStacks()) {
ItemStack remainder = network.insertItem(stack, stack.getCount(), Action.PERFORM);
toPerform.add(() -> {
if (remainder == null) {
internalStorage.extract(stack, stack.getCount(), IComparer.COMPARE_NBT, Action.PERFORM);
} else {
internalStorage.extract(stack, stack.getCount() - remainder.getCount(), IComparer.COMPARE_NBT, Action.PERFORM);
}
});
toPerform.add(() -> internalStorage.extract(stack, stack.getCount() - remainder.getCount(), IComparer.COMPARE_NBT, Action.PERFORM));
}
for (FluidStack stack : internalFluidStorage.getStacks()) {
@@ -960,9 +952,7 @@ public class CraftingTask implements ICraftingTask {
} else {
ItemStack remainder = network.insertItem(stack, needed, Action.PERFORM);
if (remainder != null) {
internalStorage.insert(stack, needed, Action.PERFORM);
}
internalStorage.insert(remainder, remainder.getCount(), Action.PERFORM);
}
if (size == 0) {

View File

@@ -1,11 +1,13 @@
package com.raoulvdberge.refinedstorage.apiimpl.network.grid;
import com.raoulvdberge.refinedstorage.api.network.INetwork;
import com.raoulvdberge.refinedstorage.api.network.grid.GridType;
import com.raoulvdberge.refinedstorage.api.network.grid.ICraftingGridBehavior;
import com.raoulvdberge.refinedstorage.api.network.grid.IGridNetworkAware;
import com.raoulvdberge.refinedstorage.api.network.security.Permission;
import com.raoulvdberge.refinedstorage.api.util.Action;
import com.raoulvdberge.refinedstorage.api.util.IComparer;
import com.raoulvdberge.refinedstorage.apiimpl.API;
import com.raoulvdberge.refinedstorage.util.StackUtils;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.CraftingInventory;
import net.minecraft.inventory.InventoryHelper;
@@ -38,7 +40,7 @@ public class CraftingGridBehavior implements ICraftingGridBehavior {
ItemStack remainderStack = network == null ? remainder.get(i).copy() : network.insertItem(remainder.get(i).copy(), remainder.get(i).getCount(), Action.PERFORM);
// If there is no space in the network, just dump it in the world.
if (remainderStack != null) {
if (!remainderStack.isEmpty()) {
InventoryHelper.spawnItemStack(player.getEntityWorld(), player.getPosition().getX(), player.getPosition().getY(), player.getPosition().getZ(), remainderStack);
}
}
@@ -49,7 +51,7 @@ public class CraftingGridBehavior implements ICraftingGridBehavior {
}
} else if (!slot.isEmpty()) { // We don't have a remainder, but the slot is not empty.
if (slot.getCount() == 1 && network != null) { // Attempt to refill the slot with the same item from the network, only if we have a network and only if it's the last item.
ItemStack refill = StackUtils.nullToEmpty(network.extractItem(slot, 1, Action.PERFORM));
ItemStack refill = network.extractItem(slot, 1, Action.PERFORM);
matrix.setInventorySlotContents(i, refill);
@@ -86,7 +88,7 @@ public class CraftingGridBehavior implements ICraftingGridBehavior {
if (!player.inventory.addItemStackToInventory(craftedItem.copy())) {
ItemStack remainder = network == null ? craftedItem : network.insertItem(craftedItem, craftedItem.getCount(), Action.PERFORM);
if (remainder != null) {
if (!remainder.isEmpty()) {
InventoryHelper.spawnItemStack(player.getEntityWorld(), player.getPosition().getX(), player.getPosition().getY(), player.getPosition().getZ(), remainder);
}
}
@@ -98,4 +100,94 @@ public class CraftingGridBehavior implements ICraftingGridBehavior {
crafted.onCrafting(player.world, player, amountCrafted);
BasicEventHooks.firePlayerCraftingEvent(player, ItemHandlerHelper.copyStackWithSize(crafted, amountCrafted), grid.getCraftingMatrix());
}
@Override
public void onRecipeTransfer(IGridNetworkAware grid, PlayerEntity player, ItemStack[][] recipe) {
INetwork network = grid.getNetwork();
if (network != null && grid.getGridType() == GridType.CRAFTING && !network.getSecurityManager().hasPermission(Permission.EXTRACT, player)) {
return;
}
// First try to empty the crafting matrix
for (int i = 0; i < grid.getCraftingMatrix().getSizeInventory(); ++i) {
ItemStack slot = grid.getCraftingMatrix().getStackInSlot(i);
if (!slot.isEmpty()) {
// Only if we are a crafting grid. Pattern grids can just be emptied.
if (grid.getGridType() == GridType.CRAFTING) {
// If we are connected, try to insert into network. If it fails, stop.
if (network != null) {
if (!network.insertItem(slot, slot.getCount(), Action.SIMULATE).isEmpty()) {
return;
} else {
network.insertItem(slot, slot.getCount(), Action.PERFORM);
network.getItemStorageTracker().changed(player, slot.copy());
}
} else {
// If we aren't connected, try to insert into player inventory. If it fails, stop.
if (!player.inventory.addItemStackToInventory(slot.copy())) {
return;
}
}
}
grid.getCraftingMatrix().setInventorySlotContents(i, ItemStack.EMPTY);
}
}
// Now let's fill the matrix
for (int i = 0; i < grid.getCraftingMatrix().getSizeInventory(); ++i) {
if (recipe[i] != null) {
ItemStack[] possibilities = recipe[i];
// If we are a crafting grid
if (grid.getGridType() == GridType.CRAFTING) {
boolean found = false;
// If we are connected, first try to get the possibilities from the network
if (network != null) {
for (ItemStack possibility : possibilities) {
ItemStack took = network.extractItem(possibility, 1, IComparer.COMPARE_NBT, Action.PERFORM);
if (!took.isEmpty()) {
grid.getCraftingMatrix().setInventorySlotContents(i, took);
network.getItemStorageTracker().changed(player, took.copy());
found = true;
break;
}
}
}
// If we haven't found anything in the network (or we are disconnected), go look in the player inventory
if (!found) {
for (ItemStack possibility : possibilities) {
for (int j = 0; j < player.inventory.getSizeInventory(); ++j) {
if (API.instance().getComparer().isEqual(possibility, player.inventory.getStackInSlot(j), IComparer.COMPARE_NBT)) {
grid.getCraftingMatrix().setInventorySlotContents(i, ItemHandlerHelper.copyStackWithSize(player.inventory.getStackInSlot(j), 1));
player.inventory.decrStackSize(j, 1);
found = true;
break;
}
}
if (found) {
break;
}
}
}
} else if (grid.getGridType() == GridType.PATTERN) {
// If we are a pattern grid we can just set the slot
grid.getCraftingMatrix().setInventorySlotContents(i, possibilities.length == 0 ? ItemStack.EMPTY : possibilities[0]);
}
}
}
}
}

View File

@@ -19,7 +19,7 @@ import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler;
import org.apache.commons.lang3.tuple.Pair;
import javax.annotation.Nullable;
import javax.annotation.Nonnull;
import java.util.UUID;
public class FluidGridHandler implements IFluidGridHandler {
@@ -52,7 +52,7 @@ public class FluidGridHandler implements IFluidGridHandler {
}
if (bucket.isEmpty()) {
bucket = StackUtils.nullToEmpty(network.extractItem(StackUtils.EMPTY_BUCKET, 1, Action.PERFORM));
bucket = network.extractItem(StackUtils.EMPTY_BUCKET, 1, Action.PERFORM);
}
if (!bucket.isEmpty()) {
@@ -79,8 +79,8 @@ public class FluidGridHandler implements IFluidGridHandler {
}
}
@Nullable
@Override
@Nonnull
public ItemStack onInsert(ServerPlayerEntity player, ItemStack container) {
if (!network.getSecurityManager().hasPermission(Permission.INSERT, player)) {
return container;
@@ -105,13 +105,14 @@ public class FluidGridHandler implements IFluidGridHandler {
@Override
public void onInsertHeldContainer(ServerPlayerEntity player) {
player.inventory.setItemStack(StackUtils.nullToEmpty(onInsert(player, player.inventory.getItemStack())));
player.inventory.setItemStack(onInsert(player, player.inventory.getItemStack()));
player.updateHeldItem();
}
@Override
@Nonnull
public ItemStack onShiftClick(ServerPlayerEntity player, ItemStack container) {
return StackUtils.nullToEmpty(onInsert(player, container));
return onInsert(player, container);
}
@Override

View File

@@ -10,7 +10,6 @@ import com.raoulvdberge.refinedstorage.api.network.security.Permission;
import com.raoulvdberge.refinedstorage.api.util.Action;
import com.raoulvdberge.refinedstorage.api.util.IStackList;
import com.raoulvdberge.refinedstorage.apiimpl.API;
import com.raoulvdberge.refinedstorage.util.StackUtils;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Direction;
@@ -78,21 +77,19 @@ public class ItemGridHandler implements IItemGridHandler {
ItemStack took = network.extractItem(item, size, 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);
if (playerInventory != null && ItemHandlerHelper.insertItem(playerInventory, took, true).isEmpty()) {
took = network.extractItem(item, size, Action.PERFORM);
if (took != null) {
ItemHandlerHelper.insertItem(playerInventory, took, false);
}
ItemHandlerHelper.insertItem(playerInventory, took, false);
}
} else {
took = network.extractItem(item, size, Action.PERFORM);
if (took != null) {
if (!took.isEmpty()) {
if (single && !held.isEmpty()) {
held.grow(1);
} else {
@@ -116,7 +113,7 @@ public class ItemGridHandler implements IItemGridHandler {
network.getItemStorageTracker().changed(player, stack.copy());
ItemStack remainder = StackUtils.nullToEmpty(network.insertItem(stack, stack.getCount(), Action.PERFORM));
ItemStack remainder = network.insertItem(stack, stack.getCount(), Action.PERFORM);
// TODO network.getNetworkItemHandler().drainEnergy(player, RS.INSTANCE.config.wirelessGridInsertUsage);
@@ -135,17 +132,18 @@ public class ItemGridHandler implements IItemGridHandler {
network.getItemStorageTracker().changed(player, stack.copy());
if (single) {
if (network.insertItem(stack, size, Action.SIMULATE) == null) {
if (network.insertItem(stack, size, Action.SIMULATE).isEmpty()) {
network.insertItem(stack, size, Action.PERFORM);
stack.shrink(size);
if (stack.getCount() == 0) {
// TODO Is this still needed?
if (stack.isEmpty()) {
player.inventory.setItemStack(ItemStack.EMPTY);
}
}
} else {
player.inventory.setItemStack(StackUtils.nullToEmpty(network.insertItem(stack, size, Action.PERFORM)));
player.inventory.setItemStack(network.insertItem(stack, size, Action.PERFORM));
}
player.updateHeldItem();

View File

@@ -16,7 +16,7 @@ import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler;
import org.apache.commons.lang3.tuple.Pair;
import javax.annotation.Nullable;
import javax.annotation.Nonnull;
import java.util.UUID;
public class PortableFluidGridHandler implements IFluidGridHandler {
@@ -68,8 +68,8 @@ public class PortableFluidGridHandler implements IFluidGridHandler {
}
}
@Nullable
@Override
@Nonnull
public ItemStack onInsert(ServerPlayerEntity player, ItemStack container) {
Pair<ItemStack, FluidStack> result = StackUtils.getFluid(container, true);
@@ -90,13 +90,14 @@ public class PortableFluidGridHandler implements IFluidGridHandler {
@Override
public void onInsertHeldContainer(ServerPlayerEntity player) {
player.inventory.setItemStack(StackUtils.nullToEmpty(onInsert(player, player.inventory.getItemStack())));
player.inventory.setItemStack(onInsert(player, player.inventory.getItemStack()));
player.updateHeldItem();
}
@Override
@Nonnull
public ItemStack onShiftClick(ServerPlayerEntity player, ItemStack container) {
return StackUtils.nullToEmpty(onInsert(player, container));
return onInsert(player, container);
}
@Override

View File

@@ -139,7 +139,7 @@ public class PortableItemGridHandler implements IItemGridHandler {
stack.shrink(size);
// TODO ???
if (stack.getCount() == 0) {
if (stack.isEmpty()) {
player.inventory.setItemStack(ItemStack.EMPTY);
}
}

View File

@@ -2,7 +2,6 @@ package com.raoulvdberge.refinedstorage.apiimpl.network.node;
import com.raoulvdberge.refinedstorage.RS;
import com.raoulvdberge.refinedstorage.RSItems;
import com.raoulvdberge.refinedstorage.api.network.INetwork;
import com.raoulvdberge.refinedstorage.api.network.grid.*;
import com.raoulvdberge.refinedstorage.api.network.grid.handler.IFluidGridHandler;
import com.raoulvdberge.refinedstorage.api.network.grid.handler.IItemGridHandler;
@@ -10,7 +9,6 @@ import com.raoulvdberge.refinedstorage.api.network.security.Permission;
import com.raoulvdberge.refinedstorage.api.storage.cache.IStorageCache;
import com.raoulvdberge.refinedstorage.api.storage.cache.IStorageCacheListener;
import com.raoulvdberge.refinedstorage.api.util.Action;
import com.raoulvdberge.refinedstorage.api.util.IComparer;
import com.raoulvdberge.refinedstorage.api.util.IFilter;
import com.raoulvdberge.refinedstorage.apiimpl.API;
import com.raoulvdberge.refinedstorage.apiimpl.storage.cache.listener.FluidGridStorageCacheListener;
@@ -47,7 +45,6 @@ import net.minecraftforge.fluids.FluidAttributes;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.IItemHandlerModifiable;
import net.minecraftforge.items.ItemHandlerHelper;
import net.minecraftforge.items.wrapper.CombinedInvWrapper;
import net.minecraftforge.items.wrapper.InvWrapper;
@@ -110,17 +107,17 @@ public class GridNetworkNode extends NetworkNode implements IGridNetworkAware, I
if (isPatternProcessing && isProcessingPattern()) {
for (int i = 0; i < 9; ++i) {
processingMatrix.setStackInSlot(i, StackUtils.nullToEmpty(PatternItem.getInputSlot(pattern, i)));
processingMatrix.setStackInSlot(i, PatternItem.getInputSlot(pattern, i));
processingMatrixFluids.setFluid(i, PatternItem.getFluidInputSlot(pattern, i));
}
for (int i = 0; i < 9; ++i) {
processingMatrix.setStackInSlot(9 + i, StackUtils.nullToEmpty(PatternItem.getOutputSlot(pattern, i)));
processingMatrix.setStackInSlot(9 + i, PatternItem.getOutputSlot(pattern, i));
processingMatrixFluids.setFluid(9 + i, PatternItem.getFluidOutputSlot(pattern, i));
}
} else if (!isPatternProcessing && !isProcessingPattern()) {
for (int i = 0; i < 9; ++i) {
matrix.setInventorySlotContents(i, StackUtils.nullToEmpty(PatternItem.getInputSlot(pattern, i)));
matrix.setInventorySlotContents(i, PatternItem.getInputSlot(pattern, i));
}
}
}
@@ -341,96 +338,7 @@ public class GridNetworkNode extends NetworkNode implements IGridNetworkAware, I
@Override
public void onRecipeTransfer(PlayerEntity player, ItemStack[][] recipe) {
onRecipeTransfer(this, player, recipe);
}
public static void onRecipeTransfer(IGridNetworkAware grid, PlayerEntity player, ItemStack[][] recipe) {
INetwork network = grid.getNetwork();
if (network != null && grid.getGridType() == GridType.CRAFTING && !network.getSecurityManager().hasPermission(Permission.EXTRACT, player)) {
return;
}
// First try to empty the crafting matrix
for (int i = 0; i < grid.getCraftingMatrix().getSizeInventory(); ++i) {
ItemStack slot = grid.getCraftingMatrix().getStackInSlot(i);
if (!slot.isEmpty()) {
// Only if we are a crafting grid. Pattern grids can just be emptied.
if (grid.getGridType() == GridType.CRAFTING) {
// If we are connected, try to insert into network. If it fails, stop.
if (network != null) {
if (network.insertItem(slot, slot.getCount(), Action.SIMULATE) != null) {
return;
} else {
network.insertItem(slot, slot.getCount(), Action.PERFORM);
network.getItemStorageTracker().changed(player, slot.copy());
}
} else {
// If we aren't connected, try to insert into player inventory. If it fails, stop.
if (!player.inventory.addItemStackToInventory(slot.copy())) {
return;
}
}
}
grid.getCraftingMatrix().setInventorySlotContents(i, ItemStack.EMPTY);
}
}
// Now let's fill the matrix
for (int i = 0; i < grid.getCraftingMatrix().getSizeInventory(); ++i) {
if (recipe[i] != null) {
ItemStack[] possibilities = recipe[i];
// If we are a crafting grid
if (grid.getGridType() == GridType.CRAFTING) {
boolean found = false;
// If we are connected, first try to get the possibilities from the network
if (network != null) {
for (ItemStack possibility : possibilities) {
ItemStack took = network.extractItem(possibility, 1, IComparer.COMPARE_NBT, Action.PERFORM);
if (took != null) {
grid.getCraftingMatrix().setInventorySlotContents(i, StackUtils.nullToEmpty(took));
network.getItemStorageTracker().changed(player, took.copy());
found = true;
break;
}
}
}
// If we haven't found anything in the network (or we are disconnected), go look in the player inventory
if (!found) {
for (ItemStack possibility : possibilities) {
for (int j = 0; j < player.inventory.getSizeInventory(); ++j) {
if (API.instance().getComparer().isEqual(possibility, player.inventory.getStackInSlot(j), IComparer.COMPARE_NBT)) {
grid.getCraftingMatrix().setInventorySlotContents(i, ItemHandlerHelper.copyStackWithSize(player.inventory.getStackInSlot(j), 1));
player.inventory.decrStackSize(j, 1);
found = true;
break;
}
}
if (found) {
break;
}
}
}
} else if (grid.getGridType() == GridType.PATTERN) {
// If we are a pattern grid we can just set the slot
grid.getCraftingMatrix().setInventorySlotContents(i, possibilities.length == 0 ? ItemStack.EMPTY : possibilities[0]);
}
}
}
API.instance().getCraftingGridBehavior().onRecipeTransfer(this, player, recipe);
}
public void clearMatrix() {
@@ -469,7 +377,7 @@ public class GridNetworkNode extends NetworkNode implements IGridNetworkAware, I
ItemStack slot = matrix.getStackInSlot(i);
if (!slot.isEmpty()) {
matrix.setInventorySlotContents(i, StackUtils.nullToEmpty(network.insertItem(slot, slot.getCount(), Action.PERFORM)));
matrix.setInventorySlotContents(i, network.insertItem(slot, slot.getCount(), Action.PERFORM));
network.getItemStorageTracker().changed(player, slot.copy());
}

View File

@@ -88,7 +88,7 @@ public class NetworkNodeConstructor extends NetworkNode implements IComparable,
if (item.getItem() == Items.FIREWORK_ROCKET && !drop) {
ItemStack took = network.extractItem(item, 1, Action.PERFORM);
if (took != null) {
if (!took.isEmpty()) {
world.addEntity(new FireworkRocketEntity(world, getDispensePositionX(), getDispensePositionY(), getDispensePositionZ(), took));
}
} else {
@@ -164,7 +164,7 @@ public class NetworkNodeConstructor extends NetworkNode implements IComparable,
ItemStack took = network.extractItem(item, 1, compare, Action.SIMULATE);
if (took != null) {
if (!took.isEmpty()) {
BlockState state = FilterSlot.getBlockState(world, front, took);
// TODO if (state != null && world.isAirBlock(front) && state.getBlock().canPlaceBlockAt(world, front)) {
@@ -177,7 +177,7 @@ public class NetworkNodeConstructor extends NetworkNode implements IComparable,
took = network.extractItem(item, 1, compare, Action.PERFORM);
if (took != null) {
if (!took.isEmpty()) {
if (item.getItem() instanceof BlockItem) {
/*((BlockItem) item.getItem()).tryPlace(new BlockItemUseContext(
took,
@@ -242,7 +242,7 @@ public class NetworkNodeConstructor extends NetworkNode implements IComparable,
private void dropItem() {
ItemStack took = network.extractItem(itemFilters.getStackInSlot(0), upgrades.getItemInteractCount(), Action.PERFORM);
if (took != null) {
if (!took.isEmpty()) {
DefaultDispenseItemBehavior.doDispense(world, took, 6, getDirection(), new Position(getDispensePositionX(), getDispensePositionY(), getDispensePositionZ()));
} else if (upgrades.hasUpgrade(UpgradeItem.Type.CRAFTING)) {
ItemStack craft = itemFilters.getStackInSlot(0);

View File

@@ -105,7 +105,7 @@ public class NetworkNodeDestructor extends NetworkNode implements IComparable, I
if (entity instanceof ItemEntity) {
ItemStack droppedItem = ((ItemEntity) entity).getItem();
if (IWhitelistBlacklist.acceptsItem(itemFilters, mode, compare, droppedItem) && network.insertItem(droppedItem, droppedItem.getCount(), Action.SIMULATE) == null) {
if (IWhitelistBlacklist.acceptsItem(itemFilters, mode, compare, droppedItem) && network.insertItem(droppedItem, droppedItem.getCount(), Action.SIMULATE).isEmpty()) {
network.insertItemTracked(droppedItem.copy(), droppedItem.getCount());
// TODO world.removeEntity(entity);
@@ -148,7 +148,7 @@ public class NetworkNodeDestructor extends NetworkNode implements IComparable, I
}*/
for (ItemStack drop : drops) {
if (network.insertItem(drop, drop.getCount(), Action.SIMULATE) != null) {
if (!network.insertItem(drop, drop.getCount(), Action.SIMULATE).isEmpty()) {
return;
}
}

View File

@@ -79,16 +79,14 @@ public class NetworkNodeExporter extends NetworkNode implements IComparable, ITy
ItemStack took = network.extractItem(slot, Math.min(slot.getMaxStackSize(), stackSize), compare, Action.SIMULATE);
if (took == null) {
if (took.isEmpty()) {
if (upgrades.hasUpgrade(UpgradeItem.Type.CRAFTING)) {
network.getCraftingManager().request(new SlottedCraftingRequest(this, filterSlot), slot, stackSize);
}
} else if (ItemHandlerHelper.insertItem(handler, took, true).isEmpty()) {
took = network.extractItem(slot, Math.min(slot.getMaxStackSize(), stackSize), compare, Action.PERFORM);
if (took != null) {
ItemHandlerHelper.insertItem(handler, took, false);
}
ItemHandlerHelper.insertItem(handler, took, false);
}
}

View File

@@ -86,12 +86,10 @@ public class NetworkNodeImporter extends NetworkNode implements IComparable, IWh
} else if (ticks % upgrades.getSpeed() == 0) {
ItemStack result = handler.extractItem(currentSlot, upgrades.getItemInteractCount(), true);
if (!result.isEmpty() && network.insertItem(result, result.getCount(), Action.SIMULATE) == null) {
if (!result.isEmpty() && network.insertItem(result, result.getCount(), Action.SIMULATE).isEmpty()) {
result = handler.extractItem(currentSlot, upgrades.getItemInteractCount(), false);
if (!result.isEmpty()) {
network.insertItemTracked(result, result.getCount());
}
network.insertItemTracked(result, result.getCount());
} else {
currentSlot++;
}

View File

@@ -72,13 +72,9 @@ public class NetworkNodeInterface extends NetworkNode implements IComparable {
ItemStack remainder = network.insertItemTracked(slot, size);
if (remainder == null) {
importItems.extractItem(currentSlot, size, false);
} else if (size - remainder.getCount() > 0) {
importItems.extractItem(currentSlot, size - remainder.getCount(), false);
importItems.extractItem(currentSlot, size - remainder.getCount(), false);
currentSlot++;
}
currentSlot++;
}
for (int i = 0; i < 9; ++i) {
@@ -87,10 +83,10 @@ public class NetworkNodeInterface extends NetworkNode implements IComparable {
if (wanted.isEmpty()) {
if (!got.isEmpty()) {
exportItems.setStackInSlot(i, StackUtils.nullToEmpty(network.insertItemTracked(got, got.getCount())));
exportItems.setStackInSlot(i, network.insertItemTracked(got, got.getCount()));
}
} else if (!got.isEmpty() && !API.instance().getComparer().isEqual(wanted, got, getCompare())) {
exportItems.setStackInSlot(i, StackUtils.nullToEmpty(network.insertItemTracked(got, got.getCount())));
exportItems.setStackInSlot(i, network.insertItemTracked(got, got.getCount()));
} else {
int delta = got.isEmpty() ? wanted.getCount() : (wanted.getCount() - got.getCount());
@@ -108,7 +104,7 @@ public class NetworkNodeInterface extends NetworkNode implements IComparable {
return !(s instanceof ItemExternalStorage) || !((ItemExternalStorage) s).isConnectedToInterface();
});
if (result != null) {
if (!result.isEmpty()) {
if (exportItems.getStackInSlot(i).isEmpty()) {
exportItems.setStackInSlot(i, result);
} else {
@@ -118,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 == null ? 0 : result.getCount();
delta -= result.isEmpty() ? 0 : result.getCount();
if (delta > 0 && upgrades.hasUpgrade(UpgradeItem.Type.CRAFTING)) {
network.getCraftingManager().request(new SlottedCraftingRequest(this, i), wanted, delta);
@@ -126,11 +122,7 @@ public class NetworkNodeInterface extends NetworkNode implements IComparable {
} else if (delta < 0) {
ItemStack remainder = network.insertItemTracked(got, Math.abs(delta));
if (remainder == null) {
exportItems.extractItem(i, Math.abs(delta), false);
} else {
exportItems.extractItem(i, Math.abs(delta) - remainder.getCount(), false);
}
exportItems.extractItem(i, Math.abs(delta) - remainder.getCount(), false);
}
}
}

View File

@@ -88,7 +88,7 @@ public class NetworkNodeStorageMonitor extends NetworkNode implements IComparabl
ItemStack toInsert = player.inventory.getStackInSlot(i);
if (API.instance().getComparer().isEqual(inserted, toInsert, compare)) {
player.inventory.setInventorySlotContents(i, StackUtils.nullToEmpty(network.insertItemTracked(toInsert, toInsert.getCount())));
player.inventory.setInventorySlotContents(i, network.insertItemTracked(toInsert, toInsert.getCount()));
}
}
}
@@ -108,7 +108,7 @@ public class NetworkNodeStorageMonitor extends NetworkNode implements IComparabl
ItemStack filter = itemFilter.getStackInSlot(0);
if (!filter.isEmpty() && API.instance().getComparer().isEqual(filter, toInsert, compare)) {
player.inventory.setInventorySlotContents(player.inventory.currentItem, StackUtils.nullToEmpty(network.insertItemTracked(toInsert, toInsert.getCount())));
player.inventory.setInventorySlotContents(player.inventory.currentItem, network.insertItemTracked(toInsert, toInsert.getCount()));
deposits.put(player.getGameProfile().getName(), Pair.of(toInsert, System.currentTimeMillis()));
}
@@ -132,10 +132,8 @@ public class NetworkNodeStorageMonitor extends NetworkNode implements IComparabl
if (!filter.isEmpty()) {
ItemStack result = network.extractItem(filter, toExtract, compare, Action.PERFORM);
if (result != null) {
if (!player.inventory.addItemStackToInventory(result.copy())) {
InventoryHelper.spawnItemStack(world, player.getPosition().getX(), player.getPosition().getY(), player.getPosition().getZ(), result);
}
if (!result.isEmpty() && !player.inventory.addItemStackToInventory(result.copy())) {
InventoryHelper.spawnItemStack(world, player.getPosition().getX(), player.getPosition().getY(), player.getPosition().getZ(), result);
}
}
}

View File

@@ -187,7 +187,7 @@ public class NetworkNodeDiskManipulator extends NetworkNode implements IComparab
}
ItemStack remainder = network.insertItem(extracted, extracted.getCount(), Action.PERFORM);
if (remainder == null) {
if (remainder.isEmpty()) {
break;
}
@@ -222,7 +222,7 @@ public class NetworkNodeDiskManipulator extends NetworkNode implements IComparab
}
ItemStack remainder = network.insertItem(extracted, extracted.getCount(), Action.SIMULATE);
if (remainder == null) { //An item could be inserted (no remainders when trying to). This disk isn't done.
if (remainder.isEmpty()) { // An item could be inserted (no remainders when trying to). This disk isn't done.
return false;
}
}
@@ -230,7 +230,7 @@ public class NetworkNodeDiskManipulator extends NetworkNode implements IComparab
}
private void extractItemFromNetwork(IStorageDisk<ItemStack> storage, int slot) {
ItemStack extracted = null;
ItemStack extracted = ItemStack.EMPTY;
int i = 0;
if (itemFilters.isEmpty()) {
@@ -260,16 +260,14 @@ public class NetworkNodeDiskManipulator extends NetworkNode implements IComparab
}
}
if (extracted == null) {
if (extracted.isEmpty()) {
moveDriveToOutput(slot);
return;
}
ItemStack remainder = storage.insert(extracted, extracted.getCount(), Action.PERFORM);
if (!remainder.isEmpty()) {
network.insertItem(remainder, remainder.getCount(), Action.PERFORM);
}
network.insertItem(remainder, remainder.getCount(), Action.PERFORM);
}
private void insertFluidIntoNetwork(IStorageDisk<FluidStack> storage, int slot) {

View File

@@ -120,20 +120,15 @@ public class PatternItem extends Item implements ICraftingPatternProvider {
pattern.getTag().put(String.format(NBT_INPUT_SLOT, slot), stack.serializeNBT());
}
@Nullable
@Nonnull
public static ItemStack getInputSlot(ItemStack pattern, int slot) {
String id = String.format(NBT_INPUT_SLOT, slot);
if (!pattern.hasTag() || !pattern.getTag().contains(id)) {
return null;
return ItemStack.EMPTY;
}
ItemStack stack = ItemStack.read(pattern.getTag().getCompound(id));
if (stack.isEmpty()) {
return null;
}
return stack;
return ItemStack.read(pattern.getTag().getCompound(id));
}
public static void setOutputSlot(ItemStack pattern, int slot, ItemStack stack) {
@@ -144,20 +139,15 @@ public class PatternItem extends Item implements ICraftingPatternProvider {
pattern.getTag().put(String.format(NBT_OUTPUT_SLOT, slot), stack.serializeNBT());
}
@Nullable
@Nonnull
public static ItemStack getOutputSlot(ItemStack pattern, int slot) {
String id = String.format(NBT_OUTPUT_SLOT, slot);
if (!pattern.hasTag() || !pattern.getTag().contains(id)) {
return null;
return ItemStack.EMPTY;
}
ItemStack stack = ItemStack.read(pattern.getTag().getCompound(id));
if (stack.isEmpty()) {
return null;
}
return stack;
return ItemStack.read(pattern.getTag().getCompound(id));
}
public static void setFluidInputSlot(ItemStack pattern, int slot, FluidStack stack) {

View File

@@ -292,8 +292,13 @@ public class ControllerTile extends BaseTile implements ITickableTileEntity, INe
}
@Override
@Nonnull
public ItemStack insertItem(@Nonnull ItemStack stack, int size, Action action) {
if (stack.isEmpty() || itemStorage.getStorages().isEmpty()) {
if (stack.isEmpty()) {
return stack;
}
if (itemStorage.getStorages().isEmpty()) {
return ItemHandlerHelper.copyStackWithSize(stack, size);
}
@@ -340,16 +345,16 @@ public class ControllerTile extends BaseTile implements ITickableTileEntity, INe
itemStorage.add(stack, inserted - insertedExternally, false, false);
}
// TODO Remove.
if (remainder.isEmpty()) {
remainder = null;
}
return remainder;
}
@Override
@Nonnull
public ItemStack extractItem(@Nonnull ItemStack stack, int size, int flags, Action action, Predicate<IStorage<ItemStack>> filter) {
if (stack.isEmpty()) {
return stack;
}
int requested = size;
int received = 0;
@@ -390,11 +395,6 @@ public class ControllerTile extends BaseTile implements ITickableTileEntity, INe
itemStorage.remove(newStack, newStack.getCount() - extractedExternally, false);
}
// TODO Remove.
if (newStack.isEmpty()) {
newStack = null;
}
return newStack;
}

View File

@@ -150,15 +150,6 @@ public final class StackUtils {
return new FluidGridStack(id, stack, entry, craftable, displayCraftText);
}
public static ItemStack nullToEmpty(@Nullable ItemStack stack) {
return stack == null ? ItemStack.EMPTY : stack;
}
@Nullable
public static ItemStack emptyToNull(@Nonnull ItemStack stack) {
return stack.isEmpty() ? null : stack;
}
@SuppressWarnings("unchecked")
public static void createStorages(ServerWorld world, ItemStack diskStack, int slot, IStorageDisk<ItemStack>[] itemDisks, IStorageDisk<FluidStack>[] fluidDisks, Function<IStorageDisk<ItemStack>, IStorageDisk> itemDiskWrapper, Function<IStorageDisk<FluidStack>, IStorageDisk> fluidDiskWrapper) {
if (diskStack.isEmpty()) {