Skip to content

Commit

Permalink
improvement, bug: Registered custom shaders properly so that Create B…
Browse files Browse the repository at this point in the history
…ig Cannons shaders don't cause logspam on crash
  • Loading branch information
rbasamoyai committed Jul 31, 2024
1 parent c174795 commit 0b3e234
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 40 deletions.
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Changed:
- Changed impact explosion queueing to better allow compatibility with Valkyrien Skies
- All Autocannon Recoil Springs on an autocannon now play the firing animation
- Format for storing Autocannon Recoil Spring positions has changed, reassemble autocannons to fix any issues
- Registered custom shaders properly so that Create Big Cannons shaders don't cause logspam on crash
Fixed:
- Fixed Trinkets compat crashing
- Fixed gas mask overlay rendering as solid color when holding Just Enough Guns firearm
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package rbasamoyai.createbigcannons.index;

import java.io.IOException;
import java.util.Locale;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;

import javax.annotation.Nullable;
Expand All @@ -11,14 +14,16 @@

import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.ShaderInstance;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.ResourceManager;
import rbasamoyai.createbigcannons.CreateBigCannons;

public enum CBCRenderTypes {
CANNON_SMOKE_PARTICLE(CBCRenderingParts.CANNON_SMOKE_PARTICLE_INPUT, VertexFormat.Mode.QUADS, CBCRenderingParts.CANNON_SMOKE_PARTICLE_STATE),
SPLINTER_PARTICLE(type -> RenderType.create(type.id, CBCRenderingParts.SPLINTER_PARTICLE_INPUT, VertexFormat.Mode.QUADS,
SPLINTER_PARTICLE(type -> RenderType.create(type.id.toString(), CBCRenderingParts.SPLINTER_PARTICLE_INPUT, VertexFormat.Mode.QUADS,
512, false, false, CBCRenderingParts.SPLINTER_PARTICLE_STATE.apply(type)));

private final String id = CreateBigCannons.MOD_ID + "_" + this.name().toLowerCase(Locale.ROOT);
private final ResourceLocation id = CreateBigCannons.resource(this.name().toLowerCase(Locale.ROOT));
private final RenderType renderType;
@Nullable private ShaderInstance shaderInstance;

Expand All @@ -27,10 +32,10 @@ public enum CBCRenderTypes {
}

CBCRenderTypes(VertexFormat format, VertexFormat.Mode mode, Function<CBCRenderTypes, RenderType.CompositeState> state) {
this(type -> RenderType.create(type.id, format, mode, 256, false, false, state.apply(type)));
this(type -> RenderType.create(type.id.toString(), format, mode, 256, false, false, state.apply(type)));
}

public String id() { return this.id; }
public ResourceLocation id() { return this.id; }
public RenderType renderType() { return this.renderType; }
@Nullable public ShaderInstance getShaderInstance() { return this.shaderInstance; }
public void setShaderInstance(ShaderInstance shaderInstance) { this.shaderInstance = shaderInstance; }
Expand All @@ -40,4 +45,14 @@ public void setRenderTypeForBuilder(BufferBuilder builder) {
builder.begin(this.renderType.mode(), this.renderType.format());
}

public static void registerAllShaders(BiConsumer<ShaderInstance, Consumer<ShaderInstance>> cons,
CreateShaderInstance shaderCreator, ResourceManager resourceManager) throws IOException {
for (CBCRenderTypes renderType : values())
cons.accept(shaderCreator.create(resourceManager, renderType.id, renderType.renderType().format()), renderType::setShaderInstance);
}

public interface CreateShaderInstance {
ShaderInstance create(ResourceManager manager, ResourceLocation location, VertexFormat format) throws IOException;
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"srcrgb": "srcalpha",
"dstrgb": "1-srcalpha"
},
"vertex": "createbigcannons_cannon_smoke_particle",
"fragment": "createbigcannons_cannon_smoke_particle",
"vertex": "createbigcannons:cannon_smoke_particle",
"fragment": "createbigcannons:cannon_smoke_particle",
"attributes": [
"Position",
"UV0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"srcrgb": "srcalpha",
"dstrgb": "1-srcalpha"
},
"vertex": "createbigcannons_splinter_particle",
"fragment": "createbigcannons_splinter_particle",
"vertex": "createbigcannons:splinter_particle",
"fragment": "createbigcannons:splinter_particle",
"attributes": [
"Position",
"UV0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"client.ClientLevelAccessor",
"client.EntityClientMixin",
"client.EntityRenderDispatcherMixin",
"client.GameRendererMixin",
"client.HumanoidPoseMixin",
"client.LevelRendererMixin",
"client.LightingMixin",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package rbasamoyai.createbigcannons.fabric;

import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.function.Consumer;

import com.mojang.blaze3d.platform.Window;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexFormat;

import io.github.fabricators_of_create.porting_lib.event.client.CameraSetupCallback;
import io.github.fabricators_of_create.porting_lib.event.client.ClientWorldEvents;
Expand All @@ -15,6 +17,7 @@
import io.github.fabricators_of_create.porting_lib.event.client.MouseButtonCallback;
import io.github.fabricators_of_create.porting_lib.event.client.MouseScrolledCallback;
import io.github.fabricators_of_create.porting_lib.event.client.ParticleManagerRegistrationCallback;
import io.github.fabricators_of_create.porting_lib.event.client.RegisterShadersCallback;
import io.github.fabricators_of_create.porting_lib.event.client.TextureStitchCallback;
import io.github.fabricators_of_create.porting_lib.event.common.ModsLoadedCallback;
import net.fabricmc.api.ClientModInitializer;
Expand All @@ -36,6 +39,7 @@
import net.minecraft.client.multiplayer.ClientPacketListener;
import net.minecraft.client.player.AbstractClientPlayer;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.ShaderInstance;
import net.minecraft.client.renderer.entity.LivingEntityRenderer;
import net.minecraft.client.renderer.entity.player.PlayerRenderer;
import net.minecraft.client.renderer.texture.TextureAtlas;
Expand All @@ -51,6 +55,7 @@
import rbasamoyai.createbigcannons.compat.trinkets.CBCTrinketsClient;
import rbasamoyai.createbigcannons.fabric.mixin.client.KeyMappingAccessor;
import rbasamoyai.createbigcannons.fabric.network.CBCNetworkFabric;
import rbasamoyai.createbigcannons.index.CBCRenderTypes;

public class CBCClientFabric implements ClientModInitializer {
@Override
Expand All @@ -77,6 +82,7 @@ public void onInitializeClient() {
ClientWorldEvents.LOAD.register(CBCClientFabric::onLoadClientLevel);
ClientLoginConnectionEvents.INIT.register(CBCClientFabric::onPlayerLogIn);
ModsLoadedCallback.EVENT.register(CBCClientFabric::onModsLoaded);
RegisterShadersCallback.EVENT.register(CBCClientFabric::onShaderReload);
}

private static void wrapOverlay(String id, CBCClientCommon.CBCGuiOverlay overlay) {
Expand Down Expand Up @@ -203,4 +209,12 @@ public CompletableFuture<Void> reload(PreparationBarrier preparationBarrier, Res
ResourceManagerHelper.get(PackType.CLIENT_RESOURCES).registerReloadListener(listener);
}

public static void onShaderReload(ResourceManager resourceManager, RegisterShadersCallback.ShaderRegistry registry) throws IOException {
CBCRenderTypes.registerAllShaders(registry::registerShader, CBCClientFabric::createShader, resourceManager);
}

private static ShaderInstance createShader(ResourceManager manager, ResourceLocation loc, VertexFormat format) throws IOException {
return new ShaderInstance(manager, loc.toString(), format);
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package rbasamoyai.createbigcannons.forge;

import java.io.IOException;

import net.minecraft.client.Minecraft;
import net.minecraft.client.player.AbstractClientPlayer;
import net.minecraft.client.renderer.ShaderInstance;
import net.minecraft.world.level.LevelAccessor;
import net.minecraftforge.client.ClientRegistry;
import net.minecraftforge.client.ConfigGuiHandler.ConfigGuiFactory;
Expand All @@ -11,6 +14,7 @@
import net.minecraftforge.client.event.InputEvent;
import net.minecraftforge.client.event.ParticleFactoryRegisterEvent;
import net.minecraftforge.client.event.RegisterClientReloadListenersEvent;
import net.minecraftforge.client.event.RegisterShadersEvent;
import net.minecraftforge.client.event.RenderPlayerEvent;
import net.minecraftforge.client.event.TextureStitchEvent;
import net.minecraftforge.client.gui.ForgeIngameGui;
Expand All @@ -29,6 +33,7 @@
import rbasamoyai.createbigcannons.compat.curios.CBCCuriosRenderers;
import rbasamoyai.createbigcannons.config.CBCConfigs;
import rbasamoyai.createbigcannons.index.CBCBlockPartials;
import rbasamoyai.createbigcannons.index.CBCRenderTypes;

public class CBCClientForge {

Expand All @@ -43,6 +48,7 @@ public static void prepareClient(IEventBus modEventBus, IEventBus forgeEventBus)
modEventBus.addListener(CBCClientForge::onTextureStitchAtlasPre);
modEventBus.addListener(CBCClientForge::onLoadComplete);
modEventBus.addListener(CBCClientForge::onRegisterClientReloadListeners);
modEventBus.addListener(CBCClientForge::onRegisterShaders);

forgeEventBus.addListener(CBCClientForge::getFogColor);
forgeEventBus.addListener(CBCClientForge::getFogDensity);
Expand Down Expand Up @@ -160,4 +166,12 @@ public static void onRegisterClientReloadListeners(RegisterClientReloadListeners
CBCClientCommon.registerClientReloadListeners((listener, id) -> evt.registerReloadListener(listener));
}

public static void onRegisterShaders(RegisterShadersEvent evt) {
try {
CBCRenderTypes.registerAllShaders(evt::registerShader, ShaderInstance::new, evt.getResourceManager());
} catch (IOException e) {
throw new RuntimeException("Failed to reload Create Big Cannons shaders: ", e);
}
}

}

0 comments on commit 0b3e234

Please sign in to comment.