Skip to content

Commit

Permalink
Remove hard dep on mobs-info (GTNewHorizons#3053)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Robertz <[email protected]>
  • Loading branch information
kuba6000 and Dream-Master authored Sep 4, 2024
1 parent d00d97c commit 5b5311a
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 50 deletions.
58 changes: 51 additions & 7 deletions src/main/java/gregtech/api/util/GTUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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();
Expand All @@ -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;
}

Expand Down
22 changes: 11 additions & 11 deletions src/main/java/kubatech/api/DynamicInventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -117,19 +117,19 @@ public Widget asWidget(ModularWindow.Builder builder, UIBuildContext buildContex
}
}), builder)
.attachSyncer(new FakeSyncWidget.ListSyncer<>(() -> {
HashMap<ItemID, Integer> itemMap = new HashMap<>();
HashMap<ItemID, ItemStack> stackMap = new HashMap<>();
HashMap<ItemID, ArrayList<Integer>> realSlotMap = new HashMap<>();
HashMap<ItemId, Integer> itemMap = new HashMap<>();
HashMap<ItemId, ItemStack> stackMap = new HashMap<>();
HashMap<ItemId, ArrayList<Integer>> 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<GTHelper.StackableItemSlot> newDrawables = new ArrayList<>();
for (Map.Entry<ItemID, Integer> entry : itemMap.entrySet()) {
for (Map.Entry<ItemId, Integer> entry : itemMap.entrySet()) {
newDrawables.add(
new GTHelper.StackableItemSlot(
entry.getValue(),
Expand Down Expand Up @@ -172,19 +172,19 @@ private Widget createWidget(EntityPlayer player) {
ArrayList<Widget> buttons = new ArrayList<>();

if (!ModUtils.isClientThreaded()) {
HashMap<ItemID, Integer> itemMap = new HashMap<>();
HashMap<ItemID, ItemStack> stackMap = new HashMap<>();
HashMap<ItemID, ArrayList<Integer>> realSlotMap = new HashMap<>();
HashMap<ItemId, Integer> itemMap = new HashMap<>();
HashMap<ItemId, ItemStack> stackMap = new HashMap<>();
HashMap<ItemId, ArrayList<Integer>> 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<ItemID, Integer> entry : itemMap.entrySet()) {
for (Map.Entry<ItemId, Integer> entry : itemMap.entrySet()) {
drawables.add(
new GTHelper.StackableItemSlot(
entry.getValue(),
Expand Down
8 changes: 1 addition & 7 deletions src/main/java/kubatech/api/EIGDynamicInventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -205,18 +203,14 @@ private Widget createWidget(EntityPlayer player) {
ArrayList<Widget> buttons = new ArrayList<>();

if (!ModUtils.isClientThreaded()) {
HashMap<ItemID, Integer> itemMap = new HashMap<>();
HashMap<ItemID, ItemStack> stackMap = new HashMap<>();
HashMap<ItemID, ArrayList<Integer>> realSlotMap = new HashMap<>();
drawables = new ArrayList<>();
for (int i = 0, inventorySize = inventory.size(); i < inventorySize; i++) {
T slot = inventory.get(i);
if (slot == null) {
continue;
}
ItemStack stack = inventoryGetter.get(slot);
drawables
.add(new GTHelper.StackableItemSlot(1, stack, new ArrayList<Integer>(Collections.singleton(i))));
drawables.add(new GTHelper.StackableItemSlot(1, stack, new ArrayList<>(Collections.singleton(i))));
}
}

Expand Down
17 changes: 7 additions & 10 deletions src/main/java/kubatech/api/helpers/GTHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -65,11 +64,14 @@ public static class StackableItemSlot {
public StackableItemSlot(int count, ItemStack stack, ArrayList<Integer> 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<Integer> realSlots;

public void write(PacketBuffer buffer) throws IOException {
Expand All @@ -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);
}
}
}
11 changes: 5 additions & 6 deletions src/main/java/kubatech/loaders/TCLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ItemID> componentsHashed = Arrays.stream(components)
.map(stack -> ItemID.createNoCopy(stack, true, false, true))
final HashSet<ItemId> componentsHashed = Arrays.stream(components)
.map(ItemId::createWithoutNBT)
.collect(Collectors.toCollection(HashSet::new));

// noinspection unchecked
Expand All @@ -109,8 +108,8 @@ public boolean matches(ArrayList<ItemStack> input, ItemStack central, World worl
if (!ThaumcraftApiHelper.isResearchComplete(player.getCommandSenderName(), this.research))
return false;
if (componentsHashed.size() > input.size()) return false;
HashSet<ItemID> hashedInputs = input.stream()
.map(stack -> ItemID.createNoCopy(stack, true, false, true))
HashSet<ItemId> hashedInputs = input.stream()
.map(ItemId::createWithoutNBT)
.collect(Collectors.toCollection(HashSet::new));
return hashedInputs.containsAll(componentsHashed);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<ItemStack, Double> ret = new HashMap<>();
HashMap<ItemID, Double> dropProgress = new HashMap<>();
HashMap<ItemId, Double> dropProgress = new HashMap<>();

for (Map.Entry<ItemID, Double> drop : this.dropProgress.entrySet()) {
for (Map.Entry<ItemId, Double> drop : this.dropProgress.entrySet()) {
dropProgress.merge(drop.getKey(), drop.getValue(), Double::sum);
}

for (Map.Entry<ItemID, Double> drop : dropProgress.entrySet()) {
for (Map.Entry<ItemId, Double> drop : dropProgress.entrySet()) {
ret.put(BeeSimulator.dropstacks.get(drop.getKey()), drop.getValue());
}
return ret;
Expand Down Expand Up @@ -962,7 +962,7 @@ protected void drawTexts(DynamicPositionedColumn screenElements, SlotWidget inve
super.drawTexts(screenElements, inventorySlot);
}

final HashMap<ItemID, Double> dropProgress = new HashMap<>();
final HashMap<ItemId, Double> dropProgress = new HashMap<>();

private static class BeeSimulator {

Expand Down Expand Up @@ -1058,7 +1058,7 @@ public NBTTagCompound toNBTTagCompound() {
return tag;
}

static final Map<ItemID, ItemStack> dropstacks = new HashMap<>();
static final Map<ItemId, ItemStack> dropstacks = new HashMap<>();

public List<ItemStack> getDrops(final MTEMegaIndustrialApiary MTE, final double timePassed) {
drops.forEach(d -> {
Expand Down Expand Up @@ -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;
Expand All @@ -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();
}

Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 5b5311a

Please sign in to comment.