make crafter insertion method default, so addons do not have to copy our code
This commit is contained in:
@@ -11,11 +11,12 @@ import net.minecraftforge.fluids.FluidStack;
|
||||
import net.minecraftforge.fluids.capability.IFluidHandler;
|
||||
import net.minecraftforge.items.IItemHandler;
|
||||
import net.minecraftforge.items.IItemHandlerModifiable;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
/**
|
||||
* Represents a network node that contains crafting patterns.
|
||||
@@ -130,26 +131,6 @@ public interface ICraftingPatternContainer {
|
||||
default void onUsedForProcessing() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when the autocrafting system wants to insert items. Will be called with Action.SIMULATE first and if that
|
||||
* succeeds will be called again with Action.PERFORM
|
||||
*
|
||||
* @param toInsert A collection of items that should be inserted.
|
||||
* @param action Action to take
|
||||
* @return whether the insertion was successful
|
||||
*/
|
||||
boolean insertItemsIntoInventory(Collection<StackListEntry<ItemStack>> toInsert, Action action);
|
||||
|
||||
/**
|
||||
* Called when the autocrafting system wants to insert fluids. Will be called with Action.SIMULATE first and if that
|
||||
* succeeds will be called again with Action.PERFORM
|
||||
*
|
||||
* @param toInsert A collection of fluids that should be inserted.
|
||||
* @param action Action to take
|
||||
* @return whether the insertion was successful
|
||||
*/
|
||||
boolean insertFluidsIntoInventory(Collection<StackListEntry<FluidStack>> toInsert, Action action);
|
||||
|
||||
/**
|
||||
* @return whether the container is successfully connected to the inventory it wants to insert to
|
||||
*/
|
||||
@@ -163,4 +144,100 @@ public interface ICraftingPatternContainer {
|
||||
default boolean hasConnectedFluidInventory() {
|
||||
return getConnectedFluidInventory() != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by Autocrafting when it uses this crafter in a processing recipe that has items as input
|
||||
*
|
||||
* @param toInsert Collection of Itemstack stacklist entries to insert into the inventory
|
||||
* @param action action to perform
|
||||
* @return whether insertion was successful
|
||||
*/
|
||||
default boolean insertItemsIntoInventory(Collection<StackListEntry<ItemStack>> toInsert, Action action) {
|
||||
IItemHandler dest = getConnectedInventory();
|
||||
|
||||
|
||||
if (toInsert.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (dest == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Deque<StackListEntry<ItemStack>> stacks = new ArrayDeque<>(toInsert);
|
||||
|
||||
StackListEntry<ItemStack> currentEntry = stacks.poll();
|
||||
|
||||
ItemStack current = currentEntry != null ? currentEntry.getStack() : null;
|
||||
|
||||
List<Integer> availableSlots = IntStream.range(0, dest.getSlots()).boxed().collect(Collectors.toList());
|
||||
|
||||
while (current != null && !availableSlots.isEmpty()) {
|
||||
ItemStack remainder = ItemStack.EMPTY;
|
||||
|
||||
for (int i = 0; i < availableSlots.size(); ++i) {
|
||||
int slot = availableSlots.get(i);
|
||||
|
||||
// .copy() is mandatory!
|
||||
remainder = dest.insertItem(slot, current.copy(), action == Action.SIMULATE);
|
||||
|
||||
// If we inserted *something*
|
||||
if (remainder.isEmpty() || current.getCount() != remainder.getCount()) {
|
||||
availableSlots.remove(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (remainder.isEmpty()) { // If we inserted successfully, get a next stack.
|
||||
currentEntry = stacks.poll();
|
||||
|
||||
current = currentEntry != null ? currentEntry.getStack() : null;
|
||||
} else if (current.getCount() == remainder.getCount()) { // If we didn't insert anything over ALL these slots, stop here.
|
||||
break;
|
||||
} else { // If we didn't insert all, continue with other slots and use our remainder.
|
||||
current = remainder;
|
||||
}
|
||||
}
|
||||
|
||||
boolean success = current == null && stacks.isEmpty();
|
||||
|
||||
if (!success && action == Action.PERFORM) {
|
||||
LogManager.getLogger().warn("Inventory unexpectedly didn't accept {}, the remainder has been voided!", current != null ? current.getTranslationKey() : null);
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by Autocrafting when it uses this crafter in a processing recipe that has fluids as input
|
||||
*
|
||||
* @param toInsert Collection of Fluidstack stacklist entries to insert into the inventory
|
||||
* @param action action to perform
|
||||
* @return whether insertion was successful
|
||||
*/
|
||||
default boolean insertFluidsIntoInventory(Collection<StackListEntry<FluidStack>> toInsert, Action action) {
|
||||
IFluidHandler dest = getConnectedFluidInventory();
|
||||
|
||||
if (toInsert.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (dest == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (StackListEntry<FluidStack> entry : toInsert) {
|
||||
int filled = dest.fill(entry.getStack(), action == Action.SIMULATE ? IFluidHandler.FluidAction.SIMULATE : IFluidHandler.FluidAction.EXECUTE);
|
||||
|
||||
if (filled != entry.getStack().getAmount()) {
|
||||
if (action == Action.PERFORM) {
|
||||
LogManager.getLogger().warn("Inventory unexpectedly didn't accept all of {}, the remainder has been voided!", entry.getStack().getTranslationKey());
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@@ -6,8 +6,6 @@ import com.refinedmods.refinedstorage.api.autocrafting.ICraftingPatternContainer
|
||||
import com.refinedmods.refinedstorage.api.autocrafting.ICraftingPatternProvider;
|
||||
import com.refinedmods.refinedstorage.api.network.INetwork;
|
||||
import com.refinedmods.refinedstorage.api.network.node.INetworkNode;
|
||||
import com.refinedmods.refinedstorage.api.util.Action;
|
||||
import com.refinedmods.refinedstorage.api.util.StackListEntry;
|
||||
import com.refinedmods.refinedstorage.apiimpl.API;
|
||||
import com.refinedmods.refinedstorage.inventory.item.BaseItemHandler;
|
||||
import com.refinedmods.refinedstorage.inventory.item.UpgradeItemHandler;
|
||||
@@ -27,22 +25,19 @@ import net.minecraft.util.text.ITextComponent;
|
||||
import net.minecraft.util.text.TranslationTextComponent;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.server.ServerWorld;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
import net.minecraftforge.fluids.capability.IFluidHandler;
|
||||
import net.minecraftforge.items.IItemHandler;
|
||||
import net.minecraftforge.items.IItemHandlerModifiable;
|
||||
import net.minecraftforge.items.wrapper.CombinedInvWrapper;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
public class CrafterNetworkNode extends NetworkNode implements ICraftingPatternContainer {
|
||||
private static final Logger LOGGER = LogManager.getLogger(CrafterNetworkNode.class);
|
||||
|
||||
public enum CrafterMode {
|
||||
IGNORE,
|
||||
@@ -474,87 +469,4 @@ public class CrafterNetworkNode extends NetworkNode implements ICraftingPatternC
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean insertItemsIntoInventory(Collection<StackListEntry<ItemStack>> toInsert, Action action) {
|
||||
IItemHandler dest = getConnectedInventory();
|
||||
|
||||
|
||||
if (toInsert.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (dest == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Deque<StackListEntry<ItemStack>> stacks = new ArrayDeque<>(toInsert);
|
||||
|
||||
StackListEntry<ItemStack> currentEntry = stacks.poll();
|
||||
|
||||
ItemStack current = currentEntry != null ? currentEntry.getStack() : null;
|
||||
|
||||
List<Integer> availableSlots = IntStream.range(0, dest.getSlots()).boxed().collect(Collectors.toList());
|
||||
|
||||
while (current != null && !availableSlots.isEmpty()) {
|
||||
ItemStack remainder = ItemStack.EMPTY;
|
||||
|
||||
for (int i = 0; i < availableSlots.size(); ++i) {
|
||||
int slot = availableSlots.get(i);
|
||||
|
||||
// .copy() is mandatory!
|
||||
remainder = dest.insertItem(slot, current.copy(), action == Action.SIMULATE);
|
||||
|
||||
// If we inserted *something*
|
||||
if (remainder.isEmpty() || current.getCount() != remainder.getCount()) {
|
||||
availableSlots.remove(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (remainder.isEmpty()) { // If we inserted successfully, get a next stack.
|
||||
currentEntry = stacks.poll();
|
||||
|
||||
current = currentEntry != null ? currentEntry.getStack() : null;
|
||||
} else if (current.getCount() == remainder.getCount()) { // If we didn't insert anything over ALL these slots, stop here.
|
||||
break;
|
||||
} else { // If we didn't insert all, continue with other slots and use our remainder.
|
||||
current = remainder;
|
||||
}
|
||||
}
|
||||
|
||||
boolean success = current == null && stacks.isEmpty();
|
||||
|
||||
if (!success && action == Action.PERFORM) {
|
||||
LOGGER.warn("Inventory unexpectedly didn't accept {}, the remainder has been voided!", current != null ? current.getTranslationKey() : null);
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean insertFluidsIntoInventory(Collection<StackListEntry<FluidStack>> toInsert, Action action) {
|
||||
IFluidHandler dest = getConnectedFluidInventory();
|
||||
|
||||
if (toInsert.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (dest == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (StackListEntry<FluidStack> entry : toInsert) {
|
||||
int filled = dest.fill(entry.getStack(), action == Action.SIMULATE ? IFluidHandler.FluidAction.SIMULATE : IFluidHandler.FluidAction.EXECUTE);
|
||||
|
||||
if (filled != entry.getStack().getAmount()) {
|
||||
if (action == Action.PERFORM) {
|
||||
LOGGER.warn("Inventory unexpectedly didn't accept all of {}, the remainder has been voided!", entry.getStack().getTranslationKey());
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user