From 444fa24941460f24b1328ff435e379077dc4afca Mon Sep 17 00:00:00 2001 From: D-Cysteine <54219287+D-Cysteine@users.noreply.github.com> Date: Fri, 29 Oct 2021 21:54:06 -0600 Subject: Make metaitem.98 more flexible, and add oredict support for steam cell --- src/main/java/gregtech/common/GT_Proxy.java | 3 - .../common/items/GT_MetaGenerated_Item_98.java | 99 ++++++++++++++++------ 2 files changed, 74 insertions(+), 28 deletions(-) (limited to 'src/main/java/gregtech/common') diff --git a/src/main/java/gregtech/common/GT_Proxy.java b/src/main/java/gregtech/common/GT_Proxy.java index dc9db6e8e3..2e18ea9693 100644 --- a/src/main/java/gregtech/common/GT_Proxy.java +++ b/src/main/java/gregtech/common/GT_Proxy.java @@ -26,7 +26,6 @@ import gregtech.api.util.*; import gregtech.common.entities.GT_Entity_Arrow; import gregtech.common.gui.GT_ContainerVolumetricFlask; import gregtech.common.gui.GT_GUIContainerVolumetricFlask; -import gregtech.common.items.GT_MetaGenerated_Item_98; import gregtech.common.items.GT_MetaGenerated_Tool_01; import net.minecraft.block.Block; import net.minecraft.enchantment.Enchantment; @@ -1813,8 +1812,6 @@ public abstract class GT_Proxy implements IGT_Mod, IGuiHandler, IFuelHandler { new FluidStack(crackedFluids[i], 1000), 40 + 20 * i, 240 + 120 * i); GT_Values.RA.addChemicalRecipe(GT_ModHandler.getIC2Item("steamCell", 1L), GT_Utility.getIntegratedCircuit(i + 1), new FluidStack(uncrackedFluid, 1000), new FluidStack(crackedFluids[i], 800), Materials.Empty.getCells(1), 160 + 80 * i, 30); - GT_Values.RA.addChemicalRecipe(GT_MetaGenerated_Item_98.FluidCell.steam.get(), GT_Utility.getIntegratedCircuit(i + 1), new FluidStack(uncrackedFluid, 1000), - new FluidStack(crackedFluids[i], 800), Materials.Empty.getCells(1), 160 + 80 * i, 30); GT_Values.RA.addChemicalRecipe(aMaterial.getCells(1), GT_Utility.getIntegratedCircuit(i + 1), GT_ModHandler.getSteam(1000), new FluidStack(crackedFluids[i], 800), Materials.Empty.getCells(1), 160 + 80 * i, 30); } diff --git a/src/main/java/gregtech/common/items/GT_MetaGenerated_Item_98.java b/src/main/java/gregtech/common/items/GT_MetaGenerated_Item_98.java index b6123fb2d0..c19ed01d30 100644 --- a/src/main/java/gregtech/common/items/GT_MetaGenerated_Item_98.java +++ b/src/main/java/gregtech/common/items/GT_MetaGenerated_Item_98.java @@ -8,6 +8,8 @@ import gregtech.api.enums.OrePrefixes; import gregtech.api.interfaces.IIconContainer; import gregtech.api.items.GT_MetaGenerated_Item; import gregtech.api.util.GT_LanguageManager; +import gregtech.api.util.GT_ModHandler; +import gregtech.api.util.GT_OreDictUnificator; import gregtech.api.util.GT_Utility; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.item.Item; @@ -18,39 +20,67 @@ import net.minecraftforge.fluids.FluidContainerRegistry; import net.minecraftforge.fluids.FluidRegistry; import net.minecraftforge.fluids.FluidStack; +import javax.annotation.Nullable; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; /** This class holds cells for non-GT fluids. */ public class GT_MetaGenerated_Item_98 extends GT_MetaGenerated_Item { public static GT_MetaGenerated_Item_98 INSTANCE; /** - * Map of internal fluid name to cell type to register for that fluid. - * - *

The fluid at index {@code i} (in entry set iteration order) will be assigned ID {@code i}. + * Registered fluids. * *

When adding a fluid, don't forget to make sure that GregTech loads after the mod that adds * that fluid! * - *

In order to avoid breaking existing worlds, the entries in this list must not be - * re-ordered or removed! The only safe modification that can be made to this list is adding new - * entries to the end. To remove an entry, pass {@code null} in for the fluid name. + *

In order to avoid breaking existing worlds, fluids must not have their IDs changed! The + * only safe modification that can be made to this list is adding new fluids, or removing + * existing fluids. When removing fluids, maybe leave a comment mentioning the old ID, so that + * we don't re-use it for a new fluid. */ public enum FluidCell { - steam(CellType.REGULAR), - bacterialsludge(CellType.REGULAR), - mutagen(CellType.REGULAR), - ender(CellType.REGULAR), - endergoo(CellType.REGULAR), + // Next unused ID: 5 + + // Railcraft + STEAM(0, "steam", CellType.REGULAR), + + // Galacticraft + BACTERIAL_SLUDGE(1, "bacterialsludge", CellType.REGULAR), + + // Gendustry + MUTAGEN(2, "mutagen", CellType.REGULAR), + + // Tinker's Construct + LIQUID_ENDER(3, "ender", CellType.REGULAR), + + // Hardcore Ender Expansion + ENDER_GOO(4, "endergoo", CellType.REGULAR), ; + private final int mId; + /** This is the Forge internal fluid name. */ + private final String mfluidName; private final CellType mType; + + @Nullable private ItemStack mStack; - FluidCell(CellType aType) { - this.mType = aType; + FluidCell(int aId, String aFluidName, CellType aType) { + mId = aId; + mfluidName = aFluidName; + mType = aType; + } + + public int getId() { + return mId; + } + + public String getFluidName() { + return mfluidName; } public CellType getDisplayType() { @@ -62,6 +92,7 @@ public class GT_MetaGenerated_Item_98 extends GT_MetaGenerated_Item { * * Might return null if not yet initialized, or the fluid referenced does not exist. */ + @Nullable public ItemStack get() { return GT_Utility.copy(mStack); } @@ -73,6 +104,7 @@ public class GT_MetaGenerated_Item_98 extends GT_MetaGenerated_Item { * * Use with caution. */ + @Nullable public ItemStack getNoCopy() { return mStack; } @@ -82,17 +114,14 @@ public class GT_MetaGenerated_Item_98 extends GT_MetaGenerated_Item { * * Might return null if not yet initialized, or the fluid referenced does not exist. */ + @Nullable public ItemStack get(int aStackSize) { return GT_Utility.copyAmount(aStackSize, mStack); } - void setStack(ItemStack mStack) { + private void setStack(ItemStack mStack) { this.mStack = mStack; } - - public int getId() { - return ordinal(); - } } /** @@ -139,6 +168,9 @@ public class GT_MetaGenerated_Item_98 extends GT_MetaGenerated_Item { public static synchronized void init() { if (INSTANCE == null) INSTANCE = new GT_MetaGenerated_Item_98(); + + INSTANCE.createItems(); + INSTANCE.registerOreDict(); } private GT_MetaGenerated_Item_98() { @@ -148,10 +180,14 @@ public class GT_MetaGenerated_Item_98 extends GT_MetaGenerated_Item { super("metaitem.98", (short) 32766, (short) FluidCell.values().length); registeredFluidDataMap = new HashMap<>(); + } + + private void createItems() { + ItemStack emptyCell = ItemList.Cell_Empty.get(1L); for (FluidCell tCell : FluidCell.values()) { - int i = tCell.getId(); // Increment first so that we don't accidentally skip doing so with continue - String fluidName = tCell.name(); + int id = tCell.getId(); + String fluidName = tCell.getFluidName(); CellType cellType = tCell.getDisplayType(); Fluid fluid = FluidRegistry.getFluid(fluidName); @@ -161,12 +197,12 @@ public class GT_MetaGenerated_Item_98 extends GT_MetaGenerated_Item { continue; } - ItemStack itemStack = new ItemStack(this, 1, i); + ItemStack itemStack = new ItemStack(this, 1, id); FluidStack fluidStack = new FluidStack(fluid, cellType.capacity); FluidContainerRegistry.registerFluidContainer( new FluidContainerRegistry.FluidContainerData( - fluidStack, itemStack, ItemList.Cell_Empty.get(1L))); + fluidStack, itemStack, emptyCell)); tCell.setStack(itemStack); @@ -183,7 +219,7 @@ public class GT_MetaGenerated_Item_98 extends GT_MetaGenerated_Item { // We'll just steal the icons from Water. They are all the same anyway (except _NULL is broken for cells). IIconContainer iconContainer = Materials.Water.mIconSet.mTextures[cellType.prefix.mTextureIndex]; - registeredFluidDataMap.put(i, new RegisteredFluidData(fluid, rgba, iconContainer)); + registeredFluidDataMap.put(id, new RegisteredFluidData(fluid, rgba, iconContainer)); } // We're not going to use these BitSets, so clear them to save memory. @@ -191,6 +227,18 @@ public class GT_MetaGenerated_Item_98 extends GT_MetaGenerated_Item { mVisibleItems.clear(); } + private void registerOreDict() { + // The GregTech ore dictionary requires an entry in the Materials enum, and since the whole + // point of this class is to add cell items for non-GregTech fluids, the vast majority of + // cell items won't have an associated material. So only a rare few cell items will need to + // be registered. + + // Register IC2 steam cell and Railcraft steam cell as synonyms. + // There is no steam material, so we'll use Water.cellMolten instead. + GT_OreDictUnificator.add(OrePrefixes.cellMolten, Materials.Water, GT_ModHandler.getIC2Item("steamCell", 1L)); + GT_OreDictUnificator.add(OrePrefixes.cellMolten, Materials.Water, FluidCell.STEAM.getNoCopy()); + } + @Override public short[] getRGBa(ItemStack aStack) { RegisteredFluidData fluidData = registeredFluidDataMap.get(aStack.getItemDamage()); @@ -209,8 +257,9 @@ public class GT_MetaGenerated_Item_98 extends GT_MetaGenerated_Item { @Override @SideOnly(Side.CLIENT) public void getSubItems(Item var1, CreativeTabs aCreativeTab, List aList) { - registeredFluidDataMap.keySet().stream() - .map(i -> new ItemStack(this, 1, i)) + Arrays.stream(FluidCell.values()) + .map(FluidCell::get) + .filter(Objects::nonNull) .forEach(aList::add); } -- cgit From f9e799cc686a8e69c66a8f034ec39acb821d575d Mon Sep 17 00:00:00 2001 From: D-Cysteine <54219287+D-Cysteine@users.noreply.github.com> Date: Sat, 30 Oct 2021 01:38:15 -0600 Subject: Add support for more fluids --- src/main/java/gregtech/common/GT_Proxy.java | 6 ++ .../common/items/GT_MetaGenerated_Item_98.java | 88 ++++++++++++++++------ 2 files changed, 73 insertions(+), 21 deletions(-) (limited to 'src/main/java/gregtech/common') diff --git a/src/main/java/gregtech/common/GT_Proxy.java b/src/main/java/gregtech/common/GT_Proxy.java index 2e18ea9693..ed99c1e3c0 100644 --- a/src/main/java/gregtech/common/GT_Proxy.java +++ b/src/main/java/gregtech/common/GT_Proxy.java @@ -26,6 +26,7 @@ import gregtech.api.util.*; import gregtech.common.entities.GT_Entity_Arrow; import gregtech.common.gui.GT_ContainerVolumetricFlask; import gregtech.common.gui.GT_GUIContainerVolumetricFlask; +import gregtech.common.items.GT_MetaGenerated_Item_98; import gregtech.common.items.GT_MetaGenerated_Tool_01; import net.minecraft.block.Block; import net.minecraft.enchantment.Enchantment; @@ -649,6 +650,11 @@ public abstract class GT_Proxy implements IGT_Mod, IGuiHandler, IFuelHandler { new Thread(new GT_PlayerActivityLogger()).start(); } GregTech_API.sPostloadStarted = true; + + // This needs to happen late enough that all of the fluids we need have been registered. + // onLoad() seems to be too early, as the New Horizons Core Mod registers some fluids in post-load. + GT_MetaGenerated_Item_98.init(); + GT_OreDictUnificator.addItemData(new ItemStack(Items.iron_door, 1), new ItemData(Materials.Iron, 21772800L)); GT_OreDictUnificator.addItemData(new ItemStack(Items.wooden_door, 1, 32767), new ItemData(Materials.Wood, 21772800L)); for (FluidContainerRegistry.FluidContainerData tData : FluidContainerRegistry.getRegisteredFluidContainerData()) { diff --git a/src/main/java/gregtech/common/items/GT_MetaGenerated_Item_98.java b/src/main/java/gregtech/common/items/GT_MetaGenerated_Item_98.java index c19ed01d30..09b884eb69 100644 --- a/src/main/java/gregtech/common/items/GT_MetaGenerated_Item_98.java +++ b/src/main/java/gregtech/common/items/GT_MetaGenerated_Item_98.java @@ -22,10 +22,13 @@ import net.minecraftforge.fluids.FluidStack; import javax.annotation.Nullable; import java.util.Arrays; +import java.util.EnumMap; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.Set; /** This class holds cells for non-GT fluids. */ public class GT_MetaGenerated_Item_98 extends GT_MetaGenerated_Item { @@ -43,16 +46,39 @@ public class GT_MetaGenerated_Item_98 extends GT_MetaGenerated_Item { * we don't re-use it for a new fluid. */ public enum FluidCell { - // Next unused ID: 5 + // Next unused ID: 18 - // Railcraft - STEAM(0, "steam", CellType.REGULAR), + // GregTech + DRILLING_FLUID(5, "liquid_drillingfluid", CellType.REGULAR), + SQUID_INK(6, "squidink", CellType.SMALL), + + // New Horizons Core Mod + UNKNOWN_NUTRIENT_AGAR(7, "unknownnutrientagar", CellType.REGULAR), + SEAWEED_BROTH(8, "seaweedbroth", CellType.REGULAR), + SUPER_LIGHT_RADOX(9, "superlightradox", CellType.REGULAR), + + // BartWorks + ENZYME_SOLUTION(10, "enzymessollution", CellType.REGULAR), + ESCHERICHIA_COLI_FLUID(11, "escherichiakolifluid", CellType.REGULAR), + PENICILLIN(12, "penicillin", CellType.REGULAR), + FLUORESCENT_DNA(13, "fluorecentddna", CellType.REGULAR), + + // Good Generator + COMBUSTION_PROMOTER(14, "combustionpromotor", CellType.REGULAR), // Galacticraft BACTERIAL_SLUDGE(1, "bacterialsludge", CellType.REGULAR), + // Railcraft + STEAM(0, "steam", CellType.REGULAR), + // Gendustry + BACTERIA(15, "binnie.bacteria", CellType.REGULAR), MUTAGEN(2, "mutagen", CellType.REGULAR), + LIQUID_DNA(16, "liquiddna", CellType.REGULAR), + + // Genetics + POLYMERASE(17, "polymerase", CellType.REGULAR), // Tinker's Construct LIQUID_ENDER(3, "ender", CellType.REGULAR), @@ -124,17 +150,12 @@ public class GT_MetaGenerated_Item_98 extends GT_MetaGenerated_Item { } } - /** - * We support adding two different types of cells. - * - *

Regular cells have capacity 1000 and use the regular cell icon. Molten cells have capacity - * 144 and use the molten cell icon. - */ + /** Cell type specifies the cell capacity, appearance, and item name format. */ private enum CellType { REGULAR(1_000, OrePrefixes.cell), - MOLTEN(144, OrePrefixes.cellMolten); - // We could also add plasma cells (cellPlasma) here if we need to. - // Plasma cells look like molten cells, but have 1000 capacity. + SMALL(144, OrePrefixes.cell), + MOLTEN(144, OrePrefixes.cellMolten), + PLASMA(1_000, OrePrefixes.cellPlasma); private final int capacity; private final OrePrefixes prefix; @@ -164,32 +185,54 @@ public class GT_MetaGenerated_Item_98 extends GT_MetaGenerated_Item { *

Only contains IDs that were successfully registered. */ private final Map registeredFluidDataMap; - - public static synchronized void init() { - if (INSTANCE == null) - INSTANCE = new GT_MetaGenerated_Item_98(); - - INSTANCE.createItems(); - INSTANCE.registerOreDict(); - } + private final EnumMap iconContainerMap; private GT_MetaGenerated_Item_98() { // For some reason, fluid cells will be rendered only if the metadata ID is less than the // offset. So we will specify maximum offset here. // See: GT_MetaGenerated_Item_Renderer.java - super("metaitem.98", (short) 32766, (short) FluidCell.values().length); + super("metaitem.98", (short) 32766, (short) 0); registeredFluidDataMap = new HashMap<>(); + iconContainerMap = new EnumMap<>(CellType.class); + } + + /** + * Loading needs to happen after the fluids we need have been registered, which means during post-load. + * However, cell icons seem to be deleted some time between load and post-load, so we must pre-cache them. + */ + public static synchronized void preInit() { + if (INSTANCE == null) + INSTANCE = new GT_MetaGenerated_Item_98(); + + // We'll just steal the icons from Water. They are all the same anyway (except _NULL is broken for cells). + for (CellType cellType : CellType.values()) { + IIconContainer iconContainer = Materials.Water.mIconSet.mTextures[cellType.prefix.mTextureIndex]; + INSTANCE.iconContainerMap.put(cellType, iconContainer); + } + } + + public static synchronized void init() { + INSTANCE.createItems(); + INSTANCE.registerOreDict(); } private void createItems() { ItemStack emptyCell = ItemList.Cell_Empty.get(1L); + // We'll check for ID uniqueness. Better to throw an exception than silently overwrite some + // fluid cells with other fluids due to ID collision. + Set idSet = new HashSet<>(); for (FluidCell tCell : FluidCell.values()) { int id = tCell.getId(); String fluidName = tCell.getFluidName(); CellType cellType = tCell.getDisplayType(); + if (idSet.contains(id)) { + throw new IllegalStateException("Got ID collision for ID: " + id); + } + idSet.add(id); + Fluid fluid = FluidRegistry.getFluid(fluidName); if (fluid == null) { // Fluid is not guaranteed to exist. @@ -217,9 +260,12 @@ public class GT_MetaGenerated_Item_98 extends GT_MetaGenerated_Item { rgba[2] = (short) (color & 0x000000FF); rgba[3] = (short) ((color & 0xFF000000) >> 24); + registeredFluidDataMap.put(id, new RegisteredFluidData(fluid, rgba, iconContainerMap.get(cellType))); + /* // We'll just steal the icons from Water. They are all the same anyway (except _NULL is broken for cells). IIconContainer iconContainer = Materials.Water.mIconSet.mTextures[cellType.prefix.mTextureIndex]; registeredFluidDataMap.put(id, new RegisteredFluidData(fluid, rgba, iconContainer)); + */ } // We're not going to use these BitSets, so clear them to save memory. -- cgit From c4f36c857fcdcf2ed9bc3923f40e0f7b3b3555e0 Mon Sep 17 00:00:00 2001 From: D-Cysteine <54219287+D-Cysteine@users.noreply.github.com> Date: Sat, 30 Oct 2021 01:50:38 -0600 Subject: A little clean-up --- .../common/items/GT_MetaGenerated_Item_98.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'src/main/java/gregtech/common') diff --git a/src/main/java/gregtech/common/items/GT_MetaGenerated_Item_98.java b/src/main/java/gregtech/common/items/GT_MetaGenerated_Item_98.java index 09b884eb69..0a157f2e6d 100644 --- a/src/main/java/gregtech/common/items/GT_MetaGenerated_Item_98.java +++ b/src/main/java/gregtech/common/items/GT_MetaGenerated_Item_98.java @@ -41,9 +41,15 @@ public class GT_MetaGenerated_Item_98 extends GT_MetaGenerated_Item { * that fluid! * *

In order to avoid breaking existing worlds, fluids must not have their IDs changed! The - * only safe modification that can be made to this list is adding new fluids, or removing + * only safe modification that can be made to this enum is adding new fluids, or removing * existing fluids. When removing fluids, maybe leave a comment mentioning the old ID, so that * we don't re-use it for a new fluid. + * + *

If, in the future, we need to get item stacks before {@link #init()} has been called (such + * as for creating recipes), we can probably just modify the {@code FluidCell} methods below to + * eagerly construct the item stack ahead of time and memoize it or something. Really, the only + * thing that we need before we can construct item stacks is for {@link #INSTANCE} to be + * initialized. */ public enum FluidCell { // Next unused ID: 18 @@ -235,7 +241,7 @@ public class GT_MetaGenerated_Item_98 extends GT_MetaGenerated_Item { Fluid fluid = FluidRegistry.getFluid(fluidName); if (fluid == null) { - // Fluid is not guaranteed to exist. + // The fluid is not guaranteed to exist. // These fluids are non-GT fluids, so the mod may not be present. continue; } @@ -244,8 +250,7 @@ public class GT_MetaGenerated_Item_98 extends GT_MetaGenerated_Item { FluidStack fluidStack = new FluidStack(fluid, cellType.capacity); FluidContainerRegistry.registerFluidContainer( - new FluidContainerRegistry.FluidContainerData( - fluidStack, itemStack, emptyCell)); + new FluidContainerRegistry.FluidContainerData(fluidStack, itemStack, emptyCell)); tCell.setStack(itemStack); @@ -261,11 +266,6 @@ public class GT_MetaGenerated_Item_98 extends GT_MetaGenerated_Item { rgba[3] = (short) ((color & 0xFF000000) >> 24); registeredFluidDataMap.put(id, new RegisteredFluidData(fluid, rgba, iconContainerMap.get(cellType))); - /* - // We'll just steal the icons from Water. They are all the same anyway (except _NULL is broken for cells). - IIconContainer iconContainer = Materials.Water.mIconSet.mTextures[cellType.prefix.mTextureIndex]; - registeredFluidDataMap.put(id, new RegisteredFluidData(fluid, rgba, iconContainer)); - */ } // We're not going to use these BitSets, so clear them to save memory. -- cgit