diff --git a/src/main/java/com/nomiceu/nomilabs/NomiLabs.java b/src/main/java/com/nomiceu/nomilabs/NomiLabs.java index 478d0538..eea07200 100644 --- a/src/main/java/com/nomiceu/nomilabs/NomiLabs.java +++ b/src/main/java/com/nomiceu/nomilabs/NomiLabs.java @@ -39,6 +39,11 @@ public void preInit(FMLPreInitializationEvent event) { CommonProxy.preInit(); } + @EventHandler + public void loadComplete(FMLLoadCompleteEvent event) { + CommonProxy.loadComplete(); + } + @EventHandler public void serverStopped(FMLServerStoppedEvent event) { DataFixerHandler.close(); diff --git a/src/main/java/com/nomiceu/nomilabs/config/LabsConfig.java b/src/main/java/com/nomiceu/nomilabs/config/LabsConfig.java index 54494e94..8a57f16b 100644 --- a/src/main/java/com/nomiceu/nomilabs/config/LabsConfig.java +++ b/src/main/java/com/nomiceu/nomilabs/config/LabsConfig.java @@ -230,12 +230,10 @@ public static class Advanced { @Config.RequiresWorldRestart public boolean enableNomiCEuDataFixes = false; - @Config.Comment({"List of Resource Locations to be Default Fluids.", - "If multiple fluids with the same name match a resource location, the last one will be picked.", - "[default: ]"}) - @Config.LangKey("config.nomilabs.advanced.default_fluids") - @Config.RequiresWorldRestart - public String[] defaultFluids = new String[0]; + @Config.Comment("Fluid Registry Settings") + @Config.LangKey("config.nomilabs.advanced.fluid_registry") + @Config.Name("fluid registry") + public final FluidRegistry fluidRegistrySettings = new FluidRegistry(); @Config.Comment({"List of Regex Patterns to ignore if they are included in the ITEM missing registry list.", "Do not change unless you know what you are doing!", @@ -280,5 +278,23 @@ public static class Advanced { @Config.LangKey("config.nomilabs.advanced.ignore_biomes") @Config.RequiresMcRestart public String[] ignoreBiomes = new String[0]; + + public static class FluidRegistry { + @Config.Comment({"List of Regex Patterns to be Default Fluids.", + "Fluids are picked based on a Hierarchy System.", + "For Example: If the config is set to ['gregtech:.*', 'advancedrocketry:.*'], and the candidates for Oxygen are 'gregtech:oxygen' and 'advancedrocketry:oxygen', the GregTech one will be picked.", + "This is only applied to conflicting fluids.", + "This can be very inefficient with lots of patterns and lots of conflicting fluids. Try to condense it into one pattern where possible!", + "[default: ]"}) + @Config.LangKey("config.nomilabs.advanced.fluid_registry.default_fluids") + @Config.RequiresMcRestart + public String[] defaultFluids = new String[0]; + + @Config.Comment({"Whether to Log Conflicting Fluids, for use in setting default fluids correctly.", + "[default: false]"}) + @Config.LangKey("config.nomilabs.advanced.fluid_registry.log_conflicting_fluids") + @Config.RequiresMcRestart + public boolean logConflictingFluids = false; + } } } diff --git a/src/main/java/com/nomiceu/nomilabs/event/CommonProxy.java b/src/main/java/com/nomiceu/nomilabs/event/CommonProxy.java index bcc560f2..644d7388 100644 --- a/src/main/java/com/nomiceu/nomilabs/event/CommonProxy.java +++ b/src/main/java/com/nomiceu/nomilabs/event/CommonProxy.java @@ -2,6 +2,7 @@ import com.nomiceu.nomilabs.LabsValues; import com.nomiceu.nomilabs.block.registry.LabsBlocks; +import com.nomiceu.nomilabs.fluid.FluidRegistryMixinHelper; import com.nomiceu.nomilabs.gregtech.LabsSounds; import com.nomiceu.nomilabs.gregtech.block.registry.LabsMetaBlocks; import com.nomiceu.nomilabs.config.LabsConfig; @@ -68,6 +69,11 @@ public static void preInit() { TOPTooltipManager.registerProviders(); DataFixerHandler.preInit(); + FluidRegistryMixinHelper.preInit(); + } + + public static void loadComplete() { + FluidRegistryMixinHelper.loadComplete(); } @SubscribeEvent diff --git a/src/main/java/com/nomiceu/nomilabs/fluid/FluidRegistryMixinHelper.java b/src/main/java/com/nomiceu/nomilabs/fluid/FluidRegistryMixinHelper.java new file mode 100644 index 00000000..e7d7c0c4 --- /dev/null +++ b/src/main/java/com/nomiceu/nomilabs/fluid/FluidRegistryMixinHelper.java @@ -0,0 +1,112 @@ +package com.nomiceu.nomilabs.fluid; + +import com.nomiceu.nomilabs.NomiLabs; +import com.nomiceu.nomilabs.config.LabsConfig; +import com.nomiceu.nomilabs.mixin.AccessibleFluidRegistry; +import org.apache.logging.log4j.Level; + +import javax.annotation.Nullable; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; + +public class FluidRegistryMixinHelper { + private static List defaultFluidsRegex; + private static Map defaultFluids; + + public static void preInit() { + defaultFluidsRegex = new ArrayList<>(); + for (var fluid : LabsConfig.advanced.fluidRegistrySettings.defaultFluids) { + try { + defaultFluidsRegex.add(Pattern.compile(fluid)); + } catch (PatternSyntaxException e) { + NomiLabs.LOGGER.error("Bad Syntax for Default Fluid Regex: {}", fluid); + NomiLabs.LOGGER.throwing(e); + } + } + NomiLabs.LOGGER.info("[Fluid Registry Mixin]: Compiled Default Fluid Regex List: {}", defaultFluidsRegex); + } + + public static void loadComplete() { + if (defaultFluidsRegex.isEmpty() && !LabsConfig.advanced.fluidRegistrySettings.logConflictingFluids) return; + + Map> nameToRlMap = new HashMap<>(); + AccessibleFluidRegistry.getMasterFluidReference().forEach((key, value) -> { + nameToRlMap.computeIfAbsent(value.getName(), (k) -> new ArrayList<>()); + nameToRlMap.get(value.getName()).add(key); + }); + defaultFluids = new HashMap<>(); + boolean hasConflicts = false; + int conflicts = 0; + int solves = 0; + + /* Validate Conflicts */ + for (var entry : nameToRlMap.entrySet()) { + if (entry.getValue().size() <= 1) continue; + if (!hasConflicts && LabsConfig.advanced.fluidRegistrySettings.logConflictingFluids) { + NomiLabs.LOGGER.info("---------------- CONFLICTING & SOLVED FLUIDS: ----------------"); + NomiLabs.LOGGER.info("=============================================================="); + hasConflicts = true; + } + var type = parseConflict(entry); + if (!hasConflicts) continue; + switch (type) { + case CONFLICT -> conflicts++; + case SOLVED -> solves++; + } + } + if (hasConflicts) { + NomiLabs.LOGGER.info("==============================================================="); + NomiLabs.LOGGER.info("--------------- END CONFLICTING & SOLVED FLUIDS ---------------"); + NomiLabs.LOGGER.info("Conflicts: {}/{} | Solves: {}/{}", + conflicts, conflicts + solves, solves, conflicts + solves); + } + NomiLabs.LOGGER.info("[Fluid Registry Mixin] Generated Default Fluid Map: {}", defaultFluids); + } + + private static ConflictType parseConflict(Map.Entry> entry) { + boolean solved = false; + Pattern ptnUsed = null; + if (!defaultFluidsRegex.isEmpty()) { + for (var ptn : defaultFluidsRegex) { + if (solved) break; + for (var option : entry.getValue()) { + if (!ptn.matcher(option).matches()) continue; + solved = true; + defaultFluids.put(entry.getKey(), option); + ptnUsed = ptn; + break; + } + } + } + + /* Log */ + if (!LabsConfig.advanced.fluidRegistrySettings.logConflictingFluids) + return solved ? ConflictType.SOLVED : ConflictType.CONFLICT; + + Level level = solved ? Level.INFO : Level.ERROR; + NomiLabs.LOGGER.log(level, "~~~~~~~~~~~~~~~~~"); + NomiLabs.LOGGER.log(level, solved ? "SOLVED:" : "CONFLICT:"); + NomiLabs.LOGGER.log(level, "Fluid: {}", entry.getKey()); + NomiLabs.LOGGER.log(level, "Possible Options: {}", entry.getValue()); + if (solved) { + NomiLabs.LOGGER.log(level, "Chosen Option: {}", defaultFluids.get(entry.getKey())); + NomiLabs.LOGGER.log(level, "Used Regex: {} (No. {})", ptnUsed.pattern(), defaultFluidsRegex.indexOf(ptnUsed) + 1); + } + NomiLabs.LOGGER.log(level, "~~~~~~~~~~~~~~~~~"); + return solved ? ConflictType.SOLVED : ConflictType.CONFLICT; + } + + public enum ConflictType { + SOLVED, + CONFLICT + } + + @Nullable + public static Map getDefaultFluids() { + return defaultFluids; + } +} diff --git a/src/main/java/com/nomiceu/nomilabs/mixin/AccessibleFluidRegistry.java b/src/main/java/com/nomiceu/nomilabs/mixin/AccessibleFluidRegistry.java new file mode 100644 index 00000000..50e28a20 --- /dev/null +++ b/src/main/java/com/nomiceu/nomilabs/mixin/AccessibleFluidRegistry.java @@ -0,0 +1,16 @@ +package com.nomiceu.nomilabs.mixin; + +import com.google.common.collect.BiMap; +import net.minecraftforge.fluids.Fluid; +import net.minecraftforge.fluids.FluidRegistry; +import org.apache.commons.lang3.NotImplementedException; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +@Mixin(value = FluidRegistry.class, remap = false) +public interface AccessibleFluidRegistry { + @Accessor(value = "masterFluidReference") + static BiMap getMasterFluidReference() { + throw new NotImplementedException("AccessibleFluidRegistry Mixin Failed to Apply!"); + } +} diff --git a/src/main/java/com/nomiceu/nomilabs/mixin/FluidRegistryMixin.java b/src/main/java/com/nomiceu/nomilabs/mixin/FluidRegistryMixin.java index 72c474e6..136499cd 100644 --- a/src/main/java/com/nomiceu/nomilabs/mixin/FluidRegistryMixin.java +++ b/src/main/java/com/nomiceu/nomilabs/mixin/FluidRegistryMixin.java @@ -2,7 +2,7 @@ import com.google.common.collect.BiMap; import com.nomiceu.nomilabs.NomiLabs; -import com.nomiceu.nomilabs.config.LabsConfig; +import com.nomiceu.nomilabs.fluid.FluidRegistryMixinHelper; import net.minecraftforge.fluids.Fluid; import net.minecraftforge.fluids.FluidRegistry; import org.spongepowered.asm.mixin.Mixin; @@ -36,31 +36,29 @@ public class FluidRegistryMixin { shift = At.Shift.AFTER), require = 1) private static void loadSpecifiedFluidDefaults(BiMap localFluidIDs, Set defaultNames, CallbackInfo ci) { - if (LabsConfig.advanced.defaultFluids.length == 0) return; + var defaultFluids = FluidRegistryMixinHelper.getDefaultFluids(); + if (defaultFluids == null) return; int changed = 0; - for (var fluidName : LabsConfig.advanced.defaultFluids) { - NomiLabs.LOGGER.debug("Processing Input {}", fluidName); + for (var entry : defaultFluids.entrySet()) { + NomiLabs.LOGGER.debug("Processing Conflict {}", entry.getKey()); - var fluid = masterFluidReference.get(fluidName); - if (fluid == null) - throw new IllegalArgumentException("Nomi Labs: Config Entry " + fluidName + "in Default Fluids Config doesn't exist!"); - var oldFluid = fluids.get(fluid.getName()); - if (oldFluid == null) continue; + var fluid = masterFluidReference.get(entry.getValue()); + var oldFluid = fluids.get(entry.getKey()); var oldName = masterFluidReference.inverse().get(oldFluid); - if (oldName.equals(fluidName)) { - NomiLabs.LOGGER.debug("Default fluid of {} is already {}. Not Changing...", fluid.getName(), fluidName); + if (oldName.equals(entry.getValue())) { + NomiLabs.LOGGER.debug("Default fluid of {} is already {}. Not Changing...", entry.getKey(), entry.getValue()); continue; } - NomiLabs.LOGGER.debug("Changing default fluid of {}, from {} to {}.", fluid.getName(), oldName, fluidName); + NomiLabs.LOGGER.debug("Changing default fluid of {}, from {} to {}.", entry.getKey(), oldName, entry.getValue()); changed++; - fluids.forcePut(fluid.getName(), fluid); - defaultFluidName.forcePut(fluid.getName(), fluidName); + fluids.forcePut(entry.getKey(), fluid); + defaultFluidName.forcePut(entry.getKey(), entry.getValue()); Integer id = localFluidIDs.remove(oldFluid); localFluidIDs.forcePut(fluid, id); - fluidNames.forcePut(id, fluid.getName()); + fluidNames.forcePut(id, entry.getKey()); } if (changed == 0) { NomiLabs.LOGGER.info("No Fluids Changed."); @@ -69,4 +67,5 @@ private static void loadSpecifiedFluidDefaults(BiMap localFluidI NomiLabs.LOGGER.info("Changed {} Default Fluid(s)!", changed); fluidIDs = localFluidIDs; } + } diff --git a/src/main/java/com/nomiceu/nomilabs/remap/LabsRemappers.java b/src/main/java/com/nomiceu/nomilabs/remap/LabsRemappers.java index ed4e1c59..eb47566a 100644 --- a/src/main/java/com/nomiceu/nomilabs/remap/LabsRemappers.java +++ b/src/main/java/com/nomiceu/nomilabs/remap/LabsRemappers.java @@ -109,6 +109,7 @@ private static List parseIgnoreConfig(String[] patterns, RemapTypes typ NomiLabs.LOGGER.throwing(e); } } + NomiLabs.LOGGER.info("[Remappers]: Parsed Ignore Config: {}, for Type: {}", ignored, type); return ignored; } diff --git a/src/main/resources/assets/nomilabs/lang/en_us.lang b/src/main/resources/assets/nomilabs/lang/en_us.lang index cc195eb2..aac5f031 100644 --- a/src/main/resources/assets/nomilabs/lang/en_us.lang +++ b/src/main/resources/assets/nomilabs/lang/en_us.lang @@ -45,12 +45,15 @@ config.nomilabs.advanced.disable_xp_scaling=Disable Anvil XP Scaling config.nomilabs.advanced.disable_advancements=Disable Advancements config.nomilabs.advanced.disable_narrator=Disable Narrator config.nomilabs.advanced.enable_nomi_ceu_data_fixes=Enable Nomi-CEu Specific Data Fixes -config.nomilabs.advanced.default_fluids=Default Fluids config.nomilabs.advanced.ignore_items=ITEM Missing Registry Ignore List config.nomilabs.advanced.ignore_blocks=BLOCK Missing Registry Ignore List config.nomilabs.advanced.ignore_entities=ENTITY Missing Registry Ignore List config.nomilabs.advanced.ignore_biomes=BIOME Missing Registry Ignore List +config.nomilabs.advanced.fluid_registry=Fluid Registry Settings +config.nomilabs.advanced.fluid_registry.default_fluids=Default Fluids +config.nomilabs.advanced.fluid_registry.log_conflicting_fluids=Log Conflicting Fluids + # Custom Fluids fluid.uranium233=Uranium 233 fluid.plutonium2=Plutonium diff --git a/src/main/resources/mixins.nomilabs.json b/src/main/resources/mixins.nomilabs.json index 15bc4c98..2887a83b 100644 --- a/src/main/resources/mixins.nomilabs.json +++ b/src/main/resources/mixins.nomilabs.json @@ -5,6 +5,7 @@ "minVersion": "0.8", "compatibilityLevel": "JAVA_8", "mixins": [ + "AccessibleFluidRegistry", "AdvancementListMixin", "AdvancementManagerMixin", "FluidRegistryMixin",