diff --git a/src/main/java/com/gtnewhorizons/angelica/common/BlockError.java b/src/main/java/com/gtnewhorizons/angelica/common/BlockError.java index d2ef2051f..43b3b97de 100644 --- a/src/main/java/com/gtnewhorizons/angelica/common/BlockError.java +++ b/src/main/java/com/gtnewhorizons/angelica/common/BlockError.java @@ -2,9 +2,11 @@ package com.gtnewhorizons.angelica.common; 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. @@ -19,6 +21,8 @@ import net.minecraft.client.renderer.texture.IIconRegister; public class BlockError extends Block { + public static final IIcon[] icons = new IIcon[2]; + public BlockError() { super(Material.rock); } @@ -27,7 +31,15 @@ public class BlockError extends Block { @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]; } } diff --git a/src/main/resources/assets/angelica/textures/blocks/error_block.png b/src/main/resources/assets/angelica/textures/blocks/error_block_0.png similarity index 100% rename from src/main/resources/assets/angelica/textures/blocks/error_block.png rename to src/main/resources/assets/angelica/textures/blocks/error_block_0.png diff --git a/src/main/resources/assets/angelica/textures/blocks/error_block_1.png b/src/main/resources/assets/angelica/textures/blocks/error_block_1.png new file mode 100644 index 000000000..91b891389 Binary files /dev/null and b/src/main/resources/assets/angelica/textures/blocks/error_block_1.png differ diff --git a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/sodium/MixinRenderBlocks.java b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/sodium/MixinRenderBlocks.java index 0d15012e4..a41624f7f 100644 --- a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/sodium/MixinRenderBlocks.java +++ b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/sodium/MixinRenderBlocks.java @@ -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; @@ -19,6 +23,17 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @Mixin(RenderBlocks.class) public abstract class MixinRenderBlocks { + @Unique + private static final ObjectOpenHashSet isbrhExceptionCache = new ObjectOpenHashSet<>(); + + @Unique + private static final Object2IntOpenHashMap> exceptionErrorBlockMap = new Object2IntOpenHashMap<>(); + + static { + exceptionErrorBlockMap.put(NullPointerException.class, 0); + exceptionErrorBlockMap.put(ArrayIndexOutOfBoundsException.class, 1); + } + @Unique private boolean isRenderingByType = false; @@ -54,12 +69,7 @@ public abstract class MixinRenderBlocks { 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( @@ -72,12 +82,7 @@ public abstract class MixinRenderBlocks { 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")) @@ -89,4 +94,23 @@ public abstract class MixinRenderBlocks { 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; + } }