-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecoBlockButton.java
314 lines (276 loc) · 10.4 KB
/
DecoBlockButton.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
package net.minecraft.src;
import java.util.List;
import java.util.Random;
public class DecoBlockButton extends Block
{
private final boolean m_IsWooden;
public DecoBlockButton(int id, boolean isWooden)
{
super(id, Material.circuits);
this.setTickRandomly(true);
this.setCreativeTab(CreativeTabs.tabRedstone);
this.m_IsWooden = isWooden;
}
public int tickRate(World world)
{
return this.m_IsWooden ? 30 : 20;
}
public void updateTick(World world, int x, int y, int z, Random random)
{
if (!world.isRemote)
{
int meta = world.getBlockMetadata(x, y, z);
if ((meta & 8) != 0)
{
if (this.m_IsWooden)
{
this.checkForArrows(world, x, y, z);
}
else
{
world.setBlockMetadataWithNotify(x, y, z, meta & 7, 3);
int orientation = meta & 7;
notifyNeighbor(world, x, y, z, orientation);
world.playSoundEffect((double)x + 0.5D, (double)y + 0.5D, (double)z + 0.5D, "random.click", 0.3F, 0.5F);
world.markBlockRangeForRenderUpdate(x, y, z, x, y, z);
}
}
}
}
public boolean canPlaceBlockAt(World world, int x, int y, int z)
{
if (FCUtilsWorld.DoesBlockHaveLargeCenterHardpointToFacing(world, x, y + 1, z, 0)) return true;
if (FCUtilsWorld.DoesBlockHaveLargeCenterHardpointToFacing(world, x, y - 1, z, 1)) return true;
if (FCUtilsWorld.DoesBlockHaveLargeCenterHardpointToFacing(world, x, y, z + 1, 2)) return true;
if (FCUtilsWorld.DoesBlockHaveLargeCenterHardpointToFacing(world, x, y, z - 1, 3)) return true;
if (FCUtilsWorld.DoesBlockHaveLargeCenterHardpointToFacing(world, x + 1, y, z, 4)) return true;
if (FCUtilsWorld.DoesBlockHaveLargeCenterHardpointToFacing(world, x - 1, y, z, 5)) return true;
return false;
}
public int onBlockPlaced(World world, int x, int y, int z, int side, float hitX, float hitY, float hitZ, int initmeta)
{
int meta = world.getBlockMetadata(x, y, z);
int press = meta & 8;
meta &= 7;
if (side == 0 && FCUtilsWorld.DoesBlockHaveLargeCenterHardpointToFacing(world, x, y + 1, z, 0))
{
meta = 0;
}
else if (side == 1 && FCUtilsWorld.DoesBlockHaveLargeCenterHardpointToFacing(world, x, y - 1, z, 1))
{
meta = 5;
}
else if (side == 2 && FCUtilsWorld.DoesBlockHaveLargeCenterHardpointToFacing(world, x, y, z + 1, 2))
{
meta = 4;
}
else if (side == 3 && FCUtilsWorld.DoesBlockHaveLargeCenterHardpointToFacing(world, x, y, z - 1, 3))
{
meta = 3;
}
else if (side == 4 && FCUtilsWorld.DoesBlockHaveLargeCenterHardpointToFacing(world, x + 1, y, z, 4))
{
meta = 2;
}
else if (side == 5 && FCUtilsWorld.DoesBlockHaveLargeCenterHardpointToFacing(world, x - 1, y, z, 5))
{
meta = 1;
}
else
{
meta = getOrientation(world, x, y, z);
}
return meta + press;
}
private int getOrientation(World world, int x, int y, int z)
{
if (FCUtilsWorld.DoesBlockHaveLargeCenterHardpointToFacing(world, x, y + 1, z, 0)) return 0;
if (FCUtilsWorld.DoesBlockHaveLargeCenterHardpointToFacing(world, x, y - 1, z, 1)) return 5;
if (FCUtilsWorld.DoesBlockHaveLargeCenterHardpointToFacing(world, x, y, z + 1, 2)) return 4;
if (FCUtilsWorld.DoesBlockHaveLargeCenterHardpointToFacing(world, x, y, z - 1, 3)) return 3;
if (FCUtilsWorld.DoesBlockHaveLargeCenterHardpointToFacing(world, x + 1, y, z, 4)) return 2;
return 1;
}
public void onNeighborBlockChange(World world, int x, int y, int z, int id)
{
int orientation = world.getBlockMetadata(x, y, z) & 7;
boolean shouldDrop = false;
if (orientation == 0 && !FCUtilsWorld.DoesBlockHaveLargeCenterHardpointToFacing(world, x, y + 1, z, 0)) shouldDrop = true;
if (orientation == 5 && !FCUtilsWorld.DoesBlockHaveLargeCenterHardpointToFacing(world, x, y - 1, z, 1)) shouldDrop = true;
if (orientation == 4 && !FCUtilsWorld.DoesBlockHaveLargeCenterHardpointToFacing(world, x, y, z + 1, 2)) shouldDrop = true;
if (orientation == 3 && !FCUtilsWorld.DoesBlockHaveLargeCenterHardpointToFacing(world, x, y, z - 1, 3)) shouldDrop = true;
if (orientation == 2 && !FCUtilsWorld.DoesBlockHaveLargeCenterHardpointToFacing(world, x + 1, y, z, 4)) shouldDrop = true;
if (orientation == 1 && !FCUtilsWorld.DoesBlockHaveLargeCenterHardpointToFacing(world, x - 1, y, z, 5)) shouldDrop = true;
if (shouldDrop)
{
dropBlockAsItem(world, x, y, z, world.getBlockMetadata(x, y, z), 0);
world.setBlockToAir(x, y, z);
}
}
public void onBlockClicked(World world, int x, int y, int z, EntityPlayer player) {}
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float xOffset, float yOffset, float zOffset)
{
int meta = world.getBlockMetadata(x, y, z);
int orientation = meta & 7;
int notPressedBit = 8 - (meta & 8);
if (notPressedBit == 0)
{
// already pressed
return true;
}
else
{
world.setBlockMetadataWithNotify(x, y, z, orientation + notPressedBit, 3);
world.markBlockRangeForRenderUpdate(x, y, z, x, y, z);
world.playSoundEffect((double)x + 0.5D, (double)y + 0.5D, (double)z + 0.5D, "random.click", 0.3F, 0.6F);
notifyNeighbor(world, x, y, z, orientation);
world.scheduleBlockUpdate(x, y, z, blockID, tickRate(world));
return true;
}
}
public void breakBlock(World world, int x, int y, int z, int id, int meta)
{
if ((meta & 8) > 0)
{
int orientation = meta & 7;
notifyNeighbor(world, x, y, z, orientation);
}
super.breakBlock(world, x, y, z, id, meta);
}
public int isProvidingWeakPower(IBlockAccess b, int x, int y, int z, int side)
{
return (b.getBlockMetadata(x, y, z) & 8) > 0 ? 15 : 0;
}
public int isProvidingStrongPower(IBlockAccess b, int x, int y, int z, int side)
{
int meta = b.getBlockMetadata(x, y, z);
if ((meta & 8) == 0)
{
return 0;
}
else
{
int orientation = meta & 7;
if (orientation == 5 && side == 1) return 15;
if (orientation == 4 && side == 2) return 15;
if (orientation == 3 && side == 3) return 15;
if (orientation == 2 && side == 4) return 15;
if (orientation == 1 && side == 5) return 15;
if (orientation == 0 && side == 0) return 15;
return 0;
}
}
public boolean canProvidePower()
{
return true;
}
public void onEntityCollidedWithBlock(World world, int x, int y, int z, Entity entity)
{
if (!world.isRemote && this.m_IsWooden && (world.getBlockMetadata(x, y, z) & 8) == 0)
{
this.checkForArrows(world, x, y, z);
}
}
private void checkForArrows(World world, int x, int y, int z)
{
int meta = world.getBlockMetadata(x, y, z);
int orientation = meta & 7;
boolean pressed = (meta & 8) != 0;
setButtonBounds(meta);
List arrows = world.getEntitiesWithinAABB(EntityArrow.class, AxisAlignedBB.getAABBPool().getAABB(
(double)x + minX, (double)y + minY, (double)z + minZ,
(double)x + maxX, (double)y + maxY, (double)z + maxZ)
);
boolean hit = !arrows.isEmpty();
if (hit && !pressed)
{
world.setBlockMetadataWithNotify(x, y, z, orientation | 8, 3);
notifyNeighbor(world, x, y, z, orientation);
world.markBlockRangeForRenderUpdate(x, y, z, x, y, z);
world.playSoundEffect((double)x + 0.5D, (double)y + 0.5D, (double)z + 0.5D, "random.click", 0.3F, 0.6F);
}
if (!hit && pressed)
{
world.setBlockMetadataWithNotify(x, y, z, orientation, 3);
notifyNeighbor(world, x, y, z, orientation);
world.markBlockRangeForRenderUpdate(x, y, z, x, y, z);
world.playSoundEffect((double)x + 0.5D, (double)y + 0.5D, (double)z + 0.5D, "random.click", 0.3F, 0.5F);
}
if (hit)
{
world.scheduleBlockUpdate(x, y, z, blockID, tickRate(world));
}
}
private void notifyNeighbor(World world, int x, int y, int z, int orientation)
{
world.notifyBlocksOfNeighborChange(x, y, z, blockID);
switch (orientation)
{
case 0: world.notifyBlocksOfNeighborChange(x, y + 1, z, blockID); break;
case 1: world.notifyBlocksOfNeighborChange(x - 1, y, z, blockID); break;
case 2: world.notifyBlocksOfNeighborChange(x + 1, y, z, blockID); break;
case 3: world.notifyBlocksOfNeighborChange(x, y, z - 1, blockID); break;
case 4: world.notifyBlocksOfNeighborChange(x, y, z + 1, blockID); break;
default: world.notifyBlocksOfNeighborChange(x, y - 1, z, blockID);
}
}
public boolean isOpaqueCube()
{
return false;
}
public boolean renderAsNormalBlock()
{
return false;
}
/**
* Returns true if the given side of this block type should be rendered, if the adjacent block is at the given
* coordinates. Args: blockAccess, x, y, z, side
*/
public boolean shouldSideBeRendered(IBlockAccess bAccess, int x, int y, int z, int side)
{
return side == 0 && this.minY > 0.0D ? true : (side == 1 && this.maxY < 1.0D ? true : (side == 2 && this.minZ > 0.0D ? true : (side == 3 && this.maxZ < 1.0D ? true : (side == 4 && this.minX > 0.0D ? true : (side == 5 && this.maxX < 1.0D ? true : !bAccess.isBlockOpaqueCube(x, y, z))))));
}
/**
* From the specified side and block metadata retrieves the blocks texture. Args: side, metadata
*/
public Icon getIcon(int side, int metadata)
{
return this.m_IsWooden ? DecoModuleTweaks.decoSubModuleExtendedWoodBlocks.decoBlockWood.getIcon(0, 0) : Block.stone.blockIcon;
}
public void registerIcons(IconRegister register) {}
public AxisAlignedBB getCollisionBoundingBoxFromPool(World world, int x, int y, int z)
{
return null;
}
public void setBlockBoundsBasedOnState(IBlockAccess b, int x, int y, int z)
{
int meta = b.getBlockMetadata(x, y, z);
this.setButtonBounds(meta);
}
public void setBlockBoundsForItemRender()
{
float dx = 3/16F;
float dy = 2/16F;
float dz = 2/16F;
this.setBlockBounds(0.5F - dx, 0.5F - dy, 0.5F - dz, 0.5F + dx, 0.5F + dy, 0.5F + dz);
}
private void setButtonBounds(int metadata)
{
int orientation = metadata & 7;
boolean isPressed = (metadata & 8) > 0;
float topY = 6 / 16F;
float bottomY = 10 / 16F;
float halfWidth = 3 / 16F;
float depth = 2 / 16F;
if (isPressed) depth = 1 / 16F;
switch (orientation)
{
case 0: this.setBlockBounds(0.5F - halfWidth, 1.0F - depth, 0.5F - halfWidth, 0.5F + halfWidth, 1.0F, 0.5F + halfWidth); break;
case 1: this.setBlockBounds(0.0F, topY, 0.5F - halfWidth, depth, bottomY, 0.5F + halfWidth); break;
case 2: this.setBlockBounds(1.0F - depth, topY, 0.5F - halfWidth, 1.0F, bottomY, 0.5F + halfWidth); break;
case 3: this.setBlockBounds(0.5F - halfWidth, topY, 0.0F, 0.5F + halfWidth, bottomY, depth); break;
case 4: this.setBlockBounds(0.5F - halfWidth, topY, 1.0F - depth, 0.5F + halfWidth, bottomY, 1.0F); break;
default: this.setBlockBounds(0.5F - halfWidth, 0.0F, 0.5F - halfWidth, 0.5F + halfWidth, depth , 0.5F + halfWidth);
}
}
}