Skip to content

Commit

Permalink
Fire block break events for extra blocks broken from AOE tools #1346
Browse files Browse the repository at this point in the history
  • Loading branch information
bonii-xx committed Jan 18, 2015
1 parent f7525b6 commit 824ec2d
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/main/java/tconstruct/library/tools/HarvestTool.java
@@ -1,5 +1,6 @@
package tconstruct.library.tools;

import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.Block;
Expand All @@ -12,10 +13,13 @@
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.client.C07PacketPlayerDigging;
import net.minecraft.network.play.server.S23PacketBlockChange;
import net.minecraft.world.World;
import net.minecraft.world.WorldSettings;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.event.world.BlockEvent;
import tconstruct.tools.TinkerTools;
import tconstruct.util.config.PHConstruct;

Expand Down Expand Up @@ -249,11 +253,16 @@ public boolean onItemUse (ItemStack stack, EntityPlayer player, World world, int
return used;
}

protected void breakExtraBlock(World world, int x, int y, int z, int sidehit, EntityPlayer player, int refX, int refY, int refZ) {
protected void breakExtraBlock(World world, int x, int y, int z, int sidehit, EntityPlayer playerEntity, 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;

// what?
if(!(playerEntity instanceof EntityPlayerMP))
return;
EntityPlayerMP player = (EntityPlayerMP) playerEntity;

// 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);
Expand All @@ -271,14 +280,19 @@ protected void breakExtraBlock(World world, int x, int y, int z, int sidehit, En
if (!ForgeHooks.canHarvestBlock(block, player, meta) || refStrength/strength > 10f)
return;

// send the blockbreak event
BlockEvent.BreakEvent event = ForgeHooks.onBlockBreakEvent(world, player.theItemInWorldManager.getGameType(), player, x,y,z);
if(event.isCanceled())
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));
player.playerNetServerHandler.sendPacket(new S23PacketBlockChange(x, y, z, world));
}
return;
}
Expand All @@ -300,12 +314,11 @@ protected void breakExtraBlock(World world, int x, int y, int z, int sidehit, En
}

// always send block update to client
EntityPlayerMP mpPlayer = (EntityPlayerMP) player;
mpPlayer.playerNetServerHandler.sendPacket(new S23PacketBlockChange(x, y, z, world));
player.playerNetServerHandler.sendPacket(new S23PacketBlockChange(x, y, z, world));
}
// client sided handling
else {
PlayerControllerMP pcmp = Minecraft.getMinecraft().playerController;
//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.

Expand Down

0 comments on commit 824ec2d

Please sign in to comment.