ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/items/DrillItem.java

581 lines
24 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
2016-05-16 22:52:27 +02:00
* This file ("ItemDrill.java") is part of the Actually Additions mod for Minecraft.
2015-08-29 14:33:25 +02:00
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
2016-05-16 22:52:27 +02:00
* http://ellpeck.de/actaddlicense
2015-08-29 14:33:25 +02:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 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
2021-11-21 22:03:07 +01:00
import com.google.common.collect.ArrayListMultimap;
2015-06-15 22:06:07 +02:00
import com.google.common.collect.Multimap;
2024-03-02 21:23:08 +01:00
import de.ellpeck.actuallyadditions.api.ActuallyTags;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
import de.ellpeck.actuallyadditions.mod.config.CommonConfig;
import de.ellpeck.actuallyadditions.mod.inventory.ContainerDrill;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.items.base.ItemEnergy;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityInventoryBase;
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA;
import de.ellpeck.actuallyadditions.mod.util.ItemUtil;
2024-03-09 01:24:19 +01:00
import de.ellpeck.actuallyadditions.mod.util.Util;
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
2024-03-02 21:23:08 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
2024-03-04 20:21:48 +01:00
import net.minecraft.core.registries.BuiltInRegistries;
2024-03-02 21:23:08 +01:00
import net.minecraft.nbt.CompoundTag;
2024-03-03 01:20:53 +01:00
import net.minecraft.network.chat.Component;
2024-03-02 21:23:08 +01:00
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.SimpleMenuProvider;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Tier;
import net.minecraft.world.item.Tiers;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
2024-03-09 01:06:58 +01:00
import net.neoforged.neoforge.common.*;
2024-03-04 20:21:48 +01:00
import net.neoforged.neoforge.items.IItemHandler;
import net.neoforged.neoforge.items.IItemHandlerModifiable;
2015-06-15 22:06:07 +02:00
2024-03-08 22:40:48 +01:00
import javax.annotation.Nonnull;
2024-03-13 01:50:37 +01:00
import java.util.ArrayList;
2024-03-08 22:40:48 +01:00
import java.util.List;
2021-11-25 21:27:45 +01:00
public class DrillItem extends ItemEnergy {
public static final int HARVEST_LEVEL = 4;
2016-07-07 17:59:45 +02:00
private static final int ENERGY_USE = 100;
2024-03-08 22:40:48 +01:00
private static final List<ToolAction> ACTIONS = List.of(ToolActions.SHOVEL_DIG, ToolActions.PICKAXE_DIG);
2021-11-25 21:27:45 +01:00
public DrillItem() {
2024-03-02 21:23:08 +01:00
super(ActuallyItems.defaultProps().defaultDurability(0).stacksTo(1), 250000, 1000);
}
@Override
2024-03-08 22:40:48 +01:00
public boolean canPerformAction(@Nonnull ItemStack stack, @Nonnull ToolAction toolAction) {
return ACTIONS.contains(toolAction);
2024-03-02 21:23:08 +01:00
}
@Override
2024-03-08 22:40:48 +01:00
public boolean isCorrectToolForDrops(@Nonnull BlockState pBlock) {
2024-03-02 21:23:08 +01:00
Tier tier = Tiers.NETHERITE; //Use Nettherite as the tier as it has the same harvest level as the drill
2024-03-04 20:21:48 +01:00
if (TierSortingRegistry.isTierSorted(tier)) {
return TierSortingRegistry.isCorrectTierForDrops(tier, pBlock) && pBlock.is(ActuallyTags.Blocks.MINEABLE_WITH_DRILL);
2024-03-02 21:23:08 +01:00
}
if (HARVEST_LEVEL < 3 && pBlock.is(BlockTags.NEEDS_DIAMOND_TOOL)) {
return false;
} else if (HARVEST_LEVEL < 2 && pBlock.is(BlockTags.NEEDS_IRON_TOOL)) {
return false;
} else {
return HARVEST_LEVEL < 1 && pBlock.is(BlockTags.NEEDS_STONE_TOOL) ? false : pBlock.is(ActuallyTags.Blocks.MINEABLE_WITH_DRILL);
}
}
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) {
2024-03-02 21:23:08 +01:00
CompoundTag compound = stack.getOrCreateTag();
2021-08-22 23:19:57 +02:00
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) {
2024-03-02 21:23:08 +01:00
CompoundTag compound = stack.getOrCreateTag();
2021-08-22 23:19:57 +02:00
2016-06-17 23:50:38 +02:00
TileEntityInventoryBase.saveSlots(slots, compound);
2021-08-22 23:19:57 +02:00
stack.setTag(compound);
2016-06-17 23:50:38 +02:00
}
2024-03-08 22:40:48 +01:00
@Nonnull
2015-06-21 02:28:49 +02:00
@Override
2024-03-02 21:23:08 +01:00
public InteractionResult useOn(UseOnContext context) {
Player player = context.getPlayer();
InteractionHand hand = context.getHand();
2021-08-22 23:19:57 +02:00
ItemStack stack = player.getItemInHand(hand);
2015-06-21 02:28:49 +02:00
ItemStack upgrade = this.getHasUpgradeAsStack(stack, ItemDrillUpgrade.UpgradeType.PLACER);
2024-03-08 22:40:48 +01:00
if (!upgrade.isEmpty()) {
2015-06-21 02:28:49 +02:00
int slot = ItemDrillUpgrade.getSlotToPlaceFrom(upgrade);
2021-08-22 23:19:57 +02:00
if (slot >= 0 && slot < 9) { // TODO: validate... old = PlayerInventory.getHotbarSize(); new = 9
2024-03-02 21:23:08 +01:00
ItemStack equip = player.getInventory().getItem(slot);
2024-03-08 22:40:48 +01:00
if (!equip.isEmpty() && equip != stack) {
ItemStack toPlaceStack = equip.copy();
WorldUtil.setHandItemWithoutAnnoyingSound(player, hand, toPlaceStack);
//tryPlaceItemIntoWorld could throw an Exception
2019-05-02 09:10:29 +02:00
try {
//Places the Block into the World
2024-03-02 21:23:08 +01:00
if (toPlaceStack.useOn(context) != InteractionResult.FAIL) {
2021-02-28 15:47:54 +01:00
if (!player.isCreative()) {
WorldUtil.setHandItemWithoutAnnoyingSound(player, hand, toPlaceStack.copy());
2015-06-22 18:09:00 +02:00
}
2015-06-21 02:28:49 +02:00
}
}
//Notify the Player and log the Exception
2019-05-02 09:10:29 +02:00
catch (Exception e) {
2021-08-22 23:19:57 +02:00
ActuallyAdditions.LOGGER.error("Player " + player.getName() + " who should place a Block using a Drill at " + player.getX() + ", " + player.getY() + ", " + player.getZ() + " in World " + context.getLevel().dimension() + " threw an Exception! Don't let that happen again!");
2015-10-02 16:48:01 +02:00
}
2024-03-02 21:23:08 +01:00
player.getInventory().setItem(slot, player.getItemInHand(hand));
WorldUtil.setHandItemWithoutAnnoyingSound(player, hand, stack);
2024-03-02 21:23:08 +01:00
return InteractionResult.SUCCESS;
2015-06-21 02:28:49 +02:00
}
}
}
2021-08-22 23:19:57 +02:00
return super.useOn(context);
2015-06-21 02:28:49 +02: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) {
ItemStackHandlerAA inv = new ItemStackHandlerAA(ContainerDrill.SLOT_AMOUNT);
loadSlotsFromNBT(inv, stack);
2019-05-02 09:10:29 +02:00
for (int i = 0; i < inv.getSlots(); i++) {
ItemStack slotStack = inv.getStackInSlot(i);
2024-03-08 22:40:48 +01:00
if (!slotStack.isEmpty() && slotStack.getItem() instanceof ItemDrillUpgrade drillUpgrade) {
2024-03-07 01:35:53 +01:00
if (drillUpgrade.type == upgrade) {
2021-02-26 22:15:48 +01:00
return slotStack;
}
2015-10-03 10:19:40 +02:00
}
}
2022-06-24 21:38:07 +02:00
return ItemStack.EMPTY;
2015-10-03 10:19:40 +02:00
}
2024-03-08 22:40:48 +01:00
@Nonnull
2015-10-03 10:19:40 +02:00
@Override
2024-03-08 22:40:48 +01:00
public InteractionResultHolder<ItemStack> use(Level world, @Nonnull Player player, @Nonnull InteractionHand hand) {
2024-03-02 21:23:08 +01:00
if (!world.isClientSide && player.isShiftKeyDown() && hand == InteractionHand.MAIN_HAND) {
2024-03-12 22:39:01 +01:00
player.openMenu(new SimpleMenuProvider((id, inv, p) -> new ContainerDrill(id, inv), Component.translatable("container." + ActuallyAdditions.MODID + ".drill")));
2021-11-13 20:09:55 +01:00
// 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
}
2024-03-02 21:23:08 +01:00
return new InteractionResultHolder<>(InteractionResult.PASS, player.getItemInHand(hand));
2015-10-03 10:19:40 +02:00
}
2024-03-08 22:40:48 +01:00
@Nonnull
2015-12-01 19:48:09 +01:00
@Override
2024-03-08 22:40:48 +01:00
public InteractionResult interactLivingEntity(@Nonnull ItemStack stack, @Nonnull Player player, @Nonnull LivingEntity entityHit, @Nonnull InteractionHand hand) {
2015-10-03 10:19:40 +02:00
int use = this.getEnergyUsePerBlock(stack);
2024-03-02 21:23:08 +01:00
if (!(entityHit instanceof Player) || !((Player) entityHit).isCreative()) {
2019-05-02 09:10:29 +02:00
if (this.getEnergyStored(stack) >= use) {
this.extractEnergyInternal(stack, use, false);
}
2015-10-03 10:19:40 +02:00
}
2024-03-02 21:23:08 +01:00
return InteractionResult.SUCCESS;
2015-10-03 10:19:40 +02:00
}
2024-03-08 22:40:48 +01:00
@Nonnull
2015-10-03 10:19:40 +02:00
@Override
2024-03-08 22:40:48 +01:00
public Multimap<Attribute, AttributeModifier> getAttributeModifiers(@Nonnull EquipmentSlot slot, @Nonnull ItemStack stack) {
2021-11-21 22:03:07 +01:00
Multimap<Attribute, AttributeModifier> map = ArrayListMultimap.create();
2021-08-22 23:19:57 +02:00
2024-03-02 21:23:08 +01:00
if (slot == EquipmentSlot.MAINHAND) {
2021-08-22 23:19:57 +02:00
map.put(Attributes.ATTACK_DAMAGE, new AttributeModifier("Drill Modifier", this.getEnergyStored(stack) >= ENERGY_USE
? 8.0F
: 0.1F, AttributeModifier.Operation.ADDITION));
2024-03-08 20:30:17 +01:00
map.put(Attributes.ATTACK_SPEED, new AttributeModifier("Tool Modifier", 1.5F, AttributeModifier.Operation.ADDITION));
2016-03-19 11:42:11 +01:00
}
2015-10-03 10:19:40 +02:00
return map;
}
2021-08-22 23:19:57 +02:00
2015-10-03 10:19:40 +02:00
@Override
2024-03-08 22:40:48 +01:00
public float getDestroySpeed(@Nonnull ItemStack stack, @Nonnull BlockState state) {
2021-02-26 22:15:48 +01:00
return this.getEnergyStored(stack) >= this.getEnergyUsePerBlock(stack)
2024-03-07 01:35:53 +01:00
? (this.hasExtraWhitelist(state.getBlock()) || state.is(ActuallyTags.Blocks.MINEABLE_WITH_DRILL))
2021-08-22 23:19:57 +02:00
? this.getEfficiencyFromUpgrade(stack)
: 1.0F
: 0.1F;
2015-10-03 10:19:40 +02:00
}
@Override
2024-03-08 22:40:48 +01:00
public boolean onBlockStartBreak(@Nonnull ItemStack stack, @Nonnull BlockPos pos, @Nonnull Player player) {
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)) {
ItemUtil.addEnchantment(stack, Enchantments.BLOCK_FORTUNE, this.getHasUpgrade(stack, ItemDrillUpgrade.UpgradeType.FORTUNE_II)
2021-08-22 23:19:57 +02:00
? 3
: 1);
2015-10-03 10:19:40 +02:00
}
}
//Block hit
2024-03-09 01:24:19 +01:00
HitResult ray = player.pick(Util.getReachDistance(player), 1f, false);
if (ray instanceof BlockHitResult trace) {
//Breaks the Blocks
if (!player.isShiftKeyDown() && this.getHasUpgrade(stack, ItemDrillUpgrade.UpgradeType.THREE_BY_THREE)) {
2019-05-02 09:10:29 +02:00
if (this.getHasUpgrade(stack, ItemDrillUpgrade.UpgradeType.FIVE_BY_FIVE)) {
2024-03-03 01:20:53 +01:00
toReturn = this.breakBlocks(stack, 2, player.level(), pos, trace.getDirection(), player);
2019-05-02 09:10:29 +02:00
} else {
2024-03-03 01:20:53 +01:00
toReturn = this.breakBlocks(stack, 1, player.level(), pos, trace.getDirection(), player);
}
2019-05-02 09:10:29 +02:00
} else {
2024-03-03 01:20:53 +01:00
toReturn = this.breakBlocks(stack, 0, player.level(), pos, trace.getDirection(), player);
2015-10-02 16:48:01 +02:00
}
2015-10-03 10:19:40 +02:00
//Removes Enchantments added above
ItemUtil.removeEnchantment(stack, Enchantments.SILK_TOUCH);
ItemUtil.removeEnchantment(stack, Enchantments.BLOCK_FORTUNE);
}
2015-06-15 22:06:07 +02:00
}
2015-10-03 10:19:40 +02:00
return toReturn;
}
2024-03-08 22:40:48 +01:00
/* @Override //TODO old one
2024-03-02 21:23:08 +01:00
public boolean isCorrectToolForDrops(ItemStack stack, BlockState state) {
2021-10-02 15:59:42 +02:00
Block block = state.getBlock();
2024-03-03 01:20:53 +01:00
return this.getEnergyStored(stack) >= this.getEnergyUsePerBlock(stack) && (this.hasExtraWhitelist(block) || state.canBeReplaced() || block == Blocks.SNOW_BLOCK || block == Blocks.SNOW || (block == Blocks.OBSIDIAN
2021-08-22 23:19:57 +02:00
? HARVEST_LEVEL >= 3
: block != Blocks.DIAMOND_BLOCK && block != Blocks.DIAMOND_ORE
2021-02-26 22:15:48 +01:00
? 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
2021-10-02 15:59:42 +02:00
? block != Blocks.REDSTONE_ORE
2024-03-03 01:20:53 +01:00
? state.is(Tags.Blocks.STONE) || state.is(Tags.Blocks.STORAGE_BLOCKS)
2021-02-26 22:15:48 +01:00
: HARVEST_LEVEL >= 2
: HARVEST_LEVEL >= 1
: HARVEST_LEVEL >= 1
: HARVEST_LEVEL >= 2
: HARVEST_LEVEL >= 2
: HARVEST_LEVEL >= 2));
2024-03-08 22:40:48 +01:00
}*/
2015-10-03 10:19:40 +02:00
@Override
public boolean isCorrectToolForDrops(ItemStack stack, BlockState state) {
return this.getEnergyStored(stack) >= this.getEnergyUsePerBlock(stack) && super.isCorrectToolForDrops(stack, state);
}
2024-03-02 21:23:08 +01:00
// @Override
// public int getHarvestLevel(ItemStack stack, ToolType p_getHarvestLevel_2_, @Nullable Player p_getHarvestLevel_3_, @Nullable BlockState p_getHarvestLevel_4_) {
// return HARVEST_LEVEL;
// }
2015-06-15 22:06:07 +02:00
/**
* Gets the Energy that is used per Block broken
2015-10-02 16:48:01 +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) {
int use = ENERGY_USE;
2015-06-21 02:28:49 +02:00
//Speed
2019-05-02 09:10:29 +02:00
if (this.getHasUpgrade(stack, ItemDrillUpgrade.UpgradeType.SPEED)) {
use += 50;
2019-05-02 09:10:29 +02:00
if (this.getHasUpgrade(stack, ItemDrillUpgrade.UpgradeType.SPEED_II)) {
use += 75;
2019-05-02 09:10:29 +02:00
if (this.getHasUpgrade(stack, ItemDrillUpgrade.UpgradeType.SPEED_III)) {
use += 175;
2015-10-02 16:48:01 +02:00
}
2015-06-15 22:06:07 +02:00
}
}
//Silk Touch
2019-05-02 09:10:29 +02:00
if (this.getHasUpgrade(stack, ItemDrillUpgrade.UpgradeType.SILK_TOUCH)) {
use += 100;
2015-10-02 16:48:01 +02:00
}
2015-06-21 02:28:49 +02:00
//Fortune
2019-05-02 09:10:29 +02:00
if (this.getHasUpgrade(stack, ItemDrillUpgrade.UpgradeType.FORTUNE)) {
use += 40;
2019-05-02 09:10:29 +02:00
if (this.getHasUpgrade(stack, ItemDrillUpgrade.UpgradeType.FORTUNE_II)) {
use += 80;
2015-10-02 16:48:01 +02:00
}
2015-06-15 22:06:07 +02:00
}
//Size
2019-05-02 09:10:29 +02:00
if (this.getHasUpgrade(stack, ItemDrillUpgrade.UpgradeType.THREE_BY_THREE)) {
use += 10;
2019-05-02 09:10:29 +02:00
if (this.getHasUpgrade(stack, ItemDrillUpgrade.UpgradeType.FIVE_BY_FIVE)) {
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
}
/**
* Checks if a certain Upgrade is applied
2015-10-02 16:48:01 +02:00
*
* @param stack The Drill
* @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) {
2024-03-08 22:40:48 +01:00
return !this.getHasUpgradeAsStack(stack, upgrade).isEmpty();
2015-06-21 02:28:49 +02:00
}
2015-06-15 22:06:07 +02:00
2021-11-13 20:09:55 +01:00
// @Override
// @OnlyIn(Dist.CLIENT)
// public void getSubItems(CreativeTabs tabs, NonNullList<ItemStack> list) {
// if (this.isInCreativeTab(tabs)) {
// for (int i = 0; i < 16; i++) {
// this.addDrillStack(list, i);
// }
// }
// }
// private void addDrillStack(List<ItemStack> 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-12-01 19:48:09 +01: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
*/
2019-05-02 09:10:29 +02:00
public float getEfficiencyFromUpgrade(ItemStack stack) {
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
}
/**
* 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
* @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
* @param player The Player who breaks the Blocks
*/
2024-03-02 21:23:08 +01:00
public boolean breakBlocks(ItemStack stack, int radius, Level world, BlockPos aPos, Direction side, Player 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
2021-08-22 23:19:57 +02:00
if (side.getAxis() == Direction.Axis.Y) {
2015-07-20 22:06:08 +02:00
zRange = radius;
yRange = 0;
}
2021-08-22 23:19:57 +02:00
if (side.getAxis() == Direction.Axis.X) {
2015-07-20 22:06:08 +02:00
xRange = 0;
2024-03-09 01:07:17 +01:00
zRange = radius;
2015-07-20 22:06:08 +02:00
}
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
2021-02-26 22:15:48 +01:00
BlockState state = world.getBlockState(aPos);
float mainHardness = state.getDestroySpeed(world, aPos);
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) {
2021-02-26 22:15:48 +01:00
if (!this.tryHarvestBlock(world, aPos, false, stack, player, use)) {
return false;
}
2019-05-02 09:10:29 +02:00
} else {
2015-10-02 16:48:01 +02:00
return false;
}
2019-02-27 19:53:05 +01:00
2021-08-22 23:19:57 +02:00
if (radius == 2 && side.getAxis() != Direction.Axis.Y) {
aPos = aPos.above();
2021-02-26 22:15:48 +01:00
BlockState theState = world.getBlockState(aPos);
if (theState.getDestroySpeed(world, aPos) <= mainHardness + 5.0F) {
2019-02-27 19:53:05 +01:00
this.tryHarvestBlock(world, aPos, true, stack, player, use);
}
}
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);
2021-02-26 22:15:48 +01:00
BlockState theState = world.getBlockState(thePos);
if (theState.getDestroySpeed(world, thePos) <= mainHardness + 5.0F) {
this.tryHarvestBlock(world, thePos, true, stack, player, use);
}
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;
}
2024-03-13 01:50:37 +01:00
/**
* Generate a list of block positions that can be broken taking radius, poker and side into account
* @param stack The Drill
* @param radius The Radius to break Blocks in (0 means only 1 Block will be broken!)
* @param world The World
* @param aPos The position of the block being broken
* @param side The side of the block being broken
* @param player The Player who breaks the Blocks
* @return A list of block positions that can be broken
*/
public List<BlockPos> gatherBreakingPositions(ItemStack stack, int radius, Level world, BlockPos aPos, Direction side, Player player) {
int energyStored = this.getEnergyStored(stack);
List<BlockPos> positions = new ArrayList<>();
int xRange = radius;
int yRange = radius;
int zRange = 0;
//Corrects Blocks to hit depending on Side of original Block hit
if (side.getAxis() == Direction.Axis.Y) {
zRange = radius;
yRange = 0;
}
if (side.getAxis() == Direction.Axis.X) {
xRange = 0;
zRange = radius;
}
//Not defined later because main Block is getting broken below
BlockState state = world.getBlockState(aPos);
float mainHardness = state.getDestroySpeed(world, aPos);
//Break Middle Block first
int use = this.getEnergyUsePerBlock(stack);
if (energyStored < use) {
return positions;
}
if (radius == 2 && side.getAxis() != Direction.Axis.Y) {
aPos = aPos.above();
BlockState theState = world.getBlockState(aPos);
if (theState.getDestroySpeed(world, aPos) <= mainHardness + 5.0F) {
positions.add(aPos.immutable());
}
}
//Break Blocks around
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 (energyStored >= use) {
//Only break Blocks around that are (about) as hard or softer
BlockPos thePos = new BlockPos(xPos, yPos, zPos);
BlockState theState = world.getBlockState(thePos);
if (theState.getDestroySpeed(world, thePos) <= mainHardness + 5.0F) {
energyStored -= use;
positions.add(thePos.immutable());
}
} else {
return positions;
}
}
}
}
}
}
return positions;
}
/**
* 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
* @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
*/
2024-03-02 21:23:08 +01:00
private boolean tryHarvestBlock(Level world, BlockPos pos, boolean isExtra, ItemStack stack, Player player, int use) {
2021-02-26 22:15:48 +01:00
BlockState state = world.getBlockState(pos);
2016-07-04 20:15:41 +02:00
Block block = state.getBlock();
float hardness = state.getDestroySpeed(world, pos);
2024-03-02 21:23:08 +01:00
2024-03-04 20:21:48 +01:00
boolean canHarvest = (CommonHooks.isCorrectToolForDrops(state, player) || this.isCorrectToolForDrops(stack, state)) && (!isExtra || this.getDestroySpeed(stack, world.getBlockState(pos)) > 1.0F);
2024-03-02 21:23:08 +01:00
if (hardness >= 0.0F && (!isExtra || canHarvest && !state.hasBlockEntity())) {
2021-02-28 15:47:54 +01:00
if (!player.isCreative()) {
this.extractEnergyInternal(stack, use, false);
}
//Break the Block
return WorldUtil.breakExtraBlock(stack, world, player, pos);
}
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) {
2024-03-04 20:21:48 +01:00
ResourceLocation location = BuiltInRegistries.BLOCK.getKey(block);
2019-05-02 09:10:29 +02:00
if (location != null) {
String name = location.toString();
2019-05-02 09:10:29 +02:00
if (name != null) {
for (String s : CommonConfig.ItemSettings.DRILL_EXTRA_MINING_WHITELIST.get()) {
2021-02-26 22:15:48 +01:00
if (s != null && s.equals(name)) {
return true;
}
}
2015-10-03 10:16:18 +02:00
}
}
}
return false;
}
2019-02-27 19:53:05 +01:00
2017-08-12 00:15:09 +02:00
@Override
2024-03-08 22:40:48 +01:00
public boolean shouldCauseBlockBreakReset(@Nonnull ItemStack oldStack, @Nonnull ItemStack newStack) {
2024-03-03 01:20:53 +01:00
return !ItemStack.isSameItem(newStack, oldStack);
2017-08-12 00:15:09 +02:00
}
2015-06-15 22:06:07 +02:00
}