-
Notifications
You must be signed in to change notification settings - Fork 150
/
BlockMachine.java
executable file
·449 lines (392 loc) · 18.8 KB
/
BlockMachine.java
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
package gregtech.api.block.machines;
import codechicken.lib.block.property.unlisted.UnlistedIntegerProperty;
import codechicken.lib.block.property.unlisted.UnlistedStringProperty;
import codechicken.lib.raytracer.CuboidRayTraceResult;
import codechicken.lib.raytracer.IndexedCuboid6;
import codechicken.lib.raytracer.RayTracer;
import codechicken.lib.vec.Cuboid6;
import com.google.common.collect.Lists;
import gregtech.api.GregTechAPI;
import gregtech.api.block.BlockCustomParticle;
import gregtech.api.capability.GregtechCapabilities;
import gregtech.api.capability.tool.IScrewdriverItem;
import gregtech.api.capability.tool.IWrenchItem;
import gregtech.api.cover.CoverBehavior;
import gregtech.api.cover.ICoverable;
import gregtech.api.cover.IFacadeCover;
import gregtech.api.metatileentity.MetaTileEntity;
import gregtech.api.metatileentity.MetaTileEntityHolder;
import gregtech.api.render.MetaTileEntityRenderer;
import gregtech.common.tools.DamageValues;
import gregtech.api.render.IBlockAppearance;
import gregtech.integration.ctm.IFacadeWrapper;
import net.minecraft.block.Block;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.state.BlockFaceShape;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving.SpawnPlacementType;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.EnumDyeColor;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.*;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.Explosion;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.property.ExtendedBlockState;
import net.minecraftforge.common.property.IExtendedBlockState;
import net.minecraftforge.common.property.IUnlistedProperty;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.apache.commons.lang3.tuple.Pair;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@SuppressWarnings("deprecation")
public class BlockMachine extends BlockCustomParticle implements ITileEntityProvider, IFacadeWrapper, IBlockAppearance {
private static final List<IndexedCuboid6> EMPTY_COLLISION_BOX = Lists.newArrayList(new IndexedCuboid6(null, Cuboid6.full));
private static final IUnlistedProperty<String> HARVEST_TOOL = new UnlistedStringProperty("harvest_tool");
private static final IUnlistedProperty<Integer> HARVEST_LEVEL = new UnlistedIntegerProperty("harvest_level");
//used for rendering purposes of non-opaque machines like chests and tanks
public static final PropertyBool OPAQUE = PropertyBool.create("opaque");
public BlockMachine() {
super(Material.IRON);
setCreativeTab(GregTechAPI.TAB_GREGTECH);
setSoundType(SoundType.METAL);
setHardness(6.0f);
setResistance(6.0f);
setTranslationKey("unnamed");
setHarvestLevel("wrench", 1);
setDefaultState(getDefaultState().withProperty(OPAQUE, true));
}
@Nullable
@Override
public String getHarvestTool(IBlockState state) {
return ((IExtendedBlockState) state).getValue(HARVEST_TOOL);
}
@Override
public int getHarvestLevel(IBlockState state) {
Integer value = ((IExtendedBlockState) state).getValue(HARVEST_LEVEL);
return value == null ? 0 : value; //safety check for mods who don't handle state properly
}
@Override
public boolean causesSuffocation(IBlockState state) {
return state.getValue(OPAQUE);
}
@Override
public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos) {
MetaTileEntity metaTileEntity = getMetaTileEntity(worldIn, pos);
if (metaTileEntity == null)
return state;
return ((IExtendedBlockState) state)
.withProperty(HARVEST_TOOL, metaTileEntity.getHarvestTool())
.withProperty(HARVEST_LEVEL, metaTileEntity.getHarvestLevel());
}
@Override
protected BlockStateContainer createBlockState() {
return new ExtendedBlockState(this, new IProperty[]{OPAQUE}, new IUnlistedProperty[]{HARVEST_TOOL, HARVEST_LEVEL});
}
@Override
public IBlockState getStateFromMeta(int meta) {
return getDefaultState().withProperty(OPAQUE, meta % 2 == 0);
}
@Override
public int getMetaFromState(IBlockState state) {
return state.getValue(OPAQUE) ? 0 : 1;
}
@Override
public boolean canCreatureSpawn(IBlockState state, IBlockAccess world, BlockPos pos, SpawnPlacementType type) {
return false;
}
public static MetaTileEntity getMetaTileEntity(IBlockAccess blockAccess, BlockPos pos) {
TileEntity holder = blockAccess.getTileEntity(pos);
return holder instanceof MetaTileEntityHolder ? ((MetaTileEntityHolder) holder).getMetaTileEntity() : null;
}
@Override
public float getBlockHardness(IBlockState blockState, World worldIn, BlockPos pos) {
MetaTileEntity metaTileEntity = getMetaTileEntity(worldIn, pos);
return metaTileEntity == null ? 1.0f : metaTileEntity.getBlockHardness();
}
@Override
public float getExplosionResistance(World world, BlockPos pos, @Nullable Entity exploder, Explosion explosion) {
MetaTileEntity metaTileEntity = getMetaTileEntity(world, pos);
return metaTileEntity == null ? 1.0f : metaTileEntity.getBlockResistance();
}
private List<IndexedCuboid6> getCollisionBox(IBlockAccess blockAccess, BlockPos pos) {
MetaTileEntity metaTileEntity = getMetaTileEntity(blockAccess, pos);
if (metaTileEntity == null)
return EMPTY_COLLISION_BOX;
ArrayList<IndexedCuboid6> collisionList = new ArrayList<>();
metaTileEntity.addCollisionBoundingBox(collisionList);
metaTileEntity.addCoverCollisionBoundingBox(collisionList);
return collisionList;
}
@Override
public ItemStack getPickBlock(IBlockState state, RayTraceResult target, World world, BlockPos pos, EntityPlayer player) {
MetaTileEntity metaTileEntity = getMetaTileEntity(world, pos);
if (metaTileEntity == null)
return ItemStack.EMPTY;
if (target instanceof CuboidRayTraceResult) {
return metaTileEntity.getPickItem((CuboidRayTraceResult) target, player);
}
return ItemStack.EMPTY;
}
@Override
public void addCollisionBoxToList(IBlockState state, World worldIn, BlockPos pos, AxisAlignedBB entityBox, List<AxisAlignedBB> collidingBoxes, @Nullable Entity entityIn, boolean isActualState) {
for (Cuboid6 axisAlignedBB : getCollisionBox(worldIn, pos)) {
AxisAlignedBB offsetBox = axisAlignedBB.aabb().offset(pos);
if (offsetBox.intersects(entityBox)) collidingBoxes.add(offsetBox);
}
}
@Nullable
@Override
public RayTraceResult collisionRayTrace(IBlockState blockState, World worldIn, BlockPos pos, Vec3d start, Vec3d end) {
return RayTracer.rayTraceCuboidsClosest(start, end, pos, getCollisionBox(worldIn, pos));
}
@Override
public boolean rotateBlock(World world, BlockPos pos, EnumFacing axis) {
MetaTileEntity metaTileEntity = getMetaTileEntity(world, pos);
if (metaTileEntity == null ||
!metaTileEntity.isValidFrontFacing(axis) ||
metaTileEntity.getFrontFacing() == axis ||
!metaTileEntity.hasFrontFacing())
return false;
metaTileEntity.setFrontFacing(axis);
return true;
}
@Nullable
@Override
public EnumFacing[] getValidRotations(World world, BlockPos pos) {
MetaTileEntity metaTileEntity = getMetaTileEntity(world, pos);
if (metaTileEntity == null || !metaTileEntity.hasFrontFacing()) return null;
return Arrays.stream(EnumFacing.VALUES)
.filter(metaTileEntity::isValidFrontFacing)
.toArray(EnumFacing[]::new);
}
@Override
public boolean recolorBlock(World world, BlockPos pos, EnumFacing side, EnumDyeColor color) {
MetaTileEntity metaTileEntity = getMetaTileEntity(world, pos);
if (metaTileEntity == null ||
metaTileEntity.getPaintingColor() == color.colorValue)
return false;
metaTileEntity.setPaintingColor(color.colorValue);
return true;
}
@Override
public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {
MetaTileEntityHolder holder = (MetaTileEntityHolder) worldIn.getTileEntity(pos);
MetaTileEntity sampleMetaTileEntity = GregTechAPI.META_TILE_ENTITY_REGISTRY.getObjectById(stack.getItemDamage());
if (holder != null && sampleMetaTileEntity != null) {
MetaTileEntity metaTileEntity = holder.setMetaTileEntity(sampleMetaTileEntity);
if (stack.hasTagCompound()) {
metaTileEntity.initFromItemStackData(stack.getTagCompound());
}
EnumFacing placeFacing = placer.getHorizontalFacing().getOpposite();
if (metaTileEntity.isValidFrontFacing(placeFacing)) {
metaTileEntity.setFrontFacing(placeFacing);
}
}
}
@Override
public void breakBlock(World worldIn, BlockPos pos, IBlockState state) {
MetaTileEntity metaTileEntity = getMetaTileEntity(worldIn, pos);
if (metaTileEntity != null) {
if (!metaTileEntity.keepsInventory()) {
NonNullList<ItemStack> inventoryContents = NonNullList.create();
metaTileEntity.clearMachineInventory(inventoryContents);
for (ItemStack itemStack : inventoryContents) {
Block.spawnAsEntity(worldIn, pos, itemStack);
}
}
metaTileEntity.dropAllCovers();
metaTileEntity.onRemoval();
tileEntities.set(metaTileEntity);
}
super.breakBlock(worldIn, pos, state);
}
@Override
public void getDrops(NonNullList<ItemStack> drops, IBlockAccess world, BlockPos pos, IBlockState state, int fortune) {
MetaTileEntity metaTileEntity = tileEntities.get() == null ? getMetaTileEntity(world, pos) : tileEntities.get();
if (metaTileEntity == null) return;
if (!metaTileEntity.shouldDropWhenDestroyed())
return;
ItemStack itemStack = metaTileEntity.getStackForm();
NBTTagCompound tagCompound = new NBTTagCompound();
metaTileEntity.writeItemStackData(tagCompound);
//only set item tag if it's not empty, so newly created items will stack with dismantled
if (!tagCompound.isEmpty())
itemStack.setTagCompound(tagCompound);
drops.add(itemStack);
metaTileEntity.getDrops(drops, harvesters.get());
}
@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
MetaTileEntity metaTileEntity = getMetaTileEntity(worldIn, pos);
CuboidRayTraceResult rayTraceResult = (CuboidRayTraceResult) RayTracer.retraceBlock(worldIn, playerIn, pos);
ItemStack itemStack = playerIn.getHeldItem(hand);
if (metaTileEntity == null || rayTraceResult == null) {
return false;
}
if (itemStack.hasCapability(GregtechCapabilities.CAPABILITY_SCREWDRIVER, null)) {
IScrewdriverItem screwdriver = itemStack.getCapability(GregtechCapabilities.CAPABILITY_SCREWDRIVER, null);
if (screwdriver.damageItem(DamageValues.DAMAGE_FOR_SCREWDRIVER, true) &&
metaTileEntity.onCoverScrewdriverClick(playerIn, hand, rayTraceResult)) {
screwdriver.damageItem(DamageValues.DAMAGE_FOR_SCREWDRIVER, false);
return true;
}
return false;
}
if (itemStack.hasCapability(GregtechCapabilities.CAPABILITY_WRENCH, null)) {
IWrenchItem wrenchItem = itemStack.getCapability(GregtechCapabilities.CAPABILITY_WRENCH, null);
EnumFacing wrenchDirection = ICoverable.determineGridSideHit(rayTraceResult);
if (wrenchItem.damageItem(DamageValues.DAMAGE_FOR_WRENCH, true) &&
metaTileEntity.onWrenchClick(playerIn, hand, wrenchDirection, rayTraceResult)) {
wrenchItem.damageItem(DamageValues.DAMAGE_FOR_WRENCH, false);
return true;
}
return false;
}
return metaTileEntity.onCoverRightClick(playerIn, hand, rayTraceResult);
}
@Override
public void onBlockClicked(World worldIn, BlockPos pos, EntityPlayer playerIn) {
MetaTileEntity metaTileEntity = getMetaTileEntity(worldIn, pos);
if (metaTileEntity == null) return;
CuboidRayTraceResult rayTraceResult = (CuboidRayTraceResult) RayTracer.retraceBlock(worldIn, playerIn, pos);
if (rayTraceResult != null) {
metaTileEntity.onCoverLeftClick(playerIn, rayTraceResult);
}
}
@Override
public boolean canConnectRedstone(IBlockState state, IBlockAccess world, BlockPos pos, @Nullable EnumFacing side) {
MetaTileEntity metaTileEntity = getMetaTileEntity(world, pos);
return metaTileEntity != null && metaTileEntity.canConnectRedstone(side == null ? null : side.getOpposite());
}
@Override
public boolean shouldCheckWeakPower(IBlockState state, IBlockAccess world, BlockPos pos, EnumFacing side) {
// The check in World::getRedstonePower in the vanilla code base is reversed. Setting this to false will
// actually cause getWeakPower to be called, rather than prevent it.
return false;
}
@Override
public int getWeakPower(IBlockState blockState, IBlockAccess blockAccess, BlockPos pos, EnumFacing side) {
MetaTileEntity metaTileEntity = getMetaTileEntity(blockAccess, pos);
return metaTileEntity == null ? 0 : metaTileEntity.getOutputRedstoneSignal(side == null ? null : side.getOpposite());
}
@Override
public void neighborChanged(IBlockState state, World worldIn, BlockPos pos, Block blockIn, BlockPos fromPos) {
MetaTileEntity metaTileEntity = getMetaTileEntity(worldIn, pos);
if (metaTileEntity != null) {
metaTileEntity.updateInputRedstoneSignals();
metaTileEntity.onNeighborChanged();
}
}
@Override
public int getComparatorInputOverride(IBlockState blockState, World worldIn, BlockPos pos) {
MetaTileEntity metaTileEntity = getMetaTileEntity(worldIn, pos);
return metaTileEntity == null ? 0 : metaTileEntity.getComparatorValue();
}
protected ThreadLocal<MetaTileEntity> tileEntities = new ThreadLocal<>();
@Override
public void harvestBlock(World worldIn, EntityPlayer player, BlockPos pos, IBlockState state, @Nullable TileEntity te, ItemStack stack) {
tileEntities.set(te == null ? tileEntities.get() : ((MetaTileEntityHolder) te).getMetaTileEntity());
super.harvestBlock(worldIn, player, pos, state, te, stack);
tileEntities.set(null);
}
@Override
public boolean hasComparatorInputOverride(IBlockState state) {
return true;
}
@Nullable
@Override
public MetaTileEntityHolder createNewTileEntity(@Nullable World worldIn, int meta) {
return new MetaTileEntityHolder();
}
@Override
@SideOnly(Side.CLIENT)
public EnumBlockRenderType getRenderType(IBlockState state) {
return MetaTileEntityRenderer.BLOCK_RENDER_TYPE;
}
@Override
public boolean canRenderInLayer(IBlockState state, BlockRenderLayer layer) {
return true;
}
@Override
public boolean isOpaqueCube(IBlockState state) {
return state.getValue(OPAQUE);
}
@Override
public boolean isFullCube(IBlockState state) {
return state.getValue(OPAQUE);
}
@Override
public BlockFaceShape getBlockFaceShape(IBlockAccess worldIn, IBlockState state, BlockPos pos, EnumFacing face) {
MetaTileEntity metaTileEntity = getMetaTileEntity(worldIn, pos);
return metaTileEntity == null ? BlockFaceShape.SOLID : metaTileEntity.getCoverFaceShape(face);
}
@Override
public int getLightValue(IBlockState state, IBlockAccess world, BlockPos pos) {
//why mc is so fucking retarded to call this method on fucking NEIGHBOUR BLOCKS!
MetaTileEntity metaTileEntity = getMetaTileEntity(world, pos);
return metaTileEntity == null ? 0 : metaTileEntity.getLightValue();
}
@Override
public int getLightOpacity(IBlockState state, IBlockAccess world, BlockPos pos) {
//why mc is so fucking retarded to call this method on fucking NEIGHBOUR BLOCKS!
MetaTileEntity metaTileEntity = getMetaTileEntity(world, pos);
return metaTileEntity == null ? 0 : metaTileEntity.getLightOpacity();
}
@Override
public void getSubBlocks(CreativeTabs tab, NonNullList<ItemStack> items) {
for (MetaTileEntity metaTileEntity : GregTechAPI.META_TILE_ENTITY_REGISTRY) {
metaTileEntity.getSubItems(tab, items);
}
}
@Nonnull
@Override
public IBlockState getFacade(@Nonnull IBlockAccess world, @Nonnull BlockPos pos, @Nullable EnumFacing side, @Nonnull BlockPos otherPos) {
return getFacade(world, pos, side);
}
@Nonnull
@Override
public IBlockState getFacade(@Nonnull IBlockAccess world, @Nonnull BlockPos pos, EnumFacing side) {
MetaTileEntity metaTileEntity = getMetaTileEntity(world, pos);
if (metaTileEntity != null && side != null) {
CoverBehavior coverBehavior = metaTileEntity.getCoverAtSide(side);
if (coverBehavior instanceof IFacadeCover) {
return ((IFacadeCover) coverBehavior).getVisualState();
}
}
return world.getBlockState(pos);
}
@Nonnull
@Override
public IBlockState getVisualState(@Nonnull IBlockAccess world, @Nonnull BlockPos pos, @Nonnull EnumFacing side) {
return getFacade(world, pos, side);
}
@Override
public boolean supportsVisualConnections() {
return true;
}
@Override
@SideOnly(Side.CLIENT)
protected Pair<TextureAtlasSprite, Integer> getParticleTexture(World world, BlockPos blockPos) {
return MetaTileEntityRenderer.INSTANCE.getParticleTexture(world, blockPos);
}
}