Skip to content

Commit

Permalink
Codec API
Browse files Browse the repository at this point in the history
  • Loading branch information
LudoCrypt committed Feb 6, 2022
1 parent 16393a2 commit f7a81f2
Show file tree
Hide file tree
Showing 26 changed files with 539 additions and 288 deletions.
11 changes: 11 additions & 0 deletions src/main/java/net/ludocrypt/limlib/access/DimensionTypeAccess.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package net.ludocrypt.limlib.access;

import net.ludocrypt.limlib.impl.LiminalEffects;

public interface DimensionTypeAccess {

public LiminalEffects getLiminalEffects();

public void setLiminalEffects(LiminalEffects effects);

}
19 changes: 19 additions & 0 deletions src/main/java/net/ludocrypt/limlib/api/LimLib.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net.ludocrypt.limlib.api;

import net.fabricmc.api.ModInitializer;
import net.ludocrypt.limlib.api.render.LiminalShader;
import net.ludocrypt.limlib.api.render.SkyHook;
import net.ludocrypt.limlib.impl.world.LiminalShaderRegistry;
import net.ludocrypt.limlib.impl.world.LiminalSkyRegistry;
import net.minecraft.util.Identifier;

public class LimLib implements ModInitializer {

@Override
public void onInitialize() {
LiminalSkyRegistry.register(new Identifier("limlib", "regular_sky"), SkyHook.RegularSky.CODEC);
LiminalSkyRegistry.register(new Identifier("limlib", "skybox_sky"), SkyHook.SkyboxSky.CODEC);
LiminalShaderRegistry.register(new Identifier("limlib", "simple_shader"), LiminalShader.SimpleShader.CODEC);
}

}
12 changes: 8 additions & 4 deletions src/main/java/net/ludocrypt/limlib/api/LimLibClient.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package net.ludocrypt.limlib.api;

import java.util.Optional;

import ladysnake.satin.api.event.ShaderEffectRenderCallback;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.ludocrypt.limlib.access.DimensionTypeAccess;
import net.ludocrypt.limlib.api.render.LiminalShader;
import net.ludocrypt.limlib.api.sound.RandomSoundEmitter;
import net.ludocrypt.limlib.impl.render.LiminalShaderRegistry;
import net.minecraft.block.BlockState;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayerEntity;
Expand All @@ -21,9 +23,11 @@ public class LimLibClient implements ClientModInitializer {
public void onInitializeClient() {
ShaderEffectRenderCallback.EVENT.register(tickDelta -> {
MinecraftClient client = MinecraftClient.getInstance();
LiminalShader shader = LiminalShaderRegistry.getCurrent(client);
if (shader.shouldRender(client, tickDelta)) {
shader.getShader(client, tickDelta).render(tickDelta);
Optional<LiminalShader> shader = ((DimensionTypeAccess) client.world.getDimension()).getLiminalEffects().getShader();
if (shader.isPresent()) {
if (shader.get().shouldRender(client, tickDelta)) {
shader.get().getShader(client, tickDelta).render(tickDelta);
}
}
});

Expand Down
33 changes: 28 additions & 5 deletions src/main/java/net/ludocrypt/limlib/api/render/LiminalShader.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,40 @@
package net.ludocrypt.limlib.api.render;

import java.util.function.Function;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;

import ladysnake.satin.api.managed.ManagedShaderEffect;
import ladysnake.satin.api.managed.ShaderEffectManager;
import net.ludocrypt.limlib.impl.world.LiminalShaderRegistry;
import net.minecraft.client.MinecraftClient;
import net.minecraft.util.Identifier;

public abstract class LiminalShader {

public static final Codec<LiminalShader> CODEC = LiminalShaderRegistry.LIMINAL_SHADER.getCodec().dispatchStable(LiminalShader::getCodec, Function.identity());

public abstract boolean shouldRender(MinecraftClient client, float tickdelta);

public abstract ManagedShaderEffect getShader(MinecraftClient client, float tickdelta);

public abstract Codec<? extends LiminalShader> getCodec();

public static class SimpleShader extends LiminalShader {

public static final Codec<SimpleShader> CODEC = RecordCodecBuilder.create((instance) -> {
return instance.group(Identifier.CODEC.fieldOf("shader").stable().forGetter((shader) -> {
return shader.getShaderId();
})).apply(instance, instance.stable(SimpleShader::new));
});

private final ManagedShaderEffect shader;
private final Identifier shaderId;

public SimpleShader(Identifier shader) {
this(ShaderEffectManager.getInstance().manage(new Identifier(shader.getNamespace(), "shaders/post/" + shader.getPath() + ".json")));
}

public SimpleShader(ManagedShaderEffect shader) {
this.shader = shader;
this.shader = ShaderEffectManager.getInstance().manage(new Identifier(shader.getNamespace(), "shaders/post/" + shader.getPath() + ".json"));
this.shaderId = shader;
}

@Override
Expand All @@ -33,6 +47,15 @@ public ManagedShaderEffect getShader(MinecraftClient client, float tickdelta) {
return shader;
}

public Identifier getShaderId() {
return shaderId;
}

@Override
public Codec<? extends LiminalShader> getCodec() {
return SimpleShader.CODEC;
}

}

}
29 changes: 29 additions & 0 deletions src/main/java/net/ludocrypt/limlib/api/render/SkyHook.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package net.ludocrypt.limlib.api.render;

import java.util.function.Function;

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.ludocrypt.limlib.impl.world.LiminalSkyRegistry;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.BufferBuilder;
import net.minecraft.client.render.BufferRenderer;
Expand All @@ -21,16 +26,35 @@
@Environment(EnvType.CLIENT)
public abstract class SkyHook {

public static final Codec<SkyHook> CODEC = LiminalSkyRegistry.LIMINAL_SKY.getCodec().dispatchStable(SkyHook::getCodec, Function.identity());

public abstract void renderSky(WorldRenderer worldRenderer, MinecraftClient client, MatrixStack matrices, Matrix4f projectionMatrix, float tickDelta);

public abstract Codec<? extends SkyHook> getCodec();

public static class RegularSky extends SkyHook {

public static final Codec<RegularSky> CODEC = RecordCodecBuilder.create((instance) -> instance.stable(new RegularSky()));

@Override
public void renderSky(WorldRenderer worldRenderer, MinecraftClient client, MatrixStack matrices, Matrix4f projectionMatrix, float tickDelta) {
}

@Override
public Codec<? extends SkyHook> getCodec() {
return CODEC;
}

}

public static class SkyboxSky extends SkyHook {

public static final Codec<SkyboxSky> CODEC = RecordCodecBuilder.create((instance) -> {
return instance.group(Identifier.CODEC.fieldOf("skybox").stable().forGetter((sky) -> {
return sky.identifier;
})).apply(instance, instance.stable(SkyboxSky::new));
});

public final Identifier identifier;

public SkyboxSky(Identifier identifier) {
Expand Down Expand Up @@ -95,6 +119,11 @@ public void renderSky(WorldRenderer worldRenderer, MinecraftClient client, Matri
RenderSystem.disableBlend();
}

@Override
public Codec<? extends SkyHook> getCodec() {
return CODEC;
}

}

}
Original file line number Diff line number Diff line change
@@ -1,37 +1,62 @@
package net.ludocrypt.limlib.api.sound;

import java.util.Optional;
import java.util.List;

import org.apache.commons.lang3.mutable.Mutable;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;

import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundEvent;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.World;

public abstract class LiminalTravelSound {
public class LiminalTravelSound {

public abstract void hookSound(ServerWorld from, ServerWorld to, Mutable<Optional<SoundEvent>> mutable);
private List<TravelRule> toTravelRules;
private List<TravelRule> fromTravelRules;

public int priority() {
return 1000;
public static final Codec<LiminalTravelSound> CODEC = RecordCodecBuilder.create((instance) -> {
return instance.group(Codec.list(TravelRule.CODEC).fieldOf("to_rules").stable().forGetter((travelSound) -> {
return travelSound.getToTravelRules();
}), Codec.list(TravelRule.CODEC).fieldOf("from_rules").stable().forGetter((travelSound) -> {
return travelSound.getFromTravelRules();
})).apply(instance, instance.stable(LiminalTravelSound::new));
});

public LiminalTravelSound(List<TravelRule> toTravelRules, List<TravelRule> fromTravelRules) {
this.toTravelRules = toTravelRules;
this.fromTravelRules = fromTravelRules;
}

public List<TravelRule> getToTravelRules() {
return toTravelRules;
}

public List<TravelRule> getFromTravelRules() {
return fromTravelRules;
}

public static class SimpleTravelSound extends LiminalTravelSound {
public static class TravelRule {

public static final Codec<TravelRule> CODEC = RecordCodecBuilder.create((instance) -> {
return instance.group(Codec.STRING.fieldOf("regex").stable().forGetter((travelRule) -> {
return travelRule.getRegex();
}), SoundEvent.CODEC.fieldOf("result").stable().forGetter((travelRule) -> {
return travelRule.getResult();
})).apply(instance, instance.stable(TravelRule::new));
});

private final RegistryKey<World> world;
private final SoundEvent sound;
private String regex;
private SoundEvent result;

public TravelRule(String regex, SoundEvent result) {
this.regex = regex;
this.result = result;
}

public SimpleTravelSound(RegistryKey<World> world, SoundEvent sound) {
this.world = world;
this.sound = sound;
public String getRegex() {
return regex;
}

@Override
public void hookSound(ServerWorld from, ServerWorld to, Mutable<Optional<SoundEvent>> mutable) {
if (to.getRegistryKey().equals(world)) {
mutable.setValue(Optional.of(sound));
}
public SoundEvent getResult() {
return result;
}

}
Expand Down
24 changes: 15 additions & 9 deletions src/main/java/net/ludocrypt/limlib/api/sound/ReverbFilter.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package net.ludocrypt.limlib.api.sound;

import java.util.Optional;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.lwjgl.openal.AL11;
import org.lwjgl.openal.EXTEfx;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.ludocrypt.limlib.impl.sound.LiminalWorldReverb;
import net.ludocrypt.limlib.access.DimensionTypeAccess;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.sound.SoundInstance;
import net.minecraft.util.math.MathHelper;
Expand Down Expand Up @@ -56,15 +58,19 @@ public static boolean update(SoundInstance soundInstance, ReverbSettings data) {

public static void update(SoundInstance soundInstance, int sourceID) {
MinecraftClient client = MinecraftClient.getInstance();

if (!(ReverbSettings.shouldIgnore(soundInstance.getId()) || client == null || client.world == null)) {
for (int i = 0; i < 2; i++) {
AL11.alSourcei(sourceID, EXTEfx.AL_DIRECT_FILTER, 0);
AL11.alSource3i(sourceID, EXTEfx.AL_AUXILIARY_SEND_FILTER, update(soundInstance, LiminalWorldReverb.getCurrent(client)) ? slot : 0, 0, 0);
int error = AL11.alGetError();
if (error == AL11.AL_NO_ERROR) {
break;
} else {
LOGGER.warn("OpenAl Error {}", error);
Optional<ReverbSettings> reverb = ((DimensionTypeAccess) client.world.getDimension()).getLiminalEffects().getReverb();
if (reverb.isPresent()) {
for (int i = 0; i < 2; i++) {
AL11.alSourcei(sourceID, EXTEfx.AL_DIRECT_FILTER, 0);
AL11.alSource3i(sourceID, EXTEfx.AL_AUXILIARY_SEND_FILTER, update(soundInstance, reverb.get()) ? slot : 0, 0, 0);
int error = AL11.alGetError();
if (error == AL11.AL_NO_ERROR) {
break;
} else {
LOGGER.warn("OpenAl Error {}", error);
}
}
}
}
Expand Down
Loading

0 comments on commit f7a81f2

Please sign in to comment.