-
Notifications
You must be signed in to change notification settings - Fork 754
/
HarvestTool.java
280 lines (244 loc) · 9.41 KB
/
HarvestTool.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
package tconstruct.library.tools;
import mantle.world.WorldHelper;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import tconstruct.library.ActiveToolMod;
import tconstruct.library.TConstructRegistry;
/* Base class for tools that should be harvesting blocks */
public abstract class HarvestTool extends ToolCore
{
public HarvestTool(int baseDamage)
{
super(baseDamage);
}
@Override
public boolean onBlockStartBreak (ItemStack stack, int x, int y, int z, EntityPlayer player)
{
if (!stack.hasTagCompound())
return false;
NBTTagCompound tags = stack.getTagCompound().getCompoundTag("InfiTool");
World world = player.worldObj;
Block block = player.worldObj.getBlock(x, y, z);
int meta = world.getBlockMetadata(x, y, z);
// Block block = Block.blocksList[bID];
if (block == null || block == Blocks.air)
return false;
int hlvl = -1;
if (!(tags.getBoolean("Broken")))
{
Block localBlock = world.getBlock(x, y, z);
int localMeta = world.getBlockMetadata(x, y, z);
if (block.getHarvestTool(meta) != null && block.getHarvestTool(meta).equals(this.getHarvestType()))
hlvl = block.getHarvestLevel(meta);
int toolLevel = tags.getInteger("HarvestLevel");
float blockHardness = block.getBlockHardness(world, x, y, z);
float localHardness = localBlock == null ? Float.MAX_VALUE : localBlock.getBlockHardness(world, x, y, z);
if (hlvl <= toolLevel && localHardness - 1.5 <= blockHardness)
{
boolean cancelHarvest = false;
for (ActiveToolMod mod : TConstructRegistry.activeModifiers)
{
if (mod.beforeBlockBreak(this, stack, x, y, z, player))
cancelHarvest = true;
}
if (!cancelHarvest)
{
if (localBlock != null && !(localHardness < 0))
{
boolean isEffective = false;
for (int iter = 0; iter < getEffectiveMaterials().length; iter++)
{
if (getEffectiveMaterials()[iter] == localBlock.getMaterial() || localBlock == Blocks.monster_egg)
{
isEffective = true;
break;
}
}
if (localBlock.getMaterial().isToolNotRequired())
{
isEffective = true;
}
if (!player.capabilities.isCreativeMode)
{
if (isEffective)
{
if (localBlock.removedByPlayer(world, player, x, y, z))
{
localBlock.onBlockDestroyedByPlayer(world, x, y, z, localMeta);
}
localBlock.harvestBlock(world, player, x, y, z, localMeta);
localBlock.onBlockHarvested(world, x, y, z, localMeta, player);
if (blockHardness > 0f)
onBlockDestroyed(stack, world, localBlock, x, y, z, player);
world.func_147479_m(x, y, z);
}
else
{
WorldHelper.setBlockToAir(world, x, y, z);
world.func_147479_m(x, y, z);
}
}
else
{
WorldHelper.setBlockToAir(world, x, y, z);
world.func_147479_m(x, y, z);
}
}
}
}
}
if (!world.isRemote)
world.playAuxSFX(2001, x, y, z, Block.getIdFromBlock(block) + (meta << 12));
return true;
}
@Override
public float getDigSpeed (ItemStack stack, Block block, int meta)
{
if (!stack.hasTagCompound())
return 1.0f;
NBTTagCompound tags = stack.getTagCompound().getCompoundTag("InfiTool");
if (tags.getBoolean("Broken"))
return 0.1f;
Material[] materials = getEffectiveMaterials();
for (int i = 0; i < materials.length; i++)
{
if (materials[i] == block.getMaterial())
{
return calculateStrength(tags, block, meta);
}
}
if (block.getHarvestLevel(meta) > 0)
{
return calculateStrength(tags, block, meta); // No issue if the
// harvest level is
// too low
}
return super.getDigSpeed(stack, block, meta);
}
float calculateStrength (NBTTagCompound tags, Block block, int meta)
{
float mineSpeed = tags.getInteger("MiningSpeed");
int heads = 1;
if (tags.hasKey("MiningSpeed2"))
{
mineSpeed += tags.getInteger("MiningSpeed2");
heads++;
}
if (tags.hasKey("MiningSpeedHandle"))
{
mineSpeed += tags.getInteger("MiningSpeedHandle");
heads++;
}
if (tags.hasKey("MiningSpeedExtra"))
{
mineSpeed += tags.getInteger("MiningSpeedExtra");
heads++;
}
float trueSpeed = mineSpeed / (heads * 100f);
int hlvl = block.getHarvestLevel(meta);
int durability = tags.getInteger("Damage");
float stonebound = tags.getFloat("Shoddy");
float bonusLog = (float) Math.log(durability / 72f + 1) * 2 * stonebound;
trueSpeed += bonusLog;
if (hlvl <= tags.getInteger("HarvestLevel"))
return trueSpeed;
return 0.1f;
}
public boolean canHarvestBlock (Block block)
{
if (block.getMaterial().isToolNotRequired())
{
return true;
}
for (Material m : getEffectiveMaterials())
{
if (m == block.getMaterial())
return true;
}
return false;
}
@Override
public String[] toolCategories ()
{
return new String[] { "harvest" };
}
protected abstract Material[] getEffectiveMaterials ();
protected abstract String getHarvestType ();
//Right-click
@Override
public boolean onItemUse (ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side, float clickX, float clickY, float clickZ)
{
/*if (world.isRemote)
return true;*/
boolean used = false;
int hotbarSlot = player.inventory.currentItem;
int itemSlot = hotbarSlot == 0 ? 8 : hotbarSlot + 1;
ItemStack nearbyStack = null;
if (hotbarSlot < 8)
{
nearbyStack = player.inventory.getStackInSlot(itemSlot);
if (nearbyStack != null)
{
Item item = nearbyStack.getItem();
if (item instanceof ItemBlock)
{
int posX = x;
int posY = y;
int posZ = z;
int playerPosX = (int) Math.floor(player.posX);
int playerPosY = (int) Math.floor(player.posY);
int playerPosZ = (int) Math.floor(player.posZ);
if (side == 0)
{
--posY;
}
if (side == 1)
{
++posY;
}
if (side == 2)
{
--posZ;
}
if (side == 3)
{
++posZ;
}
if (side == 4)
{
--posX;
}
if (side == 5)
{
++posX;
}
if (posX == playerPosX && (posY == playerPosY || posY == playerPosY + 1 || posY == playerPosY - 1) && posZ == playerPosZ)
{
return false;
}
used = item.onItemUse(nearbyStack, player, world, x, y, z, side, clickX, clickY, clickZ);
if (nearbyStack.stackSize < 1)
{
nearbyStack = null;
player.inventory.setInventorySlotContents(itemSlot, null);
}
}
}
}
/*
if (used) //Update client
{
Packet103SetSlot packet = new Packet103SetSlot(player.openContainer.windowId, itemSlot, nearbyStack);
((EntityPlayerMP)player).playerNetServerHandler.sendPacketToPlayer(packet);
}
*/
return used;
}
}