Skip to content

Commit

Permalink
Check ItemStack NBT for comparison in more places (#5491)
Browse files Browse the repository at this point in the history
* Check NBT in a bunch more item comparision checks
* Fix Gas conversion checking stack size when comparing itemstacks
  • Loading branch information
pupnewfster authored and thiakil committed May 27, 2019
1 parent 7438ad4 commit eb2de11
Show file tree
Hide file tree
Showing 20 changed files with 105 additions and 81 deletions.
3 changes: 2 additions & 1 deletion src/main/java/mekanism/api/infuse/InfuseRegistry.java
Expand Up @@ -3,6 +3,7 @@
import java.util.HashMap;
import java.util.Map;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.ItemHandlerHelper;

/**
* Use this class to add a new object that registers as an infuse object.
Expand Down Expand Up @@ -82,7 +83,7 @@ public static void registerInfuseObject(ItemStack itemStack, InfuseObject infuse
*/
public static InfuseObject getObject(ItemStack itemStack) {
for (Map.Entry<ItemStack, InfuseObject> obj : infuseObjects.entrySet()) {
if (itemStack.isItemEqual(obj.getKey())) {
if (ItemHandlerHelper.canItemStacksStack(obj.getKey(), itemStack)) {
return obj.getValue();
}
}
Expand Down
Expand Up @@ -33,6 +33,7 @@
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.items.ItemHandlerHelper;
import org.lwjgl.opengl.GL11;

@SideOnly(Side.CLIENT)
Expand Down Expand Up @@ -164,7 +165,7 @@ protected void drawGuiContainerBackgroundLayer(float partialTick, int mouseX, in

int guiX = guiWidth + slot.xPos;
int guiY = guiHeight + slot.yPos;
if (slot.getStack().isEmpty() || !slot.getStack().isItemEqual(stack)) {
if (slot.getStack().isEmpty() || !ItemHandlerHelper.canItemStacksStack(slot.getStack(), stack)) {
drawGradientRect(guiX, guiY, guiX + 16, guiY + 16, -2137456640, -2137456640);
}

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/mekanism/common/base/IFactory.java
Expand Up @@ -30,6 +30,7 @@
import net.minecraft.util.IStringSerializable;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundEvent;
import net.minecraftforge.items.ItemHandlerHelper;

/**
* Internal interface for managing various Factory types.
Expand Down Expand Up @@ -163,11 +164,10 @@ public MachineRecipe getAnyRecipe(ItemStack slotStack, ItemStack extraStack, Gas
} else if (this == INFUSING) {
if (infuse.getType() != null) {
return RecipeHandler.getMetallurgicInfuserRecipe(new InfusionInput(infuse, slotStack));
} else {
for (Entry<InfusionInput, MetallurgicInfuserRecipe> entry : Recipe.METALLURGIC_INFUSER.get().entrySet()) {
if (entry.getKey().inputStack.isItemEqual(slotStack)) {
return entry.getValue();
}
}
for (Entry<InfusionInput, MetallurgicInfuserRecipe> entry : Recipe.METALLURGIC_INFUSER.get().entrySet()) {
if (ItemHandlerHelper.canItemStacksStack(entry.getKey().inputStack, slotStack)) {
return entry.getValue();
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/mekanism/common/inventory/InventoryBin.java
Expand Up @@ -7,6 +7,7 @@
import mekanism.common.util.StackUtils;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.items.ItemHandlerHelper;

public class InventoryBin {

Expand Down Expand Up @@ -66,7 +67,7 @@ public boolean isValid(ItemStack stack) {
if (getItemType().isEmpty()) {
return true;
}
return stack.isItemEqual(getItemType()) && ItemStack.areItemStackTagsEqual(stack, getItemType());
return ItemHandlerHelper.canItemStacksStack(stack, getItemType());
}

public int getMaxStorage() {
Expand Down
@@ -1,6 +1,5 @@
package mekanism.common.inventory.container;

import java.util.Arrays;
import javax.annotation.Nonnull;
import mekanism.api.infuse.InfuseRegistry;
import mekanism.common.base.IFactory.RecipeType;
Expand All @@ -15,6 +14,7 @@
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.ItemHandlerHelper;

public class ContainerFactory extends ContainerMekanism<TileEntityFactory> {

Expand Down Expand Up @@ -69,7 +69,7 @@ public ItemStack transferStackInSlot(EntityPlayer player, int slotID) {
if (!mergeItemStack(slotStack, tileEntity.inventory.size() - 1, inventorySlots.size(), true)) {
return ItemStack.EMPTY;
}
} else if (slotID != 1 && slotID != 2 && isProperMachine(slotStack) && !slotStack.isItemEqual(tileEntity.getMachineStack())) {
} else if (slotID != 1 && slotID != 2 && isProperMachine(slotStack) && !ItemHandlerHelper.canItemStacksStack(slotStack, tileEntity.getMachineStack())) {
if (!mergeItemStack(slotStack, 1, 2, false)) {
return ItemStack.EMPTY;
}
Expand Down Expand Up @@ -138,7 +138,11 @@ private boolean transferExtraSlot(int slotID, ItemStack slotStack) {

public boolean isProperMachine(ItemStack itemStack) {
if (!itemStack.isEmpty() && itemStack.getItem() instanceof ItemBlockMachine) {
return Arrays.stream(RecipeType.values()).findFirst().filter(type -> itemStack.isItemEqual(type.getStack())).isPresent();
for (RecipeType type : RecipeType.values()) {
if (ItemHandlerHelper.canItemStacksStack(itemStack, type.getStack())) {
return true;
}
}
}
return false;
}
Expand Down
Expand Up @@ -13,6 +13,7 @@
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.ItemHandlerHelper;

public class ContainerMetallurgicInfuser extends ContainerMekanism<TileEntityMetallurgicInfuser> {

Expand Down Expand Up @@ -70,7 +71,12 @@ public boolean isInputItem(ItemStack itemStack) {
if (tileEntity.infuseStored.getType() != null) {
return RecipeHandler.getMetallurgicInfuserRecipe(new InfusionInput(tileEntity.infuseStored, itemStack)) != null;
}
return Recipe.METALLURGIC_INFUSER.get().keySet().stream().anyMatch(input -> input.inputStack.isItemEqual(itemStack));
for (InfusionInput input : Recipe.METALLURGIC_INFUSER.get().keySet()) {
if (ItemHandlerHelper.canItemStacksStack(input.inputStack, itemStack)) {
return true;
}
}
return false;
}

@Override
Expand Down
18 changes: 10 additions & 8 deletions src/main/java/mekanism/common/item/ItemAtomicDisassembler.java
Expand Up @@ -41,6 +41,7 @@
import net.minecraftforge.common.util.Constants.WorldEvents;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.items.ItemHandlerHelper;

public class ItemAtomicDisassembler extends ItemEnergized {

Expand Down Expand Up @@ -340,12 +341,13 @@ public static class Finder {
}

private final EntityPlayer player;
public World world;
public ItemStack stack;
public Coord4D location;
public Set<Coord4D> found = new HashSet<>();
RayTraceResult rayTraceResult;
private Block startBlock;
public final World world;
public final ItemStack stack;
public final Coord4D location;
public final Set<Coord4D> found = new HashSet<>();
private final RayTraceResult rayTraceResult;
private final Block startBlock;
private final boolean isWood;

public Finder(EntityPlayer p, ItemStack s, Coord4D loc, RayTraceResult traceResult) {
player = p;
Expand All @@ -354,6 +356,7 @@ public Finder(EntityPlayer p, ItemStack s, Coord4D loc, RayTraceResult traceResu
location = loc;
startBlock = loc.getBlock(world);
rayTraceResult = traceResult;
isWood = OreDictCache.getOreDictName(stack).contains("logWood");
}

public void loop(Coord4D pointer) {
Expand All @@ -365,8 +368,7 @@ public void loop(Coord4D pointer) {
Coord4D coord = pointer.offset(side);
ItemStack blockStack = coord.getBlock(world).getPickBlock(coord.getBlockState(world), rayTraceResult, world, coord.getPos(), player);
if (coord.exists(world) && checkID(coord.getBlock(world)) &&
(stack.isItemEqual(blockStack) || (coord.getBlock(world) == startBlock && OreDictCache.getOreDictName(stack).contains("logWood")
&& coord.getBlockMeta(world) % 4 == stack.getItemDamage() % 4))) {
(ItemHandlerHelper.canItemStacksStack(stack, blockStack) || (coord.getBlock(world) == startBlock && isWood && coord.getBlockMeta(world) % 4 == stack.getItemDamage() % 4))) {
loop(coord);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/mekanism/common/recipe/BinRecipe.java
Expand Up @@ -17,6 +17,7 @@
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.PlayerEvent.ItemCraftedEvent;
import net.minecraftforge.items.ItemHandlerHelper;
import net.minecraftforge.registries.IForgeRegistryEntry;

public class BinRecipe extends IForgeRegistryEntry.Impl<IRecipe> implements IRecipe {
Expand Down Expand Up @@ -80,7 +81,7 @@ public ItemStack getResult(IInventory inv) {

if (!addStack.isEmpty()) {
if (!(addStack.getItem() instanceof ItemProxy)) {
if (!binInv.getItemType().isEmpty() && !binInv.getItemType().isItemEqual(addStack)) {
if (!binInv.getItemType().isEmpty() && !ItemHandlerHelper.canItemStacksStack(binInv.getItemType(), addStack)) {
return ItemStack.EMPTY;
}
binInv.add(addStack);
Expand Down
Expand Up @@ -4,6 +4,7 @@
import java.util.List;
import javax.annotation.Nonnull;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.ItemHandlerHelper;

public class ItemStackMekIngredient implements IMekanismIngredient<ItemStack> {

Expand All @@ -27,7 +28,7 @@ public List<ItemStack> getMatching() {

@Override
public boolean contains(@Nonnull ItemStack stack) {
return ItemStack.areItemStacksEqual(this.stack, stack);
return ItemHandlerHelper.canItemStacksStack(this.stack, stack);
}

@Override
Expand Down
40 changes: 18 additions & 22 deletions src/main/java/mekanism/common/recipe/outputs/ChanceOutput.java
Expand Up @@ -4,6 +4,7 @@
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.NonNullList;
import net.minecraftforge.items.ItemHandlerHelper;

public class ChanceOutput extends MachineOutput<ChanceOutput> {

Expand Down Expand Up @@ -49,33 +50,28 @@ public boolean hasSecondary() {

public boolean applyOutputs(NonNullList<ItemStack> inventory, int primaryIndex, int secondaryIndex, boolean doEmit) {
if (hasPrimary()) {
ItemStack primaryStack = inventory.get(primaryIndex);
if (primaryStack.isEmpty()) {
if (doEmit) {
inventory.set(primaryIndex, primaryOutput.copy());
}
} else if (primaryStack.isItemEqual(primaryOutput) && primaryStack.getCount() + primaryOutput.getCount() <= primaryStack.getMaxStackSize()) {
if (doEmit) {
primaryStack.grow(primaryOutput.getCount());
}
} else {
if (applyOutputs(inventory, primaryIndex, doEmit, primaryOutput)) {
return false;
}
}

if (hasSecondary() && (!doEmit || checkSecondary())) {
ItemStack secondaryStack = inventory.get(secondaryIndex);
if (secondaryStack.isEmpty()) {
if (doEmit) {
inventory.set(secondaryIndex, secondaryOutput.copy());
}
} else if (secondaryStack.isItemEqual(secondaryOutput) && secondaryStack.getCount() + primaryOutput.getCount() <= secondaryStack.getMaxStackSize()) {
if (doEmit) {
secondaryStack.grow(secondaryOutput.getCount());
}
} else {
return false;
return !applyOutputs(inventory, secondaryIndex, doEmit, secondaryOutput);
}
return true;
}

private boolean applyOutputs(NonNullList<ItemStack> inventory, int index, boolean doEmit, ItemStack output) {
ItemStack stack = inventory.get(index);
if (stack.isEmpty()) {
if (doEmit) {
inventory.set(index, output.copy());
}
return false;
} else if (ItemHandlerHelper.canItemStacksStack(stack, output) && stack.getCount() + output.getCount() <= stack.getMaxStackSize()) {
if (doEmit) {
stack.grow(output.getCount());
}
return false;
}
return true;
}
Expand Down
Expand Up @@ -3,6 +3,7 @@
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.NonNullList;
import net.minecraftforge.items.ItemHandlerHelper;

public class ItemStackOutput extends MachineOutput<ItemStackOutput> {

Expand All @@ -27,7 +28,7 @@ public boolean applyOutputs(NonNullList<ItemStack> inventory, int index, boolean
inventory.set(index, output.copy());
}
return true;
} else if (stack.isItemEqual(output) && stack.getCount() + output.getCount() <= stack.getMaxStackSize()) {
} else if (ItemHandlerHelper.canItemStacksStack(stack, output) && stack.getCount() + output.getCount() <= stack.getMaxStackSize()) {
if (doEmit) {
stack.grow(output.getCount());
}
Expand Down
Expand Up @@ -5,6 +5,7 @@
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.NonNullList;
import net.minecraftforge.items.ItemHandlerHelper;

public class PressurizedOutput extends MachineOutput<PressurizedOutput> {

Expand All @@ -30,19 +31,20 @@ public boolean canFillTank(GasTank tank) {
}

public boolean canAddProducts(NonNullList<ItemStack> inventory, int index) {
return inventory.get(index).isEmpty() ||
(inventory.get(index).isItemEqual(itemOutput) && inventory.get(index).getCount() + itemOutput.getCount() <= inventory.get(index).getMaxStackSize());
ItemStack stack = inventory.get(index);
return stack.isEmpty() || (ItemHandlerHelper.canItemStacksStack(stack, itemOutput) && stack.getCount() + itemOutput.getCount() <= stack.getMaxStackSize());
}

public void fillTank(GasTank tank) {
tank.receive(gasOutput, true);
}

public void addProducts(NonNullList<ItemStack> inventory, int index) {
if (inventory.get(index).isEmpty()) {
ItemStack stack = inventory.get(index);
if (stack.isEmpty()) {
inventory.set(index, itemOutput.copy());
} else if (inventory.get(index).isItemEqual(itemOutput)) {
inventory.get(index).grow(itemOutput.getCount());
} else if (ItemHandlerHelper.canItemStacksStack(stack, itemOutput)) {
stack.grow(itemOutput.getCount());
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/mekanism/common/tile/TileEntityBin.java
Expand Up @@ -41,6 +41,7 @@
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemHandlerHelper;

public class TileEntityBin extends TileEntityBasicBlock implements ISidedInventory, IActiveState, IConfigurable, ITierUpgradeable, IComparatorSupport {

Expand Down Expand Up @@ -120,7 +121,7 @@ public boolean isValid(ItemStack stack) {
if (itemType.isEmpty()) {
return true;
}
return stack.isItemEqual(itemType) && ItemStack.areItemStackTagsEqual(stack, itemType);
return ItemHandlerHelper.canItemStacksStack(itemType, stack);
}

public ItemStack add(ItemStack stack, boolean simulate) {
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/mekanism/common/tile/TileEntityDigitalMiner.java
Expand Up @@ -372,8 +372,9 @@ public ItemStack getReplace(int index) {
return ItemStack.EMPTY;
}
for (int i = 0; i < 27; i++) {
if (!inventory.get(i).isEmpty() && inventory.get(i).isItemEqual(filter.replaceStack)) {
inventory.get(i).shrink(1);
ItemStack stack = inventory.get(i);
if (!stack.isEmpty() && stack.isItemEqual(filter.replaceStack)) {
stack.shrink(1);
return StackUtils.size(filter.replaceStack, 1);
}
}
Expand Down Expand Up @@ -456,15 +457,15 @@ public void add(List<ItemStack> stacks) {
return;
}

stacks:
for (ItemStack stack : stacks) {
for (int i = 0; i < 27; i++) {
if (inventory.get(i).isEmpty()) {
ItemStack currentStack = inventory.get(i);
if (currentStack.isEmpty()) {
inventory.set(i, stack);
continue stacks;
} else if (inventory.get(i).isItemEqual(stack) && inventory.get(i).getCount() + stack.getCount() <= stack.getMaxStackSize()) {
inventory.get(i).grow(stack.getCount());
continue stacks;
break;
} else if (ItemHandlerHelper.canItemStacksStack(currentStack, stack) && currentStack.getCount() + stack.getCount() <= stack.getMaxStackSize()) {
currentStack.grow(stack.getCount());
break;
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/mekanism/common/tile/TileEntityFactory.java
Expand Up @@ -70,6 +70,7 @@
import net.minecraft.util.text.translation.I18n;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.items.ItemHandlerHelper;

public class TileEntityFactory extends TileEntityMachine implements IComputerIntegration, ISideConfiguration, IGasHandler, ISpecialConfigData, ITierUpgradeable,
ISustainedData, IComparatorSupport {
Expand Down Expand Up @@ -240,7 +241,7 @@ public void onUpdate() {
RecipeType toSet = null;

for (RecipeType type : RecipeType.values()) {
if (inventory.get(2).isItemEqual(type.getStack())) {
if (ItemHandlerHelper.canItemStacksStack(inventory.get(2), type.getStack())) {
toSet = type;
break;
}
Expand Down

0 comments on commit eb2de11

Please sign in to comment.