-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Greatly Improve Default Fluids Config
- Loading branch information
1 parent
399c979
commit 6cc215e
Showing
9 changed files
with
181 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
src/main/java/com/nomiceu/nomilabs/fluid/FluidRegistryMixinHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/nomiceu/nomilabs/mixin/AccessibleFluidRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters