Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/1.21/dev' into 1.21/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
AMereBagatelle committed Sep 25, 2024
2 parents 07efb70 + f2e7995 commit b046407
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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 org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand All @@ -10,6 +11,7 @@ public class NuitClient {

private static Logger LOGGER;
private static NuitConfig CONFIG;
private static final SkyboxResourceListener skyboxResourceListener = new SkyboxResourceListener();

public static void init() {
SkyboxManager.getInstance().setEnabled(config().generalSettings.enable);
Expand All @@ -30,6 +32,10 @@ public static NuitConfig config() {
return CONFIG;
}

public static SkyboxResourceListener skyboxResourceListener() {
return skyboxResourceListener;
}

private static NuitConfig loadConfig() {
return NuitConfig.load(NuitPlatformHelper.INSTANCE.getConfigDir().resolve("nuit-config.json").toFile());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
import io.github.amerebagatelle.mods.nuit.components.Metadata;
import io.github.amerebagatelle.mods.nuit.mixin.LevelRendererAccessor;
import io.github.amerebagatelle.mods.nuit.skybox.DefaultHandler;
import io.github.amerebagatelle.mods.nuit.skybox.TextureRegistrar;
import io.github.amerebagatelle.mods.nuit.skybox.SkyboxType;
import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap;
import net.minecraft.client.Camera;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.renderer.texture.SimpleTexture;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.ApiStatus.Internal;
import org.joml.Matrix4f;
Expand All @@ -23,6 +26,7 @@

public class SkyboxManager implements NuitApi {
private static final SkyboxManager INSTANCE = new SkyboxManager();
private final List<ResourceLocation> preloadedTextures = new ArrayList<>();
private final Map<ResourceLocation, Skybox> skyboxMap = new Object2ObjectLinkedOpenHashMap<>();
/**
* Stores a list of permanent skyboxes
Expand Down Expand Up @@ -65,7 +69,7 @@ public static SkyboxManager getInstance() {
}

public void addSkybox(ResourceLocation identifier, JsonObject jsonObject) {
var skybox = SkyboxManager.parseSkyboxJson(identifier, jsonObject);
Optional<Skybox> skybox = SkyboxManager.parseSkyboxJson(identifier, jsonObject);
if (skybox.isPresent()) {
NuitClient.getLogger().info("Adding skybox {}", identifier.toString());
this.addSkybox(identifier, skybox.get());
Expand All @@ -76,6 +80,14 @@ public void addSkybox(ResourceLocation identifier, Skybox skybox) {
Preconditions.checkNotNull(identifier, "Identifier was null");
Preconditions.checkNotNull(skybox, "Skybox was null");
DefaultHandler.addConditions(skybox);

if (skybox instanceof TextureRegistrar textureRegistrar) {
textureRegistrar.getTexturesToRegister().forEach(resourceLocation -> {
Minecraft.getInstance().getTextureManager().register(resourceLocation, new SimpleTexture(resourceLocation));
this.preloadedTextures.add(resourceLocation);
});
}

this.skyboxMap.put(identifier, skybox);
}

Expand All @@ -98,6 +110,8 @@ public void clearSkyboxes() {
DefaultHandler.clearConditionsExcept(this.permanentSkyboxMap.values());
this.skyboxMap.clear();
this.activeSkyboxes.clear();
this.preloadedTextures.forEach(texture -> Minecraft.getInstance().getTextureManager().release(texture));
this.preloadedTextures.clear();
}

@Internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@
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 {
public class SkyboxResourceListener implements PreparableReloadListener {
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().serializeNulls().setLenient().create();

public void readFiles(ResourceManager resourceManager) {
public void readFiles(ResourceManager resourceManager, Executor backgroundExecutor) {
NuitApi skyboxManager = NuitApi.getInstance();

skyboxManager.clearSkyboxes();
Expand All @@ -31,4 +36,9 @@ public void readFiles(ResourceManager resourceManager) {
}
});
}

@Override
public @NotNull CompletableFuture<Void> reload(PreparationBarrier preparationBarrier, ResourceManager resourceManager, ProfilerFiller profilerFiller, ProfilerFiller profilerFiller2, Executor executor, Executor executor2) {
return CompletableFuture.runAsync(() -> this.readFiles(resourceManager, executor), executor2).thenCompose(preparationBarrier::wait);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.github.amerebagatelle.mods.nuit.skybox;

import net.minecraft.resources.ResourceLocation;

import java.util.List;

public interface TextureRegistrar {
List<ResourceLocation> getTexturesToRegister();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.github.amerebagatelle.mods.nuit.skybox.AbstractSkybox;
import io.github.amerebagatelle.mods.nuit.util.Utils;
import net.minecraft.client.Camera;
import net.minecraft.resources.ResourceLocation;
import org.joml.Matrix4f;

import java.util.ArrayList;
Expand Down Expand Up @@ -89,4 +90,9 @@ public void renderSkybox(LevelRendererAccessor worldRendererAccess, PoseStack ma
public List<AnimatableTexture> getAnimations() {
return this.animatableTextures;
}

@Override
public List<ResourceLocation> getTexturesToRegister() {
return this.animatableTextures.stream().map(texture -> texture.getTexture().getTextureId()).toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
import io.github.amerebagatelle.mods.nuit.skybox.AbstractSkybox;
import io.github.amerebagatelle.mods.nuit.util.Utils;
import net.minecraft.client.Camera;
import net.minecraft.resources.ResourceLocation;
import org.joml.Matrix4f;

import java.util.List;

public class SquareTexturedSkybox extends TexturedSkybox {
public static Codec<SquareTexturedSkybox> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Properties.CODEC.optionalFieldOf("properties", Properties.of()).forGetter(AbstractSkybox::getProperties),
Expand Down Expand Up @@ -68,4 +71,9 @@ public void renderSkybox(LevelRendererAccessor worldRendererAccess, PoseStack ma
}
BufferUploader.drawWithShader(bufferBuilder.buildOrThrow());
}

@Override
public List<ResourceLocation> getTexturesToRegister() {
return List.of(this.texture.getTextureId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.github.amerebagatelle.mods.nuit.components.Rotation;
import io.github.amerebagatelle.mods.nuit.mixin.LevelRendererAccessor;
import io.github.amerebagatelle.mods.nuit.skybox.AbstractSkybox;
import io.github.amerebagatelle.mods.nuit.skybox.TextureRegistrar;
import net.minecraft.client.Camera;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
Expand All @@ -17,7 +18,7 @@

import java.util.Objects;

public abstract class TexturedSkybox extends AbstractSkybox implements RotatableSkybox {
public abstract class TexturedSkybox extends AbstractSkybox implements RotatableSkybox, TextureRegistrar {
public Rotation rotation;
public Blend blend;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,16 @@ public class Utils {
* @return The output intersection
*/
public static UVRange mapUVRanges(UVRange input, UVRange output, UVRange inputIntersection) {
float u1 = (inputIntersection.getMinU() - input.getMinU()) / (input.getMaxU() - input.getMinU()) * (output.getMaxU() - output.getMinU()) + output.getMinU();
float u2 = (inputIntersection.getMaxU() - input.getMinU()) / (input.getMaxU() - input.getMinU()) * (output.getMaxU() - output.getMinU()) + output.getMinU();
float v1 = (inputIntersection.getMinV() - input.getMinV()) / (input.getMaxV() - input.getMinV()) * (output.getMaxV() - output.getMinV()) + output.getMinV();
float v2 = (inputIntersection.getMaxV() - input.getMinV()) / (input.getMaxV() - input.getMinV()) * (output.getMaxV() - output.getMinV()) + output.getMinV();
float inputUWidth = input.getMaxU() - input.getMinU();
float outputUWidth = output.getMaxU() - output.getMinU();
float inputVHeight = input.getMaxV() - input.getMinV();
float outputVHeight = output.getMaxV() - output.getMinV();

float u1 = (inputIntersection.getMinU() - input.getMinU()) / inputUWidth * outputUWidth + output.getMinU();
float u2 = (inputIntersection.getMaxU() - input.getMinU()) / inputUWidth * outputUWidth + output.getMinU();
float v1 = (inputIntersection.getMinV() - input.getMinV()) / inputVHeight * outputVHeight + output.getMinV();
float v2 = (inputIntersection.getMaxV() - input.getMinV()) / inputVHeight * outputVHeight + output.getMinV();

return new UVRange(u1, v1, u2, v2);
}

Expand Down Expand Up @@ -70,8 +76,8 @@ public static UVRange findUVIntersection(UVRange first, UVRange second) {
*/
public static boolean checkRanges(double value, List<MinMaxEntry> minMaxEntries, boolean inverse) {
return minMaxEntries.isEmpty() || (inverse ^ minMaxEntries.stream()
.anyMatch(minMaxEntry -> Range.closed(minMaxEntry.min(), minMaxEntry.max())
.contains((float) value)));
.map(entry -> Range.closed(entry.min(), entry.max()))
.anyMatch(range -> range.contains((float) value)));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,37 @@
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback;
import net.fabricmc.fabric.api.event.registry.FabricRegistryBuilder;
import net.fabricmc.fabric.api.resource.IdentifiableResourceReloadListener;
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener;
import net.minecraft.client.Minecraft;
import net.minecraft.core.MappedRegistry;
import net.minecraft.core.Registry;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.PackType;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.util.profiling.ProfilerFiller;
import org.jetbrains.annotations.NotNull;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;

public class NuitClientFabric implements ClientModInitializer {
public static final Registry<SkyboxType<? extends Skybox>> REGISTRY = FabricRegistryBuilder.from(new MappedRegistry<>(SkyboxType.SKYBOX_TYPE_REGISTRY_KEY, Lifecycle.stable())).buildAndRegister();

@Override
public void onInitializeClient() {
SkyboxType.register(skyboxType -> Registry.register(REGISTRY, skyboxType.createId(), skyboxType));
ResourceManagerHelper.get(PackType.CLIENT_RESOURCES).registerReloadListener(new SimpleSynchronousResourceReloadListener() {
ResourceManagerHelper.get(PackType.CLIENT_RESOURCES).registerReloadListener(new IdentifiableResourceReloadListener() {
@Override
public ResourceLocation getFabricId() {
return ResourceLocation.tryBuild(NuitClient.MOD_ID, "skybox_reader");
public @NotNull CompletableFuture<Void> reload(PreparationBarrier preparationBarrier, ResourceManager resourceManager, ProfilerFiller profilerFiller, ProfilerFiller profilerFiller2, Executor executor, Executor executor2) {
return NuitClient.skyboxResourceListener().reload(preparationBarrier, resourceManager, profilerFiller, profilerFiller2, executor, executor2);
}

@Override
public void onResourceManagerReload(ResourceManager resourceManager) {
new SkyboxResourceListener().readFiles(resourceManager);
public ResourceLocation getFabricId() {
return ResourceLocation.tryBuild(NuitClient.MOD_ID, "skybox_reader");
}
});

Expand All @@ -46,6 +53,5 @@ public void onResourceManagerReload(ResourceManager resourceManager) {
KeyBindingHelper.registerKeyBinding(NuitClient.config().getKeyBinding().toggleNuit);
KeyBindingHelper.registerKeyBinding(NuitClient.config().getKeyBinding().toggleSkyboxDebugHud);
NuitClient.init();

}
}
2 changes: 1 addition & 1 deletion fabric/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@
],
"depends": {
"fabricloader": ">=0.15.11",
"minecraft": ">=1.20.6"
"minecraft": ">=1.21"
}
}
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ maven_group=io.github.amerebagatelle.mods
archives_name=nuit
enabled_platforms=fabric,neoforge
# Minecraft properties
minecraft_version=1.21
minecraft_version=1.21.1
# Dependencies
architectury_api_version=12.1.2
fabric_loader_version=0.15.11
fabric_api_version=0.100.1+1.21
neoforge_version=21.0.2-beta
fabric_api_version=0.102.1+1.21.1
neoforge_version=21.1.15
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import io.github.amerebagatelle.mods.nuit.NuitClient;
import io.github.amerebagatelle.mods.nuit.SkyboxManager;
import io.github.amerebagatelle.mods.nuit.api.skyboxes.Skybox;
import io.github.amerebagatelle.mods.nuit.resource.SkyboxResourceListener;
import io.github.amerebagatelle.mods.nuit.screen.SkyboxDebugScreen;
import io.github.amerebagatelle.mods.nuit.skybox.SkyboxType;
import net.minecraft.client.Minecraft;
Expand All @@ -23,8 +22,6 @@
import net.neoforged.neoforge.registries.RegisterEvent;
import net.neoforged.neoforge.registries.RegistryBuilder;

import java.util.concurrent.CompletableFuture;

@Mod(NuitClient.MOD_ID)
public final class NuitNeoForge {
public static final Registry<SkyboxType<? extends Skybox>> REGISTRY = new RegistryBuilder<>(SkyboxType.SKYBOX_TYPE_REGISTRY_KEY).create();
Expand Down Expand Up @@ -76,6 +73,6 @@ public void registerSkyTypes(RegisterEvent event) {

@SubscribeEvent
public void registerClientReloadListener(RegisterClientReloadListenersEvent event) {
event.registerReloadListener((pPreparationBarrier, pResourceManager, pPreparationsProfiler, pReloadProfiler, pBackgroundExecutor, pGameExecutor) -> CompletableFuture.runAsync(() -> new SkyboxResourceListener().readFiles(pResourceManager), pGameExecutor).thenCompose(pPreparationBarrier::wait));
event.registerReloadListener((pPreparationBarrier, pResourceManager, pPreparationsProfiler, pReloadProfiler, pBackgroundExecutor, pGameExecutor) -> NuitClient.skyboxResourceListener().reload(pPreparationBarrier, pResourceManager, pPreparationsProfiler, pReloadProfiler, pBackgroundExecutor, pGameExecutor));
}
}

0 comments on commit b046407

Please sign in to comment.