aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/common/render/GT_MultiTile_Renderer.java
blob: 4d55979658c29557935765beeabe97b291fdf5b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package gregtech.common.render;

import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.RenderBlocks;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.IBlockAccess;
import net.minecraftforge.common.util.ForgeDirection;

import org.lwjgl.opengl.GL11;

import cpw.mods.fml.client.registry.ISimpleBlockRenderingHandler;
import cpw.mods.fml.client.registry.RenderingRegistry;
import gregtech.GT_Mod;
import gregtech.api.interfaces.ITexture;
import gregtech.api.logic.ModelRenderLogic;
import gregtech.api.logic.interfaces.ModelRenderLogicHost;
import gregtech.api.multitileentity.MultiTileEntityBlockInternal;
import gregtech.api.multitileentity.MultiTileEntityClassContainer;
import gregtech.api.multitileentity.MultiTileEntityRegistry;
import gregtech.api.multitileentity.interfaces.IMultiBlockController;
import gregtech.api.multitileentity.multiblock.base.MultiBlockPart;

public class GT_MultiTile_Renderer implements ISimpleBlockRenderingHandler {

    private final int renderID;
    public static GT_MultiTile_Renderer INSTANCE;

    public GT_MultiTile_Renderer() {
        this.renderID = RenderingRegistry.getNextAvailableRenderId();
        INSTANCE = this;
        RenderingRegistry.registerBlockHandler(this);
    }

    @Override
    public void renderInventoryBlock(Block block, int metadata, int modelId, RenderBlocks renderer) {
        if (!(block instanceof MultiTileEntityBlockInternal)) {
            return;
        }

        GL11.glRotatef(90.0F, 0.0F, 1.0F, 0.0F);
        GL11.glTranslatef(-0.5F, -0.5F, -0.5F);

        MultiTileEntityBlockInternal mteBlock = (MultiTileEntityBlockInternal) block;
        MultiTileEntityRegistry registry = mteBlock.getRegistry();
        if (registry == null) return;
        MultiTileEntityClassContainer classContainer = registry.getClassContainer(metadata);
        if (classContainer == null) return;
        renderer.setRenderBoundsFromBlock(mteBlock);

        for (ForgeDirection side : ForgeDirection.VALID_DIRECTIONS) {
            ITexture texture = classContainer.mCanonicalTileEntity.getTexture(side);
            if (texture == null) continue;
            switch (side) {
                case DOWN -> renderYNegative(null, renderer, 0, 0, 0, block, texture, side);
                case UP -> renderYPositive(null, renderer, 0, 0, 0, block, texture, side);
                case WEST -> renderXNegative(null, renderer, 0, 0, 0, block, texture, side);
                case EAST -> renderXPositive(null, renderer, 0, 0, 0, block, texture, side);
                case NORTH -> renderZNegative(null, renderer, 0, 0, 0, block, texture, side);
                case SOUTH -> renderZPositive(null, renderer, 0, 0, 0, block, texture, side);
                default -> {
                    // Do nothing
                }
            }
        }

        GL11.glTranslatef(0.5F, 0.5F, 0.5F);
    }

    @Override
    public boolean renderWorldBlock(IBlockAccess world, int x, int y, int z, Block block, int modelId,
        RenderBlocks renderer) {
        TileEntity entity = world.getTileEntity(x, y, z);
        if (entity == null) {
            return false;
        }

        renderer.enableAO = Minecraft.isAmbientOcclusionEnabled() && GT_Mod.gregtechproxy.mRenderTileAmbientOcclusion;
        renderer.useInventoryTint = false;

        if (entity instanceof ModelRenderLogicHost modelEntity && modelEntity.shouldRenderModel()) {
            ModelRenderLogic renderLogic = modelEntity.getRenderLogic();
            return true;
        }

        if (!(entity instanceof MultiTileBasicRender)) {
            return false;
        }

        if (entity instanceof MultiBlockPart) {
            IMultiBlockController controller = ((MultiBlockPart) entity).getTarget(false);
            if (controller instanceof ModelRenderLogicHost && ((ModelRenderLogicHost) controller).shouldRenderModel()) {
                return false;
            }
        }

        MultiTileBasicRender renderedEntity = (MultiTileBasicRender) entity;

        for (ForgeDirection side : ForgeDirection.VALID_DIRECTIONS) {
            ITexture texture = renderedEntity.getTexture(side);
            if (texture == null) continue;
            switch (side) {
                case DOWN -> renderYNegative(world, renderer, x, y, z, block, texture, side);
                case UP -> renderYPositive(world, renderer, x, y, z, block, texture, side);
                case WEST -> renderXNegative(world, renderer, x, y, z, block, texture, side);
                case EAST -> renderXPositive(world, renderer, x, y, z, block, texture, side);
                case NORTH -> renderZNegative(world, renderer, x, y, z, block, texture, side);
                case SOUTH -> renderZPositive(world, renderer, x, y, z, block, texture, side);
                default -> {
                    // Do nothing
                }
            }
        }
        return true;
    }

    @Override
    public boolean shouldRender3DInInventory(int modelId) {
        return true;
    }

    @Override
    public int getRenderId() {
        return renderID;
    }

    private static void renderYNegative(IBlockAccess world, RenderBlocks renderer, int x, int y, int z, Block block,
        ITexture texture, ForgeDirection side) {
        if (world != null) {
            if (!block.shouldSideBeRendered(world, x, y - 1, z, side.ordinal())) return;
            Tessellator.instance.setBrightness(block.getMixedBrightnessForBlock(world, x, y - 1, z));
        }
        texture.renderYNeg(renderer, block, x, y, z);
    }

    private static void renderZNegative(IBlockAccess world, RenderBlocks renderer, int x, int y, int z, Block block,
        ITexture texture, ForgeDirection side) {
        if (world != null) {
            if (!block.shouldSideBeRendered(world, x, y, z - 1, side.ordinal())) return;
            Tessellator.instance.setBrightness(block.getMixedBrightnessForBlock(world, x, y, z - 1));
        }
        texture.renderZNeg(renderer, block, x, y, z);
    }

    private static void renderXNegative(IBlockAccess world, RenderBlocks renderer, int x, int y, int z, Block block,
        ITexture texture, ForgeDirection side) {
        if (world != null) {
            if (!block.shouldSideBeRendered(world, x - 1, y, z, side.ordinal())) return;
            Tessellator.instance.setBrightness(block.getMixedBrightnessForBlock(world, x - 1, y, z));
        }
        texture.renderXNeg(renderer, block, x, y, z);
    }

    private static void renderYPositive(IBlockAccess world, RenderBlocks renderer, int x, int y, int z, Block block,
        ITexture texture, ForgeDirection side) {
        if (world != null) {
            if (!block.shouldSideBeRendered(world, x, y + 1, z, side.ordinal())) return;
            Tessellator.instance.setBrightness(block.getMixedBrightnessForBlock(world, x, y + 1, z));
        }
        texture.renderYPos(renderer, block, x, y, z);
    }

    private static void renderXPositive(IBlockAccess world, RenderBlocks renderer, int x, int y, int z, Block block,
        ITexture texture, ForgeDirection side) {
        if (world != null) {
            if (!block.shouldSideBeRendered(world, x + 1, y, z, side.ordinal())) return;
            Tessellator.instance.setBrightness(block.getMixedBrightnessForBlock(world, x + 1, y, z));
        }
        texture.renderXPos(renderer, block, x, y, z);
    }

    private static void renderZPositive(IBlockAccess world, RenderBlocks renderer, int x, int y, int z, Block block,
        ITexture texture, ForgeDirection side) {
        if (world != null) {
            if (!block.shouldSideBeRendered(world, x, y, z + 1, side.ordinal())) return;
            Tessellator.instance.setBrightness(block.getMixedBrightnessForBlock(world, x, y, z + 1));
        }
        texture.renderZPos(renderer, block, x, y, z);
    }
}