aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/api/multitileentity/MultiTileEntityBlockInternal.java
blob: 852ffa3c8ee79716bf3bc96688bc8905a8e38991 (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
package gregtech.api.multitileentity;

import gregtech.api.GregTech_API;
import gregtech.api.interfaces.ITexture;
import gregtech.api.multitileentity.interfaces.IMultiTileEntity;
import gregtech.api.multitileentity.interfaces.IMultiTileEntity.IMTE_HasMultiBlockMachineRelevantData;
import gregtech.common.render.GT_Renderer_Block;
import gregtech.common.render.IRenderedBlock;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.StatCollector;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;

import static gregtech.GT_Mod.GT_FML_LOGGER;
import static gregtech.api.util.GT_Util.setTileEntity;

public class MultiTileEntityBlockInternal extends Block implements IRenderedBlock {
    public MultiTileEntityRegistry mMultiTileEntityRegistry;

    public MultiTileEntityBlockInternal() {
        super(Material.anvil);
    }

    @Override
    public void registerBlockIcons(IIconRegister aIconRegister) { /* Do Nothing */ }

    @Override
    public int getRenderType() {
        return GT_Renderer_Block.INSTANCE == null ? super.getRenderType() : GT_Renderer_Block.INSTANCE.mRenderID;
    }
    @Override public final String getUnlocalizedName() {return mMultiTileEntityRegistry.mNameInternal;}
    @Override public final String getLocalizedName() {return StatCollector.translateToLocal(mMultiTileEntityRegistry.mNameInternal + ".name");}

    public boolean placeBlock(World aWorld, int aX, int aY, int aZ, byte aSide, short aMetaData, NBTTagCompound aNBT, boolean aCauseBlockUpdates, boolean aForcePlacement) {
        final MultiTileEntityContainer aMTEContainer = mMultiTileEntityRegistry.getNewTileEntityContainer(aWorld, aX, aY, aZ, aMetaData, aNBT);
        if (aMTEContainer == null) return false;

        final Block tReplacedBlock = aWorld.getBlock(aX, aY, aZ);


        // This is some complicated bullshit Greg had to do to make his MTEs work right.
        // Set Block with reverse MetaData first.
        aWorld.setBlock(aX, aY, aZ, aMTEContainer.mBlock, 15-aMTEContainer.mBlockMetaData, 2);
        // Make sure the Block has been set, yes I know setBlock has a true/false return value, but guess what, it is not reliable in 0.0001% of cases! -Greg
        if (aWorld.getBlock(aX, aY, aZ) != aMTEContainer.mBlock) {aWorld.setBlock(aX, aY, aZ, Blocks.air, 0, 0); return false;}
        // TileEntity should not refresh yet! -Greg
        ((IMultiTileEntity)aMTEContainer.mTileEntity).setShouldRefresh(false);
        // Fake-Set the TileEntity first, bypassing a lot of checks. -Greg
        setTileEntity(aWorld, aX, aY, aZ, (TileEntity) aMTEContainer.mTileEntity, false);
        // Now set the Block with the REAL MetaData. -Greg
        setTileEntity(aWorld, aX, aY, aZ, aMTEContainer.mBlock, aMTEContainer.mBlockMetaData, 0, false);
        // When the TileEntity is set now it SHOULD refresh! -Greg
        ((IMultiTileEntity)aMTEContainer.mTileEntity).setShouldRefresh(true);
        // But make sure again that the Block we have set was actually set properly, because 0.0001%! -Greg
        if (aWorld.getBlock(aX, aY, aZ) != aMTEContainer.mBlock) {aWorld.setBlock(aX, aY, aZ, Blocks.air, 0, 0); return false;}
        // And finally properly set the TileEntity for real! -Greg
        setTileEntity(aWorld, aX, aY, aZ, (TileEntity) aMTEContainer.mTileEntity, aCauseBlockUpdates);
        // Yep, all this just to set one Block and its TileEntity properly... -Greg


        try {
            if (aMTEContainer.mTileEntity instanceof IMTE_HasMultiBlockMachineRelevantData) {
                if (((IMTE_HasMultiBlockMachineRelevantData)aMTEContainer.mTileEntity).hasMultiBlockMachineRelevantData()) GregTech_API.causeMachineUpdate(aWorld, aX, aY, aZ);
            }
        } catch(Throwable e) {
            GT_FML_LOGGER.error("causeMachineUpdate", e);
        }

        try {
            if (!aWorld.isRemote && aCauseBlockUpdates) {
                aWorld.notifyBlockChange(aX, aY, aZ, tReplacedBlock);
                aWorld.func_147453_f(aX, aY, aZ, aMTEContainer.mBlock);
            }
        } catch(Throwable e) {
            GT_FML_LOGGER.error("aCauseBlockUpdates", e);
        }

        try {
            ((IMultiTileEntity)aMTEContainer.mTileEntity).onTileEntityPlaced();
        } catch(Throwable e) {
            GT_FML_LOGGER.error("onTileEntityPlaced", e);
        }


        try {
            aWorld.func_147451_t/*updateAllLightTypes*/(aX, aY, aZ);
        } catch(Throwable e) {
            GT_FML_LOGGER.error("updateAllLightTypes", e);
        }
        return true;
    }

    @Override
    public ITexture[] getTexture(Block aBlock, byte aSide, int aRenderPass, boolean[] aShouldSideBeRendered) {
        return null;
    }

    @Override
    public ITexture[] getTexture(Block aBlock, byte aSide, boolean isActive, int aRenderPass) {
        // TODO: MTE(Texture)
        return null;
    }

    @Override
    public int getRenderPasses(Block aBlock) {
        return 0;
    }

    @Override
    public boolean usesRenderPass(int aRenderPass) {
        return true;
    }

    @Override
    public boolean setBlockBounds(Block aBlock, int aRenderPass) {
        return false;
    }

    @Override
    public IRenderedBlock passRenderingToObject(ItemStack aStack) {
        final TileEntity tTileEntity = mMultiTileEntityRegistry.getNewTileEntity(aStack);
        return tTileEntity instanceof IRenderedBlock ? (IRenderedBlock)tTileEntity : null;
    }

    @Override
    public IRenderedBlock passRenderingToObject(IBlockAccess aWorld, int aX, int aY, int aZ) {
        return null;
    }
}