Strict separation of worldpreset code from rest of bclib

This commit is contained in:
Frank 2022-07-05 17:42:56 +02:00
parent a73bd23ddf
commit 85e1d35496
27 changed files with 234 additions and 51 deletions

View file

@ -1,16 +1,17 @@
package org.betterx.worlds.together;
import org.betterx.bclib.util.Logger;
import org.betterx.worlds.together.surfaceRules.SurfaceRuleRegistry;
import org.betterx.worlds.together.tag.v3.TagManager;
import org.betterx.worlds.together.util.Logger;
import org.betterx.worlds.together.world.WorldConfig;
import org.betterx.worlds.together.worldPreset.WorldPresets;
import net.minecraft.resources.ResourceLocation;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.loader.api.FabricLoader;
public class WorldsTogether {
public class WorldsTogether implements ModInitializer {
public static boolean SURPRESS_EXPERIMENTAL_DIALOG = false;
public static boolean FORCE_SERVER_TO_BETTERX_PRESET = false;
public static final String MOD_ID = "worlds_together";
@ -23,7 +24,7 @@ public class WorldsTogether {
return FabricLoader.getInstance().isDevelopmentEnvironment();
}
public static void onInitialize() {
public void onInitialize() {
TagManager.ensureStaticallyLoaded();
SurfaceRuleRegistry.ensureStaticallyLoaded();

View file

@ -2,8 +2,10 @@ package org.betterx.worlds.together.client;
import org.betterx.worlds.together.worldPreset.client.WorldPresetsClient;
public class WorldsTogetherClient {
public static void onInitializeClient() {
import net.fabricmc.api.ClientModInitializer;
public class WorldsTogetherClient implements ClientModInitializer {
public void onInitializeClient() {
WorldPresetsClient.setupClientside();
}
}

View file

@ -0,0 +1,29 @@
package org.betterx.worlds.together.entrypoints;
import net.fabricmc.loader.api.FabricLoader;
import java.util.List;
import org.jetbrains.annotations.ApiStatus;
@ApiStatus.Internal
public class EntrypointUtil {
private static <T extends WorldsTogetherEntrypoint> List<T> getEntryPoints(boolean client, Class<T> select) {
return FabricLoader.getInstance()
.getEntrypoints(
client ? "worlds_together_client" : "worlds_together",
WorldsTogetherEntrypoint.class
)
.stream()
.filter(o -> select.isAssignableFrom(o.getClass()))
.map(e -> (T) e)
.toList();
}
public static <T extends WorldsTogetherEntrypoint> List<T> getCommon(Class<T> select) {
return getEntryPoints(false, select);
}
public static <T extends WorldsTogetherEntrypoint> List<T> getClient(Class<T> select) {
return getEntryPoints(true, select);
}
}

View file

@ -0,0 +1,5 @@
package org.betterx.worlds.together.entrypoints;
public interface WorldPresetBootstrap extends WorldsTogetherEntrypoint {
void bootstrapWorldPresets();
}

View file

@ -0,0 +1,4 @@
package org.betterx.worlds.together.entrypoints;
public interface WorldsTogetherEntrypoint {
}

View file

@ -6,6 +6,7 @@ import org.betterx.worlds.together.chunkgenerator.EnforceableChunkGenerator;
import org.betterx.worlds.together.world.BiomeSourceWithNoiseRelatedSettings;
import org.betterx.worlds.together.world.BiomeSourceWithSeed;
import org.betterx.worlds.together.world.WorldConfig;
import org.betterx.worlds.together.world.event.WorldBootstrap;
import org.betterx.worlds.together.worldPreset.TogetherWorldPreset;
import org.betterx.worlds.together.worldPreset.WorldPresets;
@ -15,6 +16,7 @@ import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.RandomSource;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.chunk.ChunkGenerator;
@ -165,4 +167,17 @@ public class WorldGenUtil {
}
return settings;
}
public static ResourceLocation getBiomeID(Biome biome) {
ResourceLocation id = null;
RegistryAccess access = WorldBootstrap.getLastRegistryAccessOrElseBuiltin();
id = access.registryOrThrow(Registry.BIOME_REGISTRY).getKey(biome);
if (id == null) {
WorldsTogether.LOGGER.error("Unable to get ID for " + biome + ".");
}
return id;
}
}

View file

@ -1,8 +1,8 @@
package org.betterx.worlds.together.tag.v3;
import org.betterx.bclib.api.v2.levelgen.biomes.BiomeAPI;
import org.betterx.bclib.api.v2.tag.TagAPI;
import org.betterx.worlds.together.levelgen.WorldGenUtil;
import org.betterx.worlds.together.mixin.common.DiggerItemAccessor;
import org.betterx.worlds.together.world.event.WorldEventsImpl;
import net.minecraft.core.DefaultedRegistry;
import net.minecraft.core.Registry;
@ -62,7 +62,7 @@ public class TagManager {
"tags/worldgen/biome",
(dir) -> new TagRegistry.Biomes(
dir,
b -> BiomeAPI.getBiomeID(b)
b -> WorldGenUtil.getBiomeID(b)
)
);
}
@ -105,7 +105,7 @@ public class TagManager {
String directory,
Map<ResourceLocation, List<TagLoader.EntryWithSource>> tagsMap
) {
tagsMap = TagAPI.apply(directory, tagsMap);
WorldEventsImpl.BEFORE_ADDING_TAGS.emit(e -> e.apply(directory, tagsMap));
TagRegistry<?> type = TYPES.get(directory);
if (type != null) {

View file

@ -1,8 +1,7 @@
package org.betterx.worlds.together.tag.v3;
import org.betterx.bclib.BCLib;
import org.betterx.bclib.api.v2.levelgen.biomes.BCLBiome;
import org.betterx.bclib.api.v2.levelgen.biomes.InternalBiomeAPI;
import org.betterx.worlds.together.WorldsTogether;
import net.minecraft.core.DefaultedRegistry;
import net.minecraft.core.Registry;
@ -90,8 +89,26 @@ public class TagRegistry<T> {
* @param tagID {@link TagKey< Biome >} tag ID.
* @param elements array of Elements to add into tag.
*/
public void add(TagKey<Biome> tagID, ResourceKey<Biome>... elements) {
if (isFrozen) WorldsTogether.LOGGER.warning("Adding Tag " + tagID + " after the API was frozen.");
Set<TagEntry> set = getSetForTag(tagID);
for (ResourceKey<Biome> element : elements) {
ResourceLocation id = element.location();
if (id != null) {
set.add(TagEntry.element(id));
}
}
}
/**
* Adds one Tag to multiple Elements.
*
* @param tagID {@link TagKey< Biome >} tag ID.
* @param elements array of Elements to add into tag.
*/
@Deprecated(forRemoval = true)
public void add(TagKey<Biome> tagID, BCLBiome... elements) {
if (isFrozen) BCLib.LOGGER.warning("Adding Tag " + tagID + " after the API was frozen.");
if (isFrozen) WorldsTogether.LOGGER.warning("Adding Tag " + tagID + " after the API was frozen.");
Set<TagEntry> set = getSetForTag(tagID);
for (BCLBiome element : elements) {
ResourceLocation id = element.getID();
@ -106,7 +123,6 @@ public class TagRegistry<T> {
}
public void apply(Map<ResourceLocation, List<TagLoader.EntryWithSource>> tagsMap) {
InternalBiomeAPI._runBiomeTagAdders();
super.apply(tagsMap);
}
}
@ -209,7 +225,7 @@ public class TagRegistry<T> {
}
public void addUntyped(TagKey<T> tagID, ResourceLocation... elements) {
if (isFrozen) BCLib.LOGGER.warning("Adding Tag " + tagID + " after the API was frozen.");
if (isFrozen) WorldsTogether.LOGGER.warning("Adding Tag " + tagID + " after the API was frozen.");
Set<TagEntry> set = getSetForTag(tagID);
for (ResourceLocation id : elements) {
if (id != null) {
@ -225,7 +241,7 @@ public class TagRegistry<T> {
}
public void addOtherTags(TagKey<T> tagID, TagKey<T>... tags) {
if (isFrozen) BCLib.LOGGER.warning("Adding Tag " + tagID + " after the API was frozen.");
if (isFrozen) WorldsTogether.LOGGER.warning("Adding Tag " + tagID + " after the API was frozen.");
Set<TagEntry> set = getSetForTag(tagID);
for (TagKey<T> tag : tags) {
ResourceLocation id = tag.location();
@ -242,7 +258,7 @@ public class TagRegistry<T> {
* @param elements array of Elements to add into tag.
*/
protected void add(TagKey<T> tagID, T... elements) {
if (isFrozen) BCLib.LOGGER.warning("Adding Tag " + tagID + " after the API was frozen.");
if (isFrozen) WorldsTogether.LOGGER.warning("Adding Tag " + tagID + " after the API was frozen.");
Set<TagEntry> set = getSetForTag(tagID);
for (T element : elements) {
ResourceLocation id = locationProvider.apply(element);
@ -260,7 +276,7 @@ public class TagRegistry<T> {
@Deprecated(forRemoval = true)
protected void add(ResourceLocation tagID, T... elements) {
if (isFrozen) BCLib.LOGGER.warning("Adding Tag " + tagID + " after the API was frozen.");
if (isFrozen) WorldsTogether.LOGGER.warning("Adding Tag " + tagID + " after the API was frozen.");
Set<TagEntry> set = getSetForTag(tagID);
for (T element : elements) {
ResourceLocation id = locationProvider.apply(element);
@ -292,7 +308,7 @@ public class TagRegistry<T> {
List<TagLoader.EntryWithSource> builder,
Set<TagEntry> ids
) {
ids.forEach(value -> builder.add(new TagLoader.EntryWithSource(value, BCLib.MOD_ID)));
ids.forEach(value -> builder.add(new TagLoader.EntryWithSource(value, WorldsTogether.MOD_ID)));
return builder;
}
}

View file

@ -0,0 +1,62 @@
package org.betterx.worlds.together.util;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
public final class Logger {
private static final org.apache.logging.log4j.Logger LOGGER = LogManager.getLogger();
private final String modPref;
public Logger(String modID) {
this.modPref = "[" + modID + "] ";
}
public void log(Level level, String message) {
LOGGER.log(level, modPref + message);
}
public void log(Level level, String message, Object... params) {
LOGGER.log(level, modPref + message, params);
}
public void debug(Object message) {
this.log(Level.DEBUG, message.toString());
}
public void debug(Object message, Object... params) {
this.log(Level.DEBUG, message.toString(), params);
}
public void catching(Throwable ex) {
this.error(ex.getLocalizedMessage());
LOGGER.catching(ex);
}
public void info(String message) {
this.log(Level.INFO, message);
}
public void info(String message, Object... params) {
this.log(Level.INFO, message, params);
}
public void warning(String message, Object... params) {
this.log(Level.WARN, message, params);
}
public void warning(String message, Object obj, Exception ex) {
LOGGER.warn(modPref + message, obj, ex);
}
public void error(String message) {
this.log(Level.ERROR, message);
}
public void error(String message, Object obj, Exception ex) {
LOGGER.error(modPref + message, obj, ex);
}
public void error(String message, Exception ex) {
LOGGER.error(modPref + message, ex);
}
}

View file

@ -1,6 +1,5 @@
package org.betterx.worlds.together.util;
import org.betterx.bclib.BCLib;
import org.betterx.worlds.together.WorldsTogether;
import net.fabricmc.loader.api.*;
@ -261,8 +260,6 @@ public class ModUtil {
* @return The version of the locally installed Mod
*/
public static String getModVersion(String modID) {
if (modID == WorldsTogether.MOD_ID) modID = BCLib.MOD_ID;
Optional<ModContainer> optional = FabricLoader.getInstance()
.getModContainer(modID);
if (optional.isPresent()) {

View file

@ -0,0 +1,14 @@
package org.betterx.worlds.together.world.event;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagLoader;
import java.util.List;
import java.util.Map;
public interface BeforeAddingTags {
void apply(
String directory,
Map<ResourceLocation, List<TagLoader.EntryWithSource>> tagsMap
);
}

View file

@ -4,7 +4,7 @@ import java.util.LinkedList;
import java.util.List;
import java.util.function.Consumer;
class EventImpl<T> implements Event<T> {
public class EventImpl<T> implements Event<T> {
final List<T> handlers = new LinkedList<>();
public final boolean on(T handler) {

View file

@ -1,6 +1,5 @@
package org.betterx.worlds.together.world.event;
import org.betterx.bclib.BCLib;
import org.betterx.worlds.together.WorldsTogether;
import org.betterx.worlds.together.levelgen.WorldGenUtil;
import org.betterx.worlds.together.mixin.common.RegistryOpsAccessor;
@ -56,10 +55,6 @@ public class WorldBootstrap {
private static void initializeWorldConfig(File levelBaseDir, boolean newWorld) {
WorldConfig.load(new File(levelBaseDir, "data"));
if (newWorld) {
WorldConfig.saveFile(BCLib.MOD_ID);
}
}
private static void onRegistryReady(RegistryAccess a) {
@ -115,7 +110,7 @@ public class WorldBootstrap {
public static void setupWorld(LevelStorageSource.LevelStorageAccess levelStorageAccess) {
File levelDat = levelStorageAccess.getLevelPath(LevelResource.LEVEL_DATA_FILE).toFile();
if (!levelDat.exists()) {
BCLib.LOGGER.info("Creating a new World, no fixes needed");
WorldsTogether.LOGGER.info("Creating a new World, no fixes needed");
final Map<ResourceKey<LevelStem>, ChunkGenerator> settings = Helpers.defaultServerDimensions();
Helpers.initializeWorldConfig(levelStorageAccess, true);
@ -241,11 +236,11 @@ public class WorldBootstrap {
false
));
} catch (Exception e) {
BCLib.LOGGER.error("Failed to initialize data in world", e);
WorldsTogether.LOGGER.error("Failed to initialize data in world", e);
}
levelStorageAccess.close();
} catch (Exception e) {
BCLib.LOGGER.error("Failed to acquire storage access", e);
WorldsTogether.LOGGER.error("Failed to acquire storage access", e);
}
}
@ -260,7 +255,7 @@ public class WorldBootstrap {
result = WorldEventsImpl.PATCH_WORLD.applyPatches(levelStorageAccess, onResume);
levelStorageAccess.close();
} catch (Exception e) {
BCLib.LOGGER.error("Failed to initialize data in world", e);
WorldsTogether.LOGGER.error("Failed to initialize data in world", e);
}
return result;
@ -287,7 +282,7 @@ public class WorldBootstrap {
InGUI.setupNewWorldCommon(levelStorageAccess, worldPreset, worldGenSettings);
levelStorageAccess.close();
} catch (Exception e) {
BCLib.LOGGER.error("Failed to initialize data in world", e);
WorldsTogether.LOGGER.error("Failed to initialize data in world", e);
}
}
}
@ -315,7 +310,7 @@ public class WorldBootstrap {
return WorldGenUtil.repairBiomeSourceInAllDimensions(acc.bcl_getRegistryAccess(), worldGenSettings);
//.repairSettingsOnLoad(LAST_REGISTRY_ACCESS, worldGenSettings);
} else {
BCLib.LOGGER.error("Unable to obtain registryAccess when enforcing generators.");
WorldsTogether.LOGGER.error("Unable to obtain registryAccess when enforcing generators.");
}
return worldGenSettings;
}

View file

@ -8,4 +8,6 @@ public class WorldEvents {
public static final Event<OnFinalizeLevelStem> ON_FINALIZE_LEVEL_STEM = WorldEventsImpl.ON_FINALIZE_LEVEL_STEM;
public static final Event<OnWorldPatch> PATCH_WORLD = WorldEventsImpl.PATCH_WORLD;
public static final Event<OnAdaptWorldPresetSettings> ADAPT_WORLD_PRESET = WorldEventsImpl.ADAPT_WORLD_PRESET;
public static final Event<BeforeAddingTags> BEFORE_ADDING_TAGS = WorldEventsImpl.BEFORE_ADDING_TAGS;
}

View file

@ -1,6 +1,9 @@
package org.betterx.worlds.together.world.event;
class WorldEventsImpl {
import org.jetbrains.annotations.ApiStatus;
@ApiStatus.Internal
public class WorldEventsImpl {
public static final EventImpl<OnWorldRegistryReady> WORLD_REGISTRY_READY = new EventImpl<>();
public static final EventImpl<BeforeWorldLoad> BEFORE_WORLD_LOAD = new EventImpl<>();
public static final EventImpl<BeforeServerWorldLoad> BEFORE_SERVER_WORLD_LOAD = new EventImpl<>();
@ -10,4 +13,6 @@ class WorldEventsImpl {
public static final PatchWorldEvent PATCH_WORLD = new PatchWorldEvent();
public static final AdaptWorldPresetSettingEvent ADAPT_WORLD_PRESET = new AdaptWorldPresetSettingEvent();
public static final EventImpl<BeforeAddingTags> BEFORE_ADDING_TAGS = new EventImpl<>();
}

View file

@ -1,7 +1,8 @@
package org.betterx.worlds.together.worldPreset;
import org.betterx.bclib.registry.PresetsRegistry;
import org.betterx.worlds.together.WorldsTogether;
import org.betterx.worlds.together.entrypoints.EntrypointUtil;
import org.betterx.worlds.together.entrypoints.WorldPresetBootstrap;
import org.betterx.worlds.together.levelgen.WorldGenUtil;
import org.betterx.worlds.together.tag.v3.TagManager;
import org.betterx.worlds.together.tag.v3.TagRegistry;
@ -83,7 +84,8 @@ public class WorldPresets {
WorldGenUtil.Context netherContext,
WorldGenUtil.Context endContext
) {
PresetsRegistry.onLoad();
EntrypointUtil.getCommon(WorldPresetBootstrap.class)
.forEach(e -> e.bootstrapWorldPresets());
for (Map.Entry<ResourceKey<WorldPreset>, PresetBuilder> e : BUILDERS.entrySet()) {
TogetherWorldPreset preset = e.getValue().create(overworldStem, netherContext, endContext);

View file

@ -1,7 +1,5 @@
package org.betterx.worlds.together.worldPreset.client;
import org.betterx.bclib.registry.PresetsRegistryClient;
import net.minecraft.client.gui.screens.worldselection.PresetEditor;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.level.levelgen.presets.WorldPreset;
@ -20,6 +18,5 @@ public class WorldPresetsClient {
}
public static void setupClientside() {
PresetsRegistryClient.onLoad();
}
}