Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle logging of ISBRH exceptions and multiple exception types #668

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/main/java/com/gtnewhorizons/angelica/common/BlockError.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.util.IIcon;

/**
* This Block is not actually getting registered. A static instance is created in AngelicaMod.
Expand All @@ -19,6 +21,8 @@

public class BlockError extends Block {

public static final IIcon[] icons = new IIcon[2];

public BlockError() {
super(Material.rock);
}
Expand All @@ -27,7 +31,15 @@ public BlockError() {
@SideOnly(Side.CLIENT)
public void registerBlockIcons(IIconRegister reg)
{
this.blockIcon = reg.registerIcon("angelica:error_block");
for (int i = 0; i < icons.length; i++) {
icons[i] = reg.registerIcon("angelica:error_block_" + i);
}
}

@Override
@SideOnly(Side.CLIENT)
public IIcon getIcon(int side, int meta) {
return icons[meta];
}

}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.gtnewhorizons.angelica.mixins.early.sodium;

import com.gtnewhorizons.angelica.AngelicaMod;
import com.gtnewhorizons.angelica.common.BlockError;
import com.gtnewhorizons.angelica.config.AngelicaConfig;
import com.gtnewhorizons.angelica.loading.AngelicaTweaker;
import cpw.mods.fml.client.registry.RenderingRegistry;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import me.jellysquid.mods.sodium.client.SodiumClientMod;
import net.coderbot.iris.block_rendering.BlockRenderingSettings;
import net.minecraft.block.Block;
Expand All @@ -19,6 +23,17 @@
@Mixin(RenderBlocks.class)
public abstract class MixinRenderBlocks {

@Unique
private static final ObjectOpenHashSet<String> isbrhExceptionCache = new ObjectOpenHashSet<>();

@Unique
private static final Object2IntOpenHashMap<Class<? extends Exception>> exceptionErrorBlockMap = new Object2IntOpenHashMap<>();

static {
exceptionErrorBlockMap.put(NullPointerException.class, 0);
exceptionErrorBlockMap.put(ArrayIndexOutOfBoundsException.class, 1);
}

@Unique
private boolean isRenderingByType = false;

Expand Down Expand Up @@ -54,12 +69,7 @@ private void renderingByTypeDisable(CallbackInfoReturnable<Boolean> ci) {
expect = 0
)
private boolean wrapRenderWorldBlockObfuscated(RenderBlocks rb, IBlockAccess world, int x, int y, int z, Block block, int modelId) {
try {
return RenderingRegistry.instance().renderWorldBlock(rb, world, x, y, z, block, modelId);
} catch (NullPointerException ignored) {
rb.renderStandardBlock(AngelicaMod.blockError, x, y, z);
}
return false;
return handleISBRHException(rb, world, x, y, z, block, modelId);
}

@Redirect(
Expand All @@ -72,12 +82,7 @@ private boolean wrapRenderWorldBlockObfuscated(RenderBlocks rb, IBlockAccess wor
expect = 0
)
private boolean wrapRenderWorldBlockDeobfuscated(RenderBlocks rb, IBlockAccess world, int x, int y, int z, Block block, int modelId) {
try {
return RenderingRegistry.instance().renderWorldBlock(rb, world, x, y, z, block, modelId);
} catch (NullPointerException ignored) {
rb.renderStandardBlock(AngelicaMod.blockError, x, y, z);
}
return false;
return handleISBRHException(rb, world, x, y, z, block, modelId);
}

@Redirect(method = "renderStandardBlock", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/Minecraft;isAmbientOcclusionEnabled()Z"))
Expand All @@ -89,4 +94,23 @@ private boolean checkAOEnabled() {

return Minecraft.isAmbientOcclusionEnabled();
}

private boolean handleISBRHException(RenderBlocks rb, IBlockAccess world, int x, int y, int z, Block block, int modelId) {
try {
return RenderingRegistry.instance().renderWorldBlock(rb, world, x, y, z, block, modelId);
} catch (NullPointerException | ArrayIndexOutOfBoundsException e) {
// Render Error Block
int meta = exceptionErrorBlockMap.getOrDefault(e.getClass(), 0);
rb.overrideBlockTexture = BlockError.icons[exceptionErrorBlockMap.getOrDefault(e.getClass(), 0)];
rb.renderStandardBlock(AngelicaMod.blockError, x, y, z);
rb.overrideBlockTexture = null;

// Check if we've already caught the exception for this block and log it if we haven't
String key = block.getUnlocalizedName() + ":" + meta;
if (isbrhExceptionCache.add(key)) {
AngelicaTweaker.LOGGER.warn("Caught an exception during ISBRH rendering for {} at position {}, {}, {} with renderer ID {}", block.getUnlocalizedName(), x, y, z, modelId, e);
}
}
return false;
}
}