2015-08-29 14:33:25 +02:00
/ *
* This file ( " ItemDrill.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.items ;
2015-06-15 22:06:07 +02:00
2015-07-16 16:52:28 +02:00
import cofh.api.energy.IEnergyContainerItem ;
2015-06-15 22:06:07 +02:00
import com.google.common.collect.Multimap ;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions ;
2016-01-09 02:36:43 +01:00
import de.ellpeck.actuallyadditions.mod.blocks.metalists.TheColoredLampColors ;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.config.ConfigValues ;
import de.ellpeck.actuallyadditions.mod.inventory.GuiHandler ;
import de.ellpeck.actuallyadditions.mod.items.base.ItemEnergy ;
import de.ellpeck.actuallyadditions.mod.util.ItemUtil ;
import de.ellpeck.actuallyadditions.mod.util.ModUtil ;
2016-01-08 13:31:58 +01:00
import de.ellpeck.actuallyadditions.mod.util.PosUtil ;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.util.WorldUtil ;
2015-06-15 22:06:07 +02:00
import net.minecraft.block.Block ;
import net.minecraft.block.material.Material ;
2016-01-07 21:41:28 +01:00
import net.minecraft.block.state.IBlockState ;
2015-11-14 14:30:35 +01:00
import net.minecraft.creativetab.CreativeTabs ;
2015-07-07 14:32:10 +02:00
import net.minecraft.enchantment.Enchantment ;
2015-07-16 16:52:28 +02:00
import net.minecraft.entity.Entity ;
2015-06-15 22:06:07 +02:00
import net.minecraft.entity.EntityLivingBase ;
import net.minecraft.entity.SharedMonsterAttributes ;
import net.minecraft.entity.ai.attributes.AttributeModifier ;
import net.minecraft.entity.player.EntityPlayer ;
2015-06-21 02:28:49 +02:00
import net.minecraft.entity.player.InventoryPlayer ;
2015-07-15 05:33:02 +02:00
import net.minecraft.init.Blocks ;
2015-06-15 22:06:07 +02:00
import net.minecraft.item.EnumRarity ;
2015-11-14 14:30:35 +01:00
import net.minecraft.item.Item ;
2015-06-15 22:06:07 +02:00
import net.minecraft.item.ItemStack ;
import net.minecraft.nbt.NBTTagCompound ;
import net.minecraft.nbt.NBTTagList ;
2016-01-09 02:36:43 +01:00
import net.minecraft.util.* ;
2015-06-15 22:06:07 +02:00
import net.minecraft.world.World ;
2015-11-23 18:22:49 +01:00
import net.minecraftforge.common.ForgeHooks ;
2016-01-07 18:20:59 +01:00
import net.minecraftforge.fml.relauncher.Side ;
import net.minecraftforge.fml.relauncher.SideOnly ;
2015-06-15 22:06:07 +02:00
import java.util.HashSet ;
2015-11-14 14:30:35 +01:00
import java.util.List ;
2015-06-15 22:06:07 +02:00
import java.util.Set ;
@SuppressWarnings ( " unchecked " )
2015-10-01 23:20:31 +02:00
public class ItemDrill extends ItemEnergy {
2015-06-15 22:06:07 +02:00
2015-12-01 19:48:09 +01:00
private static final int ENERGY_USE = 100 ;
2015-12-30 19:34:15 +01:00
private static final int HARVEST_LEVEL = 4 ;
2015-11-14 14:30:35 +01:00
2015-12-03 20:15:07 +01:00
public ItemDrill ( String name ) {
super ( 500000 , 5000 , name ) ;
2015-11-14 14:30:35 +01:00
this . setMaxDamage ( 0 ) ;
2015-12-01 18:27:40 +01:00
this . setHasSubtypes ( true ) ;
2015-12-30 19:34:15 +01:00
//For Iguana Tweaks author
//
//You know what? It's bad, when you know
//There is already getHarvestLevel(), yo
//But then Iguana comes and fucks with you
//So that you need to use setHarvestLevel() too.
this . setHarvestLevel ( " shovel " , HARVEST_LEVEL ) ;
this . setHarvestLevel ( " pickaxe " , HARVEST_LEVEL ) ;
2015-11-14 14:30:35 +01:00
}
2015-06-21 02:28:49 +02:00
@Override
2015-07-17 05:11:49 +02:00
//Places Blocks if the Placing Upgrade is installed
2016-01-07 21:41:28 +01:00
public boolean onItemUse ( ItemStack stack , EntityPlayer player , World world , BlockPos pos , EnumFacing side , float hitX , float hitY , float hitZ ) {
2015-06-21 02:28:49 +02:00
ItemStack upgrade = this . getHasUpgradeAsStack ( stack , ItemDrillUpgrade . UpgradeType . PLACER ) ;
if ( upgrade ! = null ) {
int slot = ItemDrillUpgrade . getSlotToPlaceFrom ( upgrade ) ;
2015-06-22 18:09:00 +02:00
if ( slot > = 0 & & slot < InventoryPlayer . getHotbarSize ( ) ) {
2015-07-17 05:11:49 +02:00
ItemStack anEquip = player . inventory . getStackInSlot ( slot ) ;
2015-06-22 18:09:00 +02:00
if ( anEquip ! = null & & anEquip ! = stack ) {
ItemStack equip = anEquip . copy ( ) ;
2015-06-21 02:28:49 +02:00
if ( ! world . isRemote ) {
2015-07-17 05:11:49 +02:00
//tryPlaceItemIntoWorld could throw an Exception
2015-06-21 02:28:49 +02:00
try {
2015-07-17 05:11:49 +02:00
//Places the Block into the World
2016-01-07 21:41:28 +01:00
if ( equip . onItemUse ( player , world , pos , side , hitX , hitY , hitZ ) ) {
2015-10-02 16:48:01 +02:00
if ( ! player . capabilities . isCreativeMode ) {
2015-07-17 05:11:49 +02:00
player . inventory . setInventorySlotContents ( slot , equip . stackSize < = 0 ? null : equip . copy ( ) ) ;
2015-10-02 16:48:01 +02:00
}
2015-07-17 05:11:49 +02:00
//Synchronizes the Client
2015-06-22 18:09:00 +02:00
player . inventoryContainer . detectAndSendChanges ( ) ;
return true ;
}
2015-06-21 02:28:49 +02:00
}
2015-07-17 05:11:49 +02:00
//Notify the Player and log the Exception
2015-06-21 02:28:49 +02:00
catch ( Exception e ) {
player . addChatComponentMessage ( new ChatComponentText ( " Ouch! That really hurt! You must have done something wrong, don't do that again please! " ) ) ;
2016-01-07 21:41:28 +01:00
ModUtil . LOGGER . error ( " Player " + player . getName ( ) + " who should place a Block using a Drill at " + player . posX + " , " + player . posY + " , " + player . posZ + " in World " + world . provider . getDimensionId ( ) + " threw an Exception! Don't let that happen again! " ) ;
2015-06-21 02:28:49 +02:00
}
}
2015-10-02 16:48:01 +02:00
else {
return true ;
}
2015-06-21 02:28:49 +02:00
}
}
}
return false ;
}
2015-10-03 10:19:40 +02:00
/ * *
* Checks if a certain Upgrade is installed and returns it as an ItemStack
*
* @param stack The Drill
* @param upgrade The Upgrade to be checked
* @return The Upgrade , if it ' s installed
* /
public ItemStack getHasUpgradeAsStack ( ItemStack stack , ItemDrillUpgrade . UpgradeType upgrade ) {
NBTTagCompound compound = stack . getTagCompound ( ) ;
if ( compound = = null ) {
return null ;
}
ItemStack [ ] slots = this . getSlotsFromNBT ( stack ) ;
if ( slots ! = null & & slots . length > 0 ) {
for ( ItemStack slotStack : slots ) {
if ( slotStack ! = null & & slotStack . getItem ( ) instanceof ItemDrillUpgrade ) {
if ( ( ( ItemDrillUpgrade ) slotStack . getItem ( ) ) . type = = upgrade ) {
return slotStack ;
}
}
}
}
return null ;
}
/ * *
* Gets all of the Slots from NBT
*
* @param stack The Drill
* @return All of the Slots
* /
public ItemStack [ ] getSlotsFromNBT ( ItemStack stack ) {
NBTTagCompound compound = stack . getTagCompound ( ) ;
if ( compound = = null ) {
return null ;
}
int slotAmount = compound . getInteger ( " SlotAmount " ) ;
ItemStack [ ] slots = new ItemStack [ slotAmount ] ;
if ( slots . length > 0 ) {
NBTTagList tagList = compound . getTagList ( " Items " , 10 ) ;
for ( int i = 0 ; i < tagList . tagCount ( ) ; i + + ) {
NBTTagCompound tagCompound = tagList . getCompoundTagAt ( i ) ;
byte slotIndex = tagCompound . getByte ( " Slot " ) ;
if ( slotIndex > = 0 & & slotIndex < slots . length ) {
slots [ slotIndex ] = ItemStack . loadItemStackFromNBT ( tagCompound ) ;
}
}
}
return slots ;
}
@Override
public ItemStack onItemRightClick ( ItemStack stack , World world , EntityPlayer player ) {
if ( ! world . isRemote & & player . isSneaking ( ) & & stack = = player . getCurrentEquippedItem ( ) ) {
player . openGui ( ActuallyAdditions . instance , GuiHandler . GuiTypes . DRILL . ordinal ( ) , world , ( int ) player . posX , ( int ) player . posY , ( int ) player . posZ ) ;
}
return stack ;
}
2015-12-01 19:48:09 +01:00
@Override
public int getMetadata ( int damage ) {
return damage ;
}
2015-10-03 10:19:40 +02:00
@Override
public boolean hitEntity ( ItemStack stack , EntityLivingBase entity1 , EntityLivingBase entity2 ) {
int use = this . getEnergyUsePerBlock ( stack ) ;
if ( this . getEnergyStored ( stack ) > = use ) {
this . extractEnergy ( stack , use , false ) ;
}
return true ;
}
2015-07-17 05:11:49 +02:00
//Checks for Energy Containers in the Upgrade Slots and charges the Drill from them
@Override
2015-07-16 16:52:28 +02:00
public void onUpdate ( ItemStack stack , World world , Entity entity , int par4 , boolean par5 ) {
ItemStack [ ] slots = this . getSlotsFromNBT ( stack ) ;
if ( slots ! = null & & slots . length > 0 ) {
for ( ItemStack slotStack : slots ) {
if ( slotStack ! = null & & slotStack . getItem ( ) instanceof IEnergyContainerItem ) {
if ( this . getEnergyStored ( stack ) < this . getMaxEnergyStored ( stack ) ) {
int energy = ( ( IEnergyContainerItem ) slotStack . getItem ( ) ) . getEnergyStored ( slotStack ) ;
if ( energy > 0 ) {
2015-07-17 05:11:49 +02:00
//Charge the Drill and discharge the "Upgrade"
2015-07-16 17:42:19 +02:00
int toReceive = ( ( IEnergyContainerItem ) stack . getItem ( ) ) . receiveEnergy ( stack , energy , true ) ;
int actualReceive = ( ( IEnergyContainerItem ) slotStack . getItem ( ) ) . extractEnergy ( slotStack , toReceive , false ) ;
( ( IEnergyContainerItem ) stack . getItem ( ) ) . receiveEnergy ( stack , actualReceive , false ) ;
2015-07-16 16:52:28 +02:00
}
}
}
}
}
}
2015-10-03 10:19:40 +02:00
@Override
public EnumRarity getRarity ( ItemStack stack ) {
2016-01-07 21:41:28 +01:00
return EnumRarity . EPIC ;
2015-10-03 10:19:40 +02:00
}
@Override
public Multimap getAttributeModifiers ( ItemStack stack ) {
Multimap map = super . getAttributeModifiers ( stack ) ;
2016-01-07 21:41:28 +01:00
map . put ( SharedMonsterAttributes . attackDamage . getAttributeUnlocalizedName ( ) , new AttributeModifier ( itemModifierUUID , " Drill Modifier " , this . getEnergyStored ( stack ) > = ENERGY_USE ? 8 . 0F : 0 . 1F , 0 ) ) ;
2015-10-03 10:19:40 +02:00
return map ;
}
@Override
2016-01-07 21:41:28 +01:00
public float getDigSpeed ( ItemStack stack , IBlockState state ) {
return this . getEnergyStored ( stack ) > = this . getEnergyUsePerBlock ( stack ) ? ( this . hasExtraWhitelist ( state . getBlock ( ) ) | | state . getBlock ( ) . getHarvestTool ( state ) = = null | | state . getBlock ( ) . getHarvestTool ( state ) . isEmpty ( ) | | this . getToolClasses ( stack ) . contains ( state . getBlock ( ) . getHarvestTool ( state ) ) ? this . getEfficiencyFromUpgrade ( stack ) : 1 . 0F ) : 0 . 1F ;
2015-10-03 10:19:40 +02:00
}
@Override
2016-01-07 21:41:28 +01:00
public boolean onBlockStartBreak ( ItemStack stack , BlockPos pos , EntityPlayer player ) {
2015-11-21 10:58:28 +01:00
boolean toReturn = false ;
2015-10-03 10:19:40 +02:00
int use = this . getEnergyUsePerBlock ( stack ) ;
if ( this . getEnergyStored ( stack ) > = use ) {
//Enchants the Drill depending on the Upgrades it has
if ( this . getHasUpgrade ( stack , ItemDrillUpgrade . UpgradeType . SILK_TOUCH ) ) {
ItemUtil . addEnchantment ( stack , Enchantment . silkTouch , 1 ) ;
}
else {
if ( this . getHasUpgrade ( stack , ItemDrillUpgrade . UpgradeType . FORTUNE ) ) {
ItemUtil . addEnchantment ( stack , Enchantment . fortune , this . getHasUpgrade ( stack , ItemDrillUpgrade . UpgradeType . FORTUNE_II ) ? 3 : 1 ) ;
}
}
//Breaks the Blocks
if ( ! player . isSneaking ( ) & & this . getHasUpgrade ( stack , ItemDrillUpgrade . UpgradeType . THREE_BY_THREE ) ) {
if ( this . getHasUpgrade ( stack , ItemDrillUpgrade . UpgradeType . FIVE_BY_FIVE ) ) {
2016-01-07 21:41:28 +01:00
toReturn = this . breakBlocks ( stack , 2 , player . worldObj , pos , player ) ;
2015-10-02 16:48:01 +02:00
}
else {
2016-01-07 21:41:28 +01:00
toReturn = this . breakBlocks ( stack , 1 , player . worldObj , pos , player ) ;
2015-10-02 16:48:01 +02:00
}
}
else {
2016-01-07 21:41:28 +01:00
toReturn = this . breakBlocks ( stack , 0 , player . worldObj , pos , player ) ;
2015-06-15 22:06:07 +02:00
}
2015-10-03 10:19:40 +02:00
//Removes Enchantments added above
ItemUtil . removeEnchantment ( stack , Enchantment . silkTouch ) ;
ItemUtil . removeEnchantment ( stack , Enchantment . fortune ) ;
2015-06-15 22:06:07 +02:00
}
2015-10-03 10:19:40 +02:00
return toReturn ;
}
@Override
public boolean canHarvestBlock ( Block block , ItemStack stack ) {
2015-11-28 19:02:01 +01:00
int harvestLevel = this . getHarvestLevel ( stack , " " ) ;
return this . getEnergyStored ( stack ) > = this . getEnergyUsePerBlock ( stack ) & & ( this . hasExtraWhitelist ( block ) | | block . getMaterial ( ) . isToolNotRequired ( ) | | ( block = = Blocks . snow_layer | | block = = Blocks . snow | | ( block = = Blocks . obsidian ? harvestLevel > = 3 : ( block ! = Blocks . diamond_block & & block ! = Blocks . diamond_ore ? ( block ! = Blocks . emerald_ore & & block ! = Blocks . emerald_block ? ( block ! = Blocks . gold_block & & block ! = Blocks . gold_ore ? ( block ! = Blocks . iron_block & & block ! = Blocks . iron_ore ? ( block ! = Blocks . lapis_block & & block ! = Blocks . lapis_ore ? ( block ! = Blocks . redstone_ore & & block ! = Blocks . lit_redstone_ore ? ( block . getMaterial ( ) = = Material . rock | | ( block . getMaterial ( ) = = Material . iron | | block . getMaterial ( ) = = Material . anvil ) ) : harvestLevel > = 2 ) : harvestLevel > = 1 ) : harvestLevel > = 1 ) : harvestLevel > = 2 ) : harvestLevel > = 2 ) : harvestLevel > = 2 ) ) ) ) ;
2015-10-03 10:19:40 +02:00
}
@Override
public Set < String > getToolClasses ( ItemStack stack ) {
HashSet < String > hashSet = new HashSet < String > ( ) ;
hashSet . add ( " pickaxe " ) ;
hashSet . add ( " shovel " ) ;
return hashSet ;
}
@Override
public int getHarvestLevel ( ItemStack stack , String toolClass ) {
2015-12-30 19:34:15 +01:00
return HARVEST_LEVEL ;
2015-06-18 13:14:57 +02:00
}
2015-06-15 22:06:07 +02:00
2015-07-17 05:11:49 +02:00
/ * *
* Gets the Energy that is used per Block broken
2015-10-02 16:48:01 +02:00
*
2015-07-17 05:11:49 +02:00
* @param stack The Drill
* @return The Energy use per Block
* /
2015-06-18 13:14:57 +02:00
public int getEnergyUsePerBlock ( ItemStack stack ) {
2015-11-28 19:02:01 +01:00
int use = ENERGY_USE ;
2015-06-21 02:28:49 +02:00
2015-07-17 05:11:49 +02:00
//Speed
2015-06-21 02:28:49 +02:00
if ( this . getHasUpgrade ( stack , ItemDrillUpgrade . UpgradeType . SPEED ) ) {
2015-11-28 19:02:01 +01:00
use + = 50 ;
2015-06-21 02:28:49 +02:00
if ( this . getHasUpgrade ( stack , ItemDrillUpgrade . UpgradeType . SPEED_II ) ) {
2015-11-28 19:02:01 +01:00
use + = 75 ;
2015-10-02 16:48:01 +02:00
if ( this . getHasUpgrade ( stack , ItemDrillUpgrade . UpgradeType . SPEED_III ) ) {
2015-11-28 19:02:01 +01:00
use + = 175 ;
2015-10-02 16:48:01 +02:00
}
2015-06-15 22:06:07 +02:00
}
}
2015-07-17 05:11:49 +02:00
//Silk Touch
2015-10-02 16:48:01 +02:00
if ( this . getHasUpgrade ( stack , ItemDrillUpgrade . UpgradeType . SILK_TOUCH ) ) {
2015-11-28 19:02:01 +01:00
use + = 100 ;
2015-10-02 16:48:01 +02:00
}
2015-06-21 02:28:49 +02:00
2015-07-17 05:11:49 +02:00
//Fortune
2015-06-21 02:28:49 +02:00
if ( this . getHasUpgrade ( stack , ItemDrillUpgrade . UpgradeType . FORTUNE ) ) {
2015-11-28 19:02:01 +01:00
use + = 40 ;
2015-10-02 16:48:01 +02:00
if ( this . getHasUpgrade ( stack , ItemDrillUpgrade . UpgradeType . FORTUNE_II ) ) {
2015-11-28 19:02:01 +01:00
use + = 80 ;
2015-10-02 16:48:01 +02:00
}
2015-06-15 22:06:07 +02:00
}
2015-07-17 05:11:49 +02:00
//Size
2015-06-21 02:28:49 +02:00
if ( this . getHasUpgrade ( stack , ItemDrillUpgrade . UpgradeType . THREE_BY_THREE ) ) {
2015-11-28 19:02:01 +01:00
use + = 10 ;
2015-10-02 16:48:01 +02:00
if ( this . getHasUpgrade ( stack , ItemDrillUpgrade . UpgradeType . FIVE_BY_FIVE ) ) {
2015-11-28 19:02:01 +01:00
use + = 30 ;
2015-10-02 16:48:01 +02:00
}
2015-06-15 22:06:07 +02:00
}
2015-06-21 02:28:49 +02:00
return use ;
2015-06-15 22:06:07 +02:00
}
2015-07-17 05:11:49 +02:00
/ * *
* Checks if a certain Upgrade is applied
2015-10-02 16:48:01 +02:00
*
* @param stack The Drill
2015-07-17 05:11:49 +02:00
* @param upgrade The Upgrade to be checked
* @return Is the Upgrade applied ?
* /
2015-06-15 22:06:07 +02:00
public boolean getHasUpgrade ( ItemStack stack , ItemDrillUpgrade . UpgradeType upgrade ) {
2015-06-21 02:28:49 +02:00
return this . getHasUpgradeAsStack ( stack , upgrade ) ! = null ;
}
2015-06-15 22:06:07 +02:00
2016-02-01 17:49:55 +01:00
@Override
protected void registerRendering ( ) {
ResourceLocation [ ] resLocs = new ResourceLocation [ 16 ] ;
for ( int i = 0 ; i < 16 ; i + + ) {
String name = this . getBaseName ( ) + TheColoredLampColors . values ( ) [ i ] . name ;
resLocs [ i ] = new ResourceLocation ( ModUtil . MOD_ID_LOWER , name ) ;
ActuallyAdditions . proxy . addRenderRegister ( new ItemStack ( this , 1 , i ) , new ResourceLocation ( ModUtil . MOD_ID_LOWER , name ) ) ;
}
ActuallyAdditions . proxy . addRenderVariant ( this , resLocs ) ;
}
2015-12-01 19:48:09 +01:00
@Override
@SuppressWarnings ( " unchecked " )
@SideOnly ( Side . CLIENT )
public void getSubItems ( Item item , CreativeTabs tabs , List list ) {
2016-01-07 21:41:28 +01:00
for ( int i = 0 ; i < 16 ; i + + ) {
2015-12-01 19:48:09 +01:00
this . addDrillStack ( list , i ) ;
}
}
private void addDrillStack ( List list , int meta ) {
ItemStack stackFull = new ItemStack ( this , 1 , meta ) ;
this . setEnergy ( stackFull , this . getMaxEnergyStored ( stackFull ) ) ;
list . add ( stackFull ) ;
ItemStack stackEmpty = new ItemStack ( this , 1 , meta ) ;
this . setEnergy ( stackEmpty , 0 ) ;
list . add ( stackEmpty ) ;
}
2015-07-17 05:11:49 +02:00
/ * *
2015-10-03 10:19:40 +02:00
* Gets the Mining Speed of the Drill
2015-10-02 16:48:01 +02:00
*
2015-10-03 10:19:40 +02:00
* @param stack The Drill
* @return The Mining Speed depending on the Speed Upgrades
2015-07-17 05:11:49 +02:00
* /
2015-10-03 10:19:40 +02:00
public float getEfficiencyFromUpgrade ( ItemStack stack ) {
2015-11-28 19:02:01 +01:00
float efficiency = 8 . 0F ;
2015-10-03 10:19:40 +02:00
if ( this . getHasUpgrade ( stack , ItemDrillUpgrade . UpgradeType . SPEED ) ) {
if ( this . getHasUpgrade ( stack , ItemDrillUpgrade . UpgradeType . SPEED_II ) ) {
if ( this . getHasUpgrade ( stack , ItemDrillUpgrade . UpgradeType . SPEED_III ) ) {
efficiency + = 37 . 0F ;
}
else {
efficiency + = 25 . 0F ;
2015-06-15 22:06:07 +02:00
}
}
2015-10-03 10:19:40 +02:00
else {
efficiency + = 8 . 0F ;
}
2015-06-15 22:06:07 +02:00
}
2015-10-03 10:19:40 +02:00
return efficiency ;
2015-06-15 22:06:07 +02:00
}
2015-07-17 05:11:49 +02:00
/ * *
* Writes all of the Slots to NBT
2015-10-02 16:48:01 +02:00
*
2015-07-17 05:11:49 +02:00
* @param slots The Slots
* @param stack The Drill
* /
2015-06-15 22:06:07 +02:00
public void writeSlotsToNBT ( ItemStack [ ] slots , ItemStack stack ) {
NBTTagCompound compound = stack . getTagCompound ( ) ;
2015-10-03 10:16:18 +02:00
if ( compound = = null ) {
compound = new NBTTagCompound ( ) ;
}
2015-06-15 22:06:07 +02:00
if ( slots ! = null & & slots . length > 0 ) {
compound . setInteger ( " SlotAmount " , slots . length ) ;
NBTTagList tagList = new NBTTagList ( ) ;
for ( int currentIndex = 0 ; currentIndex < slots . length ; currentIndex + + ) {
if ( slots [ currentIndex ] ! = null ) {
NBTTagCompound tagCompound = new NBTTagCompound ( ) ;
tagCompound . setByte ( " Slot " , ( byte ) currentIndex ) ;
slots [ currentIndex ] . writeToNBT ( tagCompound ) ;
tagList . appendTag ( tagCompound ) ;
}
}
compound . setTag ( " Items " , tagList ) ;
}
stack . setTagCompound ( compound ) ;
}
2015-07-17 05:11:49 +02:00
/ * *
* Breaks Blocks in a certain Radius
* Has to be called on both Server and Client
2015-10-02 16:48:01 +02:00
*
* @param stack The Drill
2015-07-17 05:11:49 +02:00
* @param radius The Radius to break Blocks in ( 0 means only 1 Block will be broken ! )
2015-10-02 16:48:01 +02:00
* @param world The World
2015-07-17 05:11:49 +02:00
* @param player The Player who breaks the Blocks
* /
2016-01-07 21:41:28 +01:00
public boolean breakBlocks ( ItemStack stack , int radius , World world , BlockPos aPos , EntityPlayer player ) {
2015-06-15 22:06:07 +02:00
int xRange = radius ;
int yRange = radius ;
int zRange = 0 ;
2015-07-17 05:11:49 +02:00
//Block hit
2015-07-08 11:49:38 +02:00
MovingObjectPosition pos = WorldUtil . getNearestBlockWithDefaultReachDistance ( world , player ) ;
2015-10-03 10:16:18 +02:00
if ( pos = = null ) {
return false ;
}
2015-07-07 20:20:24 +02:00
2015-07-20 22:06:08 +02:00
//Corrects Blocks to hit depending on Side of original Block hit
2016-01-07 21:41:28 +01:00
int side = pos . sideHit . ordinal ( ) ;
2015-07-20 22:06:08 +02:00
if ( side = = 0 | | side = = 1 ) {
zRange = radius ;
yRange = 0 ;
}
if ( side = = 4 | | side = = 5 ) {
xRange = 0 ;
zRange = radius ;
}
2015-06-15 22:06:07 +02:00
2015-07-20 22:06:08 +02:00
//Not defined later because main Block is getting broken below
2016-01-08 13:31:58 +01:00
float mainHardness = PosUtil . getBlock ( aPos , world ) . getBlockHardness ( world , aPos ) ;
2015-07-15 05:33:02 +02:00
2015-07-20 22:06:08 +02:00
//Break Middle Block first
int use = this . getEnergyUsePerBlock ( stack ) ;
if ( this . getEnergyStored ( stack ) > = use ) {
2016-01-08 13:31:58 +01:00
if ( ! this . tryHarvestBlock ( world , aPos , false , stack , player , use ) ) {
2015-10-03 10:16:18 +02:00
return false ;
}
2015-07-20 22:06:08 +02:00
}
2015-10-02 16:48:01 +02:00
else {
return false ;
}
2015-07-20 22:06:08 +02:00
//Break Blocks around
2015-11-08 23:16:41 +01:00
if ( radius > 0 & & mainHardness > = 0 . 2F ) {
2016-01-07 21:41:28 +01:00
for ( int xPos = aPos . getX ( ) - xRange ; xPos < = aPos . getX ( ) + xRange ; xPos + + ) {
for ( int yPos = aPos . getY ( ) - yRange ; yPos < = aPos . getY ( ) + yRange ; yPos + + ) {
for ( int zPos = aPos . getZ ( ) - zRange ; zPos < = aPos . getZ ( ) + zRange ; zPos + + ) {
if ( ! ( aPos . getX ( ) = = xPos & & aPos . getY ( ) = = yPos & & aPos . getZ ( ) = = zPos ) ) {
2015-07-20 22:06:08 +02:00
if ( this . getEnergyStored ( stack ) > = use ) {
//Only break Blocks around that are (about) as hard or softer
2016-01-08 13:31:58 +01:00
BlockPos thePos = new BlockPos ( xPos , yPos , zPos ) ;
if ( PosUtil . getBlock ( thePos , world ) . getBlockHardness ( world , thePos ) < = mainHardness + 5 . 0F ) {
2016-01-07 21:41:28 +01:00
this . tryHarvestBlock ( world , thePos , true , stack , player , use ) ;
2015-07-09 14:20:37 +02:00
}
2015-06-15 22:06:07 +02:00
}
2015-10-02 16:48:01 +02:00
else {
return false ;
}
2015-06-15 22:06:07 +02:00
}
}
}
}
}
2015-07-20 22:06:08 +02:00
return true ;
2015-07-09 00:43:54 +02:00
}
2015-07-17 05:11:49 +02:00
/ * *
* Tries to harvest a certain Block
* Breaks the Block , drops Particles etc .
* Has to be called on both Server and Client
2015-10-02 16:48:01 +02:00
*
* @param world The World
2015-07-17 05:11:49 +02:00
* @param isExtra If the Block is the Block that was looked at when breaking or an additional Block
2015-10-02 16:48:01 +02:00
* @param stack The Drill
* @param player The Player breaking the Blocks
* @param use The Energy that should be extracted per Block
2015-07-17 05:11:49 +02:00
* /
2016-01-08 13:31:58 +01:00
private boolean tryHarvestBlock ( World world , BlockPos pos , boolean isExtra , ItemStack stack , EntityPlayer player , int use ) {
Block block = PosUtil . getBlock ( pos , world ) ;
2016-01-07 21:41:28 +01:00
float hardness = block . getBlockHardness ( world , pos ) ;
2016-01-08 13:31:58 +01:00
boolean canHarvest = ( ForgeHooks . canHarvestBlock ( block , player , world , pos ) | | this . canHarvestBlock ( block , stack ) ) & & ( ! isExtra | | this . getDigSpeed ( stack , world . getBlockState ( pos ) ) > 1 . 0F ) ;
if ( hardness > = 0 . 0F & & ( ! isExtra | | ( canHarvest & & ! block . hasTileEntity ( world . getBlockState ( pos ) ) ) ) ) {
2015-07-09 14:20:37 +02:00
this . extractEnergy ( stack , use , false ) ;
2015-07-27 08:26:13 +02:00
//Break the Block
2016-01-07 21:41:28 +01:00
return WorldUtil . playerHarvestBlock ( world , pos , player ) ;
2015-07-09 00:43:54 +02:00
}
2015-07-20 22:06:08 +02:00
return false ;
2015-06-15 22:06:07 +02:00
}
2015-09-13 16:41:49 +02:00
private boolean hasExtraWhitelist ( Block block ) {
2016-01-07 21:41:28 +01:00
String name = block . getRegistryName ( ) ;
2015-09-13 16:41:49 +02:00
if ( name ! = null ) {
for ( String list : ConfigValues . drillExtraminingWhitelist ) {
2015-10-03 10:16:18 +02:00
if ( list . equals ( name ) ) {
return true ;
}
2015-09-13 16:41:49 +02:00
}
}
return false ;
}
2015-06-15 22:06:07 +02:00
}