Skip to content

Commit

Permalink
update lotr biome detection to more elegant version
Browse files Browse the repository at this point in the history
  • Loading branch information
mist475 committed Dec 5, 2023
1 parent b279420 commit e2d87bb
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 49 deletions.
11 changes: 7 additions & 4 deletions src/main/java/org/blockartistry/mod/DynSurround/Module.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package org.blockartistry.mod.DynSurround;

import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.Mod.Instance;
Expand Down Expand Up @@ -53,6 +54,7 @@ public class Module {
public static final String LOTR_PROXY_LOCATION = "org.blockartistry.mod.DynSurround.compat.LotrProxy";

public static ILOTRProxy LOTR_PROXY;
public static boolean LOTR;

@Instance(MOD_ID)
protected static Module instance;
Expand Down Expand Up @@ -110,18 +112,19 @@ public void init(final FMLInitializationEvent event) {

@EventHandler
public void postInit(final FMLPostInitializationEvent event) {
proxy.postInit(event);
if (Proxy.LOTR) {
LOTR = Loader.isModLoaded("lotr");
if (LOTR) {
try {
LOTR_PROXY = Class.forName(LOTR_PROXY_LOCATION).asSubclass(ILOTRProxy.class).getDeclaredConstructor().newInstance();
}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException | NoSuchMethodException |
InvocationTargetException ignored) {
LOTR_PROXY = new NoLotrProxy();
}
} else {
}
if (LOTR_PROXY == null) {
LOTR_PROXY = new NoLotrProxy();
}
proxy.postInit(event);
config.save();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

public interface ILOTRProxy {
String getSeason();

void registerLOTRBiomes();
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
package org.blockartistry.mod.DynSurround.compat;

import lotr.common.LOTRDate;
import lotr.common.LOTRDimension;
import org.blockartistry.mod.DynSurround.data.BiomeRegistry;

import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Stream;

public class LotrProxy implements ILOTRProxy {

@Override
public String getSeason() {
return LOTRDate.ShireReckoning.getSeason().name();
}

@Override
public void registerLOTRBiomes() {
Stream.concat(
Arrays.stream(LOTRDimension.MIDDLE_EARTH.biomeList),
Arrays.stream(LOTRDimension.UTUMNO.biomeList))
.filter(Objects::nonNull)
.forEach(lotrBiome -> BiomeRegistry.registry.put(lotrBiome.biomeName, new BiomeRegistry.BiomeRegistryEntry(lotrBiome)));
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package org.blockartistry.mod.DynSurround.compat;

public class NoLotrProxy implements ILOTRProxy{
public class NoLotrProxy implements ILOTRProxy {
@Override
public String getSeason() {
return "noseason";
}

@Override
public void registerLOTRBiomes() {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.blockartistry.mod.DynSurround.data.config.BiomeConfig;
import org.blockartistry.mod.DynSurround.data.config.SoundConfig;
import org.blockartistry.mod.DynSurround.event.RegistryReloadEvent;
import org.blockartistry.mod.DynSurround.proxy.Proxy;
import org.blockartistry.mod.DynSurround.util.Color;
import org.blockartistry.mod.DynSurround.util.MyUtils;

Expand All @@ -50,7 +49,7 @@
import java.util.regex.Pattern;

public final class BiomeRegistry {
private static final Map<String,Entry> registry = new HashMap<>();
public static final Map<String, BiomeRegistryEntry> registry = new HashMap<>();
private static final Map<String, String> biomeAliases = new HashMap<>();

public static final BiomeGenBase UNDERGROUND = new FakeBiome(-1, "Underground");
Expand All @@ -68,7 +67,7 @@ public final class BiomeRegistry {
// and should default to something to avoid crap.
private static final BiomeGenBase WTF = new FakeBiome(-256, "(FooBar)");

private static class Entry implements Comparable<Entry> {
public static class BiomeRegistryEntry implements Comparable<BiomeRegistryEntry> {

private static Class<?> bopBiome;
private static Field bopBiomeFogDensity;
Expand Down Expand Up @@ -101,7 +100,7 @@ private static class Entry implements Comparable<Entry> {
public int spotSoundChance;
public List<SoundEffect> spotSounds;

public Entry(final BiomeGenBase biome) {
public BiomeRegistryEntry(final BiomeGenBase biome) {
this.biome = biome;
this.hasPrecipitation = biome.canSpawnLightningBolt() || biome.getEnableSnow();
this.sounds = new ArrayList<>();
Expand Down Expand Up @@ -177,7 +176,7 @@ public String toString() {
}

@Override
public int compareTo(Entry e) {
public int compareTo(BiomeRegistryEntry e) {
return this.biome.biomeName.compareToIgnoreCase(e.biome.biomeName);
}
}
Expand All @@ -202,24 +201,22 @@ public static void initialize() {

registry.clear();

if (Proxy.LOTR) {
registerLOTRBiomes();
}
Module.LOTR_PROXY.registerLOTRBiomes();
for (BiomeGenBase biomeGenBase : BiomeGenBase.getBiomeGenArray()) {
if (biomeGenBase != null) {
registry.put(biomeGenBase.biomeName, new Entry(biomeGenBase));
registry.put(biomeGenBase.biomeName, new BiomeRegistryEntry(biomeGenBase));
}
}
// Add our fake biomes
registry.put(UNDERGROUND.biomeName, new Entry(UNDERGROUND));
registry.put(UNDERWATER.biomeName, new Entry(UNDERWATER));
registry.put(UNDEROCEAN.biomeName, new Entry(UNDEROCEAN));
registry.put(UNDERDEEPOCEAN.biomeName, new Entry(UNDERDEEPOCEAN));
registry.put(UNDERRIVER.biomeName, new Entry(UNDERRIVER));
registry.put(OUTERSPACE.biomeName, new Entry(OUTERSPACE));
registry.put(CLOUDS.biomeName, new Entry(CLOUDS));
registry.put(PLAYER.biomeName, new Entry(PLAYER));
registry.put(WTF.biomeName, new Entry(WTF));
registry.put(UNDERGROUND.biomeName, new BiomeRegistryEntry(UNDERGROUND));
registry.put(UNDERWATER.biomeName, new BiomeRegistryEntry(UNDERWATER));
registry.put(UNDEROCEAN.biomeName, new BiomeRegistryEntry(UNDEROCEAN));
registry.put(UNDERDEEPOCEAN.biomeName, new BiomeRegistryEntry(UNDERDEEPOCEAN));
registry.put(UNDERRIVER.biomeName, new BiomeRegistryEntry(UNDERRIVER));
registry.put(OUTERSPACE.biomeName, new BiomeRegistryEntry(OUTERSPACE));
registry.put(CLOUDS.biomeName, new BiomeRegistryEntry(CLOUDS));
registry.put(PLAYER.biomeName, new BiomeRegistryEntry(PLAYER));
registry.put(WTF.biomeName, new BiomeRegistryEntry(WTF));

processConfig();

Expand All @@ -240,33 +237,16 @@ public static void initialize() {
MinecraftForge.EVENT_BUS.post(new RegistryReloadEvent.Biome());
}

private static void registerLOTRBiomes() {
try {
Class<?> lotrBiome = Class.forName("lotr.common.world.biome.LOTRBiome");
Field[] declaredFields = lotrBiome.getDeclaredFields();
for (Field field : declaredFields) {
if (java.lang.reflect.Modifier.isStatic(field.getModifiers())) {
field.setAccessible(true);
if (field.get(null) instanceof BiomeGenBase biome) {
registry.put(biome.biomeName, new Entry(biome));
}
}
}
}
catch (ClassNotFoundException| IllegalAccessException ignored) {
}
}

private static Entry get(final BiomeGenBase biome) {
private static BiomeRegistryEntry get(final BiomeGenBase biome) {
synchronized (registry) {
Entry entry = registry.get(biome == null ? WTF.biomeName : biome.biomeName);
BiomeRegistryEntry entry = registry.get(biome == null ? WTF.biomeName : biome.biomeName);
if (entry == null) {
ModLog.warn("Biome [%s] was not detected during initial scan! Reloading config...", resolveName(biome));
initialize();
entry = registry.get(biome.biomeName);
if (entry == null) {
ModLog.warn("Still can't find biome [%s]! Explicitly adding at defaults", resolveName(biome));
entry = new Entry(biome);
entry = new BiomeRegistryEntry(biome);
registry.put(biome.biomeName, entry);
}
}
Expand Down Expand Up @@ -311,7 +291,7 @@ public static List<SoundEffect> getSounds(final BiomeGenBase biome, final String
}

public static SoundEffect getSpotSound(final BiomeGenBase biome, final String conditions, final Random random) {
final Entry e = get(biome);
final BiomeRegistryEntry e = get(biome);
if (e.spotSounds.isEmpty() || random.nextInt(e.spotSoundChance) != 0)
return null;

Expand Down Expand Up @@ -371,7 +351,7 @@ static boolean isBiomeMatch(final BiomeConfig.Entry entry, final String biomeNam

private static void process(final BiomeConfig config) {
for (final BiomeConfig.Entry entry : config.entries) {
for (final Entry biomeEntry : registry.values()) {
for (final BiomeRegistryEntry biomeEntry : registry.values()) {
if (isBiomeMatch(entry, resolveName(biomeEntry.biome))) {
if (entry.hasPrecipitation != null)
biomeEntry.hasPrecipitation = entry.hasPrecipitation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public boolean getHasWeather() {

public String getSeason() {
//TODO: lotr shire reckoning -> CalenderAPI bridge in MistLotrTweaks
if (Proxy.LOTR && this.name.equals("MiddleEarth")) {
if (Module.LOTR && this.name.equals("MiddleEarth")) {
return Module.LOTR_PROXY.getSeason();
}
if (!CALENDAR_API) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

package org.blockartistry.mod.DynSurround.proxy;

import cpw.mods.fml.common.Loader;
import org.blockartistry.mod.DynSurround.VersionCheck;
import org.blockartistry.mod.DynSurround.client.DamageEffectHandler;
import org.blockartistry.mod.DynSurround.client.waila.WailaHandler;
Expand All @@ -43,7 +42,6 @@
import net.minecraft.server.MinecraftServer;

public class Proxy {
public static boolean LOTR = false;

public void preInit(final FMLPreInitializationEvent event) {
// Register early to give the background process a good amount
Expand All @@ -59,7 +57,6 @@ public void init(final FMLInitializationEvent event) {
}

public void postInit(final FMLPostInitializationEvent event) {
LOTR = Loader.isModLoaded("lotr");
BiomeRegistry.initialize();
DimensionRegistry.initialize();
}
Expand Down

0 comments on commit e2d87bb

Please sign in to comment.