-
Notifications
You must be signed in to change notification settings - Fork 497
/
BlockEngine.java
288 lines (248 loc) · 8.61 KB
/
BlockEngine.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
/**
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.energy;
import java.util.List;
import java.util.Random;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
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.util.MovingObjectPosition;
import net.minecraft.util.Vec3;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.BuildCraftCore;
import buildcraft.api.events.BlockInteractionEvent;
import buildcraft.core.BlockBuildCraft;
import buildcraft.core.ICustomHighlight;
import buildcraft.core.IItemPipe;
public class BlockEngine extends BlockBuildCraft implements ICustomHighlight {
private static final AxisAlignedBB[][] boxes = {
{AxisAlignedBB.getBoundingBox(0.0, 0.5, 0.0, 1.0, 1.0, 1.0), AxisAlignedBB.getBoundingBox(0.25, 0.0, 0.25, 0.75, 0.5, 0.75)}, // -Y
{AxisAlignedBB.getBoundingBox(0.0, 0.0, 0.0, 1.0, 0.5, 1.0), AxisAlignedBB.getBoundingBox(0.25, 0.5, 0.25, 0.75, 1.0, 0.75)}, // +Y
{AxisAlignedBB.getBoundingBox(0.0, 0.0, 0.5, 1.0, 1.0, 1.0), AxisAlignedBB.getBoundingBox(0.25, 0.25, 0.0, 0.75, 0.75, 0.5)}, // -Z
{AxisAlignedBB.getBoundingBox(0.0, 0.0, 0.0, 1.0, 1.0, 0.5), AxisAlignedBB.getBoundingBox(0.25, 0.25, 0.5, 0.75, 0.75, 1.0)}, // +Z
{AxisAlignedBB.getBoundingBox(0.5, 0.0, 0.0, 1.0, 1.0, 1.0), AxisAlignedBB.getBoundingBox(0.0, 0.25, 0.25, 0.5, 0.75, 0.75)}, // -X
{AxisAlignedBB.getBoundingBox(0.0, 0.0, 0.0, 0.5, 1.0, 1.0), AxisAlignedBB.getBoundingBox(0.5, 0.25, 0.25, 1.0, 0.75, 0.75)} // +X
};
private static IIcon woodTexture;
private static IIcon stoneTexture;
private static IIcon ironTexture;
public BlockEngine() {
super(Material.iron);
setBlockName("engineBlock");
}
@Override
public boolean isOpaqueCube() {
return false;
}
@Override
public boolean renderAsNormalBlock() {
return false;
}
@Override
@SideOnly(Side.CLIENT)
public void registerBlockIcons(IIconRegister par1IconRegister) {
woodTexture = par1IconRegister.registerIcon("buildcraft:engineWoodBottom");
stoneTexture = par1IconRegister.registerIcon("buildcraft:engineStoneBottom");
ironTexture = par1IconRegister.registerIcon("buildcraft:engineIronBottom");
}
@Override
public int getRenderType() {
return BuildCraftCore.blockByEntityModel;
}
@Override
public TileEntity createTileEntity(World world, int metadata) {
switch (metadata) {
case 0:
return new TileEngineWood();
case 1:
return new TileEngineStone();
case 2:
return new TileEngineIron();
case 3:
return new TileEngineCreative();
default:
return new TileEngineWood();
}
}
@Override
public boolean isSideSolid(IBlockAccess world, int x, int y, int z, ForgeDirection side) {
TileEntity tile = world.getTileEntity(x, y, z);
if (tile instanceof TileEngine) {
return ((TileEngine) tile).orientation.getOpposite() == side;
} else {
return false;
}
}
@Override
public boolean rotateBlock(World world, int x, int y, int z, ForgeDirection axis) {
TileEntity tile = world.getTileEntity(x, y, z);
if (tile instanceof TileEngine) {
return ((TileEngine) tile).switchOrientation(false);
} else {
return false;
}
}
@Override
public boolean onBlockActivated(World world, int i, int j, int k, EntityPlayer player, int side, float par7, float par8, float par9) {
TileEntity tile = world.getTileEntity(i, j, k);
// REMOVED DUE TO CREATIVE ENGINE REQUIREMENTS - dmillerw
// Drop through if the player is sneaking
// if (player.isSneaking()) {
// return false;
// }
BlockInteractionEvent event = new BlockInteractionEvent(player, this, world.getBlockMetadata(i, j, k));
FMLCommonHandler.instance().bus().post(event);
if (event.isCanceled()) {
return false;
}
// Do not open guis when having a pipe in hand
if (player.getCurrentEquippedItem() != null) {
if (player.getCurrentEquippedItem().getItem() instanceof IItemPipe) {
return false;
}
}
if (tile instanceof TileEngine) {
return ((TileEngine) tile).onBlockActivated(player, ForgeDirection.getOrientation(side));
}
return false;
}
@Override
@SuppressWarnings("unchecked")
public void addCollisionBoxesToList(World wrd, int x, int y, int z, AxisAlignedBB mask, List list, Entity ent) {
TileEntity tile = wrd.getTileEntity(x, y, z);
if (tile instanceof TileEngine) {
AxisAlignedBB[] aabbs = boxes[((TileEngine) tile).orientation.ordinal()];
for (AxisAlignedBB aabb : aabbs) {
AxisAlignedBB aabbTmp = aabb.getOffsetBoundingBox(x, y, z);
if (mask.intersectsWith(aabbTmp)) {
list.add(aabbTmp);
}
}
} else {
super.addCollisionBoxesToList(wrd, x, y, z, mask, list, ent);
}
}
@Override
public AxisAlignedBB[] getBoxes(World wrd, int x, int y, int z, EntityPlayer player) {
TileEntity tile = wrd.getTileEntity(x, y, z);
if (tile instanceof TileEngine) {
return boxes[((TileEngine) tile).orientation.ordinal()];
} else {
return new AxisAlignedBB[]{AxisAlignedBB.getBoundingBox(0.0, 0.0, 0.0, 1.0, 1.0, 1.0)};
}
}
@Override
public double getExpansion() {
return 0.0075;
}
@Override
public MovingObjectPosition collisionRayTrace(World wrd, int x, int y, int z, Vec3 origin, Vec3 direction) {
TileEntity tile = wrd.getTileEntity(x, y, z);
if (tile instanceof TileEngine) {
AxisAlignedBB[] aabbs = boxes[((TileEngine) tile).orientation.ordinal()];
MovingObjectPosition closest = null;
for (AxisAlignedBB aabb : aabbs) {
MovingObjectPosition mop = aabb.getOffsetBoundingBox(x, y, z).calculateIntercept(origin, direction);
if (mop != null) {
if (closest != null && mop.hitVec.distanceTo(origin) < closest.hitVec.distanceTo(origin)) {
closest = mop;
} else {
closest = mop;
}
}
}
if (closest != null) {
closest.blockX = x;
closest.blockY = y;
closest.blockZ = z;
}
return closest;
} else {
return super.collisionRayTrace(wrd, x, y, z, origin, direction);
}
}
@Override
public void onPostBlockPlaced(World world, int x, int y, int z, int par5) {
TileEntity tile = world.getTileEntity(x, y, z);
if (tile instanceof TileEngine) {
TileEngine engine = (TileEngine) tile;
engine.orientation = ForgeDirection.UP;
if (!engine.isOrientationValid()) {
engine.switchOrientation(true);
}
}
}
@Override
public int damageDropped(int i) {
return i;
}
@SuppressWarnings({"all"})
@Override
public void randomDisplayTick(World world, int i, int j, int k, Random random) {
TileEntity tile = world.getTileEntity(i, j, k);
if (tile instanceof TileEngine && !((TileEngine) tile).isBurning()) {
return;
}
float f = i + 0.5F;
float f1 = j + 0.0F + (random.nextFloat() * 6F) / 16F;
float f2 = k + 0.5F;
float f3 = 0.52F;
float f4 = random.nextFloat() * 0.6F - 0.3F;
world.spawnParticle("reddust", f - f3, f1, f2 + f4, 0.0D, 0.0D, 0.0D);
world.spawnParticle("reddust", f + f3, f1, f2 + f4, 0.0D, 0.0D, 0.0D);
world.spawnParticle("reddust", f + f4, f1, f2 - f3, 0.0D, 0.0D, 0.0D);
world.spawnParticle("reddust", f + f4, f1, f2 + f3, 0.0D, 0.0D, 0.0D);
}
@SuppressWarnings({"unchecked", "rawtypes"})
@Override
public void getSubBlocks(Item item, CreativeTabs par2CreativeTabs, List itemList) {
itemList.add(new ItemStack(this, 1, 0)); // WOOD
itemList.add(new ItemStack(this, 1, 1)); // STONE
itemList.add(new ItemStack(this, 1, 2)); // IRON
itemList.add(new ItemStack(this, 1, 3)); // CREATIVE
}
@Override
public void onNeighborBlockChange(World world, int x, int y, int z, Block block) {
TileEntity tile = world.getTileEntity(x, y, z);
if (tile instanceof TileEngine) {
((TileEngine) tile).checkRedstonePower();
}
}
@Override
@SideOnly(Side.CLIENT)
public IIcon getIcon(int side, int meta) {
switch (meta) {
case 0:
return woodTexture;
case 1:
return stoneTexture;
case 2:
return ironTexture;
default:
return null;
}
}
@Override
public TileEntity createNewTileEntity(World world, int metadata) {
return null;
}
}