aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gtPlusPlus/core/block/base/BlockBaseModular.java
blob: b70e96108c6622305a68c2ac3af0cd554172ce66 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package gtPlusPlus.core.block.base;

import static gregtech.api.enums.Mods.GTPlusPlus;
import static gregtech.api.enums.Mods.GregTech;

import java.util.HashMap;
import java.util.Map;

import net.minecraft.block.Block;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.item.ItemStack;
import net.minecraft.world.IBlockAccess;

import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import gregtech.api.enums.OrePrefixes;
import gregtech.api.enums.TextureSet;
import gregtech.api.util.GTLanguageManager;
import gregtech.api.util.GTOreDictUnificator;
import gtPlusPlus.api.objects.Logger;
import gtPlusPlus.core.config.Configuration;
import gtPlusPlus.core.item.base.itemblock.ItemBlockGtBlock;
import gtPlusPlus.core.material.Material;
import gtPlusPlus.core.util.Utils;
import gtPlusPlus.core.util.math.MathUtils;
import gtPlusPlus.core.util.minecraft.ItemUtils;

public class BlockBaseModular extends BasicBlock {

    protected Material blockMaterial;

    protected int blockColour;
    public BlockTypes thisBlock;
    protected String thisBlockMaterial;
    protected String thisBlockMaterialTranslatedName;
    protected final String thisBlockType;

    private static final HashMap<String, Block> sBlockCache = new HashMap<>();

    public static Block getMaterialBlock(Material aMaterial, BlockTypes aType) {
        return sBlockCache.get(aMaterial.getUnlocalizedName() + "." + aType.name());
    }

    public BlockBaseModular(final Material material, final BlockTypes blockType) {
        this(material, blockType, material.getRgbAsHex());
    }

    public BlockBaseModular(final Material material, final BlockTypes blockType, final int colour) {
        this(
            material.getUnlocalizedName(),
            material.getLocalizedName(),
            net.minecraft.block.material.Material.iron,
            blockType,
            colour,
            Math.min(Math.max(material.vTier, 1), 6));
        blockMaterial = material;
        registerComponent();
        sBlockCache.put(material.getUnlocalizedName() + "." + blockType.name(), this);
        thisBlockMaterialTranslatedName = material.getTranslatedName();
        GTLanguageManager.addStringLocalization("gtplusplus." + getUnlocalizedName() + ".name", getProperName());
    }

    protected BlockBaseModular(final String unlocalizedName, final String blockMaterialString,
        final net.minecraft.block.material.Material vanillaMaterial, final BlockTypes blockType, final int colour,
        final int miningLevel) {
        super(blockType, unlocalizedName, vanillaMaterial, miningLevel);
        this.setHarvestLevel(blockType.getHarvestTool(), miningLevel);
        this.setBlockTextureName(GTPlusPlus.ID + ":" + blockType.getTexture());
        this.blockColour = colour;
        this.thisBlock = blockType;
        this.thisBlockMaterial = blockMaterialString;
        this.thisBlockType = blockType.name()
            .toUpperCase();
        this.setBlockName(this.getUnlocalizedProperName());
        int fx = getBlockTypeMeta();
        GameRegistry.registerBlock(
            this,
            ItemBlockGtBlock.class,
            Utils.sanitizeString(blockType.getTexture() + unlocalizedName));
        if (fx == 0) {
            GTOreDictUnificator
                .registerOre("block" + unifyMaterialName(thisBlockMaterial), ItemUtils.getSimpleStack(this));
        } else if (fx == 1) {
            GTOreDictUnificator
                .registerOre("frameGt" + unifyMaterialName(thisBlockMaterial), ItemUtils.getSimpleStack(this));
        } else if (fx == 2) {
            GTOreDictUnificator
                .registerOre("frameGt" + unifyMaterialName(thisBlockMaterial), ItemUtils.getSimpleStack(this));
        }
    }

    public static String unifyMaterialName(String rawMaterName) {
        return rawMaterName.replace(" ", "")
            .replace("-", "")
            .replace("_", "");
    }

    public void registerComponent() {
        Logger.MATERIALS("Attempting to register " + this.getUnlocalizedName() + ".");
        if (this.blockMaterial == null) {
            Logger.MATERIALS("Tried to register " + this.getUnlocalizedName() + " but the material was null.");
            return;
        }
        String aName = blockMaterial.getUnlocalizedName();
        // Register Component
        Map<String, ItemStack> aMap = Material.mComponentMap.get(aName);
        if (aMap == null) {
            aMap = new HashMap<>();
        }
        int fx = getBlockTypeMeta();
        String aKey = (fx == 0 ? OrePrefixes.block.name()
            : (fx == 1 ? OrePrefixes.frameGt.name() : OrePrefixes.ore.name()));
        ItemStack x = aMap.get(aKey);
        if (x == null) {
            aMap.put(aKey, ItemUtils.getSimpleStack(this));
            Logger.MATERIALS("Registering a material component. Item: [" + aName + "] Map: [" + aKey + "]");
            Material.mComponentMap.put(aName, aMap);
        } else {
            // Bad
            Logger.MATERIALS("Tried to double register a material component.");
        }
    }

    public int getBlockTypeMeta() {
        if (this.thisBlockType.equals(
            BlockTypes.STANDARD.name()
                .toUpperCase())) {
            return 0;
        } else if (this.thisBlockType.equals(
            BlockTypes.FRAME.name()
                .toUpperCase())) {
                    return 1;
                } else
            if (this.thisBlockType.equals(
                BlockTypes.ORE.name()
                    .toUpperCase())) {
                        return 2;
                    }
        return 0;
    }

    /**
     * Returns which pass should this block be rendered on. 0 for solids and 1 for alpha
     */
    @Override
    @SideOnly(Side.CLIENT)
    public int getRenderBlockPass() {
        if (this.thisBlock == BlockTypes.FRAME) {
            return 1;
        }
        return 0;
    }

    public String getProperName() {
        String tempIngot = null;
        if (this.thisBlock == BlockTypes.STANDARD) {
            tempIngot = "Block of %material";
        } else if (this.thisBlock == BlockTypes.FRAME) {
            tempIngot = "%material Frame Box";
        } else if (this.thisBlock == BlockTypes.ORE) {
            tempIngot = "%material Ore [Old]";
        }
        return tempIngot;
    }

    public String getUnlocalizedProperName() {
        return getProperName().replace("%s", "%temp")
            .replace("%material", this.thisBlockMaterial)
            .replace("%temp", "%s");
    }

    @Override
    public String getLocalizedName() {
        return GTLanguageManager.getTranslation("gtplusplus." + getUnlocalizedName() + ".name")
            .replace("%s", "%temp")
            .replace("%material", this.thisBlockMaterialTranslatedName)
            .replace("%temp", "%s");
    }

    @Override
    public String getUnlocalizedName() {
        return "block." + blockMaterial.getUnlocalizedName()
            + "."
            + this.thisBlock.name()
                .toLowerCase();
    }

    @Override
    public boolean isOpaqueCube() {
        return false;
    }

    public Material getMaterialEx() {
        return this.blockMaterial;
    }

    @Override
    @SideOnly(Side.CLIENT)
    public void registerBlockIcons(final IIconRegister iIcon) {
        if (!Configuration.visual.useGregtechTextures || this.blockMaterial == null
            || this.thisBlock == BlockTypes.ORE) {
            this.blockIcon = iIcon.registerIcon(GTPlusPlus.ID + ":" + this.thisBlock.getTexture());
        }
        String metType = "9j4852jyo3rjmh3owlhw9oe";
        if (this.blockMaterial != null) {
            TextureSet u = this.blockMaterial.getTextureSet();
            if (u != null) {
                metType = u.mSetName;
            }
        }
        metType = (metType.equals("9j4852jyo3rjmh3owlhw9oe") ? "METALLIC" : metType);
        int tier = blockMaterial != null ? this.blockMaterial.vTier : 0;
        String aType = (this.thisBlock == BlockTypes.FRAME) ? "frameGt" : (tier <= 4 ? "block1" : "block5");
        this.blockIcon = iIcon.registerIcon(GregTech.ID + ":" + "materialicons/" + metType + "/" + aType);
    }

    @Override
    public int colorMultiplier(final IBlockAccess par1IBlockAccess, final int par2, final int par3, final int par4) {

        if (this.blockColour == 0) {
            return MathUtils.generateSingularRandomHexValue();
        }

        return this.blockColour;
    }

    @Override
    public int getRenderColor(final int aMeta) {
        if (this.blockColour == 0) {
            return MathUtils.generateSingularRandomHexValue();
        }

        return this.blockColour;
    }

    @Override
    public int getBlockColor() {
        if (this.blockColour == 0) {
            return MathUtils.generateSingularRandomHexValue();
        }

        return this.blockColour;
    }
}