made crafting terminals select the crafting item that you have most of

Closes #49
This commit is contained in:
Ell 2020-10-27 15:20:17 +01:00
parent 072fc3b924
commit 36e358d2bd

View file

@ -9,13 +9,11 @@ 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;
import de.ellpeck.prettypipes.network.NetworkLocation; import de.ellpeck.prettypipes.network.NetworkLocation;
import de.ellpeck.prettypipes.network.NetworkLock;
import de.ellpeck.prettypipes.network.PipeNetwork; import de.ellpeck.prettypipes.network.PipeNetwork;
import de.ellpeck.prettypipes.packets.PacketGhostSlot; import de.ellpeck.prettypipes.packets.PacketGhostSlot;
import de.ellpeck.prettypipes.packets.PacketHandler; import de.ellpeck.prettypipes.packets.PacketHandler;
import de.ellpeck.prettypipes.pipe.PipeTileEntity; import de.ellpeck.prettypipes.pipe.PipeTileEntity;
import de.ellpeck.prettypipes.terminal.containers.CraftingTerminalContainer; import de.ellpeck.prettypipes.terminal.containers.CraftingTerminalContainer;
import de.ellpeck.prettypipes.terminal.containers.ItemTerminalContainer;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory; import net.minecraft.entity.player.PlayerInventory;
@ -28,14 +26,10 @@ import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.Style; import net.minecraft.util.text.Style;
import net.minecraft.util.text.TextFormatting; import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.TranslationTextComponent; import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemHandlerHelper; import net.minecraftforge.items.ItemHandlerHelper;
import net.minecraftforge.items.ItemStackHandler; import net.minecraftforge.items.ItemStackHandler;
import org.apache.commons.lang3.mutable.MutableInt; import org.apache.commons.lang3.mutable.MutableInt;
import org.apache.commons.lang3.tuple.Pair;
import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.*; import java.util.*;
import java.util.function.Consumer; import java.util.function.Consumer;
@ -70,29 +64,31 @@ public class CraftingTerminalTileEntity extends ItemTerminalTileEntity {
public void setGhostItems(ListMultimap<Integer, ItemStack> stacks) { public void setGhostItems(ListMultimap<Integer, ItemStack> stacks) {
this.updateItems(); this.updateItems();
items:
for (int i = 0; i < this.ghostItems.getSlots(); i++) { for (int i = 0; i < this.ghostItems.getSlots(); i++) {
List<ItemStack> items = stacks.get(i); List<ItemStack> items = stacks.get(i);
if (items.isEmpty()) { if (items.isEmpty()) {
this.ghostItems.setStackInSlot(i, ItemStack.EMPTY); this.ghostItems.setStackInSlot(i, ItemStack.EMPTY);
continue; continue;
} }
ItemStack toSet = items.get(0);
// if we have more than one item to choose from, we want to pick the one that we have most of in the system
if (items.size() > 1) { if (items.size() > 1) {
// set the item into the ghost slot that already has a variant of itself available in the system int highestAmount = 0;
for (ItemStack stack : items) { for (ItemStack stack : items) {
EquatableItemStack equatable = new EquatableItemStack(stack); EquatableItemStack equatable = new EquatableItemStack(stack);
NetworkItem network = this.networkItems.get(equatable); NetworkItem network = this.networkItems.get(equatable);
if (network == null) if (network == null)
continue; continue;
if (network.getLocations().stream().anyMatch(l -> l.getItemAmount(this.world, stack, ItemEqualityType.NBT) > 0)) { int amount = network.getLocations().stream()
this.ghostItems.setStackInSlot(i, stack); .mapToInt(l -> l.getItemAmount(this.world, stack, ItemEqualityType.NBT))
continue items; .sum();
if (amount > highestAmount) {
highestAmount = amount;
toSet = stack;
} }
} }
} }
// if the ghost slot wasn't set, then we don't have the item in the system this.ghostItems.setStackInSlot(i, toSet.copy());
// so just pick a random one to put into the slot
this.ghostItems.setStackInSlot(i, items.get(0));
} }
if (!this.world.isRemote) { if (!this.world.isRemote) {