Skip to content

Commit

Permalink
change: Remove dependency on Architectury API
Browse files Browse the repository at this point in the history
This is a gross refactor to remove dependency on Architectury API. A lot needs to be done to properly clean this up but this works for now.
  • Loading branch information
FlashyReese committed May 11, 2024
1 parent 0083b57 commit b027bdb
Show file tree
Hide file tree
Showing 26 changed files with 283 additions and 146 deletions.
2 changes: 1 addition & 1 deletion common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ dependencies {
modImplementation "net.fabricmc:fabric-loader:$rootProject.fabric_loader_version"

// Architectury API. This is optional, and you can comment it out if you don't need it.
modImplementation "dev.architectury:architectury:$rootProject.architectury_api_version"
//modImplementation "dev.architectury:architectury:$rootProject.architectury_api_version"
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
package io.github.amerebagatelle.mods.nuit;

import dev.architectury.event.events.client.ClientGuiEvent;
import dev.architectury.event.events.client.ClientTickEvent;
import dev.architectury.platform.Platform;
import dev.architectury.registry.ReloadListenerRegistry;
import io.github.amerebagatelle.mods.nuit.api.NuitApi;
import io.github.amerebagatelle.mods.nuit.api.NuitPlatformHelper;
import io.github.amerebagatelle.mods.nuit.config.NuitConfig;
import io.github.amerebagatelle.mods.nuit.resource.SkyboxResourceListener;
import io.github.amerebagatelle.mods.nuit.screen.SkyboxDebugScreen;
import io.github.amerebagatelle.mods.nuit.skyboxes.SkyboxType;
import net.minecraft.network.chat.Component;
import net.minecraft.server.packs.PackType;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand All @@ -20,13 +13,7 @@ public class NuitClient {
private static NuitConfig CONFIG;

public static void init() {
SkyboxType.initRegistry();
ReloadListenerRegistry.register(PackType.CLIENT_RESOURCES, new SkyboxResourceListener());
SkyboxManager.getInstance().setEnabled(config().generalSettings.enable);
ClientTickEvent.CLIENT_LEVEL_POST.register(SkyboxManager.getInstance());
ClientTickEvent.CLIENT_POST.register(config().getKeyBinding());
SkyboxDebugScreen screen = new SkyboxDebugScreen(Component.nullToEmpty("Skybox Debug Screen"));
ClientGuiEvent.RENDER_HUD.register(screen);
NuitApi.getInstance().setEnabled(config().generalSettings.enable);
}

public static Logger getLogger() {
Expand All @@ -45,6 +32,6 @@ public static NuitConfig config() {
}

private static NuitConfig loadConfig() {
return NuitConfig.load(Platform.getConfigFolder().resolve("nuit-config.json").toFile());
return NuitConfig.load(NuitPlatformHelper.INSTANCE.getConfigDir().resolve("nuit-config.json").toFile());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.google.gson.JsonObject;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.serialization.JsonOps;
import dev.architectury.event.events.client.ClientTickEvent;
import io.github.amerebagatelle.mods.nuit.api.NuitPlatformHelper;
import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap;
import io.github.amerebagatelle.mods.nuit.api.NuitApi;
import io.github.amerebagatelle.mods.nuit.api.skyboxes.Skybox;
Expand All @@ -25,7 +25,7 @@
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

public class SkyboxManager implements NuitApi, ClientTickEvent.ClientLevel {
public class SkyboxManager implements NuitApi {
private static final SkyboxManager INSTANCE = new SkyboxManager();
private final Map<ResourceLocation, Skybox> skyboxMap = new Object2ObjectLinkedOpenHashMap<>();
/**
Expand All @@ -50,7 +50,7 @@ public static Skybox parseSkyboxJson(ResourceLocation id, JsonObject jsonObject)
return null;
}

SkyboxType<? extends Skybox> type = SkyboxType.REGISTRY.get(metadata.getType());
SkyboxType<? extends Skybox> type = NuitPlatformHelper.INSTANCE.getSkyboxTypeRegistry().get(metadata.getType());
if (type == null) {
NuitClient.getLogger().warn("Skipping skybox {} with unknown type {}", id.toString(), metadata.getType().getPath().replace('_', '-'));
return null;
Expand All @@ -65,6 +65,7 @@ public static SkyboxManager getInstance() {
public void addSkybox(ResourceLocation identifier, JsonObject jsonObject) {
Skybox skybox = SkyboxManager.parseSkyboxJson(identifier, jsonObject);
if (skybox != null) {
NuitClient.getLogger().info("Adding skybox {}", identifier.toString());
this.addSkybox(identifier, skybox);
}
}
Expand Down Expand Up @@ -140,7 +141,6 @@ public List<Skybox> getActiveSkyboxes() {
return this.activeSkyboxes;
}

@Override
public void tick(net.minecraft.client.multiplayer.ClientLevel client) {
StreamSupport
.stream(Iterables.concat(this.skyboxMap.values(), this.permanentSkyboxMap.values()).spliterator(), false)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.github.amerebagatelle.mods.nuit.api;

import io.github.amerebagatelle.mods.nuit.api.skyboxes.Skybox;
import io.github.amerebagatelle.mods.nuit.skyboxes.SkyboxType;
import io.github.amerebagatelle.mods.nuit.util.Utils;
import net.minecraft.core.Registry;

import java.nio.file.Path;

public interface NuitPlatformHelper {
public static NuitPlatformHelper INSTANCE = Utils.loadService(NuitPlatformHelper.class);

Path getGameDir();

Path getConfigDir();

Registry<SkyboxType<? extends Skybox>> getSkyboxTypeRegistry();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.mojang.blaze3d.platform.InputConstants;
import dev.architectury.event.events.client.ClientTickEvent;
import dev.architectury.registry.client.keymappings.KeyMappingRegistry;
import io.github.amerebagatelle.mods.nuit.NuitClient;
import io.github.amerebagatelle.mods.nuit.SkyboxManager;
import io.github.amerebagatelle.mods.nuit.api.NuitApi;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.minecraft.network.chat.Component;
Expand Down Expand Up @@ -76,25 +74,24 @@ public static class GeneralSettings {
}


public static class KeyBindingImpl implements ClientTickEvent.Client {
public static class KeyBindingImpl {

public final KeyMapping toggleFabricSkyBoxes = new KeyMapping("key.nuit.toggle", InputConstants.Type.KEYSYM, -1, "category.nuit");
public final KeyMapping toggleSkyboxDebugHud = new KeyMapping("key.nuit.toggle.debug_hud", InputConstants.Type.KEYSYM, -1, "category.nuit");

public KeyBindingImpl() {
KeyMappingRegistry.register(this.toggleFabricSkyBoxes);
KeyMappingRegistry.register(this.toggleSkyboxDebugHud);
//KeyMappingRegistry.register(this.toggleFabricSkyBoxes);
//KeyMappingRegistry.register(this.toggleSkyboxDebugHud);
}

@Override
public void tick(Minecraft client) {
while (this.toggleFabricSkyBoxes.consumeClick()) {
NuitClient.config().generalSettings.enable = !NuitClient.config().generalSettings.enable;
NuitClient.config().save();
SkyboxManager.getInstance().setEnabled(NuitClient.config().generalSettings.enable);
NuitApi.getInstance().setEnabled(NuitClient.config().generalSettings.enable);

assert client.player != null;
if (SkyboxManager.getInstance().isEnabled()) {
if (NuitApi.getInstance().isEnabled()) {
client.player.displayClientMessage(Component.translatable("nuit.message.enabled"), false);
} else {
client.player.displayClientMessage(Component.translatable("nuit.message.disabled"), false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,32 @@
import io.github.amerebagatelle.mods.nuit.NuitClient;
import io.github.amerebagatelle.mods.nuit.api.NuitApi;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.PreparableReloadListener;
import net.minecraft.server.packs.resources.Resource;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.util.profiling.ProfilerFiller;
import org.jetbrains.annotations.NotNull;

import java.io.InputStreamReader;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;

public class SkyboxResourceListener implements PreparableReloadListener {
public class SkyboxResourceListener {
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().serializeNulls().setLenient().create();

@Override
public @NotNull CompletableFuture<Void> reload(PreparationBarrier preparationBarrier, ResourceManager resourceManager, ProfilerFiller profilerFiller, ProfilerFiller profilerFiller2, Executor executor, Executor executor2) {
return CompletableFuture.runAsync(() -> {
NuitApi skyboxManager = NuitApi.getInstance();
public void readFiles(ResourceManager resourceManager) {
NuitApi skyboxManager = NuitApi.getInstance();

// clear registered skyboxes on reload
skyboxManager.clearSkyboxes();
// clear registered skyboxes on reload
skyboxManager.clearSkyboxes();

// load new skyboxes
Map<ResourceLocation, Resource> resources = resourceManager.listResources("sky", identifier -> identifier.getPath().endsWith(".json"));
// load new skyboxes
Map<ResourceLocation, Resource> resources = resourceManager.listResources("sky", identifier -> identifier.getPath().endsWith(".json"));

resources.forEach((identifier, resource) -> {
try {
JsonObject json = GSON.fromJson(new InputStreamReader(resource.open()), JsonObject.class);
skyboxManager.addSkybox(identifier, json);
} catch (Exception e) {
NuitClient.getLogger().error("Error reading skybox {}", identifier.toString());
NuitClient.getLogger().error(e.getMessage());
}
});
}, executor2).thenCompose(preparationBarrier::wait);
resources.forEach((identifier, resource) -> {
try {
JsonObject json = GSON.fromJson(new InputStreamReader(resource.open()), JsonObject.class);
skyboxManager.addSkybox(identifier, json);
} catch (Exception e) {
NuitClient.getLogger().error("Error reading skybox {}", identifier.toString());
NuitClient.getLogger().error(e.getMessage());
}
});
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.amerebagatelle.mods.nuit.screen;

import dev.architectury.event.events.client.ClientGuiEvent;
import io.github.amerebagatelle.mods.nuit.NuitClient;
import io.github.amerebagatelle.mods.nuit.SkyboxManager;
import io.github.amerebagatelle.mods.nuit.api.skyboxes.NuitSkybox;
Expand All @@ -13,7 +12,7 @@

import java.util.Map;

public class SkyboxDebugScreen extends Screen implements ClientGuiEvent.RenderHud {
public class SkyboxDebugScreen extends Screen {
public SkyboxDebugScreen(Component title) {
super(title);
}
Expand All @@ -28,10 +27,10 @@ public void render(GuiGraphics context, int mouseX, int mouseY, float delta) {
this.renderHud(context, delta);
}

@Override
public void renderHud(GuiGraphics drawContext, float tickDelta) {
if (NuitClient.config().generalSettings.debugHud || Minecraft.getInstance().screen == this) {
int yPadding = 2;
int yPadding = 16;
drawContext.drawString(Minecraft.getInstance().font, "Skybox Debug Screen", 2, 2, 0xffffffff, false);
for (Map.Entry<ResourceLocation, Skybox> identifierSkyboxEntry : SkyboxManager.getInstance().getSkyboxMap().entrySet()) {
Skybox activeSkybox = identifierSkyboxEntry.getValue();
if (activeSkybox instanceof NuitSkybox nuitSkybox && nuitSkybox.isActive()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.*;
import com.mojang.math.Axis;
import dev.architectury.registry.registries.RegistrySupplier;
import io.github.amerebagatelle.mods.nuit.NuitClient;
import io.github.amerebagatelle.mods.nuit.api.skyboxes.NuitSkybox;
import io.github.amerebagatelle.mods.nuit.api.skyboxes.Skybox;
Expand Down Expand Up @@ -36,7 +35,7 @@
* have a default constructor as it is required when checking
* the type of the skybox.
*/
public abstract class AbstractSkybox<T extends Skybox> implements NuitSkybox {
public abstract class AbstractSkybox implements NuitSkybox {

/**
* The current alpha for the skybox. Expects all skyboxes extending this to accommodate this.
Expand Down Expand Up @@ -215,8 +214,6 @@ protected boolean checkWeather() {
}
}

public abstract RegistrySupplier<SkyboxType<T>> getType();

public void renderDecorations(LevelRendererAccessor worldRendererAccess, PoseStack matrixStack, Matrix4f projectionMatrix, float tickDelta, BufferBuilder bufferBuilder, float alpha, Runnable fogCallback) {
RenderSystem.enableBlend();
Vector3f rotationStatic = this.decorations.getRotation().getStatic();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
import com.mojang.math.Axis;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import dev.architectury.registry.registries.RegistrySupplier;
import io.github.amerebagatelle.mods.nuit.mixin.LevelRendererAccessor;
import io.github.amerebagatelle.mods.nuit.skybox.*;
import net.minecraft.client.Camera;
import net.minecraft.client.renderer.GameRenderer;
import org.joml.Matrix4f;

public class MonoColorSkybox extends AbstractSkybox<MonoColorSkybox> {
public class MonoColorSkybox extends AbstractSkybox {
public static Codec<MonoColorSkybox> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Properties.CODEC.fieldOf("properties").forGetter(AbstractSkybox::getProperties),
Conditions.CODEC.optionalFieldOf("conditions", Conditions.DEFAULT).forGetter(AbstractSkybox::getConditions),
Expand All @@ -29,11 +28,6 @@ public MonoColorSkybox(Properties properties, Conditions conditions, Decorations
this.blend = blend;
}

@Override
public RegistrySupplier<SkyboxType<MonoColorSkybox>> getType() {
return SkyboxType.MONO_COLOR_SKYBOX;
}

@Override
public void render(LevelRendererAccessor worldRendererAccess, PoseStack matrices, Matrix4f projectionMatrix, float tickDelta, Camera camera, boolean thickFog, Runnable fogCallback) {
if (this.alpha > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,31 @@
import com.google.common.collect.BiMap;
import com.google.common.collect.ImmutableBiMap;
import com.mojang.serialization.Codec;
import dev.architectury.registry.registries.Registrar;
import dev.architectury.registry.registries.RegistrarManager;
import dev.architectury.registry.registries.RegistrySupplier;
import io.github.amerebagatelle.mods.nuit.NuitClient;
import io.github.amerebagatelle.mods.nuit.api.skyboxes.Skybox;
import io.github.amerebagatelle.mods.nuit.skyboxes.textured.MultiTexturedSkybox;
import io.github.amerebagatelle.mods.nuit.skyboxes.textured.SquareTexturedSkybox;
import io.github.amerebagatelle.mods.nuit.skyboxes.vanilla.EndSkybox;
import io.github.amerebagatelle.mods.nuit.skyboxes.vanilla.OverworldSkybox;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;

import java.util.Objects;
import java.util.function.Function;

public class SkyboxType<T extends Skybox> {
public static final Codec<ResourceLocation> SKYBOX_ID_CODEC;
public static final Registrar<SkyboxType<? extends Skybox>> REGISTRY = RegistrarManager.get(NuitClient.MOD_ID).<SkyboxType<? extends Skybox>>builder(new ResourceLocation(NuitClient.MOD_ID, "skybox_type")).syncToClients().build();
public static final ResourceKey<Registry<SkyboxType<? extends Skybox>>> SKYBOX_TYPE_REGISTRY_KEY = ResourceKey.createRegistryKey(new ResourceLocation(NuitClient.MOD_ID, "skybox_type"));

// Vanilla skyboxes
public static final RegistrySupplier<SkyboxType<OverworldSkybox>> OVERWORLD_SKYBOX;
public static final RegistrySupplier<SkyboxType<EndSkybox>> END_SKYBOX;
public static final SkyboxType<OverworldSkybox> OVERWORLD;
public static final SkyboxType<EndSkybox> END;

// FSB skyboxes
public static final RegistrySupplier<SkyboxType<MonoColorSkybox>> MONO_COLOR_SKYBOX;
public static final RegistrySupplier<SkyboxType<SquareTexturedSkybox>> SQUARE_TEXTURED_SKYBOX;
public static final RegistrySupplier<SkyboxType<MultiTexturedSkybox>> MULTI_TEXTURE_SKYBOX;
public static final SkyboxType<MonoColorSkybox> MONO_COLOR_SKYBOX;
public static final SkyboxType<SquareTexturedSkybox> SQUARE_TEXTURED_SKYBOX;
public static final SkyboxType<MultiTexturedSkybox> MULTI_TEXTURED_SKYBOX;

static {
SKYBOX_ID_CODEC = Codec.STRING.xmap((s) -> {
Expand All @@ -44,14 +43,15 @@ public class SkyboxType<T extends Skybox> {
return id.toString().replace('_', '-');
});


// Vanilla skyboxes
OVERWORLD_SKYBOX = register(SkyboxType.Builder.create(OverworldSkybox.class, "overworld").add(1, OverworldSkybox.CODEC).build());
END_SKYBOX = register(SkyboxType.Builder.create(EndSkybox.class, "end").add(1, EndSkybox.CODEC).build());
OVERWORLD = SkyboxType.Builder.create(OverworldSkybox.class, "overworld").add(1, OverworldSkybox.CODEC).build();
END = SkyboxType.Builder.create(EndSkybox.class, "end").add(1, EndSkybox.CODEC).build();

// FSB skyboxes
MONO_COLOR_SKYBOX = register(SkyboxType.Builder.create(MonoColorSkybox.class, "monocolor").add(1, MonoColorSkybox.CODEC).build());
SQUARE_TEXTURED_SKYBOX = register(SkyboxType.Builder.create(SquareTexturedSkybox.class, "square-textured").add(1, SquareTexturedSkybox.CODEC).build());
MULTI_TEXTURE_SKYBOX = register(SkyboxType.Builder.create(MultiTexturedSkybox.class, "multi-textured").add(1, MultiTexturedSkybox.CODEC).build());
MONO_COLOR_SKYBOX = SkyboxType.Builder.create(MonoColorSkybox.class, "monocolor").add(1, MonoColorSkybox.CODEC).build();
SQUARE_TEXTURED_SKYBOX = SkyboxType.Builder.create(SquareTexturedSkybox.class, "square-textured").add(1, SquareTexturedSkybox.CODEC).build();
MULTI_TEXTURED_SKYBOX = SkyboxType.Builder.create(MultiTexturedSkybox.class, "multi-textured").add(1, MultiTexturedSkybox.CODEC).build();
}

private final BiMap<Integer, Codec<T>> codecBiMap;
Expand All @@ -62,22 +62,12 @@ private SkyboxType(BiMap<Integer, Codec<T>> codecBiMap, String name) {
this.name = name;
}

public static void initRegistry() {
if (REGISTRY == null) {
System.err.println("[Nuit] Registry not loaded?");
}
}

private static <T extends Skybox> RegistrySupplier<SkyboxType<T>> register(SkyboxType<T> type) {
return REGISTRY.register(type.createId(NuitClient.MOD_ID), () -> type);
}

public String getName() {
return this.name;
}

public ResourceLocation createId(String namespace) {
return this.createIdFactory().apply(namespace);
public ResourceLocation createId(/*String namespace*/) {
return this.createIdFactory().apply(/*namespace*/ NuitClient.MOD_ID);
}


Expand Down Expand Up @@ -117,9 +107,5 @@ public Builder<T> add(int schemaVersion, Codec<T> codec) {
public SkyboxType<T> build() {
return new SkyboxType<>(this.builder.build(), this.name);
}

public SkyboxType<T> buildAndRegister(String namespace) {
return REGISTRY.register(new ResourceLocation(namespace, this.name.replace('-', '_')), this::build).get();
}
}
}
Loading

0 comments on commit b027bdb

Please sign in to comment.