-
Notifications
You must be signed in to change notification settings - Fork 81
/
ItemBoneBag.java
127 lines (104 loc) · 3.89 KB
/
ItemBoneBag.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
package com.progwml6.natura.shared.item.bags;
import com.progwml6.natura.library.NaturaRegistry;
import net.minecraft.block.IGrowable;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockPos.MutableBlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.ForgeEventFactory;
import net.minecraftforge.event.entity.player.BonemealEvent;
import net.minecraftforge.fml.common.eventhandler.Event.Result;
public class ItemBoneBag extends Item
{
public ItemBoneBag()
{
super();
this.setCreativeTab(NaturaRegistry.tabGeneral);
}
/**
* Called when a Block is right-clicked with this Item
*/
@Override
public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
{
if (facing != EnumFacing.UP)
{
return EnumActionResult.FAIL;
}
else
{
ItemStack itemstack = player.getHeldItem(hand);
MutableBlockPos mutableblockpos = new MutableBlockPos();
int posY = pos.getY();
boolean planted = false;
for (int posX = pos.getX() - 1; posX <= pos.getX() + 1; posX++)
{
for (int posZ = pos.getZ() - 1; posZ <= pos.getZ() + 1; posZ++)
{
BlockPos position = mutableblockpos.setPos(posX, posY, posZ);
if (player.canPlayerEdit(position, facing, itemstack) && player.canPlayerEdit(position.up(), facing, itemstack))
{
if (applyBonemeal(itemstack, worldIn, position, player, hand))
{
planted = true;
if (!worldIn.isRemote)
{
worldIn.playEvent(2005, position, 0);
}
}
}
}
}
if (planted)
{
if (!player.capabilities.isCreativeMode)
{
itemstack.shrink(1);
}
if (itemstack.getCount() < 1)
{
ForgeEventFactory.onPlayerDestroyItem(player, itemstack, hand);
player.setHeldItem(hand, ItemStack.EMPTY);
}
return EnumActionResult.SUCCESS;
}
return EnumActionResult.PASS;
}
}
public static boolean applyBonemeal(ItemStack stack, World worldIn, BlockPos target, EntityPlayer player, EnumHand hand)
{
IBlockState iblockstate = worldIn.getBlockState(target);
BonemealEvent event = new BonemealEvent(player, worldIn, target, iblockstate, hand, stack);
if (MinecraftForge.EVENT_BUS.post(event))
{
return false;
}
if (event.getResult() == Result.ALLOW)
{
return true;
}
if (iblockstate.getBlock() instanceof IGrowable)
{
IGrowable igrowable = (IGrowable) iblockstate.getBlock();
if (igrowable.canGrow(worldIn, target, iblockstate, worldIn.isRemote))
{
if (!worldIn.isRemote)
{
if (igrowable.canUseBonemeal(worldIn, worldIn.rand, target, iblockstate))
{
igrowable.grow(worldIn, worldIn.rand, target, iblockstate);
}
}
return true;
}
}
return false;
}
}