Skip to content

Commit

Permalink
Greatly Improve Default Fluids Config
Browse files Browse the repository at this point in the history
  • Loading branch information
IntegerLimit committed Jan 25, 2024
1 parent 399c979 commit 6cc215e
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 22 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/nomiceu/nomilabs/NomiLabs.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
28 changes: 22 additions & 6 deletions src/main/java/com/nomiceu/nomilabs/config/LabsConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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!",
Expand Down Expand Up @@ -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;
}
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/nomiceu/nomilabs/event/CommonProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -68,6 +69,11 @@ public static void preInit() {
TOPTooltipManager.registerProviders();

DataFixerHandler.preInit();
FluidRegistryMixinHelper.preInit();
}

public static void loadComplete() {
FluidRegistryMixinHelper.loadComplete();
}

@SubscribeEvent
Expand Down
112 changes: 112 additions & 0 deletions src/main/java/com/nomiceu/nomilabs/fluid/FluidRegistryMixinHelper.java
Original file line number Diff line number Diff line change
@@ -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<Pattern> defaultFluidsRegex;
private static Map<String, String> 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<String, List<String>> 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<String, List<String>> 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<String, String> getDefaultFluids() {
return defaultFluids;
}
}
Original file line number Diff line number Diff line change
@@ -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<String, Fluid> getMasterFluidReference() {
throw new NotImplementedException("AccessibleFluidRegistry Mixin Failed to Apply!");
}
}
29 changes: 14 additions & 15 deletions src/main/java/com/nomiceu/nomilabs/mixin/FluidRegistryMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -36,31 +36,29 @@ public class FluidRegistryMixin {
shift = At.Shift.AFTER),
require = 1)
private static void loadSpecifiedFluidDefaults(BiMap<Fluid, Integer> localFluidIDs, Set<String> 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.");
Expand All @@ -69,4 +67,5 @@ private static void loadSpecifiedFluidDefaults(BiMap<Fluid, Integer> localFluidI
NomiLabs.LOGGER.info("Changed {} Default Fluid(s)!", changed);
fluidIDs = localFluidIDs;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ private static List<Pattern> parseIgnoreConfig(String[] patterns, RemapTypes typ
NomiLabs.LOGGER.throwing(e);
}
}
NomiLabs.LOGGER.info("[Remappers]: Parsed Ignore Config: {}, for Type: {}", ignored, type);
return ignored;
}

Expand Down
5 changes: 4 additions & 1 deletion src/main/resources/assets/nomilabs/lang/en_us.lang
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/mixins.nomilabs.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"minVersion": "0.8",
"compatibilityLevel": "JAVA_8",
"mixins": [
"AccessibleFluidRegistry",
"AdvancementListMixin",
"AdvancementManagerMixin",
"FluidRegistryMixin",
Expand Down

0 comments on commit 6cc215e

Please sign in to comment.