Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make EffectBreak find a proper tool from the inventory #1547

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.hollingsworth.arsnouveau.common.spell.effect;

import com.hollingsworth.arsnouveau.api.item.inv.InteractType;
import com.hollingsworth.arsnouveau.api.item.inv.InventoryManager;
import com.hollingsworth.arsnouveau.api.item.inv.SlotReference;
import com.hollingsworth.arsnouveau.api.spell.*;
import com.hollingsworth.arsnouveau.api.util.BlockUtil;
import com.hollingsworth.arsnouveau.api.util.SpellUtil;
Expand All @@ -8,24 +11,28 @@
import com.hollingsworth.arsnouveau.common.lib.GlyphLib;
import com.hollingsworth.arsnouveau.common.spell.augment.*;
import com.hollingsworth.arsnouveau.common.util.HolderHelper;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Holder;
import net.minecraft.core.component.DataComponents;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.*;
import net.minecraft.world.item.component.Unbreakable;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.function.Function;

public class EffectBreak extends AbstractEffect {
public static EffectBreak INSTANCE = new EffectBreak();
Expand All @@ -39,13 +46,28 @@ public int getDefaultManaCost() {
return 10;
}

public ItemStack getStack(LivingEntity shooter, BlockHitResult blockHitResult) {
public ItemStack getStack(double ampMultiplier, LivingEntity shooter, InventoryManager inventory, BlockHitResult blockHitResult) {
ItemStack stack = shooter.getMainHandItem().copy();
boolean usePick = shooter.level.getBlockState(blockHitResult.getBlockPos()).is(BlockTagProvider.BREAK_WITH_PICKAXE);
if (usePick) {
BlockState state = shooter.level.getBlockState(blockHitResult.getBlockPos());

if (!stack.isCorrectToolForDrops(state)) {
SlotReference tool = inventory.findItem(s -> !s.isEmpty() && s.isCorrectToolForDrops(state), InteractType.EXTRACT);
if (!tool.isEmpty() && tool.getHandler() != null) {
return tool.getHandler().getStackInSlot(tool.getSlot()).copy();
}
}

return getDefaultTool(ampMultiplier);
}

public ItemStack getDefaultTool(double ampMultiplier) {
if (ampMultiplier < 1.0) {
return new ItemStack(Items.IRON_PICKAXE);
} else if (ampMultiplier < 2.0) {
return new ItemStack(Items.DIAMOND_PICKAXE);
} else {
return new ItemStack(Items.NETHERITE_PICKAXE);
}
return stack.isEmpty() ? new ItemStack(Items.DIAMOND_PICKAXE) : stack;
}

@Override
Expand All @@ -60,27 +82,54 @@ public void onResolveBlock(BlockHitResult rayTraceResult, Level world, @NotNull
double aoeBuff = spellStats.getAoeMultiplier();
int pierceBuff = spellStats.getBuffCount(AugmentPierce.INSTANCE);
List<BlockPos> posList = SpellUtil.calcAOEBlocks(shooter, pos, rayTraceResult, aoeBuff, pierceBuff);
ItemStack stack = spellStats.isSensitive() ? new ItemStack(Items.SHEARS) : getStack(shooter, rayTraceResult);

int numFortune = spellStats.getBuffCount(AugmentFortune.INSTANCE);
int numSilkTouch = spellStats.getBuffCount(AugmentExtract.INSTANCE);
if (numFortune > 0 && stack.getEnchantmentLevel(HolderHelper.unwrap(world,Enchantments.FORTUNE)) < numFortune) {
stack.enchant(HolderHelper.unwrap(world,Enchantments.FORTUNE), numFortune);
}
if (numSilkTouch > 0 && stack.getEnchantmentLevel(HolderHelper.unwrap(world,Enchantments.SILK_TOUCH)) < numSilkTouch) {
stack.enchant(HolderHelper.unwrap(world,Enchantments.SILK_TOUCH), numSilkTouch);
}
for (BlockPos pos1 : posList) {
Holder<Enchantment> fortune = HolderHelper.unwrap(world, Enchantments.FORTUNE);
Holder<Enchantment> silkTouch = HolderHelper.unwrap(world, Enchantments.FORTUNE);

Map<BlockState, ItemStack> toolCache = posList.size() > 1 ? new Object2ObjectOpenHashMap<>(8) : null;
InventoryManager manager = spellContext.getCaster().getInvManager();

Function<BlockState, ItemStack> compute = s -> {
ItemStack stack = getStack(spellStats.getAmpMultiplier(), shooter, manager, rayTraceResult);
stack.set(DataComponents.UNBREAKABLE, new Unbreakable(true));

if (numFortune > 0 && stack.getEnchantmentLevel(fortune) < numFortune) {
stack.enchant(fortune, numFortune);
}
if (numSilkTouch > 0 && stack.getEnchantmentLevel(silkTouch) < numSilkTouch) {
stack.enchant(silkTouch, numSilkTouch);
}

return stack;
};

for (int i = 0; i < posList.size(); i++) {
BlockPos pos1 = posList.get(i);

if (world.isOutsideBuildHeight(pos1) || world.random.nextFloat() < spellStats.getBuffCount(AugmentRandomize.INSTANCE) * 0.25F) {
continue;
}
state = world.getBlockState(pos1);
if (state.is(BlockTags.AIR)) {
continue;
}

ItemStack tool;
if (spellStats.isSensitive()) {
tool = new ItemStack(Items.SHEARS);
} else if (toolCache != null && i < posList.size() - 1) {
tool = toolCache.computeIfAbsent(state, compute);
} else {
tool = compute.apply(state);
}

if (!canBlockBeHarvested(spellStats, world, pos1) || !BlockUtil.destroyRespectsClaim(getPlayer(shooter, (ServerLevel) world), world, pos1) || state.is(BlockTagProvider.BREAK_BLACKLIST)) {
if (world.getBlockState(pos).getDestroySpeed(world, pos) < 0 || !tool.isCorrectToolForDrops(state) || !BlockUtil.destroyRespectsClaim(getPlayer(shooter, (ServerLevel) world), world, pos1) || state.is(BlockTagProvider.BREAK_BLACKLIST)) {
continue;
}

if (!BlockUtil.breakExtraBlock((ServerLevel) world, pos1, stack, shooter.getUUID(), true)) {
if (!BlockUtil.breakExtraBlock((ServerLevel) world, pos1, tool, shooter.getUUID(), true)) {
continue;
}

Expand Down