-
Notifications
You must be signed in to change notification settings - Fork 54
/
BlockAdventureBackpack.java
450 lines (395 loc) · 13.2 KB
/
BlockAdventureBackpack.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
450
package com.darkona.adventurebackpack.block;
import com.darkona.adventurebackpack.AdventureBackpack;
import com.darkona.adventurebackpack.client.Icons;
import com.darkona.adventurebackpack.reference.ModInfo;
import cpw.mods.fml.common.network.internal.FMLNetworkHandler;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.EnumCreatureType;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.*;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import java.util.Random;
/**
* Created by Darkona on 12/10/2014.
*/
public class BlockAdventureBackpack extends BlockContainer
{
public BlockAdventureBackpack()
{
super(Material.cloth);
setHardness(1.0f);
setStepSound(soundTypeCloth);
}
/**
* Pretty effects for the bookshelf ;)
*
* @param world
* @param x
* @param y
* @param z
* @param random
*/
@Override
@SideOnly(Side.CLIENT)
public void randomDisplayTick(World world, int x, int y, int z, Random random)
{
if (!getAssociatedTileColorName(world, x, y, z).equals("Bookshelf")) return;
for (int i = x - 2; i <= x + 2; ++i)
{
for (int j = z - 2; j <= z + 2; ++j)
{
if (i > x - 2 && i < x + 2 && j == z - 1)
{
j = z + 2;
}
if (random.nextInt(8) == 0)
{
for (int k = y; k <= y + 1; ++k)
{
if (world.getBlock(i, k, j) == Blocks.enchanting_table)
{
if (!world.isAirBlock((i - x) / 2 + x, k, (j - z) / 2 + z))
{
break;
}
for(int o = 0; o < 2; o++)
{
world.spawnParticle("enchantmenttable",
(double) i + 0.5D,
(double) k + 2.0D,
(double) j + 0.5D,
(double) ((float) (x - i) + random.nextFloat()) - 0.5D,
(double) ((float) (y - k) - random.nextFloat() - 1.0F),
(double) ((float) (z - j) + random.nextFloat()) - 0.5D);
}
}
}
}
}
}
}
public int getMobilityFlag()
{
return 0;
}
@Override
public String getHarvestTool(int metadata)
{
return null;
}
@Override
public int getHarvestLevel(int metadata)
{
return 0;
}
@Override
public boolean isToolEffective(String type, int metadata)
{
return true;
}
private String getAssociatedTileColorName(IBlockAccess world, int x, int y, int z)
{
return ((TileAdventureBackpack) world.getTileEntity(x, y, z)).getColorName();
}
/**
* Determines if this block should render in this pass.
*
* @param pass The pass in question
* @return True to render
*/
@Override
public boolean canRenderInPass(int pass)
{
return true;
}
@Override
public boolean canEntityDestroy(IBlockAccess world, int x, int y, int z, Entity entity)
{
return false;
}
@Override
public float getEnchantPowerBonus(World world, int x, int y, int z)
{
return getAssociatedTileColorName(world, x, y, z).equals("Bookshelf") ? 10 : 0;
}
@Override
public boolean canBeReplacedByLeaves(IBlockAccess world, int x, int y, int z)
{
return false;
}
@Override
public boolean isWood(IBlockAccess world, int x, int y, int z)
{
return false;
}
@Override
public boolean isLeaves(IBlockAccess world, int x, int y, int z)
{
return false;
}
@Override
public boolean canCreatureSpawn(EnumCreatureType type, IBlockAccess world, int x, int y, int z)
{
return false;
}
@Override
public int getFlammability(IBlockAccess world, int x, int y, int z, ForgeDirection face)
{
return 0;
}
@Override
public boolean canHarvestBlock(EntityPlayer player, int meta)
{
return true;
}
@Override
public void onEntityCollidedWithBlock(World world, int x, int y, int z, Entity entity)
{
if (getAssociatedTileColorName(world, x, y, z).equals("Cactus"))
{
entity.attackEntityFrom(DamageSource.cactus, 1.0F);
}
}
/**
* Called when a player hits the block. Args: world, x, y, z, player
*
* @param p_149699_1_
* @param p_149699_2_
* @param p_149699_3_
* @param p_149699_4_
* @param p_149699_5_
*/
@Override
public void onBlockClicked(World p_149699_1_, int p_149699_2_, int p_149699_3_, int p_149699_4_, EntityPlayer p_149699_5_)
{
super.onBlockClicked(p_149699_1_, p_149699_2_, p_149699_3_, p_149699_4_, p_149699_5_);
}
/**
* Called when a block is placed using its ItemBlock. Args: World, X, Y, Z, side, hitX, hitY, hitZ, block metadata
*
* @param p_149660_1_
* @param p_149660_2_
* @param p_149660_3_
* @param p_149660_4_
* @param p_149660_5_
* @param p_149660_6_
* @param p_149660_7_
* @param p_149660_8_
* @param p_149660_9_
*/
@Override
public int onBlockPlaced(World p_149660_1_, int p_149660_2_, int p_149660_3_, int p_149660_4_, int p_149660_5_, float p_149660_6_, float p_149660_7_, float p_149660_8_, int p_149660_9_)
{
return super.onBlockPlaced(p_149660_1_, p_149660_2_, p_149660_3_, p_149660_4_, p_149660_5_, p_149660_6_, p_149660_7_, p_149660_8_, p_149660_9_);
}
@Override
public float getExplosionResistance(Entity p_149638_1_)
{
return 500f;
}
@Override
public boolean isFlammable(IBlockAccess world, int x, int y, int z, ForgeDirection face)
{
return false;
}
@Override
public String getUnlocalizedName()
{
return "blockAdventureBackpack";
}
@Override
@SideOnly(Side.CLIENT)
public void registerBlockIcons(IIconRegister iconRegister)
{
Icons.milkStill = iconRegister.registerIcon(ModInfo.MOD_ID + ":fluid.milk");
Icons.melonJuiceStill = iconRegister.registerIcon(ModInfo.MOD_ID + ":fluid.melonJuiceStill");
Icons.melonJuiceFlowing = iconRegister.registerIcon(ModInfo.MOD_ID + ":fluid.melonJuiceFlowing");
}
@Override
public int getLightValue(IBlockAccess world, int x, int y, int z)
{
if (getAssociatedTileColorName(world, x, y, z).equals("Glowstone"))
{
return 15;
} else if (world.getTileEntity(x, y, z) != null && world.getTileEntity(x, y, z) instanceof TileAdventureBackpack)
{
return ((TileAdventureBackpack) world.getTileEntity(x, y, z)).getLuminosity();
} else
{
return 0;
}
}
public int isProvidingWeakPower(IBlockAccess world, int x, int y, int z, int meta)
{
return getAssociatedTileColorName(world, x, y, z).equals("Redstone") ? 15 : 0;
}
@Override
public boolean canConnectRedstone(IBlockAccess world, int x, int y, int z, int side)
{
return getAssociatedTileColorName(world, x, y, z).equals("Redstone");
}
@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float hitX, float hitY, float hitZ)
{
FMLNetworkHandler.openGui(player, AdventureBackpack.instance, 0, world, x, y, z);
return true;
}
@Override
public boolean hasTileEntity(int meta)
{
return true;
}
@Override
public void setBlockBoundsBasedOnState(IBlockAccess blockAccess, int x, int y, int z)
{
int meta = blockAccess.getBlockMetadata(x, y, z);
meta = (meta & 8) >= 8 ? meta - 8 : meta;
meta = (meta & 4) >= 4 ? meta - 4 : meta;
switch (meta)
{
case 0:
case 2:
setBlockBounds(0.0F, 0.0F, 0.4F, 1.0F, 0.6F, 0.6F);
break;
case 1:
case 3:
setBlockBounds(0.4F, 0.0F, 0.0F, 0.6F, 0.6F, 1.0F);
break;
}
}
@Override
public int getRenderType()
{
return -1;
}
@Override
public boolean isOpaqueCube()
{
return false;
}
@Override
public boolean renderAsNormalBlock()
{
return false;
}
@Override
public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase player, ItemStack stack)
{
int dir = MathHelper.floor_double((player.rotationYaw * 4F) / 360F + 0.5D) & 3;
if (stack != null && stack.stackTagCompound != null && stack.stackTagCompound.hasKey("color"))
{
if (stack.stackTagCompound.getString("color").contains("BlockRedstone"))
{
dir = dir | 8;
}
if (stack.stackTagCompound.getString("color").contains("Lightgem"))
{
dir = dir | 4;
}
}
world.setBlockMetadataWithNotify(x, y, z, dir, 3);
createNewTileEntity(world, world.getBlockMetadata(x, y, z));
}
@Override
public boolean canPlaceBlockOnSide(World par1World, int par2, int par3, int par4, int side)
{
return (ForgeDirection.getOrientation(side) == ForgeDirection.UP);
}
@Override
public int quantityDropped(int meta, int fortune, Random random)
{
return 0;
}
@Override
@SideOnly(Side.CLIENT)
public AxisAlignedBB getSelectedBoundingBoxFromPool(World world, int x, int y, int z)
{
setBlockBoundsBasedOnState(world, x, y, z);
return super.getSelectedBoundingBoxFromPool(world, x, y, z);
}
@Override
public IIcon getIcon(int par1, int par2)
{
return null;
}
@Override
public AxisAlignedBB getCollisionBoundingBoxFromPool(World world, int x, int y, int z)
{
setBlockBoundsBasedOnState(world, x, y, z);
return super.getCollisionBoundingBoxFromPool(world, x, y, z);
}
@Override
public MovingObjectPosition collisionRayTrace(World world, int x, int y, int z, Vec3 start, Vec3 end)
{
setBlockBoundsBasedOnState(world, x, y, z);
return super.collisionRayTrace(world, x, y, z, start, end);
}
@Override
public boolean removedByPlayer(World world, EntityPlayer player, int x, int y, int z, boolean harvest)
{
TileEntity tile = world.getTileEntity(x, y, z);
if (tile instanceof TileAdventureBackpack && !world.isRemote && player != null)
{
if ((player.isSneaking()) ?
((TileAdventureBackpack) tile).equip(world, player, x, y, z) :
((TileAdventureBackpack) tile).drop(world, player, x, y, z))
{
return world.func_147480_a(x, y, z, false);
}
} else
{
return world.func_147480_a(x, y, z, false);
}
return false;
}
@Override
public void breakBlock(World world, int x, int y, int z, Block block, int meta)
{
TileEntity te = world.getTileEntity(x, y, z);
if (te != null && te instanceof IInventory)
{
IInventory inventory = (IInventory) te;
for (int i = 0; i < inventory.getSizeInventory(); i++)
{
ItemStack stack = inventory.getStackInSlotOnClosing(i);
if (stack != null)
{
float spawnX = x + world.rand.nextFloat();
float spawnY = y + world.rand.nextFloat();
float spawnZ = z + world.rand.nextFloat();
float mult = 0.05F;
EntityItem droppedItem = new EntityItem(world, spawnX, spawnY, spawnZ, stack);
droppedItem.motionX = -0.5F + world.rand.nextFloat() * mult;
droppedItem.motionY = 4 + world.rand.nextFloat() * mult;
droppedItem.motionZ = -0.5 + world.rand.nextFloat() * mult;
world.spawnEntityInWorld(droppedItem);
}
}
}
super.breakBlock(world, x, y, z, world.getBlock(x, y, z), meta);
}
@Override
public TileEntity createTileEntity(World world, int metadata)
{
return new TileAdventureBackpack();
}
@Override
public TileEntity createNewTileEntity(World world, int metadata)
{
return createTileEntity(world, metadata);
}
}