use of net.minecraft.network.play.server.SPacketBlockChange in project MinecraftForge by MinecraftForge.
the class ForgeHooks method onBlockBreakEvent.
public static int onBlockBreakEvent(World world, GameType gameType, EntityPlayerMP entityPlayer, BlockPos pos) {
// Logic from tryHarvestBlock for pre-canceling the event
boolean preCancelEvent = false;
if (gameType.isCreative() && !entityPlayer.getHeldItemMainhand().isEmpty() && entityPlayer.getHeldItemMainhand().getItem() instanceof ItemSword)
preCancelEvent = true;
if (gameType.isAdventure()) {
if (gameType == GameType.SPECTATOR)
preCancelEvent = true;
if (!entityPlayer.isAllowEdit()) {
ItemStack itemstack = entityPlayer.getHeldItemMainhand();
if (itemstack.isEmpty() || !itemstack.canDestroy(world.getBlockState(pos).getBlock()))
preCancelEvent = true;
}
}
// Tell client the block is gone immediately then process events
if (world.getTileEntity(pos) == null) {
SPacketBlockChange packet = new SPacketBlockChange(world, pos);
packet.blockState = Blocks.AIR.getDefaultState();
entityPlayer.connection.sendPacket(packet);
}
// Post the block break event
IBlockState state = world.getBlockState(pos);
BlockEvent.BreakEvent event = new BlockEvent.BreakEvent(world, pos, state, entityPlayer);
event.setCanceled(preCancelEvent);
MinecraftForge.EVENT_BUS.post(event);
// Handle if the event is canceled
if (event.isCanceled()) {
// Let the client know the block still exists
entityPlayer.connection.sendPacket(new SPacketBlockChange(world, pos));
// Update any tile entity data for this block
TileEntity tileentity = world.getTileEntity(pos);
if (tileentity != null) {
Packet<?> pkt = tileentity.getUpdatePacket();
if (pkt != null) {
entityPlayer.connection.sendPacket(pkt);
}
}
}
return event.isCanceled() ? -1 : event.getExpToDrop();
}
use of net.minecraft.network.play.server.SPacketBlockChange in project Overloaded by CJ-MC-Mods.
the class PlayerInteractionUtil method tryHarvestBlock.
public static boolean tryHarvestBlock(EntityPlayerMP player, World world, BlockPos pos) {
int exp = net.minecraftforge.common.ForgeHooks.onBlockBreakEvent(world, player.interactionManager.getGameType(), player, pos);
if (exp == -1) {
return false;
} else {
IBlockState iblockstate = world.getBlockState(pos);
TileEntity tileentity = world.getTileEntity(pos);
Block block = iblockstate.getBlock();
if ((block instanceof BlockCommandBlock || block instanceof BlockStructure) && !player.canUseCommandBlock()) {
world.notifyBlockUpdate(pos, iblockstate, iblockstate, 3);
return false;
} else {
world.playEvent(null, 2001, pos, Block.getStateId(iblockstate));
boolean flag1;
if (player.capabilities.isCreativeMode) {
flag1 = removeBlock(world, pos, player, false);
player.connection.sendPacket(new SPacketBlockChange(world, pos));
} else {
ItemStack itemstack1 = player.getHeldItemMainhand();
ItemStack itemstack2 = itemstack1.isEmpty() ? ItemStack.EMPTY : itemstack1.copy();
boolean flag = iblockstate.getBlock().canHarvestBlock(world, pos, player);
itemstack1.onBlockDestroyed(world, iblockstate, pos, player);
flag1 = removeBlock(world, pos, player, flag);
if (flag1 && flag) {
iblockstate.getBlock().harvestBlock(world, player, pos, iblockstate, tileentity, itemstack2);
}
}
// Drop experience
if (!player.isCreative() && flag1 && exp > 0) {
iblockstate.getBlock().dropXpOnBlockBreak(world, player.getPosition(), exp);
}
return flag1;
}
}
}
use of net.minecraft.network.play.server.SPacketBlockChange in project ImmersiveEngineering by BluSunrize.
the class ItemDrill method onBlockStartBreak.
@Override
public boolean onBlockStartBreak(ItemStack stack, BlockPos iPos, EntityPlayer player) {
World world = player.worldObj;
if (player.isSneaking() || world.isRemote || !(player instanceof EntityPlayerMP))
return false;
RayTraceResult mop = this.rayTrace(world, player, true);
ItemStack head = getHead(stack);
if (mop == null || head == null || this.isDrillBroken(stack))
return false;
// EnumFacing side = mop.sideHit;
// int diameter = ((IDrillHead)head.getItem()).getMiningSize(head)+getUpgrades(stack).getInteger("size");
// int depth = ((IDrillHead)head.getItem()).getMiningDepth(head)+getUpgrades(stack).getInteger("depth");
//
// BlockPos startPos=iPos;
// if(diameter%2==0)//even numbers
// {
// float hx = (float)mop.hitVec.xCoord-iPos.getX();
// float hy = (float)mop.hitVec.yCoord-iPos.getY();
// float hz = (float)mop.hitVec.zCoord-iPos.getZ();
// if((side.getAxis()==Axis.Y&&hx<.5)||(side.getAxis()==Axis.Z&&hx<.5))
// startPos.add(-diameter/2,0,0);
// if(side.getAxis()!=Axis.Y&&hy<.5)
// startPos.add(0,-diameter/2,0);
// if((side.getAxis()==Axis.Y&&hz<.5)||(side.getAxis()==Axis.X&&hz<.5))
// startPos.add(0,0,-diameter/2);
// }
// else//odd numbers
// {
// startPos.add(-(side.getAxis()==Axis.X?0: diameter/2), -(side.getAxis()==Axis.Y?0: diameter/2), -(side.getAxis()==Axis.Z?0: diameter/2));
// }
//
// for(int dd=0; dd<depth; dd++)
// for(int dw=0; dw<diameter; dw++)
// for(int dh=0; dh<diameter; dh++)
// {
// BlockPos pos = startPos.add((side.getAxis()==Axis.X?dd: dw), (side.getAxis()==Axis.Y?dd: dh), (side.getAxis()==Axis.Y?dh: side.getAxis()==Axis.X?dw: dd));
// if(pos.equals(iPos))
// continue;
ImmutableList<BlockPos> additional = ((IDrillHead) head.getItem()).getExtraBlocksDug(head, world, player, mop);
for (BlockPos pos : additional) {
if (!world.isBlockLoaded(pos))
continue;
IBlockState state = world.getBlockState(pos);
Block block = state.getBlock();
if (block != null && !block.isAir(state, world, pos) && state.getPlayerRelativeBlockHardness(player, world, pos) != 0) {
if (!this.canBreakExtraBlock(world, block, pos, state, player, stack, head, true))
continue;
int xpDropEvent = ForgeHooks.onBlockBreakEvent(world, ((EntityPlayerMP) player).interactionManager.getGameType(), (EntityPlayerMP) player, pos);
if (xpDropEvent < 0)
continue;
if (player.capabilities.isCreativeMode) {
block.onBlockHarvested(world, pos, state, player);
if (block.removedByPlayer(state, world, pos, player, false))
block.onBlockDestroyedByPlayer(world, pos, state);
} else {
block.onBlockHarvested(world, pos, state, player);
TileEntity te = world.getTileEntity(pos);
//implicitly damages head
stack.onBlockDestroyed(world, state, pos, player);
if (block.removedByPlayer(state, world, pos, player, true)) {
block.onBlockDestroyedByPlayer(world, pos, state);
block.harvestBlock(world, player, pos, state, te, stack);
block.dropXpOnBlockBreak(world, pos, xpDropEvent);
}
}
world.playEvent(2001, pos, Block.getStateId(state));
((EntityPlayerMP) player).connection.sendPacket(new SPacketBlockChange(world, pos));
}
}
return false;
}
Aggregations