Merge remote-tracking branch 'origin/main' into main

This commit is contained in:
Ell 2021-01-06 22:31:54 +01:00
commit 1dec3206ee
6 changed files with 34 additions and 15 deletions

View file

@ -9,6 +9,7 @@ import net.minecraft.util.math.BlockPos;
import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.IItemHandler;
import java.util.List; import java.util.List;
import java.util.Stack;
import java.util.function.Consumer; import java.util.function.Consumer;
public interface IModule { public interface IModule {
@ -35,7 +36,7 @@ public interface IModule {
List<ItemStack> getAllCraftables(ItemStack module, PipeTileEntity tile); List<ItemStack> getAllCraftables(ItemStack module, PipeTileEntity tile);
int getCraftableAmount(ItemStack module, PipeTileEntity tile, Consumer<ItemStack> unavailableConsumer, ItemStack stack); int getCraftableAmount(ItemStack module, PipeTileEntity tile, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<IModule> dependencyChain);
ItemStack craft(ItemStack module, PipeTileEntity tile, BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack); ItemStack craft(ItemStack module, PipeTileEntity tile, BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack);

View file

@ -20,6 +20,7 @@ import javax.annotation.Nullable;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.Stack;
import java.util.function.Consumer; import java.util.function.Consumer;
public abstract class ModuleItem extends Item implements IModule { public abstract class ModuleItem extends Item implements IModule {
@ -84,7 +85,7 @@ public abstract class ModuleItem extends Item implements IModule {
} }
@Override @Override
public int getCraftableAmount(ItemStack module, PipeTileEntity tile, Consumer<ItemStack> unavailableConsumer, ItemStack stack) { public int getCraftableAmount(ItemStack module, PipeTileEntity tile, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<IModule> dependencyChain) {
return 0; return 0;
} }

View file

@ -6,6 +6,8 @@ import com.google.common.collect.Streams;
import de.ellpeck.prettypipes.PrettyPipes; import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.Registry; import de.ellpeck.prettypipes.Registry;
import de.ellpeck.prettypipes.Utility; import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.items.IModule;
import de.ellpeck.prettypipes.items.ModuleItem;
import de.ellpeck.prettypipes.misc.ItemEqualityType; import de.ellpeck.prettypipes.misc.ItemEqualityType;
import de.ellpeck.prettypipes.packets.PacketHandler; import de.ellpeck.prettypipes.packets.PacketHandler;
import de.ellpeck.prettypipes.packets.PacketItemEnterPipe; import de.ellpeck.prettypipes.packets.PacketItemEnterPipe;
@ -299,7 +301,7 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundNBT>, GraphL
return craftables; return craftables;
} }
public int getCraftableAmount(BlockPos node, Consumer<ItemStack> unavailableConsumer, ItemStack stack, ItemEqualityType... equalityTypes) { public int getCraftableAmount(BlockPos node, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<IModule> dependencyChain, ItemEqualityType... equalityTypes) {
int total = 0; int total = 0;
for (Pair<BlockPos, ItemStack> pair : this.getAllCraftables(node)) { for (Pair<BlockPos, ItemStack> pair : this.getAllCraftables(node)) {
if (!ItemEqualityType.compareItems(pair.getRight(), stack, equalityTypes)) if (!ItemEqualityType.compareItems(pair.getRight(), stack, equalityTypes))
@ -308,7 +310,7 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundNBT>, GraphL
continue; continue;
PipeTileEntity pipe = this.getPipe(pair.getLeft()); PipeTileEntity pipe = this.getPipe(pair.getLeft());
if (pipe != null) if (pipe != null)
total += pipe.getCraftableAmount(unavailableConsumer, stack); total += pipe.getCraftableAmount(unavailableConsumer, stack, dependencyChain);
} }
return total; return total;
} }

View file

@ -4,6 +4,7 @@ import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.Registry; import de.ellpeck.prettypipes.Registry;
import de.ellpeck.prettypipes.Utility; import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.items.IModule; import de.ellpeck.prettypipes.items.IModule;
import de.ellpeck.prettypipes.items.ModuleItem;
import de.ellpeck.prettypipes.network.NetworkLock; import de.ellpeck.prettypipes.network.NetworkLock;
import de.ellpeck.prettypipes.network.PipeNetwork; import de.ellpeck.prettypipes.network.PipeNetwork;
import de.ellpeck.prettypipes.pipe.containers.MainPipeContainer; import de.ellpeck.prettypipes.pipe.containers.MainPipeContainer;
@ -306,10 +307,19 @@ public class PipeTileEntity extends TileEntity implements INamedContainerProvide
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
public int getCraftableAmount(Consumer<ItemStack> unavailableConsumer, ItemStack stack) { public int getCraftableAmount(Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<IModule> dependencyChain) {
return this.streamModules() int total = 0;
.mapToInt(m -> m.getRight().getCraftableAmount(m.getLeft(), this, unavailableConsumer, stack)) Iterator<Pair<ItemStack, IModule>> modules = this.streamModules().iterator();
.sum(); while (modules.hasNext()) {
Pair<ItemStack, IModule> module = modules.next();
// make sure we don't factor in recursive dependencies like ingot -> block -> ingot etc.
if (!dependencyChain.contains(module.getRight())) {
int amount = module.getRight().getCraftableAmount(module.getLeft(), this, unavailableConsumer, stack, dependencyChain);
if (amount > 0)
total += amount;
}
}
return total;
} }
public ItemStack craft(BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack) { public ItemStack craft(BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack) {

View file

@ -24,6 +24,7 @@ import org.apache.commons.lang3.tuple.Pair;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Stack;
import java.util.function.Consumer; import java.util.function.Consumer;
public class CraftingModuleItem extends ModuleItem { public class CraftingModuleItem extends ModuleItem {
@ -136,7 +137,7 @@ public class CraftingModuleItem extends ModuleItem {
} }
@Override @Override
public int getCraftableAmount(ItemStack module, PipeTileEntity tile, Consumer<ItemStack> unavailableConsumer, ItemStack stack) { public int getCraftableAmount(ItemStack module, PipeTileEntity tile, Consumer<ItemStack> unavailableConsumer, ItemStack stack, Stack<IModule> dependencyChain) {
PipeNetwork network = PipeNetwork.get(tile.getWorld()); PipeNetwork network = PipeNetwork.get(tile.getWorld());
List<NetworkLocation> items = network.getOrderedNetworkItems(tile.getPos()); List<NetworkLocation> items = network.getOrderedNetworkItems(tile.getPos());
ItemEqualityType[] equalityTypes = ItemFilter.getEqualityTypes(tile); ItemEqualityType[] equalityTypes = ItemFilter.getEqualityTypes(tile);
@ -147,8 +148,10 @@ public class CraftingModuleItem extends ModuleItem {
for (int i = 0; i < output.getSlots(); i++) { for (int i = 0; i < output.getSlots(); i++) {
ItemStack out = output.getStackInSlot(i); ItemStack out = output.getStackInSlot(i);
if (!out.isEmpty() && ItemEqualityType.compareItems(out, stack, equalityTypes)) { if (!out.isEmpty() && ItemEqualityType.compareItems(out, stack, equalityTypes)) {
// we can craft this item *in general*, so add us to the dependency chain
dependencyChain.push(this);
// figure out how many crafting operations we can actually do with the input items we have in the network // figure out how many crafting operations we can actually do with the input items we have in the network
int availableCrafts = CraftingTerminalTileEntity.getAvailableCrafts(tile, input.getSlots(), input::getStackInSlot, k -> true, s -> items, unavailableConsumer, equalityTypes); int availableCrafts = CraftingTerminalTileEntity.getAvailableCrafts(tile, input.getSlots(), input::getStackInSlot, k -> true, s -> items, unavailableConsumer, dependencyChain, equalityTypes);
if (availableCrafts > 0) if (availableCrafts > 0)
craftable += out.getCount() * availableCrafts; craftable += out.getCount() * availableCrafts;
} }
@ -159,7 +162,7 @@ public class CraftingModuleItem extends ModuleItem {
@Override @Override
public ItemStack craft(ItemStack module, PipeTileEntity tile, BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack) { public ItemStack craft(ItemStack module, PipeTileEntity tile, BlockPos destPipe, Consumer<ItemStack> unavailableConsumer, ItemStack stack) {
// check if we can craft the required amount of items // check if we can craft the required amount of items
int craftableAmount = this.getCraftableAmount(module, tile, unavailableConsumer, stack); int craftableAmount = this.getCraftableAmount(module, tile, unavailableConsumer, stack, new Stack<>());
if (craftableAmount <= 0) if (craftableAmount <= 0)
return stack; return stack;

View file

@ -5,6 +5,8 @@ import com.google.common.collect.ListMultimap;
import de.ellpeck.prettypipes.PrettyPipes; import de.ellpeck.prettypipes.PrettyPipes;
import de.ellpeck.prettypipes.Registry; import de.ellpeck.prettypipes.Registry;
import de.ellpeck.prettypipes.Utility; import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.items.IModule;
import de.ellpeck.prettypipes.items.ModuleItem;
import de.ellpeck.prettypipes.misc.EquatableItemStack; import de.ellpeck.prettypipes.misc.EquatableItemStack;
import de.ellpeck.prettypipes.misc.ItemEqualityType; import de.ellpeck.prettypipes.misc.ItemEqualityType;
import de.ellpeck.prettypipes.network.NetworkItem; import de.ellpeck.prettypipes.network.NetworkItem;
@ -87,7 +89,7 @@ public class CraftingTerminalTileEntity extends ItemTerminalTileEntity {
if (amount <= 0 && highestAmount <= 0) { if (amount <= 0 && highestAmount <= 0) {
PipeTileEntity pipe = this.getConnectedPipe(); PipeTileEntity pipe = this.getConnectedPipe();
if (pipe != null) if (pipe != null)
amount = PipeNetwork.get(this.world).getCraftableAmount(pipe.getPos(), null, stack, ItemEqualityType.NBT); amount = PipeNetwork.get(this.world).getCraftableAmount(pipe.getPos(), null, stack, new Stack<>(), ItemEqualityType.NBT);
} }
if (amount > highestAmount) { if (amount > highestAmount) {
highestAmount = amount; highestAmount = amount;
@ -117,7 +119,7 @@ public class CraftingTerminalTileEntity extends ItemTerminalTileEntity {
int lowestAvailable = getAvailableCrafts(pipe, this.craftItems.getSlots(), i -> ItemHandlerHelper.copyStackWithSize(this.getRequestedCraftItem(i), 1), this::isGhostItem, s -> { int lowestAvailable = getAvailableCrafts(pipe, this.craftItems.getSlots(), i -> ItemHandlerHelper.copyStackWithSize(this.getRequestedCraftItem(i), 1), this::isGhostItem, s -> {
NetworkItem item = this.networkItems.get(s); NetworkItem item = this.networkItems.get(s);
return item != null ? item.getLocations() : Collections.emptyList(); return item != null ? item.getLocations() : Collections.emptyList();
}, onItemUnavailable(player), ItemEqualityType.NBT); }, onItemUnavailable(player), new Stack<>(), ItemEqualityType.NBT);
if (lowestAvailable > 0) { if (lowestAvailable > 0) {
// if we're limiting the amount, pretend we only have that amount available // if we're limiting the amount, pretend we only have that amount available
if (maxAmount < lowestAvailable) if (maxAmount < lowestAvailable)
@ -194,7 +196,7 @@ public class CraftingTerminalTileEntity extends ItemTerminalTileEntity {
return remain; return remain;
} }
public static int getAvailableCrafts(PipeTileEntity tile, int slots, Function<Integer, ItemStack> inputFunction, Predicate<Integer> isGhost, Function<EquatableItemStack, Collection<NetworkLocation>> locationsFunction, Consumer<ItemStack> unavailableConsumer, ItemEqualityType... equalityTypes) { public static int getAvailableCrafts(PipeTileEntity tile, int slots, Function<Integer, ItemStack> inputFunction, Predicate<Integer> isGhost, Function<EquatableItemStack, Collection<NetworkLocation>> locationsFunction, Consumer<ItemStack> unavailableConsumer, Stack<IModule> dependencyChain, ItemEqualityType... equalityTypes) {
PipeNetwork network = PipeNetwork.get(tile.getWorld()); PipeNetwork network = PipeNetwork.get(tile.getWorld());
// the highest amount we can craft with the items we have // the highest amount we can craft with the items we have
int lowestAvailable = Integer.MAX_VALUE; int lowestAvailable = Integer.MAX_VALUE;
@ -229,7 +231,7 @@ public class CraftingTerminalTileEntity extends ItemTerminalTileEntity {
// check how many craftable items we have and add those on if we need to // check how many craftable items we have and add those on if we need to
if (available < lowestAvailable) { if (available < lowestAvailable) {
int craftable = network.getCraftableAmount(tile.getPos(), unavailableConsumer, stack.stack, equalityTypes); int craftable = network.getCraftableAmount(tile.getPos(), unavailableConsumer, stack.stack, dependencyChain, equalityTypes);
if (craftable > 0) if (craftable > 0)
available += craftable / entry.getValue().intValue(); available += craftable / entry.getValue().intValue();
} }