|
| 1 | +package com.falsepattern.falsetweaks.mixin.mixins.client.threadedupdates.malisis; |
| 2 | + |
| 3 | +import com.falsepattern.falsetweaks.api.threading.ThreadSafeBlockRenderer; |
| 4 | +import net.malisis.core.renderer.IRenderWorldLast; |
| 5 | +import net.malisis.core.renderer.MalisisRenderer; |
| 6 | +import org.spongepowered.asm.mixin.Mixin; |
| 7 | +import org.spongepowered.asm.mixin.Unique; |
| 8 | +import org.spongepowered.asm.mixin.injection.At; |
| 9 | +import org.spongepowered.asm.mixin.injection.Inject; |
| 10 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; |
| 11 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; |
| 12 | + |
| 13 | +import net.minecraft.block.Block; |
| 14 | +import net.minecraft.client.renderer.RenderBlocks; |
| 15 | +import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer; |
| 16 | +import net.minecraft.item.ItemStack; |
| 17 | +import net.minecraft.tileentity.TileEntity; |
| 18 | +import net.minecraft.world.IBlockAccess; |
| 19 | + |
| 20 | +import net.minecraftforge.client.IItemRenderer; |
| 21 | +import net.minecraftforge.client.event.RenderWorldLastEvent; |
| 22 | + |
| 23 | +import cpw.mods.fml.client.registry.ISimpleBlockRenderingHandler; |
| 24 | + |
| 25 | +import java.util.concurrent.atomic.AtomicInteger; |
| 26 | +import java.util.concurrent.locks.ReentrantLock; |
| 27 | + |
| 28 | +@Mixin(MalisisRenderer.class) |
| 29 | +public abstract class MalisisRendererMixin extends TileEntitySpecialRenderer |
| 30 | + implements ISimpleBlockRenderingHandler, IItemRenderer, IRenderWorldLast, ThreadSafeBlockRenderer { |
| 31 | + @Unique |
| 32 | + private static final ReentrantLock ft$lock = new ReentrantLock(); |
| 33 | + @Unique |
| 34 | + private static final AtomicInteger ft$lockCounter = new AtomicInteger(0); |
| 35 | + @Inject(method = "renderInventoryBlock", |
| 36 | + at = @At("HEAD"), |
| 37 | + remap = false, |
| 38 | + require = 1) |
| 39 | + private void beginRIB(Block block, int metadata, int modelId, RenderBlocks renderer, CallbackInfo ci) { |
| 40 | + ft$lock(); |
| 41 | + } |
| 42 | + |
| 43 | + @Inject(method = "renderWorldBlock", |
| 44 | + at = @At("HEAD"), |
| 45 | + remap = false, |
| 46 | + require = 1) |
| 47 | + private void beginRWB(IBlockAccess world, int x, int y, int z, Block block, int modelId, RenderBlocks renderer, CallbackInfoReturnable<Boolean> cir) { |
| 48 | + ft$lock(); |
| 49 | + } |
| 50 | + |
| 51 | + @Inject(method = "renderItem", |
| 52 | + at = @At("HEAD"), |
| 53 | + remap = false, |
| 54 | + require = 1) |
| 55 | + private void beginRI(IItemRenderer.ItemRenderType type, ItemStack item, Object[] data, CallbackInfo ci) { |
| 56 | + ft$lock(); |
| 57 | + } |
| 58 | + |
| 59 | + @Inject(method = "renderTileEntityAt", |
| 60 | + at = @At("HEAD"), |
| 61 | + require = 1) |
| 62 | + private void beginRTEA(TileEntity te, double x, double y, double z, float partialTick, CallbackInfo ci) { |
| 63 | + ft$lock(); |
| 64 | + } |
| 65 | + |
| 66 | + @Inject(method = "renderWorldLastEvent", |
| 67 | + at = @At("HEAD"), |
| 68 | + remap = false, |
| 69 | + require = 1) |
| 70 | + private void beginRWLE(RenderWorldLastEvent event, IBlockAccess world, CallbackInfo ci) { |
| 71 | + ft$lock(); |
| 72 | + } |
| 73 | + |
| 74 | + @Inject(method = "renderInventoryBlock", |
| 75 | + at = @At("RETURN"), |
| 76 | + remap = false, |
| 77 | + require = 1) |
| 78 | + private void endRIB(Block block, int metadata, int modelId, RenderBlocks renderer, CallbackInfo ci) { |
| 79 | + ft$unlock(); |
| 80 | + } |
| 81 | + |
| 82 | + @Inject(method = "renderWorldBlock", |
| 83 | + at = @At("RETURN"), |
| 84 | + remap = false, |
| 85 | + require = 1) |
| 86 | + private void endRWB(IBlockAccess world, int x, int y, int z, Block block, int modelId, RenderBlocks renderer, CallbackInfoReturnable<Boolean> cir) { |
| 87 | + ft$unlock(); |
| 88 | + } |
| 89 | + |
| 90 | + @Inject(method = "renderItem", |
| 91 | + at = @At("RETURN"), |
| 92 | + remap = false, |
| 93 | + require = 1) |
| 94 | + private void endRI(IItemRenderer.ItemRenderType type, ItemStack item, Object[] data, CallbackInfo ci) { |
| 95 | + ft$unlock(); |
| 96 | + } |
| 97 | + |
| 98 | + @Inject(method = "renderTileEntityAt", |
| 99 | + at = @At("RETURN"), |
| 100 | + require = 1) |
| 101 | + private void endRTEA(TileEntity te, double x, double y, double z, float partialTick, CallbackInfo ci) { |
| 102 | + ft$unlock(); |
| 103 | + } |
| 104 | + |
| 105 | + @Inject(method = "renderWorldLastEvent", |
| 106 | + at = @At("RETURN"), |
| 107 | + remap = false, |
| 108 | + require = 1) |
| 109 | + private void endRWLE(RenderWorldLastEvent event, IBlockAccess world, CallbackInfo ci) { |
| 110 | + ft$unlock(); |
| 111 | + } |
| 112 | + |
| 113 | + @Unique |
| 114 | + private static void ft$lock() { |
| 115 | + if (!ft$lock.isHeldByCurrentThread()) { |
| 116 | + while (!ft$lock.tryLock()) { |
| 117 | + Thread.yield(); |
| 118 | + } |
| 119 | + } |
| 120 | + ft$lockCounter.incrementAndGet(); |
| 121 | + } |
| 122 | + |
| 123 | + @Unique |
| 124 | + private static void ft$unlock() { |
| 125 | + if (ft$lockCounter.decrementAndGet() == 0) { |
| 126 | + ft$lock.unlock(); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public ISimpleBlockRenderingHandler forCurrentThread() { |
| 132 | + return this; |
| 133 | + } |
| 134 | +} |
0 commit comments