-
Notifications
You must be signed in to change notification settings - Fork 753
/
HarvestTool.java
301 lines (248 loc) · 10.2 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package tconstruct.library.tools;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.PlayerControllerMP;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.play.client.C07PacketPlayerDigging;
import net.minecraft.network.play.server.S23PacketBlockChange;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeHooks;
import tconstruct.TConstruct;
import tconstruct.util.config.PHConstruct;
/* 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)
{
return super.onBlockStartBreak(stack, x,y,z, player);
}
@Override
public int getHarvestLevel(ItemStack stack, String toolClass) {
// well, we can only get the harvestlevel if we have an item to get it from!
if(stack == null || !(stack.getItem() instanceof HarvestTool))
return -1;
// invalid query or wrong toolclass
if(toolClass == null || !this.getHarvestType().equals(toolClass))
return -1;
if(!stack.hasTagCompound())
return -1;
NBTTagCompound tags = stack.getTagCompound().getCompoundTag("InfiTool");
// broken tools suck.
if (tags.getBoolean("Broken"))
return -1;
// tadaaaa
return tags.getInteger("HarvestLevel");
}
@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;
if(isEffective(block, meta))
return calculateStrength(tags, block, meta);
return super.getDigSpeed(stack, block, meta);
}
public float calculateStrength (NBTTagCompound tags, Block block, int meta)
{
int hlvl = block.getHarvestLevel(meta);
if (hlvl > tags.getInteger("HarvestLevel"))
return 0.1f;
return AbilityHelper.calcToolSpeed(this, tags);
}
public float breakSpeedModifier ()
{
return 1.0f;
}
public float stoneboundModifier ()
{
return 72f;
}
@Override
public boolean func_150897_b (Block block)
{
if (block.getMaterial().isToolNotRequired())
{
return true;
}
return isEffective(block.getMaterial());
}
@Override
public boolean canHarvestBlock (Block block, ItemStack itemStack)
{
return func_150897_b(block);
}
@Override
public String[] getTraits ()
{
return new String[] { "harvest" };
}
protected abstract Material[] getEffectiveMaterials ();
protected abstract String getHarvestType ();
public boolean isEffective (Block block, int meta)
{
if(this.getHarvestType().equals(block.getHarvestTool(meta)))
return true;
else return isEffective(block.getMaterial());
}
public boolean isEffective (Material material)
{
for (Material m : getEffectiveMaterials())
if (m == material)
return true;
return false;
}
//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;
}
int dmg = nearbyStack.getItemDamage();
int count = nearbyStack.stackSize;
used = item.onItemUse(nearbyStack, player, world, x, y, z, side, clickX, clickY, clickZ);
// handle creative mode
if(player.capabilities.isCreativeMode) {
// fun fact: vanilla minecraft does it exactly the same way
nearbyStack.setItemDamage(dmg);
nearbyStack.stackSize = count;
}
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;
}
protected void breakExtraBlock(World world, int x, int y, int z, int sidehit, EntityPlayer player, int refX, int refY, int refZ) {
// prevent calling that stuff for air blocks, could lead to unexpected behaviour since it fires events
if (world.isAirBlock(x, y, z))
return;
// check if the block can be broken, since extra block breaks shouldn't instantly break stuff like obsidian
// or precious ores you can't harvest while mining stone
Block block = world.getBlock(x, y, z);
int meta = world.getBlockMetadata(x, y, z);
// only effective materials
if (!isEffective(block, meta))
return;
Block refBlock = world.getBlock(refX, refY, refZ);
float refStrength = ForgeHooks.blockStrength(refBlock, player, world, refX, refY, refZ);
float strength = ForgeHooks.blockStrength(block, player, world, x,y,z);
// only harvestable blocks that aren't impossibly slow to harvest
if (!ForgeHooks.canHarvestBlock(block, player, meta) || refStrength/strength > 10f)
return;
if (player.capabilities.isCreativeMode) {
block.onBlockHarvested(world, x, y, z, meta, player);
if (block.removedByPlayer(world, player, x, y, z, false))
block.onBlockDestroyedByPlayer(world, x, y, z, meta);
// send update to client
if (!world.isRemote) {
((EntityPlayerMP)player).playerNetServerHandler.sendPacket(new S23PacketBlockChange(x, y, z, world));
}
return;
}
// callback to the tool the player uses. Called on both sides. This damages the tool n stuff.
player.getCurrentEquippedItem().func_150999_a(world, block, x, y, z, player);
// server sided handling
if (!world.isRemote) {
// serverside we reproduce ItemInWorldManager.tryHarvestBlock
// ItemInWorldManager.removeBlock
block.onBlockHarvested(world, x,y,z, meta, player);
if(block.removedByPlayer(world, player, x,y,z, true)) // boolean is if block can be harvested, checked above
{
block.onBlockDestroyedByPlayer( world, x,y,z, meta);
block.harvestBlock(world, player, x,y,z, meta);
}
// always send block update to client
EntityPlayerMP mpPlayer = (EntityPlayerMP) player;
mpPlayer.playerNetServerHandler.sendPacket(new S23PacketBlockChange(x, y, z, world));
}
// client sided handling
else {
PlayerControllerMP pcmp = Minecraft.getMinecraft().playerController;
// clientside we do a "this clock has been clicked on long enough to be broken" call. This should not send any new packets
// the code above, executed on the server, sends a block-updates that give us the correct state of the block we destroy.
// following code can be found in PlayerControllerMP.onPlayerDestroyBlock
world.playAuxSFX(2001, x, y, z, Block.getIdFromBlock(block) + (meta << 12));
if(block.removedByPlayer(world, player, x,y,z))
{
block.onBlockDestroyedByPlayer(world, x,y,z, meta);
}
pcmp.onPlayerDestroyBlock(x, y, z, sidehit);
// send an update to the server, so we get an update back
if(PHConstruct.extraBlockUpdates)
Minecraft.getMinecraft().getNetHandler().addToSendQueue(new C07PacketPlayerDigging(2, x,y,z, Minecraft.getMinecraft().objectMouseOver.sideHit));
}
}
}