2020-09-09 16:49:01 +02:00
package de.ellpeck.actuallyadditions.common.items ;
2015-06-15 22:06:07 +02:00
import com.google.common.collect.Multimap ;
2020-09-09 16:49:01 +02:00
import de.ellpeck.actuallyadditions.common.ActuallyAdditions ;
import de.ellpeck.actuallyadditions.common.blocks.metalists.TheColoredLampColors ;
import de.ellpeck.actuallyadditions.common.config.values.ConfigStringListValues ;
import de.ellpeck.actuallyadditions.common.inventory.ContainerDrill ;
import de.ellpeck.actuallyadditions.common.items.base.ItemEnergy ;
import de.ellpeck.actuallyadditions.common.tile.TileEntityInventoryBase ;
import de.ellpeck.actuallyadditions.common.util.ItemStackHandlerAA ;
import de.ellpeck.actuallyadditions.common.util.ItemUtil ;
import de.ellpeck.actuallyadditions.common.util.StackUtil ;
import de.ellpeck.actuallyadditions.common.util.WorldUtil ;
2015-06-15 22:06:07 +02:00
import net.minecraft.block.Block ;
2020-10-31 21:42:32 +01:00
import net.minecraft.block.BlockState ;
import net.minecraft.block.Blocks ;
2015-06-15 22:06:07 +02:00
import net.minecraft.block.material.Material ;
2020-10-31 21:42:32 +01:00
import net.minecraft.enchantment.Enchantments ;
import net.minecraft.entity.LivingEntity ;
2015-06-15 22:06:07 +02:00
import net.minecraft.entity.SharedMonsterAttributes ;
import net.minecraft.entity.ai.attributes.AttributeModifier ;
2020-10-31 21:42:32 +01:00
import net.minecraft.entity.player.PlayerEntity ;
import net.minecraft.entity.player.PlayerInventory ;
import net.minecraft.inventory.EquipmentSlotType ;
2015-06-15 22:06:07 +02:00
import net.minecraft.item.ItemStack ;
2020-10-31 21:42:32 +01:00
import net.minecraft.item.ItemUseContext ;
import net.minecraft.nbt.CompoundNBT ;
import net.minecraft.util.* ;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.math.BlockPos ;
2020-10-31 21:42:32 +01:00
import net.minecraft.util.math.BlockRayTraceResult ;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.math.RayTraceResult ;
2015-06-15 22:06:07 +02:00
import net.minecraft.world.World ;
2018-01-31 06:07:29 +01:00
import net.minecraftforge.common.ForgeHooks ;
2020-10-31 21:42:32 +01:00
import net.minecraftforge.common.ToolType ;
2018-06-23 01:39:30 +02:00
import net.minecraftforge.items.IItemHandler ;
import net.minecraftforge.items.IItemHandlerModifiable ;
2015-06-15 22:06:07 +02:00
2020-10-31 21:42:32 +01:00
import java.util.List ;
2019-05-02 09:10:29 +02:00
public class ItemDrill extends ItemEnergy {
2015-06-15 22:06:07 +02:00
2016-07-02 17:15:53 +02:00
public static final int HARVEST_LEVEL = 4 ;
2016-07-07 17:59:45 +02:00
private static final int ENERGY_USE = 100 ;
2015-11-14 14:30:35 +01:00
2020-10-31 21:42:32 +01:00
public ItemDrill ( Properties properties , String name ) {
super ( properties . maxDamage ( 0 ) . addToolType ( ToolType . SHOVEL , HARVEST_LEVEL ) . addToolType ( ToolType . PICKAXE , HARVEST_LEVEL ) , 250000 , 1000 , name ) ;
2015-11-14 14:30:35 +01:00
}
2016-06-17 23:50:38 +02:00
/ * *
* Gets all of the Slots from NBT
*
* @param stack The Drill
* /
2019-05-02 09:10:29 +02:00
public static void loadSlotsFromNBT ( IItemHandlerModifiable slots , ItemStack stack ) {
2020-10-31 21:42:32 +01:00
CompoundNBT compound = stack . getOrCreateTag ( ) ;
TileEntityInventoryBase . loadSlots ( slots , compound ) ;
2016-06-17 23:50:38 +02:00
}
/ * *
* Writes all of the Slots to NBT
*
* @param slots The Slots
* @param stack The Drill
* /
2019-05-02 09:10:29 +02:00
public static void writeSlotsToNBT ( IItemHandler slots , ItemStack stack ) {
2020-10-31 21:42:32 +01:00
CompoundNBT compound = stack . getOrCreateTag ( ) ;
2016-06-17 23:50:38 +02:00
TileEntityInventoryBase . saveSlots ( slots , compound ) ;
2020-10-31 21:42:32 +01:00
stack . setTag ( compound ) ;
2016-06-17 23:50:38 +02:00
}
2016-05-29 23:49:35 +02:00
2015-06-21 02:28:49 +02:00
@Override
2020-10-31 21:42:32 +01:00
public ActionResultType onItemUse ( ItemUseContext context ) {
ItemStack stack = context . getPlayer ( ) . getHeldItem ( context . getHand ( ) ) ;
2015-06-21 02:28:49 +02:00
ItemStack upgrade = this . getHasUpgradeAsStack ( stack , ItemDrillUpgrade . UpgradeType . PLACER ) ;
2019-05-02 09:10:29 +02:00
if ( StackUtil . isValid ( upgrade ) ) {
2015-06-21 02:28:49 +02:00
int slot = ItemDrillUpgrade . getSlotToPlaceFrom ( upgrade ) ;
2020-10-31 21:42:32 +01:00
if ( slot > = 0 & & slot < PlayerInventory . getHotbarSize ( ) ) {
ItemStack equip = context . getPlayer ( ) . inventory . getStackInSlot ( slot ) ;
2019-05-02 09:10:29 +02:00
if ( StackUtil . isValid ( equip ) & & equip ! = stack ) {
2016-11-22 19:52:33 +01:00
ItemStack toPlaceStack = equip . copy ( ) ;
2020-10-31 21:42:32 +01:00
WorldUtil . setHandItemWithoutAnnoyingSound ( context . getPlayer ( ) , context . getHand ( ) , toPlaceStack ) ;
2016-11-22 19:52:33 +01:00
//tryPlaceItemIntoWorld could throw an Exception
2019-05-02 09:10:29 +02:00
try {
2016-11-22 19:52:33 +01:00
//Places the Block into the World
2020-10-31 21:42:32 +01:00
if ( toPlaceStack . onItemUse ( context ) ! = ActionResultType . FAIL ) {
if ( ! context . getPlayer ( ) . isCreative ( ) ) {
WorldUtil . setHandItemWithoutAnnoyingSound ( context . getPlayer ( ) , context . getHand ( ) , toPlaceStack . copy ( ) ) ;
2015-06-22 18:09:00 +02:00
}
2015-06-21 02:28:49 +02:00
}
}
2016-11-22 19:52:33 +01:00
//Notify the Player and log the Exception
2019-05-02 09:10:29 +02:00
catch ( Exception e ) {
2020-10-31 21:42:32 +01:00
ActuallyAdditions . LOGGER . error ( " Player " + context . getPlayer ( ) . getName ( ) + " who should place a Block using a Drill at " + context . getPlayer ( ) . getPosX ( ) + " , " + context . getPlayer ( ) . getPosY ( ) + " , " + context . getPlayer ( ) . getPosY ( ) + " in World " + context . getWorld ( ) . getDimension ( ) + " threw an Exception! Don't let that happen again! " ) ;
2015-10-02 16:48:01 +02:00
}
2016-11-22 19:52:33 +01:00
2020-10-31 21:42:32 +01:00
context . getPlayer ( ) . inventory . setInventorySlotContents ( slot , context . getPlayer ( ) . getHeldItem ( context . getHand ( ) ) ) ;
WorldUtil . setHandItemWithoutAnnoyingSound ( context . getPlayer ( ) , context . getHand ( ) , stack ) ;
2016-11-22 19:52:33 +01:00
2020-10-31 21:42:32 +01:00
return ActionResultType . SUCCESS ;
2015-06-21 02:28:49 +02:00
}
}
}
2020-10-31 21:42:32 +01:00
return ActionResultType . FAIL ;
2015-06-21 02:28:49 +02:00
}
2020-10-31 21:42:32 +01:00
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
* /
2019-05-02 09:10:29 +02:00
public ItemStack getHasUpgradeAsStack ( ItemStack stack , ItemDrillUpgrade . UpgradeType upgrade ) {
2020-10-31 21:42:32 +01:00
CompoundNBT compound = stack . getOrCreateTag ( ) ;
2019-05-02 09:10:29 +02:00
if ( compound = = null ) { return StackUtil . getEmpty ( ) ; }
2015-10-03 10:19:40 +02:00
2018-06-23 01:39:30 +02:00
ItemStackHandlerAA inv = new ItemStackHandlerAA ( ContainerDrill . SLOT_AMOUNT ) ;
2016-11-27 11:37:55 +01:00
loadSlotsFromNBT ( inv , stack ) ;
2019-05-02 09:10:29 +02:00
for ( int i = 0 ; i < inv . getSlots ( ) ; i + + ) {
2016-11-27 11:37:55 +01:00
ItemStack slotStack = inv . getStackInSlot ( i ) ;
2019-05-02 09:10:29 +02:00
if ( StackUtil . isValid ( slotStack ) & & slotStack . getItem ( ) instanceof ItemDrillUpgrade ) {
if ( ( ( ItemDrillUpgrade ) slotStack . getItem ( ) ) . type = = upgrade ) { return slotStack ; }
2015-10-03 10:19:40 +02:00
}
}
2017-11-02 22:49:53 +01:00
return StackUtil . getEmpty ( ) ;
2015-10-03 10:19:40 +02:00
}
@Override
2020-10-31 21:42:32 +01:00
public ActionResult < ItemStack > onItemRightClick ( World world , PlayerEntity player , Hand hand ) {
// todo: reimplement
if ( ! world . isRemote & & player . isSneaking ( ) & & hand = = Hand . MAIN_HAND ) {
// player.openGui(ActuallyAdditions.INSTANCE, GuiHandler.GuiTypes.DRILL.ordinal(), world, (int) player.posX, (int) player.posY, (int) player.posZ);
2015-10-03 10:19:40 +02:00
}
2020-10-31 21:42:32 +01:00
return new ActionResult < > ( ActionResultType . PASS , player . getHeldItem ( hand ) ) ;
2015-12-01 19:48:09 +01:00
}
2015-10-03 10:19:40 +02:00
@Override
2020-10-31 21:42:32 +01:00
public boolean hitEntity ( ItemStack stack , LivingEntity target , LivingEntity attacker ) {
2015-10-03 10:19:40 +02:00
int use = this . getEnergyUsePerBlock ( stack ) ;
2020-10-31 21:42:32 +01:00
if ( ! ( attacker instanceof PlayerEntity ) | | ! ( ( PlayerEntity ) attacker ) . isCreative ( ) ) {
2019-05-02 09:10:29 +02:00
if ( this . getEnergyStored ( stack ) > = use ) {
2017-01-28 17:50:34 +01:00
this . extractEnergyInternal ( stack , use , false ) ;
}
2015-10-03 10:19:40 +02:00
}
return true ;
}
@Override
2020-10-31 21:42:32 +01:00
public Multimap < String , AttributeModifier > getAttributeModifiers ( EquipmentSlotType slot , ItemStack stack ) {
2016-03-19 11:42:11 +01:00
Multimap < String , AttributeModifier > map = super . getAttributeModifiers ( slot , stack ) ;
2020-10-31 21:42:32 +01:00
// todo: operation might be wrong here
if ( slot = = EquipmentSlotType . MAINHAND ) {
map . put ( SharedMonsterAttributes . ATTACK_DAMAGE . getName ( ) , new AttributeModifier ( ATTACK_DAMAGE_MODIFIER , " Drill Modifier " , this . getEnergyStored ( stack ) > = ENERGY_USE ? 8 . 0F : 0 . 1F , AttributeModifier . Operation . ADDITION ) ) ;
map . put ( SharedMonsterAttributes . ATTACK_SPEED . getName ( ) , new AttributeModifier ( ATTACK_SPEED_MODIFIER , " Tool Modifier " , - 2 . 5F , AttributeModifier . Operation . ADDITION ) ) ;
2016-03-19 11:42:11 +01:00
}
2015-10-03 10:19:40 +02:00
return map ;
}
@Override
2020-10-31 21:42:32 +01:00
public float getDestroySpeed ( ItemStack stack , BlockState state ) {
return this . getEnergyStored ( stack ) > = this . getEnergyUsePerBlock ( stack )
? ( this . hasExtraWhitelist ( state . getBlock ( ) )
| | state . getBlock ( ) . getHarvestTool ( state ) = = null
| | this . getToolTypes ( stack ) . contains ( state . getBlock ( ) . getHarvestTool ( state ) ) )
? this . getEfficiencyFromUpgrade ( stack )
: 1 . 0F
: 0 . 1F ;
2015-10-03 10:19:40 +02:00
}
@Override
2020-10-31 21:42:32 +01:00
public boolean onBlockStartBreak ( ItemStack stack , BlockPos pos , PlayerEntity 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 ) ;
2019-05-02 09:10:29 +02:00
if ( this . getEnergyStored ( stack ) > = use ) {
2015-10-03 10:19:40 +02:00
//Enchants the Drill depending on the Upgrades it has
2019-05-02 09:10:29 +02:00
if ( this . getHasUpgrade ( stack , ItemDrillUpgrade . UpgradeType . SILK_TOUCH ) ) {
2016-04-20 21:39:03 +02:00
ItemUtil . addEnchantment ( stack , Enchantments . SILK_TOUCH , 1 ) ;
2019-05-02 09:10:29 +02:00
} else {
if ( this . getHasUpgrade ( stack , ItemDrillUpgrade . UpgradeType . FORTUNE ) ) {
2016-04-20 21:39:03 +02:00
ItemUtil . addEnchantment ( stack , Enchantments . FORTUNE , this . getHasUpgrade ( stack , ItemDrillUpgrade . UpgradeType . FORTUNE_II ) ? 3 : 1 ) ;
2015-10-03 10:19:40 +02:00
}
}
2016-07-28 18:09:48 +02:00
//Block hit
2016-11-26 21:32:27 +01:00
RayTraceResult ray = WorldUtil . getNearestBlockWithDefaultReachDistance ( player . world , player ) ;
2019-05-02 09:10:29 +02:00
if ( ray ! = null ) {
2019-02-27 19:53:05 +01:00
2016-07-28 18:09:48 +02:00
//Breaks the Blocks
2019-05-02 09:10:29 +02:00
if ( ! player . isSneaking ( ) & & this . getHasUpgrade ( stack , ItemDrillUpgrade . UpgradeType . THREE_BY_THREE ) ) {
if ( this . getHasUpgrade ( stack , ItemDrillUpgrade . UpgradeType . FIVE_BY_FIVE ) ) {
2020-10-31 21:42:32 +01:00
toReturn = this . breakBlocks ( stack , 2 , player . world , pos , ( ( BlockRayTraceResult ) ray ) . getFace ( ) , player ) ;
2019-05-02 09:10:29 +02:00
} else {
2020-10-31 21:42:32 +01:00
toReturn = this . breakBlocks ( stack , 1 , player . world , pos , ( ( BlockRayTraceResult ) ray ) . getFace ( ) , player ) ;
2016-07-28 18:09:48 +02:00
}
2019-05-02 09:10:29 +02:00
} else {
2020-10-31 21:42:32 +01:00
toReturn = this . breakBlocks ( stack , 0 , player . world , pos , ( ( BlockRayTraceResult ) ray ) . getFace ( ) , player ) ;
2015-10-02 16:48:01 +02:00
}
2015-10-03 10:19:40 +02:00
2016-07-28 18:09:48 +02:00
//Removes Enchantments added above
ItemUtil . removeEnchantment ( stack , Enchantments . SILK_TOUCH ) ;
ItemUtil . removeEnchantment ( stack , Enchantments . FORTUNE ) ;
}
2015-06-15 22:06:07 +02:00
}
2015-10-03 10:19:40 +02:00
return toReturn ;
}
@Override
2020-10-31 21:42:32 +01:00
public boolean canHarvestBlock ( ItemStack stack , BlockState state ) {
2016-03-18 23:47:22 +01:00
Block block = state . getBlock ( ) ;
2020-10-31 21:42:32 +01:00
return this . getEnergyStored ( stack ) > = this . getEnergyUsePerBlock ( stack ) & & ( this . hasExtraWhitelist ( block ) | | state . getMaterial ( ) . isToolNotRequired ( ) | | block = = Blocks . SNOW_BLOCK | | block = = Blocks . SNOW | | ( block = = Blocks . OBSIDIAN ? HARVEST_LEVEL > = 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 ? state . getMaterial ( ) = = Material . ROCK | | state . getMaterial ( ) = = Material . IRON | | state . getMaterial ( ) = = Material . ANVIL : HARVEST_LEVEL > = 2 : HARVEST_LEVEL > = 1 : HARVEST_LEVEL > = 1 : HARVEST_LEVEL > = 2 : HARVEST_LEVEL > = 2 : HARVEST_LEVEL > = 2 ) ) ;
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
* /
2019-05-02 09:10:29 +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
2019-05-02 09:10:29 +02:00
if ( this . getHasUpgrade ( stack , ItemDrillUpgrade . UpgradeType . SPEED ) ) {
2015-11-28 19:02:01 +01:00
use + = 50 ;
2019-05-02 09:10:29 +02:00
if ( this . getHasUpgrade ( stack , ItemDrillUpgrade . UpgradeType . SPEED_II ) ) {
2015-11-28 19:02:01 +01:00
use + = 75 ;
2019-05-02 09:10:29 +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
2019-05-02 09:10:29 +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
2019-05-02 09:10:29 +02:00
if ( this . getHasUpgrade ( stack , ItemDrillUpgrade . UpgradeType . FORTUNE ) ) {
2015-11-28 19:02:01 +01:00
use + = 40 ;
2019-05-02 09:10:29 +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
2019-05-02 09:10:29 +02:00
if ( this . getHasUpgrade ( stack , ItemDrillUpgrade . UpgradeType . THREE_BY_THREE ) ) {
2015-11-28 19:02:01 +01:00
use + = 10 ;
2019-05-02 09:10:29 +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 ?
* /
2019-05-02 09:10:29 +02:00
public boolean getHasUpgrade ( ItemStack stack , ItemDrillUpgrade . UpgradeType upgrade ) {
2016-11-16 16:59:00 +01:00
return StackUtil . isValid ( this . getHasUpgradeAsStack ( stack , upgrade ) ) ;
2015-06-21 02:28:49 +02:00
}
2015-06-15 22:06:07 +02:00
2016-02-01 17:49:55 +01:00
@Override
2019-05-02 09:10:29 +02:00
protected void registerRendering ( ) {
for ( int i = 0 ; i < 16 ; i + + ) {
String name = this . getRegistryName ( ) + " _ " + TheColoredLampColors . values ( ) [ i ] . regName ;
2020-10-31 21:42:32 +01:00
ActuallyAdditions . PROXY . addRenderRegister ( new ItemStack ( this , 1 ) , new ResourceLocation ( name ) , " inventory " ) ;
2015-12-01 19:48:09 +01:00
}
}
2020-10-31 21:42:32 +01:00
// @Override
// @SideOnly(Side.CLIENT)
// public void getSubItems(CreativeTabs tabs, NonNullList<ItemStack> list) {
// if (this.isInCreativeTab(tabs)) {
// for (int i = 0; i < 16; i++) {
// this.addDrillStack(list, i);
// }
// }
// }
// todo: fix this
2019-05-02 09:10:29 +02:00
private void addDrillStack ( List < ItemStack > list , int meta ) {
2020-10-31 21:42:32 +01:00
ItemStack stackFull = new ItemStack ( this , 1 ) ;
2015-12-01 19:48:09 +01:00
this . setEnergy ( stackFull , this . getMaxEnergyStored ( stackFull ) ) ;
list . add ( stackFull ) ;
2020-10-31 21:42:32 +01:00
ItemStack stackEmpty = new ItemStack ( this , 1 ) ;
2015-12-01 19:48:09 +01:00
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
* /
2019-05-02 09:10:29 +02:00
public float getEfficiencyFromUpgrade ( ItemStack stack ) {
2015-11-28 19:02:01 +01:00
float efficiency = 8 . 0F ;
2019-05-02 09:10:29 +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 ) ) {
2015-10-03 10:19:40 +02:00
efficiency + = 37 . 0F ;
2019-05-02 09:10:29 +02:00
} else {
2015-10-03 10:19:40 +02:00
efficiency + = 25 . 0F ;
2015-06-15 22:06:07 +02:00
}
2019-05-02 09:10:29 +02:00
} else {
2015-10-03 10:19:40 +02:00
efficiency + = 8 . 0F ;
}
2015-06-15 22:06:07 +02:00
}
2019-05-02 09:10:29 +02:00
if ( this . getHasUpgrade ( stack , ItemDrillUpgrade . UpgradeType . THREE_BY_THREE ) ) {
2016-06-09 21:14:43 +02:00
efficiency * = 0 . 5F ;
2019-05-02 09:10:29 +02:00
if ( this . getHasUpgrade ( stack , ItemDrillUpgrade . UpgradeType . FIVE_BY_FIVE ) ) {
2016-06-09 21:14:43 +02:00
efficiency * = 0 . 35F ;
}
}
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
/ * *
* 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
* /
2020-10-31 21:42:32 +01:00
public boolean breakBlocks ( ItemStack stack , int radius , World world , BlockPos aPos , Direction side , PlayerEntity player ) {
2015-06-15 22:06:07 +02:00
int xRange = radius ;
int yRange = radius ;
int zRange = 0 ;
2015-07-20 22:06:08 +02:00
//Corrects Blocks to hit depending on Side of original Block hit
2020-10-31 21:42:32 +01:00
if ( side . getAxis ( ) = = Direction . Axis . Y ) {
2015-07-20 22:06:08 +02:00
zRange = radius ;
yRange = 0 ;
}
2020-10-31 21:42:32 +01:00
if ( side . getAxis ( ) = = Direction . Axis . X ) {
2015-07-20 22:06:08 +02:00
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
2020-10-31 21:42:32 +01:00
BlockState state = world . getBlockState ( aPos ) ;
2016-07-04 20:15:41 +02:00
float mainHardness = state . 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 ) ;
2019-05-02 09:10:29 +02:00
if ( this . getEnergyStored ( stack ) > = use ) {
if ( ! this . tryHarvestBlock ( world , aPos , false , stack , player , use ) ) { return false ; }
} else {
2015-10-02 16:48:01 +02:00
return false ;
}
2019-02-27 19:53:05 +01:00
2020-10-31 21:42:32 +01:00
if ( radius = = 2 & & side . getAxis ( ) ! = Direction . Axis . Y ) {
2019-02-27 19:53:05 +01:00
aPos = aPos . up ( ) ;
2020-10-31 21:42:32 +01:00
BlockState theState = world . getBlockState ( aPos ) ;
2019-05-02 09:10:29 +02:00
if ( theState . getBlockHardness ( world , aPos ) < = mainHardness + 5 . 0F ) {
2019-02-27 19:53:05 +01:00
this . tryHarvestBlock ( world , aPos , true , stack , player , use ) ;
}
2018-01-29 08:17:31 +01:00
}
2015-07-20 22:06:08 +02:00
//Break Blocks around
2019-05-02 09:10:29 +02:00
if ( radius > 0 & & mainHardness > = 0 . 2F ) {
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 ) ) {
if ( this . getEnergyStored ( stack ) > = use ) {
2015-07-20 22:06:08 +02:00
//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 ) ;
2020-10-31 21:42:32 +01:00
BlockState theState = world . getBlockState ( thePos ) ;
2019-05-02 09:10:29 +02:00
if ( theState . 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
}
2019-05-02 09:10:29 +02:00
} else {
2015-10-02 16:48:01 +02:00
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
* /
2020-10-31 21:42:32 +01:00
private boolean tryHarvestBlock ( World world , BlockPos pos , boolean isExtra , ItemStack stack , PlayerEntity player , int use ) {
BlockState state = world . getBlockState ( pos ) ;
2016-07-04 20:15:41 +02:00
Block block = state . getBlock ( ) ;
2017-07-28 03:17:50 +02:00
float hardness = state . getBlockHardness ( world , pos ) ;
2020-10-31 21:42:32 +01:00
boolean canHarvest = ( ForgeHooks . canHarvestBlock ( state , player , world , pos ) | | this . canHarvestBlock ( stack , state ) ) & & ( ! isExtra | | this . getDestroySpeed ( stack , world . getBlockState ( pos ) ) > 1 . 0F ) ;
2019-05-02 09:10:29 +02:00
if ( hardness > = 0 . 0F & & ( ! isExtra | | canHarvest & & ! block . hasTileEntity ( world . getBlockState ( pos ) ) ) ) {
2020-10-31 21:42:32 +01:00
if ( ! player . isCreative ( ) ) {
2017-01-28 17:50:34 +01:00
this . extractEnergyInternal ( stack , use , false ) ;
}
2015-07-27 08:26:13 +02:00
//Break the Block
2018-01-29 08:17:31 +01:00
return WorldUtil . breakExtraBlock ( stack , world , player , pos ) ;
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
}
2019-05-02 09:10:29 +02:00
private boolean hasExtraWhitelist ( Block block ) {
if ( block ! = null ) {
2016-07-27 02:01:15 +02:00
ResourceLocation location = block . getRegistryName ( ) ;
2019-05-02 09:10:29 +02:00
if ( location ! = null ) {
2016-07-27 02:01:15 +02:00
String name = location . toString ( ) ;
2019-05-02 09:10:29 +02:00
if ( name ! = null ) {
for ( String s : ConfigStringListValues . DRILL_EXTRA_MINING_WHITELIST . getValue ( ) ) {
if ( s ! = null & & s . equals ( name ) ) { return true ; }
2016-07-27 02:01:15 +02:00
}
2015-10-03 10:16:18 +02:00
}
2015-09-13 16:41:49 +02:00
}
}
return false ;
}
2019-02-27 19:53:05 +01:00
2017-08-12 00:15:09 +02:00
@Override
public boolean shouldCauseBlockBreakReset ( ItemStack oldStack , ItemStack newStack ) {
2019-02-27 19:53:05 +01:00
return ! newStack . isItemEqual ( oldStack ) ;
2017-08-12 00:15:09 +02:00
}
2015-06-15 22:06:07 +02:00
}