From bee4553851d5f47ac70f1bdd006310d168fdf3d8 Mon Sep 17 00:00:00 2001 From: sirjoekcb Date: Sun, 10 Mar 2024 08:35:44 -0700 Subject: [PATCH] Use singleton EmptyMapMarkerEntry instead of null in ITEM_UNIFICATION_ENTRY_COLLECTED (#966) Co-authored-by: sirjoekcb --- .../gtceu/api/data/chemical/ChemicalHelper.java | 10 +++++----- .../data/chemical/material/stack/UnificationEntry.java | 10 +++------- .../gtceu/api/recipe/ToolHeadReplaceRecipe.java | 4 ++-- .../gtceu/data/recipe/misc/RecyclingRecipes.java | 8 ++++---- 4 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/gregtechceu/gtceu/api/data/chemical/ChemicalHelper.java b/src/main/java/com/gregtechceu/gtceu/api/data/chemical/ChemicalHelper.java index a66eeee30f..55b6771aae 100644 --- a/src/main/java/com/gregtechceu/gtceu/api/data/chemical/ChemicalHelper.java +++ b/src/main/java/com/gregtechceu/gtceu/api/data/chemical/ChemicalHelper.java @@ -130,7 +130,7 @@ public static MaterialStack getMaterial(UnificationEntry entry) { @Nullable public static MaterialStack getMaterial(ItemLike itemLike) { var entry = getUnificationEntry(itemLike); - if (entry != null) { + if (entry != null && entry != UnificationEntry.EmptyMapMarkerEntry) { Material entryMaterial = entry.material; if (entryMaterial != null) { return new MaterialStack(entryMaterial, entry.tagPrefix.getMaterialAmount(entryMaterial)); @@ -165,7 +165,7 @@ public static Material getMaterial(Fluid fluid) { public static TagPrefix getPrefix(ItemLike itemLike) { if (itemLike == null) return null; UnificationEntry entry = getUnificationEntry(itemLike); - if (entry != null) return entry.tagPrefix; + if (entry != null && entry != UnificationEntry.EmptyMapMarkerEntry) return entry.tagPrefix; return null; } @@ -228,7 +228,7 @@ public static UnificationEntry getUnificationEntry(ItemLike itemLike) { return entry.getValue(); } } - return null; + return UnificationEntry.EmptyMapMarkerEntry; }); } @@ -241,7 +241,7 @@ public static UnificationEntry getUnificationEntry(TagKey tag) { } } } - return new UnificationEntry.EmptyMapMarkerEntry(); + return UnificationEntry.EmptyMapMarkerEntry; }); } @@ -251,7 +251,7 @@ public static UnificationEntry getOrComputeUnificationEntry(ItemLike itemLike) { return ITEM_UNIFICATION_ENTRY_COLLECTED.computeIfAbsent(itemLike, item -> { Holder holder = Registry.ITEM.getOrCreateHolderOrThrow(Registry.ITEM.getResourceKey(item.asItem()).orElseThrow()); return holder.tags().map(ChemicalHelper::getUnificationEntry).filter(Objects::nonNull) - .filter(entry -> !(entry instanceof UnificationEntry.EmptyMapMarkerEntry)).findFirst().orElse(null); + .filter(entry -> !(entry == UnificationEntry.EmptyMapMarkerEntry)).findFirst().orElse(null); }); } diff --git a/src/main/java/com/gregtechceu/gtceu/api/data/chemical/material/stack/UnificationEntry.java b/src/main/java/com/gregtechceu/gtceu/api/data/chemical/material/stack/UnificationEntry.java index 382a906770..5f6bece3fb 100644 --- a/src/main/java/com/gregtechceu/gtceu/api/data/chemical/material/stack/UnificationEntry.java +++ b/src/main/java/com/gregtechceu/gtceu/api/data/chemical/material/stack/UnificationEntry.java @@ -45,12 +45,7 @@ public String toString() { return tagPrefix.name + (material != null ? material.toCamelCaseString() : ""); } - public static class EmptyMapMarkerEntry extends UnificationEntry { - - public EmptyMapMarkerEntry() { - super(null); - } - + public static final UnificationEntry EmptyMapMarkerEntry = new UnificationEntry(null) { @Override public boolean equals(Object o) { return this == o; @@ -65,5 +60,6 @@ public int hashCode() { public String toString() { return "EMPTY UNIFICATION ENTRY"; } - } + }; + } diff --git a/src/main/java/com/gregtechceu/gtceu/api/recipe/ToolHeadReplaceRecipe.java b/src/main/java/com/gregtechceu/gtceu/api/recipe/ToolHeadReplaceRecipe.java index 1a2b720cab..920546d097 100644 --- a/src/main/java/com/gregtechceu/gtceu/api/recipe/ToolHeadReplaceRecipe.java +++ b/src/main/java/com/gregtechceu/gtceu/api/recipe/ToolHeadReplaceRecipe.java @@ -69,7 +69,7 @@ public boolean matches(CraftingContainer inv, @NotNull Level level) { } else return false; if (!tool.isElectric()) return false; - if (toolHead == null) return false; + if (toolHead == null || toolHead == UnificationEntry.EmptyMapMarkerEntry) return false; GTToolType[] output = TOOL_HEAD_TO_TOOL_MAP.get(toolHead.tagPrefix); return output != null && output[tool.getElectricTier()] != null; } @@ -105,7 +105,7 @@ public ItemStack assemble(CraftingContainer inv) { } else return ItemStack.EMPTY; if (!tool.isElectric()) return ItemStack.EMPTY; IElectricItem powerUnit = GTCapabilityHelper.getElectricItem(realTool); - if (toolHead == null) return ItemStack.EMPTY; + if (toolHead == null || toolHead == UnificationEntry.EmptyMapMarkerEntry) return ItemStack.EMPTY; GTToolType[] toolArray = TOOL_HEAD_TO_TOOL_MAP.get(toolHead.tagPrefix); ItemStack newTool = GTItems.TOOL_ITEMS.get(toolHead.material, toolArray[tool.getElectricTier()]) .get().get(powerUnit.getCharge(), powerUnit.getMaxCharge()); diff --git a/src/main/java/com/gregtechceu/gtceu/data/recipe/misc/RecyclingRecipes.java b/src/main/java/com/gregtechceu/gtceu/data/recipe/misc/RecyclingRecipes.java index c142fcaed7..3c8c4545f4 100644 --- a/src/main/java/com/gregtechceu/gtceu/data/recipe/misc/RecyclingRecipes.java +++ b/src/main/java/com/gregtechceu/gtceu/data/recipe/misc/RecyclingRecipes.java @@ -109,7 +109,7 @@ private static void registerMaceratorRecycling(Consumer provider UnificationEntry entry = ChemicalHelper.getUnificationEntry(input.getItem()); TagKey inputTag = null; - if (entry != null) { + if (entry != null && entry != UnificationEntry.EmptyMapMarkerEntry) { inputTag = ChemicalHelper.getTag(entry.tagPrefix, entry.material); } @@ -134,7 +134,7 @@ private static void registerExtractorRecycling(Consumer provider UnificationEntry entry = ChemicalHelper.getUnificationEntry(input.getItem()); TagKey inputTag = null; - if (entry != null) { + if (entry != null && entry != UnificationEntry.EmptyMapMarkerEntry) { inputTag = ChemicalHelper.getTag(entry.tagPrefix, entry.material); } @@ -217,7 +217,7 @@ private static void registerExtractorRecycling(Consumer provider private static void registerArcRecycling(Consumer provider, ItemStack input, List materials, @Nullable TagPrefix prefix) { UnificationEntry entry = ChemicalHelper.getUnificationEntry(input.getItem()); TagKey inputTag = null; - if (entry != null) { + if (entry != null && entry != UnificationEntry.EmptyMapMarkerEntry) { inputTag = ChemicalHelper.getTag(entry.tagPrefix, entry.material); } @@ -407,7 +407,7 @@ private static List finalizeOutputs(List materials, in if (stack == ItemStack.EMPTY) continue; if (stack.getCount() > 64) { UnificationEntry entry = ChemicalHelper.getUnificationEntry(stack.getItem()); - if (entry != null) { // should always be true + if (entry != null && entry != UnificationEntry.EmptyMapMarkerEntry) { // should always be true TagPrefix prefix = entry.tagPrefix; // These are the highest forms that a Material can have (for Ingot and Dust, respectively),