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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
|
package gregtech.common.blocks;
import static gregtech.api.enums.GTValues.W;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EnumCreatureType;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.IIcon;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import gregtech.api.GregTechAPI;
import gregtech.api.enums.Dyes;
import gregtech.api.enums.Materials;
import gregtech.api.enums.OrePrefixes;
import gregtech.api.interfaces.ITexture;
import gregtech.api.interfaces.tileentity.ICoverable;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.metatileentity.BaseMetaPipeEntity;
import gregtech.api.metatileentity.BaseMetaTileEntity;
import gregtech.api.metatileentity.BaseTileEntity;
import gregtech.api.metatileentity.CoverableTileEntity;
import gregtech.api.metatileentity.implementations.MTEFrame;
import gregtech.api.render.TextureFactory;
import gregtech.api.util.GTLanguageManager;
import gregtech.api.util.GTUtility;
import gregtech.common.render.GTRendererBlock;
public class BlockFrameBox extends BlockContainer {
protected final String mUnlocalizedName;
private static final String DOT_NAME = ".name";
private static final String DOT_TOOLTIP = ".tooltip";
// We need to keep around a temporary TE to preserve this TE after breaking the block, so we can
// properly call getDrops() on it
private static final ThreadLocal<IGregTechTileEntity> mTemporaryTileEntity = new ThreadLocal<>();
public BlockFrameBox() {
super(new MaterialMachines());
this.mUnlocalizedName = "gt.blockframes";
setBlockName(this.mUnlocalizedName);
GTLanguageManager.addStringLocalization(getUnlocalizedName() + "." + W + ".name", "Any Sub Block of this one");
GameRegistry.registerBlock(this, ItemFrames.class, getUnlocalizedName());
for (int meta = 1; meta < GregTechAPI.sGeneratedMaterials.length; meta++) {
Materials material = GregTechAPI.sGeneratedMaterials[meta];
if (material != null && (material.mTypes & 0x02) != 0) {
GTLanguageManager.addStringLocalization(
getUnlocalizedName() + "." + meta + DOT_NAME,
GTLanguageManager.i18nPlaceholder ? getLocalizedNameFormat(material) : getLocalizedName(material));
GTLanguageManager
.addStringLocalization(getUnlocalizedName() + "." + meta + DOT_TOOLTIP, material.getToolTip());
}
}
}
public ItemStack getStackForm(int amount, int meta) {
return new ItemStack(this, amount, meta);
}
public String getLocalizedNameFormat(Materials aMaterial) {
return switch (aMaterial.mName) {
case "InfusedAir", "InfusedDull", "InfusedEarth", "InfusedEntropy", "InfusedFire", "InfusedOrder", "InfusedVis", "InfusedWater" -> "%material Infused Stone";
case "Vermiculite", "Bentonite", "Kaolinite", "Talc", "BasalticMineralSand", "GraniticMineralSand", "GlauconiteSand", "CassiteriteSand", "GarnetSand", "QuartzSand", "Pitchblende", "FullersEarth" -> "%material";
default -> "%material" + " Frame Box";
};
}
@Override
public String getUnlocalizedName() {
return mUnlocalizedName;
}
public String getLocalizedName(Materials aMaterial) {
return aMaterial.getDefaultLocalizedNameForItem(getLocalizedNameFormat(aMaterial));
}
private boolean isCover(ItemStack item) {
return GTUtility.isStackInList(item, GregTechAPI.sCovers.keySet());
}
private void createFrame(World worldIn, int x, int y, int z, BaseMetaPipeEntity baseMte) {
// Obtain metadata to grab proper material identifier
int meta = worldIn.getBlockMetadata(x, y, z);
Materials material = getMaterial(meta);
MTEFrame frame = new MTEFrame("GT_Frame_" + material, material);
baseMte.setMetaTileEntity(frame);
frame.setBaseMetaTileEntity(baseMte);
}
private BaseMetaPipeEntity spawnFrameEntity(World worldIn, int x, int y, int z) {
// Spawn a TE frame box at this location, then apply the cover
BaseMetaPipeEntity newTileEntity = new BaseMetaPipeEntity();
createFrame(worldIn, x, y, z, newTileEntity);
worldIn.setTileEntity(x, y, z, newTileEntity);
return newTileEntity;
}
// Get the material that this frame box is made of
public static Materials getMaterial(int meta) {
return GregTechAPI.sGeneratedMaterials[meta];
}
@Override
public boolean onBlockActivated(World worldIn, int x, int y, int z, EntityPlayer player, int side, float subX,
float subY, float subZ) {
// Get ForgeDirection from side identifier.
ForgeDirection direction = ForgeDirection.getOrientation(side);
// If this block already holds a TE, just forward the call
TileEntity te = worldIn.getTileEntity(x, y, z);
if (te instanceof BaseMetaPipeEntity baseTileEntity) {
// If this baseTileEntity has no MetaTileEntity associated with it, we need to create it
// This happens on world load for some reason
if (baseTileEntity.getMetaTileEntity() == null) {
createFrame(worldIn, x, y, z, baseTileEntity);
}
return baseTileEntity.onRightclick(player, direction, subX, subY, subZ);
}
// If there was no TileEntity yet, we need to check if the player was holding a cover item and if so
// spawn a new frame box to apply the cover to
ItemStack item = player.getHeldItem();
if (isCover(item)) {
BaseMetaPipeEntity newTileEntity = spawnFrameEntity(worldIn, x, y, z);
return newTileEntity.onRightclick(player, direction, subX, subY, subZ);
}
return false;
}
@Override
public int getRenderType() {
if (GTRendererBlock.INSTANCE == null) {
return super.getRenderType();
}
return GTRendererBlock.mRenderID;
}
@Override
public boolean isOpaqueCube() {
return false;
}
@Override
public int getRenderBlockPass() {
return 1;
}
@Override
@SideOnly(Side.CLIENT)
public void getSubBlocks(Item aItem, CreativeTabs aTab, List<ItemStack> aList) {
for (int i = 0; i < GregTechAPI.sGeneratedMaterials.length; i++) {
Materials tMaterial = GregTechAPI.sGeneratedMaterials[i];
// If material is not null and has a frame box item associated with it
if ((tMaterial != null) && ((tMaterial.mTypes & 0x02) != 0)) {
aList.add(new ItemStack(aItem, 1, i));
}
}
}
@Override
public String getHarvestTool(int aMeta) {
return "wrench";
}
@Override
public int getHarvestLevel(int aMeta) {
return 3;
}
@Override
public boolean canConnectRedstone(IBlockAccess aWorld, int aX, int aY, int aZ, int ordinalSide) {
ForgeDirection forgeSide = switch (ordinalSide) {
case (-2) -> ForgeDirection.DOWN;
case (-1) -> ForgeDirection.UP;
case (0) -> ForgeDirection.NORTH;
case (2) -> ForgeDirection.SOUTH;
case (3) -> ForgeDirection.WEST;
case (1) -> ForgeDirection.EAST;
default -> ForgeDirection.UNKNOWN;
};
final TileEntity frameEntity = aWorld.getTileEntity(aX, aY, aZ);
return frameEntity instanceof CoverableTileEntity cte && cte.getCoverInfoAtSide(forgeSide)
.getCoverID() != 0;
}
@Override
public int getDamageValue(World aWorld, int aX, int aY, int aZ) {
return aWorld.getBlockMetadata(aX, aY, aZ);
}
@Override
public void breakBlock(World aWorld, int aX, int aY, int aZ, Block aBlock, int aMetadata) {
if (aWorld.isRemote) return;
final TileEntity tTileEntity = aWorld.getTileEntity(aX, aY, aZ);
if (tTileEntity instanceof IGregTechTileEntity gtTE) {
gtTE.onBlockDestroyed();
mTemporaryTileEntity.set(gtTE);
}
// Cause structure update
GregTechAPI.causeMachineUpdate(aWorld, aX, aY, aZ);
super.breakBlock(aWorld, aX, aY, aZ, aBlock, aMetadata);
}
@Override
public void onBlockAdded(World worldIn, int x, int y, int z) {
// Cause structure update
GregTechAPI.causeMachineUpdate(worldIn, x, y, z);
super.onBlockAdded(worldIn, x, y, z);
}
@Override
public void onEntityCollidedWithBlock(World aWorld, int aX, int aY, int aZ, Entity collider) {
final TileEntity tTileEntity = aWorld.getTileEntity(aX, aY, aZ);
if (tTileEntity instanceof IGregTechTileEntity gtTE && gtTE.getMetaTileEntity() != null) {
gtTE.onEntityCollidedWithBlock(aWorld, aX, aY, aZ, collider);
return;
}
super.onEntityCollidedWithBlock(aWorld, aX, aY, aZ, collider);
}
@Override
public float getBlockHardness(World aWorld, int aX, int aY, int aZ) {
return Blocks.iron_block.getBlockHardness(aWorld, aX, aY, aZ);
}
@Override
public float getPlayerRelativeBlockHardness(EntityPlayer aPlayer, World aWorld, int aX, int aY, int aZ) {
final TileEntity tTileEntity = aWorld.getTileEntity(aX, aY, aZ);
return (tTileEntity instanceof BaseMetaTileEntity baseMTE) && baseMTE.privateAccess()
&& !baseMTE.playerOwnsThis(aPlayer, true) ? -1.0F
: super.getPlayerRelativeBlockHardness(aPlayer, aWorld, aX, aY, aZ);
}
@Override
public void onNeighborChange(IBlockAccess aWorld, int aX, int aY, int aZ, int aTileX, int aTileY, int aTileZ) {
final TileEntity tTileEntity = aWorld.getTileEntity(aX, aY, aZ);
if (tTileEntity instanceof BaseTileEntity baseTE) {
baseTE.onAdjacentBlockChange(aTileX, aTileY, aTileZ);
}
}
@Override
public void onNeighborBlockChange(World aWorld, int aX, int aY, int aZ, Block aBlock) {
final TileEntity tTileEntity = aWorld.getTileEntity(aX, aY, aZ);
if (tTileEntity instanceof BaseMetaPipeEntity baseMetaPipe) {
baseMetaPipe.onNeighborBlockChange(aX, aY, aZ);
}
}
@Override
public void addCollisionBoxesToList(World aWorld, int aX, int aY, int aZ, AxisAlignedBB inputAABB,
List<AxisAlignedBB> outputAABB, Entity collider) {
final TileEntity tTileEntity = aWorld.getTileEntity(aX, aY, aZ);
if (tTileEntity instanceof IGregTechTileEntity gtTE && gtTE.getMetaTileEntity() != null) {
gtTE.addCollisionBoxesToList(aWorld, aX, aY, aZ, inputAABB, outputAABB, collider);
return;
}
super.addCollisionBoxesToList(aWorld, aX, aY, aZ, inputAABB, outputAABB, collider);
}
@Override
public AxisAlignedBB getCollisionBoundingBoxFromPool(World aWorld, int aX, int aY, int aZ) {
final TileEntity tTileEntity = aWorld.getTileEntity(aX, aY, aZ);
if (tTileEntity instanceof IGregTechTileEntity gtTE && gtTE.getMetaTileEntity() != null) {
return gtTE.getCollisionBoundingBoxFromPool(aWorld, aX, aY, aZ);
}
return super.getCollisionBoundingBoxFromPool(aWorld, aX, aY, aZ);
}
@Override
@SideOnly(Side.CLIENT)
public AxisAlignedBB getSelectedBoundingBoxFromPool(World aWorld, int aX, int aY, int aZ) {
final TileEntity tTileEntity = aWorld.getTileEntity(aX, aY, aZ);
if (tTileEntity instanceof IGregTechTileEntity gtTE && gtTE.getMetaTileEntity() != null) {
return gtTE.getCollisionBoundingBoxFromPool(aWorld, aX, aY, aZ);
}
return super.getSelectedBoundingBoxFromPool(aWorld, aX, aY, aZ);
}
@Override
public boolean isSideSolid(IBlockAccess aWorld, int aX, int aY, int aZ, ForgeDirection side) {
if (aWorld.getBlockMetadata(aX, aY, aZ) == 0) {
return true;
}
final TileEntity tTileEntity = aWorld.getTileEntity(aX, aY, aZ);
if (tTileEntity instanceof BaseMetaTileEntity) {
return true;
}
if (tTileEntity instanceof BaseMetaPipeEntity baseMetaPipe && (baseMetaPipe.mConnections & 0xFFFFFFC0) != 0) {
return true;
}
return (tTileEntity instanceof ICoverable coverable) && coverable.getCoverIDAtSide(side) != 0;
}
@Override // THIS
public void setBlockBoundsBasedOnState(IBlockAccess blockAccess, int aX, int aY, int aZ) {
final TileEntity tTileEntity = blockAccess.getTileEntity(aX, aY, aZ);
if (tTileEntity instanceof IGregTechTileEntity gtTE && gtTE.getMetaTileEntity() != null) {
final AxisAlignedBB bbb = gtTE.getCollisionBoundingBoxFromPool(gtTE.getWorld(), 0, 0, 0);
minX = bbb.minX; // This essentially sets block bounds
minY = bbb.minY;
minZ = bbb.minZ;
maxX = bbb.maxX;
maxY = bbb.maxY;
maxZ = bbb.maxZ;
return;
}
super.setBlockBoundsBasedOnState(blockAccess, aX, aY, aZ);
}
@Override
public void setBlockBoundsForItemRender() {
super.setBlockBounds(0, 0, 0, 1, 1, 1);
}
@Override
public int getFlammability(IBlockAccess aWorld, int aX, int aY, int aZ, ForgeDirection face) {
return 0;
}
@Override
public int getFireSpreadSpeed(IBlockAccess aWorld, int aX, int aY, int aZ, ForgeDirection face) {
return 0;
}
@Override
public boolean isFireSource(World aWorld, int aX, int aY, int aZ, ForgeDirection side) {
return false;
}
@Override
public boolean isFlammable(IBlockAccess aWorld, int aX, int aY, int aZ, ForgeDirection face) {
return false;
}
@Override
public boolean canCreatureSpawn(EnumCreatureType type, IBlockAccess aWorld, int aX, int aY, int aZ) {
return false;
}
@Override
public boolean canBeReplacedByLeaves(IBlockAccess aWorld, int aX, int aY, int aZ) {
return false;
}
@Override
public boolean isNormalCube(IBlockAccess aWorld, int aX, int aY, int aZ) {
return false;
}
@Override
public boolean hasTileEntity(int aMeta) {
return true;
}
@Override
public boolean onBlockEventReceived(World aWorld, int aX, int aY, int aZ, int aData1, int aData2) {
super.onBlockEventReceived(aWorld, aX, aY, aZ, aData1, aData2);
final TileEntity tTileEntity = aWorld.getTileEntity(aX, aY, aZ);
return tTileEntity != null && tTileEntity.receiveClientEvent(aData1, aData2);
}
@Override
public ArrayList<ItemStack> getDrops(World world, int x, int y, int z, int metadata, int fortune) {
// Find temporary TE if there was one
final IGregTechTileEntity tempTe = mTemporaryTileEntity.get();
ArrayList<ItemStack> drops = new ArrayList<>();
drops.add(getStackForm(1, metadata));
// If there is one, grab all attached covers and drop them
if (tempTe != null) {
for (ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS) {
ItemStack cover = tempTe.getCoverItemAtSide(direction);
if (cover != null) drops.add(cover);
}
}
// Make sure to clear the temporary TE
mTemporaryTileEntity.remove();
return drops;
}
@Override
public boolean canProvidePower() {
return true;
}
@Override
public int isProvidingWeakPower(IBlockAccess aWorld, int aX, int aY, int aZ, int ordinalSide) {
if (ordinalSide < 0 || ordinalSide > 5) {
return 0;
}
final TileEntity tTileEntity = aWorld.getTileEntity(aX, aY, aZ);
if (tTileEntity instanceof IGregTechTileEntity gtTE) {
return gtTE.getOutputRedstoneSignal(
ForgeDirection.getOrientation(ordinalSide)
.getOpposite());
}
return 0;
}
@Override
public int isProvidingStrongPower(IBlockAccess aWorld, int aX, int aY, int aZ, int ordinalSide) {
if (ordinalSide < 0 || ordinalSide > 5) {
return 0;
}
final TileEntity tTileEntity = aWorld.getTileEntity(aX, aY, aZ);
if (tTileEntity instanceof IGregTechTileEntity gtTE) {
return gtTE.getStrongOutputRedstoneSignal(
ForgeDirection.getOrientation(ordinalSide)
.getOpposite());
}
return 0;
}
@Override
public IIcon getIcon(int side, int meta) {
Materials material = GregTechAPI.sGeneratedMaterials[meta];
if (material == null) return null;
return material.mIconSet.mTextures[OrePrefixes.frameGt.mTextureIndex].getIcon();
}
public ITexture[] getTexture(int meta) {
Materials material = getMaterial(meta);
if (material == null) return null;
return new ITexture[] { TextureFactory.of(
material.mIconSet.mTextures[OrePrefixes.frameGt.mTextureIndex],
Dyes.getModulation(-1, material.mRGBa)) };
}
@Override
public TileEntity createNewTileEntity(World worldIn, int meta) {
return null;
}
}
|