mirror of
https://github.com/Thorfusion/Mekanism-Community-Edition.git
synced 2026-11-26 14:52:58 +00:00
Improve Itemstack equality checks (#5451)
* fix bad stacking check for Digital Miner. #5449 * map to forge's stack stackability check so caps are accounted for * check nbt when taking a defined item * fix bad reference equality check for empty * Switch sorting to using our stackable method * Fix bad stack check for robit #5454 * Improve stack compare for robit smelting
This commit is contained in:
1 parent
82d62ef96f
commit
4c4e88dca5
6 files changed
+50
-38
No files matched your search
@@ -52,7 +52,7 @@ public class StackSearcher {
|
||||
for (slotCount = slotCount - 1; slotCount >= 0; slotCount--) {
|
||||
ItemStack stack = inventory.extractItem(slotCount, max, true);
|
||||
|
||||
if (!stack.isEmpty() && StackUtils.equalsWildcard(stack, type)) {
|
||||
if (!stack.isEmpty() && StackUtils.equalsWildcardWithNBT(stack, type)) {
|
||||
int current = !ret.getStack().isEmpty() ? ret.getStack().getCount() : 0;
|
||||
|
||||
if (current + stack.getCount() <= max) {
|
||||
|
||||
@@ -58,6 +58,7 @@ import net.minecraftforge.energy.CapabilityEnergy;
|
||||
import net.minecraftforge.energy.IEnergyStorage;
|
||||
import net.minecraftforge.fml.common.FMLCommonHandler;
|
||||
import net.minecraftforge.fml.common.Optional.Interface;
|
||||
import net.minecraftforge.items.ItemHandlerHelper;
|
||||
|
||||
@Interface(iface = "micdoodle8.mods.galacticraft.api.entity.IEntityBreathable", modid = MekanismHooks.GALACTICRAFT_MOD_ID)
|
||||
public class EntityRobit extends EntityCreature implements IInventory, ISustainedInventory, IEntityBreathable {
|
||||
@@ -282,8 +283,8 @@ public class EntityRobit extends EntityCreature implements IInventory, ISustaine
|
||||
((rand.nextFloat() - rand.nextFloat()) * 0.7F + 1.0F) * 2.0F);
|
||||
|
||||
break;
|
||||
} else if (itemStack.isItemEqual(item.getItem()) && itemStack.getCount() < itemStack
|
||||
.getMaxStackSize()) {
|
||||
} else if (ItemHandlerHelper.canItemStacksStack(itemStack, item.getItem())
|
||||
&& itemStack.getCount() < itemStack.getMaxStackSize()) {
|
||||
int needed = itemStack.getMaxStackSize() - itemStack.getCount();
|
||||
int toAdd = Math.min(needed, item.getItem().getCount());
|
||||
|
||||
@@ -323,35 +324,38 @@ public class EntityRobit extends EntityCreature implements IInventory, ISustaine
|
||||
}
|
||||
|
||||
private boolean canSmelt() {
|
||||
if (inventory.get(28).isEmpty()) {
|
||||
ItemStack input = inventory.get(28);
|
||||
if (input.isEmpty()) {
|
||||
return false;
|
||||
} else {
|
||||
ItemStack itemstack = FurnaceRecipes.instance().getSmeltingResult(inventory.get(28));
|
||||
if (itemstack.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
if (inventory.get(30).isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
if (!inventory.get(30).isItemEqual(itemstack)) {
|
||||
return false;
|
||||
}
|
||||
int result = inventory.get(30).getCount() + itemstack.getCount();
|
||||
return (result <= getInventoryStackLimit() && result <= itemstack.getMaxStackSize());
|
||||
}
|
||||
ItemStack result = FurnaceRecipes.instance().getSmeltingResult(input);
|
||||
if (result.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
ItemStack currentOutput = inventory.get(30);
|
||||
if (currentOutput.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
if (!ItemHandlerHelper.canItemStacksStack(currentOutput, result)) {
|
||||
return false;
|
||||
}
|
||||
int newAmount = currentOutput.getCount() + result.getCount();
|
||||
return newAmount <= getInventoryStackLimit() && newAmount <= result.getMaxStackSize();
|
||||
}
|
||||
|
||||
public void smeltItem() {
|
||||
if (canSmelt()) {
|
||||
ItemStack itemstack = FurnaceRecipes.instance().getSmeltingResult(inventory.get(28));
|
||||
ItemStack input = inventory.get(28);
|
||||
ItemStack result = FurnaceRecipes.instance().getSmeltingResult(input);
|
||||
|
||||
if (inventory.get(30).isEmpty()) {
|
||||
inventory.set(30, itemstack.copy());
|
||||
} else if (inventory.get(30).isItemEqual(itemstack)) {
|
||||
inventory.get(30).grow(itemstack.getCount());
|
||||
ItemStack currentOutput = inventory.get(30);
|
||||
if (currentOutput.isEmpty()) {
|
||||
inventory.set(30, result.copy());
|
||||
} else if (ItemHandlerHelper.canItemStacksStack(currentOutput, result)) {
|
||||
currentOutput.grow(result.getCount());
|
||||
}
|
||||
|
||||
inventory.get(28).shrink(1);
|
||||
//There shouldn't be any other case where the item doesn't stack but should we double check it anyways
|
||||
input.shrink(1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -76,6 +76,7 @@ import net.minecraftforge.fml.common.FMLCommonHandler;
|
||||
import net.minecraftforge.fml.relauncher.Side;
|
||||
import net.minecraftforge.fml.relauncher.SideOnly;
|
||||
import net.minecraftforge.items.CapabilityItemHandler;
|
||||
import net.minecraftforge.items.ItemHandlerHelper;
|
||||
|
||||
public class TileEntityDigitalMiner extends TileEntityElectricBlock implements IUpgradeTile, IRedstoneControl,
|
||||
IActiveState, ISustainedData, IChunkLoader, IAdvancedBoundingBlock {
|
||||
@@ -473,15 +474,16 @@ public class TileEntityDigitalMiner extends TileEntityElectricBlock implements I
|
||||
}
|
||||
|
||||
for (int i = 0; i < 27; i++) {
|
||||
if (testInv.get(i).isEmpty()) {
|
||||
ItemStack existingStack = testInv.get(i);
|
||||
if (existingStack.isEmpty()) {
|
||||
testInv.set(i, stack);
|
||||
added++;
|
||||
|
||||
continue stacks;
|
||||
} else if (testInv.get(i).isItemEqual(stack)
|
||||
&& testInv.get(i).getCount() + stack.getCount() <= stack
|
||||
} else if (ItemHandlerHelper.canItemStacksStack(existingStack, stack)
|
||||
&& existingStack.getCount() + stack.getCount() <= stack
|
||||
.getMaxStackSize()) {
|
||||
testInv.get(i).grow(stack.getCount());
|
||||
existingStack.grow(stack.getCount());
|
||||
added++;
|
||||
|
||||
continue stacks;
|
||||
|
||||
@@ -67,7 +67,6 @@ import net.minecraft.util.NonNullList;
|
||||
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 {
|
||||
@@ -406,11 +405,8 @@ public class TileEntityFactory extends TileEntityMachine implements IComputerInt
|
||||
for (int j = i + 1; j < inputSlots.length; j++) {
|
||||
int checkSlotID = inputSlots[j];
|
||||
ItemStack checkStack = inventory.get(checkSlotID);
|
||||
if (Math.abs(count - checkStack.getCount()) < 2) {
|
||||
continue;
|
||||
}
|
||||
if (!stack.isEmpty() && !checkStack.isEmpty() && !ItemHandlerHelper
|
||||
.canItemStacksStack(stack, checkStack)) {
|
||||
if (Math.abs(count - checkStack.getCount()) < 2 ||
|
||||
!InventoryUtils.areItemsStackable(stack, checkStack)) {
|
||||
continue;
|
||||
}
|
||||
//Output/Input will not match
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package mekanism.common.util;
|
||||
|
||||
import java.util.Map;
|
||||
import net.minecraftforge.items.ItemHandlerHelper;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import mekanism.api.EnumColor;
|
||||
import mekanism.common.Mekanism;
|
||||
@@ -70,12 +71,21 @@ public final class InventoryUtils {
|
||||
return TransitResponse.EMPTY;
|
||||
}
|
||||
|
||||
/**
|
||||
* Like {@link ItemHandlerHelper#canItemStacksStack(ItemStack, ItemStack)} but empty stacks mean equal (either param).
|
||||
* Thiakil: not sure why.
|
||||
*
|
||||
* @param toInsert stack a
|
||||
* @param inSlot stack b
|
||||
* @return true if they are compatible
|
||||
*/
|
||||
public static boolean areItemsStackable(ItemStack toInsert, ItemStack inSlot) {
|
||||
if (toInsert.isEmpty() || inSlot.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return inSlot.isItemEqual(toInsert) && ItemStack.areItemStackTagsEqual(inSlot, toInsert);
|
||||
return ItemHandlerHelper.canItemStacksStack(inSlot, toInsert);
|
||||
//return inSlot.isItemEqual(toInsert) && ItemStack.areItemStackTagsEqual(inSlot, toInsert);
|
||||
}
|
||||
|
||||
public static InvStack takeDefinedItem(TileEntity tile, EnumFacing side, ItemStack type, int min, int max) {
|
||||
@@ -90,7 +100,7 @@ public final class InventoryUtils {
|
||||
for (int i = inventory.getSlots() - 1; i >= 0; i--) {
|
||||
ItemStack stack = inventory.extractItem(i, max, true);
|
||||
|
||||
if (!stack.isEmpty() && StackUtils.equalsWildcard(stack, type)) {
|
||||
if (!stack.isEmpty() && StackUtils.equalsWildcardWithNBT(stack, type)) {
|
||||
int current = ret.getStack().getCount();
|
||||
|
||||
if (current + stack.getCount() <= max) {
|
||||
|
||||
@@ -11,8 +11,8 @@ public final class StackUtils {
|
||||
|
||||
//ignores count
|
||||
public static boolean equalsWildcard(ItemStack wild, ItemStack check) {
|
||||
if (wild.isEmpty() || check.isEmpty()) {
|
||||
return check == wild;
|
||||
if (wild.isEmpty() && check.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return wild.getItem() == check.getItem() && (wild.getItemDamage() == OreDictionary.WILDCARD_VALUE
|
||||
|
||||
Reference in new issue
Block a user