2015-08-29 14:33:25 +02:00
/ *
* This file ( " WorldUtil.java " ) is part of the Actually Additions Mod for Minecraft .
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
2016-01-03 16:05:51 +01:00
* http : //ellpeck.de/actaddlicense/
2015-08-29 14:33:25 +02:00
* View the source code at https : //github.com/Ellpeck/ActuallyAdditions
*
2016-01-03 16:05:51 +01:00
* © 2016 Ellpeck
2015-08-29 14:33:25 +02:00
* /
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.util ;
2015-04-19 01:50:02 +02:00
2015-05-20 22:39:43 +02:00
import cofh.api.energy.EnergyStorage ;
import cofh.api.energy.IEnergyReceiver ;
2015-05-30 17:47:57 +02:00
import net.minecraft.block.Block ;
2016-01-31 03:31:39 +01:00
import net.minecraft.block.BlockLiquid ;
2015-06-28 03:12:32 +02:00
import net.minecraft.block.material.Material ;
2016-01-08 08:18:27 +01:00
import net.minecraft.block.state.IBlockState ;
2015-07-27 08:26:13 +02:00
import net.minecraft.client.Minecraft ;
import net.minecraft.enchantment.EnchantmentHelper ;
2015-05-04 17:26:50 +02:00
import net.minecraft.entity.item.EntityItem ;
2015-07-07 20:20:24 +02:00
import net.minecraft.entity.player.EntityPlayer ;
2015-07-08 11:49:38 +02:00
import net.minecraft.entity.player.EntityPlayerMP ;
2015-05-30 17:47:57 +02:00
import net.minecraft.init.Blocks ;
2015-12-23 23:09:50 +01:00
import net.minecraft.init.Items ;
2015-12-08 18:10:37 +01:00
import net.minecraft.inventory.IInventory ;
2015-12-09 13:35:18 +01:00
import net.minecraft.inventory.ISidedInventory ;
2015-04-19 01:50:02 +02:00
import net.minecraft.item.ItemStack ;
2015-07-27 08:26:13 +02:00
import net.minecraft.network.play.client.C07PacketPlayerDigging ;
import net.minecraft.network.play.server.S23PacketBlockChange ;
2015-04-19 01:50:02 +02:00
import net.minecraft.tileentity.TileEntity ;
2016-01-08 13:31:58 +01:00
import net.minecraft.util.* ;
2015-04-19 01:50:02 +02:00
import net.minecraft.world.World ;
2015-05-04 17:26:50 +02:00
import net.minecraft.world.WorldServer ;
2015-11-22 23:02:59 +01:00
import net.minecraftforge.common.ForgeHooks ;
2015-05-22 17:48:50 +02:00
import net.minecraftforge.common.IPlantable ;
2015-05-30 17:47:57 +02:00
import net.minecraftforge.fluids.* ;
2015-04-19 01:50:02 +02:00
2015-06-28 03:12:32 +02:00
import java.util.ArrayList ;
2015-04-19 01:50:02 +02:00
public class WorldUtil {
2016-01-08 13:31:58 +01:00
public static void breakBlockAtSide ( EnumFacing side , World world , BlockPos pos ) {
2016-01-07 18:20:59 +01:00
breakBlockAtSide ( side , world , pos , 0 ) ;
2015-10-05 19:55:28 +02:00
}
2016-01-08 13:31:58 +01:00
public static void breakBlockAtSide ( EnumFacing side , World world , BlockPos pos , int offset ) {
BlockPos c = getCoordsFromSide ( side , pos , offset ) ;
2015-04-19 01:50:02 +02:00
if ( c ! = null ) {
2016-01-08 21:55:39 +01:00
world . setBlockToAir ( c ) ;
2015-04-19 01:50:02 +02:00
}
}
2016-01-08 13:31:58 +01:00
public static BlockPos getCoordsFromSide ( EnumFacing side , BlockPos pos , int offset ) {
return new BlockPos ( pos . getX ( ) + side . getFrontOffsetX ( ) * ( offset + 1 ) , pos . getY ( ) + side . getFrontOffsetY ( ) * ( offset + 1 ) , pos . getZ ( ) + side . getFrontOffsetZ ( ) * ( offset + 1 ) ) ;
2015-10-03 10:19:40 +02:00
}
2016-01-17 19:22:14 +01:00
2016-01-08 13:31:58 +01:00
public static void pushEnergyToAllSides ( World world , BlockPos pos , EnergyStorage storage ) {
2016-01-08 15:21:05 +01:00
pushEnergy ( world , pos , EnumFacing . UP , storage ) ;
pushEnergy ( world , pos , EnumFacing . DOWN , storage ) ;
pushEnergy ( world , pos , EnumFacing . NORTH , storage ) ;
pushEnergy ( world , pos , EnumFacing . EAST , storage ) ;
pushEnergy ( world , pos , EnumFacing . SOUTH , storage ) ;
pushEnergy ( world , pos , EnumFacing . WEST , storage ) ;
2016-01-07 19:04:29 +01:00
}
2015-10-03 10:19:40 +02:00
2016-01-08 13:31:58 +01:00
public static void pushEnergy ( World world , BlockPos pos , EnumFacing side , EnergyStorage storage ) {
2016-01-07 18:20:59 +01:00
TileEntity tile = getTileEntityFromSide ( side , world , pos ) ;
2015-05-29 18:17:28 +02:00
if ( tile ! = null & & tile instanceof IEnergyReceiver & & storage . getEnergyStored ( ) > 0 ) {
2015-05-20 22:39:43 +02:00
if ( ( ( IEnergyReceiver ) tile ) . canConnectEnergy ( side . getOpposite ( ) ) ) {
int receive = ( ( IEnergyReceiver ) tile ) . receiveEnergy ( side . getOpposite ( ) , Math . min ( storage . getMaxExtract ( ) , storage . getEnergyStored ( ) ) , false ) ;
storage . extractEnergy ( receive , false ) ;
}
}
}
2016-01-08 13:31:58 +01:00
public static TileEntity getTileEntityFromSide ( EnumFacing side , World world , BlockPos pos ) {
BlockPos c = getCoordsFromSide ( side , pos , 0 ) ;
2015-10-03 10:19:40 +02:00
if ( c ! = null ) {
2016-01-08 15:21:05 +01:00
return world . getTileEntity ( c ) ;
2015-10-03 10:19:40 +02:00
}
return null ;
}
2015-07-25 05:28:31 +02:00
/ * *
* Checks if a given Block with a given Meta is present in given Positions
2015-07-27 08:26:13 +02:00
*
2016-01-07 18:20:59 +01:00
* @param positions The Positions , an array of { xCoord , yCoord , zCoord } arrays containing Positions
2015-07-27 08:26:13 +02:00
* @param block The Block
* @param meta The Meta
* @param world The World
2015-07-25 05:28:31 +02:00
* @return Is every block present ?
* /
2016-01-08 13:31:58 +01:00
public static boolean hasBlocksInPlacesGiven ( BlockPos [ ] positions , Block block , int meta , World world ) {
for ( BlockPos pos : positions ) {
if ( ! ( PosUtil . getBlock ( pos , world ) = = block & & PosUtil . getMetadata ( pos , world ) = = meta ) ) {
2015-07-25 05:28:31 +02:00
return false ;
}
}
return true ;
}
2016-01-08 13:31:58 +01:00
public static void pushFluid ( World world , BlockPos pos , EnumFacing side , FluidTank tank ) {
2016-01-07 18:20:59 +01:00
TileEntity tile = getTileEntityFromSide ( side , world , pos ) ;
2015-05-20 22:39:43 +02:00
if ( tile ! = null & & tank . getFluid ( ) ! = null & & tile instanceof IFluidHandler ) {
if ( ( ( IFluidHandler ) tile ) . canFill ( side . getOpposite ( ) , tank . getFluid ( ) . getFluid ( ) ) ) {
int receive = ( ( IFluidHandler ) tile ) . fill ( side . getOpposite ( ) , tank . getFluid ( ) , true ) ;
tank . drain ( receive , true ) ;
}
}
}
2016-01-31 03:31:39 +01:00
public static ItemStack useItemAtSide ( EnumFacing side , World world , BlockPos pos , ItemStack stack ) {
2015-05-30 17:47:57 +02:00
if ( world instanceof WorldServer & & stack ! = null & & stack . getItem ( ) ! = null ) {
2016-01-08 13:31:58 +01:00
BlockPos offsetPos = pos . offset ( side ) ;
2016-01-31 03:31:39 +01:00
Block block = PosUtil . getBlock ( offsetPos , world ) ;
boolean replaceable = block . isReplaceable ( world , offsetPos ) ;
2015-05-27 21:57:53 +02:00
//Fluids
2016-01-31 03:31:39 +01:00
if ( replaceable & & FluidContainerRegistry . isFilledContainer ( stack ) & & ! ( block instanceof IFluidBlock ) & & ! ( block instanceof BlockLiquid ) ) {
FluidStack fluid = FluidContainerRegistry . getFluidForFilledItem ( stack ) ;
if ( fluid ! = null & & fluid . getFluid ( ) . getBlock ( ) ! = null & & fluid . getFluid ( ) . getBlock ( ) . canPlaceBlockAt ( world , offsetPos ) ) {
if ( PosUtil . setBlock ( offsetPos , world , fluid . getFluid ( ) . getBlock ( ) , 0 , 2 ) ) {
2015-05-30 17:47:57 +02:00
return stack . getItem ( ) . getContainerItem ( stack ) ;
}
}
2015-05-27 21:57:53 +02:00
}
2015-12-23 23:09:50 +01:00
//Redstone
2016-01-31 03:31:39 +01:00
else if ( replaceable & & stack . getItem ( ) = = Items . redstone ) {
PosUtil . setBlock ( offsetPos , world , Blocks . redstone_wire , 0 , 2 ) ;
2015-12-23 23:09:50 +01:00
stack . stackSize - - ;
}
2015-05-27 21:57:53 +02:00
//Plants
2016-01-31 03:31:39 +01:00
else if ( replaceable & & stack . getItem ( ) instanceof IPlantable ) {
2016-01-07 21:41:28 +01:00
if ( ( ( IPlantable ) stack . getItem ( ) ) . getPlant ( world , offsetPos ) . getBlock ( ) . canPlaceBlockAt ( world , offsetPos ) ) {
2016-01-08 13:31:58 +01:00
if ( world . setBlockState ( offsetPos , ( ( IPlantable ) stack . getItem ( ) ) . getPlant ( world , offsetPos ) , 2 ) ) {
2015-05-30 17:47:57 +02:00
stack . stackSize - - ;
}
2015-05-22 17:48:50 +02:00
}
}
2016-01-31 03:31:39 +01:00
//Everything else
2015-12-23 23:09:50 +01:00
else {
try {
2016-01-31 03:31:39 +01:00
stack . onItemUse ( FakePlayerUtil . getFakePlayer ( world ) , world , offsetPos , side . getOpposite ( ) , 0 . 5F , 0 . 5F , 0 . 5F ) ;
2015-12-23 23:09:50 +01:00
return stack ;
}
catch ( Exception e ) {
2016-01-31 03:31:39 +01:00
ModUtil . LOGGER . error ( " Something that places Blocks at " + offsetPos . getX ( ) + " , " + offsetPos . getY ( ) + " , " + offsetPos . getZ ( ) + " in World " + world . provider . getDimensionId ( ) + " threw an Exception! Don't let that happen again! " , e ) ;
2015-12-23 23:09:50 +01:00
}
2015-05-30 17:47:57 +02:00
}
2015-04-19 01:50:02 +02:00
}
2015-05-30 17:47:57 +02:00
return stack ;
2015-04-19 01:50:02 +02:00
}
2016-01-08 13:31:58 +01:00
public static void dropItemAtSide ( EnumFacing side , World world , BlockPos pos , ItemStack stack ) {
BlockPos coords = getCoordsFromSide ( side , pos , 0 ) ;
2016-01-07 18:20:59 +01:00
if ( coords ! = null ) {
EntityItem item = new EntityItem ( world , coords . getX ( ) + 0 . 5 , coords . getY ( ) + 0 . 5 , coords . getZ ( ) + 0 . 5 , stack ) ;
item . motionX = 0 ;
item . motionY = 0 ;
item . motionZ = 0 ;
world . spawnEntityInWorld ( item ) ;
2015-05-04 17:26:50 +02:00
}
}
2015-07-01 15:14:39 +02:00
public static void fillBucket ( FluidTank tank , ItemStack [ ] slots , int inputSlot , int outputSlot ) {
if ( slots [ inputSlot ] ! = null & & tank . getFluid ( ) ! = null ) {
ItemStack filled = FluidContainerRegistry . fillFluidContainer ( tank . getFluid ( ) , slots [ inputSlot ] . copy ( ) ) ;
if ( filled ! = null & & FluidContainerRegistry . isEmptyContainer ( slots [ inputSlot ] ) & & ( slots [ outputSlot ] = = null | | ( slots [ outputSlot ] . isItemEqual ( filled ) & & slots [ outputSlot ] . stackSize < slots [ outputSlot ] . getMaxStackSize ( ) ) ) ) {
int cap = FluidContainerRegistry . getContainerCapacity ( tank . getFluid ( ) , slots [ inputSlot ] ) ;
if ( cap > 0 & & cap < = tank . getFluidAmount ( ) ) {
2015-10-02 16:48:01 +02:00
if ( slots [ outputSlot ] = = null ) {
2015-07-27 08:26:13 +02:00
slots [ outputSlot ] = FluidContainerRegistry . fillFluidContainer ( tank . getFluid ( ) , slots [ inputSlot ] . copy ( ) ) ;
2015-10-02 16:48:01 +02:00
}
else {
slots [ outputSlot ] . stackSize + + ;
}
2015-07-01 15:14:39 +02:00
if ( slots [ outputSlot ] ! = null ) {
tank . drain ( cap , true ) ;
slots [ inputSlot ] . stackSize - - ;
2015-10-03 10:16:18 +02:00
if ( slots [ inputSlot ] . stackSize < = 0 ) {
slots [ inputSlot ] = null ;
}
2015-07-01 15:14:39 +02:00
}
}
}
}
}
public static void emptyBucket ( FluidTank tank , ItemStack [ ] slots , int inputSlot , int outputSlot ) {
emptyBucket ( tank , slots , inputSlot , outputSlot , null ) ;
}
public static void emptyBucket ( FluidTank tank , ItemStack [ ] slots , int inputSlot , int outputSlot , Fluid containedFluid ) {
if ( slots [ inputSlot ] ! = null & & FluidContainerRegistry . isFilledContainer ( slots [ inputSlot ] ) & & ( slots [ outputSlot ] = = null | | ( slots [ outputSlot ] . isItemEqual ( FluidContainerRegistry . drainFluidContainer ( slots [ inputSlot ] . copy ( ) ) ) & & slots [ outputSlot ] . stackSize < slots [ outputSlot ] . getMaxStackSize ( ) ) ) ) {
if ( containedFluid = = null | | FluidContainerRegistry . containsFluid ( slots [ inputSlot ] , new FluidStack ( containedFluid , 0 ) ) ) {
if ( ( tank . getFluid ( ) = = null | | FluidContainerRegistry . getFluidForFilledItem ( slots [ inputSlot ] ) . isFluidEqual ( tank . getFluid ( ) ) ) & & tank . getCapacity ( ) - tank . getFluidAmount ( ) > = FluidContainerRegistry . getContainerCapacity ( slots [ inputSlot ] ) ) {
2015-10-02 16:48:01 +02:00
if ( slots [ outputSlot ] = = null ) {
2015-07-27 08:26:13 +02:00
slots [ outputSlot ] = FluidContainerRegistry . drainFluidContainer ( slots [ inputSlot ] . copy ( ) ) ;
2015-10-02 16:48:01 +02:00
}
else {
slots [ outputSlot ] . stackSize + + ;
}
2015-07-01 15:14:39 +02:00
tank . fill ( FluidContainerRegistry . getFluidForFilledItem ( slots [ inputSlot ] ) , true ) ;
slots [ inputSlot ] . stackSize - - ;
2015-10-03 10:16:18 +02:00
if ( slots [ inputSlot ] . stackSize < = 0 ) {
slots [ inputSlot ] = null ;
}
2015-07-01 15:14:39 +02:00
}
}
}
}
2015-05-04 17:26:50 +02:00
2016-01-07 18:20:59 +01:00
public static EnumFacing getDirectionBySidesInOrder ( int side ) {
switch ( side ) {
case 0 :
return EnumFacing . UP ;
case 1 :
return EnumFacing . DOWN ;
case 2 :
return EnumFacing . NORTH ;
case 3 :
return EnumFacing . EAST ;
case 4 :
return EnumFacing . SOUTH ;
default :
return EnumFacing . WEST ;
2015-05-24 13:58:34 +02:00
}
2015-04-19 01:50:02 +02:00
}
2016-01-07 19:04:29 +01:00
public static EnumFacing getDirectionByPistonRotation ( int meta ) {
2016-01-08 13:31:58 +01:00
return EnumFacing . values ( ) [ meta ] ;
2016-01-07 19:04:29 +01:00
}
2016-01-08 13:31:58 +01:00
public static ArrayList < Material > getMaterialsAround ( World world , BlockPos pos ) {
2015-06-28 03:12:32 +02:00
ArrayList < Material > blocks = new ArrayList < Material > ( ) ;
2016-01-08 13:31:58 +01:00
blocks . add ( PosUtil . getMaterial ( pos . offset ( EnumFacing . NORTH ) , world ) ) ;
blocks . add ( PosUtil . getMaterial ( pos . offset ( EnumFacing . EAST ) , world ) ) ;
blocks . add ( PosUtil . getMaterial ( pos . offset ( EnumFacing . SOUTH ) , world ) ) ;
blocks . add ( PosUtil . getMaterial ( pos . offset ( EnumFacing . WEST ) , world ) ) ;
2015-06-28 03:12:32 +02:00
return blocks ;
}
2016-01-25 19:05:01 +01:00
//TODO make this work for the stupid new system
2016-01-31 03:31:39 +01:00
2016-02-01 17:49:55 +01:00
public static boolean addToInventory ( IInventory inventory , ArrayList < ItemStack > stacks , boolean actuallyDo , boolean shouldAlwaysWork ) {
return addToInventory ( inventory , stacks , EnumFacing . UP , actuallyDo , shouldAlwaysWork ) ;
}
public static boolean addToInventory ( IInventory inventory , ArrayList < ItemStack > stacks , EnumFacing side , boolean actuallyDo , boolean shouldAlwaysWork ) {
return addToInventory ( inventory , 0 , inventory . getSizeInventory ( ) , stacks , side , actuallyDo , shouldAlwaysWork ) ;
}
2015-10-05 11:27:53 +02:00
/ * *
* Add an ArrayList of ItemStacks to an Array of slots
2015-10-05 19:55:28 +02:00
*
2015-12-08 18:10:37 +01:00
* @param inventory The inventory to try to put the items into
2015-10-05 19:55:28 +02:00
* @param stacks The stacks to be put into the slots ( Items don ' t actually get removed from there ! )
2016-01-07 18:20:59 +01:00
* @param side The side to input from
2015-10-05 11:27:53 +02:00
* @param actuallyDo Do it or just test if it works ?
* @return Does it work ?
* /
2016-01-07 18:20:59 +01:00
public static boolean addToInventory ( IInventory inventory , int start , int end , ArrayList < ItemStack > stacks , EnumFacing side , boolean actuallyDo , boolean shouldAlwaysWork ) {
2015-12-08 18:10:37 +01:00
//Copy the slots if just testing to later load them again
ItemStack [ ] backupSlots = null ;
if ( ! actuallyDo ) {
backupSlots = new ItemStack [ inventory . getSizeInventory ( ) ] ;
for ( int i = 0 ; i < backupSlots . length ; i + + ) {
ItemStack stack = inventory . getStackInSlot ( i ) ;
if ( stack ! = null ) {
backupSlots [ i ] = stack . copy ( ) ;
2015-10-10 02:51:06 +02:00
}
2015-10-05 11:27:53 +02:00
}
}
2015-07-07 12:32:25 +02:00
int working = 0 ;
2015-10-05 11:27:53 +02:00
for ( ItemStack stackToPutIn : stacks ) {
2015-10-10 02:51:06 +02:00
for ( int i = start ; i < end ; i + + ) {
2016-01-07 18:20:59 +01:00
if ( shouldAlwaysWork | | ( ( ! ( inventory instanceof ISidedInventory ) | | ( ( ISidedInventory ) inventory ) . canInsertItem ( i , stackToPutIn , side ) ) & & inventory . isItemValidForSlot ( i , stackToPutIn ) ) ) {
2015-12-09 13:35:18 +01:00
ItemStack stackInQuestion = inventory . getStackInSlot ( i ) ;
if ( stackToPutIn ! = null & & ( stackInQuestion = = null | | ( stackInQuestion . isItemEqual ( stackToPutIn ) & & stackInQuestion . getMaxStackSize ( ) > = stackInQuestion . stackSize + stackToPutIn . stackSize ) ) ) {
if ( stackInQuestion = = null ) {
inventory . setInventorySlotContents ( i , stackToPutIn . copy ( ) ) ;
}
else {
stackInQuestion . stackSize + = stackToPutIn . stackSize ;
}
working + + ;
2015-10-05 11:27:53 +02:00
2015-12-09 13:35:18 +01:00
break ;
}
2015-07-07 12:32:25 +02:00
}
}
}
2015-12-08 18:10:37 +01:00
//Load the slots again
if ( ! actuallyDo & & backupSlots ! = null ) {
for ( int i = 0 ; i < backupSlots . length ; i + + ) {
inventory . setInventorySlotContents ( i , backupSlots [ i ] ) ;
}
}
2015-07-07 12:32:25 +02:00
return working > = stacks . size ( ) ;
}
2015-07-07 12:34:20 +02:00
public static int findFirstFilledSlot ( ItemStack [ ] slots ) {
2015-07-07 12:32:25 +02:00
for ( int i = 0 ; i < slots . length ; i + + ) {
if ( slots [ i ] ! = null ) {
return i ;
}
}
return 0 ;
}
2015-07-07 20:20:24 +02:00
2015-07-08 11:49:38 +02:00
public static MovingObjectPosition getNearestPositionWithAir ( World world , EntityPlayer player , int reach ) {
return getMovingObjectPosWithReachDistance ( world , player , reach , false , false , true ) ;
}
private static MovingObjectPosition getMovingObjectPosWithReachDistance ( World world , EntityPlayer player , double distance , boolean p1 , boolean p2 , boolean p3 ) {
2016-01-17 19:22:14 +01:00
float f = player . rotationPitch ;
float f1 = player . rotationYaw ;
double d0 = player . posX ;
double d1 = player . posY + ( double ) player . getEyeHeight ( ) ;
double d2 = player . posZ ;
2016-01-07 18:20:59 +01:00
Vec3 vec3 = new Vec3 ( d0 , d1 , d2 ) ;
2016-01-17 19:22:14 +01:00
float f2 = MathHelper . cos ( - f1 * 0 . 017453292F - ( float ) Math . PI ) ;
float f3 = MathHelper . sin ( - f1 * 0 . 017453292F - ( float ) Math . PI ) ;
float f4 = - MathHelper . cos ( - f * 0 . 017453292F ) ;
float f5 = MathHelper . sin ( - f * 0 . 017453292F ) ;
float f6 = f3 * f4 ;
float f7 = f2 * f4 ;
Vec3 vec31 = vec3 . addVector ( ( double ) f6 * distance , ( double ) f5 * distance , ( double ) f7 * distance ) ;
2016-01-07 18:20:59 +01:00
return world . rayTraceBlocks ( vec3 , vec31 , p1 , p2 , p3 ) ;
2015-07-07 20:20:24 +02:00
}
2015-07-27 08:26:13 +02:00
2015-10-03 10:19:40 +02:00
public static MovingObjectPosition getNearestBlockWithDefaultReachDistance ( World world , EntityPlayer player ) {
return getMovingObjectPosWithReachDistance ( world , player , player instanceof EntityPlayerMP ? ( ( EntityPlayerMP ) player ) . theItemInWorldManager . getBlockReachDistance ( ) : 5 . 0D , false , true , false ) ;
}
2015-07-27 08:26:13 +02:00
/ * *
* Harvests a Block by a Player
*
* @param world The World
* @param player The Player
* @return If the Block could be harvested normally ( so that it drops an item )
* /
2016-01-08 13:31:58 +01:00
public static boolean playerHarvestBlock ( World world , BlockPos pos , EntityPlayer player ) {
Block block = PosUtil . getBlock ( pos , world ) ;
IBlockState state = world . getBlockState ( pos ) ;
int meta = PosUtil . getMetadata ( pos , world ) ;
TileEntity tile = world . getTileEntity ( pos ) ;
2015-07-27 08:26:13 +02:00
//If the Block can be harvested or not
2016-01-07 19:47:53 +01:00
boolean canHarvest = block . canHarvestBlock ( world , pos , player ) ;
2015-07-27 08:26:13 +02:00
2015-11-22 23:02:59 +01:00
//Send Block Breaking Event
if ( player instanceof EntityPlayerMP ) {
2016-01-07 19:47:53 +01:00
int event = ForgeHooks . onBlockBreakEvent ( world , ( ( EntityPlayerMP ) player ) . theItemInWorldManager . getGameType ( ) , ( EntityPlayerMP ) player , pos ) ;
2016-01-07 18:20:59 +01:00
if ( event = = - 1 ) {
2015-11-22 23:02:59 +01:00
return false ;
}
}
2015-07-27 08:26:13 +02:00
if ( ! world . isRemote ) {
//Server-Side only, special cases
2016-01-08 08:18:27 +01:00
block . onBlockHarvested ( world , pos , state , player ) ;
2015-07-27 08:26:13 +02:00
}
else {
//Shows the Harvest Particles and plays the Block's Sound
2016-01-23 23:29:19 +01:00
world . playAuxSFX ( 2001 , pos , Block . getStateId ( state ) ) ;
2015-07-27 08:26:13 +02:00
}
//If the Block was actually "removed", meaning it will drop an Item
2016-01-07 19:47:53 +01:00
boolean removed = block . removedByPlayer ( world , pos , player , canHarvest ) ;
2015-07-27 08:26:13 +02:00
//Actually removes the Block from the World
if ( removed ) {
//Before the Block is destroyed, special cases
2016-01-08 08:18:27 +01:00
block . onBlockDestroyedByPlayer ( world , pos , state ) ;
2015-07-27 08:26:13 +02:00
if ( ! world . isRemote & & ! player . capabilities . isCreativeMode ) {
//Actually drops the Block's Items etc.
if ( canHarvest ) {
2016-01-08 13:31:58 +01:00
block . harvestBlock ( world , player , pos , state , tile ) ;
2015-07-27 08:26:13 +02:00
}
//Only drop XP when no Silk Touch is applied
if ( ! EnchantmentHelper . getSilkTouchModifier ( player ) ) {
//Drop XP depending on Fortune Level
2016-01-07 19:47:53 +01:00
block . dropXpOnBlockBreak ( world , pos , block . getExpDrop ( world , pos , EnchantmentHelper . getFortuneModifier ( player ) ) ) ;
2015-07-27 08:26:13 +02:00
}
}
}
if ( ! world . isRemote ) {
//Update the Client of a Block Change
2015-11-22 23:02:59 +01:00
if ( player instanceof EntityPlayerMP ) {
2016-01-07 19:47:53 +01:00
( ( EntityPlayerMP ) player ) . playerNetServerHandler . sendPacket ( new S23PacketBlockChange ( world , pos ) ) ;
2015-11-22 23:02:59 +01:00
}
2015-07-27 08:26:13 +02:00
}
else {
//Check the Server if a Block that changed on the Client really changed, if not, revert the change
2016-01-08 15:21:05 +01:00
Minecraft . getMinecraft ( ) . getNetHandler ( ) . addToSendQueue ( new C07PacketPlayerDigging ( C07PacketPlayerDigging . Action . STOP_DESTROY_BLOCK , pos , Minecraft . getMinecraft ( ) . objectMouseOver . sideHit ) ) ;
2015-07-27 08:26:13 +02:00
}
return removed ;
}
2015-04-19 01:50:02 +02:00
}