diff --git a/src/main/java/gregtech/api/util/GTUtility.java b/src/main/java/gregtech/api/util/GTUtility.java index 08a1711b976..8eeb71753eb 100644 --- a/src/main/java/gregtech/api/util/GTUtility.java +++ b/src/main/java/gregtech/api/util/GTUtility.java @@ -4741,7 +4741,8 @@ public static ItemId create(NBTTagCompound tag) { return new AutoValue_GTUtility_ItemId( Item.getItemById(tag.getShort("item")), tag.getShort("meta"), - tag.hasKey("tag", Constants.NBT.TAG_COMPOUND) ? tag.getCompoundTag("tag") : null); + tag.hasKey("tag", Constants.NBT.TAG_COMPOUND) ? tag.getCompoundTag("tag") : null, + tag.hasKey("stackSize", Constants.NBT.TAG_INT) ? tag.getInteger("stackSize") : null); } /** @@ -4753,7 +4754,23 @@ public static ItemId create(ItemStack itemStack) { nbt = (NBTTagCompound) nbt.copy(); } - return new AutoValue_GTUtility_ItemId(itemStack.getItem(), Items.feather.getDamage(itemStack), nbt); + return new AutoValue_GTUtility_ItemId(itemStack.getItem(), Items.feather.getDamage(itemStack), nbt, null); + } + + /** + * This method copies NBT, as it is mutable. + */ + public static ItemId createWithStackSize(ItemStack itemStack) { + NBTTagCompound nbt = itemStack.getTagCompound(); + if (nbt != null) { + nbt = (NBTTagCompound) nbt.copy(); + } + + return new AutoValue_GTUtility_ItemId( + itemStack.getItem(), + Items.feather.getDamage(itemStack), + nbt, + itemStack.stackSize); } /** @@ -4763,21 +4780,32 @@ public static ItemId create(Item item, int metaData, @Nullable NBTTagCompound nb if (nbt != null) { nbt = (NBTTagCompound) nbt.copy(); } - return new AutoValue_GTUtility_ItemId(item, metaData, nbt); + return new AutoValue_GTUtility_ItemId(item, metaData, nbt, null); + } + + /** + * This method copies NBT, as it is mutable. + */ + public static ItemId create(Item item, int metaData, @Nullable NBTTagCompound nbt, + @Nullable Integer stackSize) { + if (nbt != null) { + nbt = (NBTTagCompound) nbt.copy(); + } + return new AutoValue_GTUtility_ItemId(item, metaData, nbt, stackSize); } /** * This method stores metadata as wildcard and NBT as null. */ public static ItemId createAsWildcard(ItemStack itemStack) { - return new AutoValue_GTUtility_ItemId(itemStack.getItem(), W, null); + return new AutoValue_GTUtility_ItemId(itemStack.getItem(), W, null, null); } /** * This method stores NBT as null. */ public static ItemId createWithoutNBT(ItemStack itemStack) { - return new AutoValue_GTUtility_ItemId(itemStack.getItem(), Items.feather.getDamage(itemStack), null); + return new AutoValue_GTUtility_ItemId(itemStack.getItem(), Items.feather.getDamage(itemStack), null, null); } /** @@ -4787,14 +4815,26 @@ public static ItemId createNoCopy(ItemStack itemStack) { return new AutoValue_GTUtility_ItemId( itemStack.getItem(), Items.feather.getDamage(itemStack), - itemStack.getTagCompound()); + itemStack.getTagCompound(), + null); + } + + /** + * This method does not copy NBT in order to save time. Make sure not to mutate it! + */ + public static ItemId createNoCopyWithStackSize(ItemStack itemStack) { + return new AutoValue_GTUtility_ItemId( + itemStack.getItem(), + Items.feather.getDamage(itemStack), + itemStack.getTagCompound(), + itemStack.stackSize); } /** * This method does not copy NBT in order to save time. Make sure not to mutate it! */ public static ItemId createNoCopy(Item item, int metaData, @Nullable NBTTagCompound nbt) { - return new AutoValue_GTUtility_ItemId(item, metaData, nbt); + return new AutoValue_GTUtility_ItemId(item, metaData, nbt, null); } protected abstract Item item(); @@ -4804,11 +4844,15 @@ public static ItemId createNoCopy(Item item, int metaData, @Nullable NBTTagCompo @Nullable protected abstract NBTTagCompound nbt(); + @Nullable + protected abstract Integer stackSize(); + public NBTTagCompound writeToNBT() { NBTTagCompound tag = new NBTTagCompound(); tag.setShort("item", (short) Item.getIdFromItem(item())); tag.setShort("meta", (short) metaData()); if (nbt() != null) tag.setTag("tag", nbt()); + if (stackSize() != null) tag.setInteger("stackSize", stackSize()); return tag; } diff --git a/src/main/java/kubatech/api/DynamicInventory.java b/src/main/java/kubatech/api/DynamicInventory.java index ef89c3a341f..f718101384f 100644 --- a/src/main/java/kubatech/api/DynamicInventory.java +++ b/src/main/java/kubatech/api/DynamicInventory.java @@ -34,8 +34,8 @@ import com.gtnewhorizons.modularui.common.widget.DynamicPositionedRow; import com.gtnewhorizons.modularui.common.widget.FakeSyncWidget; import com.gtnewhorizons.modularui.common.widget.Scrollable; -import com.kuba6000.mobsinfo.api.utils.ItemID; +import gregtech.api.util.GTUtility.ItemId; import kubatech.api.helpers.GTHelper; import kubatech.api.utils.ModUtils; @@ -117,19 +117,19 @@ public Widget asWidget(ModularWindow.Builder builder, UIBuildContext buildContex } }), builder) .attachSyncer(new FakeSyncWidget.ListSyncer<>(() -> { - HashMap itemMap = new HashMap<>(); - HashMap stackMap = new HashMap<>(); - HashMap> realSlotMap = new HashMap<>(); + HashMap itemMap = new HashMap<>(); + HashMap stackMap = new HashMap<>(); + HashMap> realSlotMap = new HashMap<>(); for (int i = 0, mStorageSize = inventory.size(); i < mStorageSize; i++) { ItemStack stack = inventoryGetter.get(inventory.get(i)); - ItemID id = ItemID.createNoCopy(stack, false); + ItemId id = ItemId.createNoCopyWithStackSize(stack); itemMap.merge(id, 1, Integer::sum); stackMap.putIfAbsent(id, stack); realSlotMap.computeIfAbsent(id, unused -> new ArrayList<>()) .add(i); } List newDrawables = new ArrayList<>(); - for (Map.Entry entry : itemMap.entrySet()) { + for (Map.Entry entry : itemMap.entrySet()) { newDrawables.add( new GTHelper.StackableItemSlot( entry.getValue(), @@ -172,19 +172,19 @@ private Widget createWidget(EntityPlayer player) { ArrayList buttons = new ArrayList<>(); if (!ModUtils.isClientThreaded()) { - HashMap itemMap = new HashMap<>(); - HashMap stackMap = new HashMap<>(); - HashMap> realSlotMap = new HashMap<>(); + HashMap itemMap = new HashMap<>(); + HashMap stackMap = new HashMap<>(); + HashMap> realSlotMap = new HashMap<>(); for (int i = 0, inventorySize = inventory.size(); i < inventorySize; i++) { ItemStack stack = inventoryGetter.get(inventory.get(i)); - ItemID id = ItemID.createNoCopy(stack, false); + ItemId id = ItemId.createNoCopyWithStackSize(stack); itemMap.merge(id, 1, Integer::sum); stackMap.putIfAbsent(id, stack); realSlotMap.computeIfAbsent(id, unused -> new ArrayList<>()) .add(i); } drawables = new ArrayList<>(); - for (Map.Entry entry : itemMap.entrySet()) { + for (Map.Entry entry : itemMap.entrySet()) { drawables.add( new GTHelper.StackableItemSlot( entry.getValue(), diff --git a/src/main/java/kubatech/api/EIGDynamicInventory.java b/src/main/java/kubatech/api/EIGDynamicInventory.java index 1c703fe2fa2..1b709a26544 100644 --- a/src/main/java/kubatech/api/EIGDynamicInventory.java +++ b/src/main/java/kubatech/api/EIGDynamicInventory.java @@ -3,7 +3,6 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Collections; -import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Objects; @@ -33,7 +32,6 @@ import com.gtnewhorizons.modularui.common.widget.DynamicPositionedRow; import com.gtnewhorizons.modularui.common.widget.FakeSyncWidget; import com.gtnewhorizons.modularui.common.widget.Scrollable; -import com.kuba6000.mobsinfo.api.utils.ItemID; import kubatech.api.gui.AutoScalingStackSizeText; import kubatech.api.helpers.GTHelper; @@ -205,9 +203,6 @@ private Widget createWidget(EntityPlayer player) { ArrayList buttons = new ArrayList<>(); if (!ModUtils.isClientThreaded()) { - HashMap itemMap = new HashMap<>(); - HashMap stackMap = new HashMap<>(); - HashMap> realSlotMap = new HashMap<>(); drawables = new ArrayList<>(); for (int i = 0, inventorySize = inventory.size(); i < inventorySize; i++) { T slot = inventory.get(i); @@ -215,8 +210,7 @@ private Widget createWidget(EntityPlayer player) { continue; } ItemStack stack = inventoryGetter.get(slot); - drawables - .add(new GTHelper.StackableItemSlot(1, stack, new ArrayList(Collections.singleton(i)))); + drawables.add(new GTHelper.StackableItemSlot(1, stack, new ArrayList<>(Collections.singleton(i)))); } } diff --git a/src/main/java/kubatech/api/helpers/GTHelper.java b/src/main/java/kubatech/api/helpers/GTHelper.java index fe6a2a61672..645cd49356a 100644 --- a/src/main/java/kubatech/api/helpers/GTHelper.java +++ b/src/main/java/kubatech/api/helpers/GTHelper.java @@ -28,16 +28,15 @@ import net.minecraft.item.ItemStack; import net.minecraft.network.PacketBuffer; -import com.kuba6000.mobsinfo.api.utils.ItemID; - import gregtech.api.metatileentity.implementations.MTEHatchEnergy; import gregtech.api.metatileentity.implementations.MTEMultiBlockBase; +import gregtech.api.util.GTUtility.ItemId; import kubatech.api.implementations.KubaTechGTMultiBlockBase; public class GTHelper { public static long getMaxInputEU(MTEMultiBlockBase mte) { - if (mte instanceof KubaTechGTMultiBlockBase) return ((KubaTechGTMultiBlockBase) mte).getMaxInputEu(); + if (mte instanceof KubaTechGTMultiBlockBase) return mte.getMaxInputEu(); long rEU = 0; for (MTEHatchEnergy tHatch : mte.mEnergyHatches) if (tHatch.isValid()) rEU += tHatch.maxEUInput() * tHatch.maxAmperesIn(); @@ -65,11 +64,14 @@ public static class StackableItemSlot { public StackableItemSlot(int count, ItemStack stack, ArrayList realSlots) { this.count = count; this.stack = stack; + this.hashcode = ItemId.createNoCopyWithStackSize(stack) + .hashCode(); this.realSlots = realSlots; } public final int count; public final ItemStack stack; + private final int hashcode; public final ArrayList realSlots; public void write(PacketBuffer buffer) throws IOException { @@ -87,13 +89,8 @@ public static StackableItemSlot read(PacketBuffer buffer) throws IOException { @Override public boolean equals(Object obj) { if (this == obj) return true; - if (!(obj instanceof StackableItemSlot)) return false; - StackableItemSlot other = (StackableItemSlot) obj; - return count == other.count && ItemID.createNoCopy(stack, false) - .hashCode() - == ItemID.createNoCopy(other.stack, false) - .hashCode() - && realSlots.equals(other.realSlots); + if (!(obj instanceof StackableItemSlot other)) return false; + return count == other.count && hashcode == other.hashcode && realSlots.equals(other.realSlots); } } } diff --git a/src/main/java/kubatech/loaders/TCLoader.java b/src/main/java/kubatech/loaders/TCLoader.java index 500dab7f3e3..75113eccb83 100644 --- a/src/main/java/kubatech/loaders/TCLoader.java +++ b/src/main/java/kubatech/loaders/TCLoader.java @@ -32,9 +32,8 @@ import net.minecraft.item.ItemStack; import net.minecraft.world.World; -import com.kuba6000.mobsinfo.api.utils.ItemID; - import cpw.mods.fml.common.registry.GameRegistry; +import gregtech.api.util.GTUtility.ItemId; import kubatech.api.enums.ItemList; import kubatech.loaders.item.items.ItemTeaUltimate; import thaumcraft.api.ThaumcraftApi; @@ -84,8 +83,8 @@ private static void registerRecipe() { ItemList.MilkTea.get(1), ItemList.OolongTea.get(1), ItemList.PeppermintTea.get(1), ItemList.PuerhTea.get(1), ItemList.WhiteTea.get(1), ItemList.YellowTea.get(1) }; - final HashSet componentsHashed = Arrays.stream(components) - .map(stack -> ItemID.createNoCopy(stack, true, false, true)) + final HashSet componentsHashed = Arrays.stream(components) + .map(ItemId::createWithoutNBT) .collect(Collectors.toCollection(HashSet::new)); // noinspection unchecked @@ -109,8 +108,8 @@ public boolean matches(ArrayList input, ItemStack central, World worl if (!ThaumcraftApiHelper.isResearchComplete(player.getCommandSenderName(), this.research)) return false; if (componentsHashed.size() > input.size()) return false; - HashSet hashedInputs = input.stream() - .map(stack -> ItemID.createNoCopy(stack, true, false, true)) + HashSet hashedInputs = input.stream() + .map(ItemId::createWithoutNBT) .collect(Collectors.toCollection(HashSet::new)); return hashedInputs.containsAll(componentsHashed); } diff --git a/src/main/java/kubatech/tileentity/gregtech/multiblock/MTEMegaIndustrialApiary.java b/src/main/java/kubatech/tileentity/gregtech/multiblock/MTEMegaIndustrialApiary.java index 0ea03b9407f..6a8f34e2da0 100644 --- a/src/main/java/kubatech/tileentity/gregtech/multiblock/MTEMegaIndustrialApiary.java +++ b/src/main/java/kubatech/tileentity/gregtech/multiblock/MTEMegaIndustrialApiary.java @@ -92,7 +92,6 @@ import com.gtnewhorizons.modularui.common.widget.FakeSyncWidget; import com.gtnewhorizons.modularui.common.widget.SlotWidget; import com.gtnewhorizons.modularui.common.widget.TextWidget; -import com.kuba6000.mobsinfo.api.utils.ItemID; import bartworks.API.BorosilicateGlass; import cpw.mods.fml.relauncher.Side; @@ -121,6 +120,7 @@ import gregtech.api.recipe.check.SimpleCheckRecipeResult; import gregtech.api.render.TextureFactory; import gregtech.api.util.GTUtility; +import gregtech.api.util.GTUtility.ItemId; import gregtech.api.util.MultiblockTooltipBuilder; import ic2.core.init.BlocksItems; import ic2.core.init.InternalName; @@ -927,13 +927,13 @@ protected void drawTexts(DynamicPositionedColumn screenElements, SlotWidget inve screenElements.widget(new FakeSyncWidget.IntegerSyncer(() -> mSecondaryMode, b -> mSecondaryMode = b)); screenElements.widget(new FakeSyncWidget<>(() -> { HashMap ret = new HashMap<>(); - HashMap dropProgress = new HashMap<>(); + HashMap dropProgress = new HashMap<>(); - for (Map.Entry drop : this.dropProgress.entrySet()) { + for (Map.Entry drop : this.dropProgress.entrySet()) { dropProgress.merge(drop.getKey(), drop.getValue(), Double::sum); } - for (Map.Entry drop : dropProgress.entrySet()) { + for (Map.Entry drop : dropProgress.entrySet()) { ret.put(BeeSimulator.dropstacks.get(drop.getKey()), drop.getValue()); } return ret; @@ -962,7 +962,7 @@ protected void drawTexts(DynamicPositionedColumn screenElements, SlotWidget inve super.drawTexts(screenElements, inventorySlot); } - final HashMap dropProgress = new HashMap<>(); + final HashMap dropProgress = new HashMap<>(); private static class BeeSimulator { @@ -1058,7 +1058,7 @@ public NBTTagCompound toNBTTagCompound() { return tag; } - static final Map dropstacks = new HashMap<>(); + static final Map dropstacks = new HashMap<>(); public List getDrops(final MTEMegaIndustrialApiary MTE, final double timePassed) { drops.forEach(d -> { @@ -1103,7 +1103,7 @@ private static class BeeDrop { private static final float MAX_PRODUCTION_MODIFIER_FROM_UPGRADES = 17.19926784f; // 4*1.2^8 final ItemStack stack; double amount; - final ItemID id; + final ItemId id; final float chance; final float beeSpeed; @@ -1114,7 +1114,7 @@ public BeeDrop(ItemStack stack, float chance, float beeSpeed, float t) { this.chance = chance; this.beeSpeed = beeSpeed; this.t = t; - id = ItemID.createNoCopy(this.stack); + id = ItemId.createNoCopy(this.stack); evaluate(); } @@ -1150,7 +1150,7 @@ public BeeDrop(NBTTagCompound tag) { beeSpeed = tag.getFloat("beeSpeed"); t = tag.getFloat("t"); amount = tag.getDouble("amount"); - id = ItemID.createNoCopy(stack); + id = ItemId.createNoCopy(stack); } public NBTTagCompound toNBTTagCompound() {