From 9f9cfee956228323f953fd577cb902548b836303 Mon Sep 17 00:00:00 2001 From: trinsdar <30245301+Trinsdar@users.noreply.github.com> Date: Fri, 8 Dec 2023 22:54:40 -0500 Subject: [PATCH] added some missing methods to MAterialSword --- .../antimatter/tool/IAntimatterTool.java | 23 +++++++++ .../antimatter/tool/MaterialSword.java | 50 +++++++++++++++---- .../antimatter/tool/MaterialTool.java | 25 +--------- 3 files changed, 65 insertions(+), 33 deletions(-) diff --git a/common/src/main/java/muramasa/antimatter/tool/IAntimatterTool.java b/common/src/main/java/muramasa/antimatter/tool/IAntimatterTool.java index 1344a74ad..1205f7886 100644 --- a/common/src/main/java/muramasa/antimatter/tool/IAntimatterTool.java +++ b/common/src/main/java/muramasa/antimatter/tool/IAntimatterTool.java @@ -155,6 +155,29 @@ default Tier getTier(ItemStack stack) { return tier.orElseGet(() -> resolveTierTag(dataTag)); } + default boolean isCorrectToolForDrops(ItemStack stack, BlockState state) { + AntimatterToolType type = this.getAntimatterToolType(); + if (type.getEffectiveMaterials().contains(state.getMaterial())) { + return true; + } + if (type.getEffectiveBlocks().contains(state.getBlock())) { + return true; + } + for (TagKey effectiveBlockTag : type.getEffectiveBlockTags()) { + if (state.is(effectiveBlockTag)){ + return true; + } + } + boolean isType = false; + for (TagKey toolType : getAntimatterToolType().getToolTypes()) { + if (state.is(toolType)){ + isType = true; + break; + } + } + return isType && ToolUtils.isCorrectTierForDrops(getTier(stack), state); + } + default float getDefaultMiningSpeed(ItemStack stack){ return getTier(stack).getSpeed() * getAntimatterToolType().getMiningSpeedMultiplier(); } diff --git a/common/src/main/java/muramasa/antimatter/tool/MaterialSword.java b/common/src/main/java/muramasa/antimatter/tool/MaterialSword.java index d006e2d30..e48a80a8a 100644 --- a/common/src/main/java/muramasa/antimatter/tool/MaterialSword.java +++ b/common/src/main/java/muramasa/antimatter/tool/MaterialSword.java @@ -2,8 +2,11 @@ import com.google.common.collect.HashMultimap; import com.google.common.collect.Multimap; +import lombok.Getter; import muramasa.antimatter.AntimatterAPI; import muramasa.antimatter.Ref; +import muramasa.antimatter.behaviour.IBehaviour; +import muramasa.antimatter.behaviour.IDestroySpeed; import muramasa.antimatter.capability.energy.ItemEnergyHandler; import muramasa.antimatter.item.IContainerItem; import muramasa.antimatter.material.Material; @@ -13,7 +16,9 @@ import net.minecraft.core.NonNullList; import net.minecraft.network.chat.Component; import net.minecraft.tags.BlockTags; +import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; +import net.minecraft.world.InteractionResultHolder; import net.minecraft.world.entity.EquipmentSlot; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.ai.attributes.Attribute; @@ -35,6 +40,7 @@ import java.util.List; import java.util.Locale; +import java.util.Map; import java.util.function.Consumer; //@ParametersAreNonnullByDefault @@ -45,6 +51,11 @@ public class MaterialSword extends SwordItem implements IAntimatterTool, IContai protected AntimatterToolType type; protected AntimatterItemTier itemTier; + /** + * -- GETTER -- + * Returns -1 if its not a powered tool + */ + @Getter protected int energyTier; protected long maxEnergy; @@ -85,13 +96,6 @@ public AntimatterItemTier getAntimatterItemTier() { return itemTier; } - /** - * Returns -1 if its not a powered tool - **/ - public int getEnergyTier() { - return energyTier; - } - @NotNull @Override public ItemStack asItemStack(@NotNull Material primary, @NotNull Material secondary) { @@ -153,10 +157,24 @@ public boolean hurtEnemy(ItemStack stack, LivingEntity target, LivingEntity atta @Override public float getDestroySpeed(ItemStack stack, BlockState state) { - if (state.getBlock() == Blocks.COBWEB) return 15.0F; - return Utils.isToolEffective(this, stack, state) ? getTier(stack).getSpeed() : 1.0F; + float destroySpeed = isCorrectToolForDrops(stack, state) ? getDefaultMiningSpeed(stack) : 1.0F; + if (type.isPowered() && getCurrentEnergy(stack) == 0){ + destroySpeed = 0.0f; + } + for (Map.Entry> e : getAntimatterToolType().getBehaviours().entrySet()) { + IBehaviour b = e.getValue(); + if (!(b instanceof IDestroySpeed destroySpeed1)) continue; + float i = destroySpeed1.getDestroySpeed(this, destroySpeed, stack, state); + if (i > 0){ + destroySpeed = i; + break; + } + } + return destroySpeed; } + + @Override public boolean mineBlock(ItemStack stack, Level world, BlockState state, BlockPos pos, LivingEntity entity) { return onGenericBlockDestroyed(stack, world, state, pos, entity); @@ -167,6 +185,20 @@ public InteractionResult useOn(UseOnContext ctx) { return onGenericItemUse(ctx); } + @Override + public InteractionResult interactLivingEntity(ItemStack stack, Player player, LivingEntity interactionTarget, InteractionHand usedHand) { + return genericInteractLivingEntity(stack, player, interactionTarget, usedHand); + } + + @Override + public InteractionResultHolder use(Level level, Player player, InteractionHand usedHand) { + InteractionResultHolder result = onGenericRightclick(level, player, usedHand); + if (result.getResult().shouldAwardStats()){ + return result; + } + return super.use(level, player, usedHand); + } + @Override public boolean canAttackBlock(BlockState state, Level world, BlockPos pos, Player player) { return type.getBlockBreakability(); diff --git a/common/src/main/java/muramasa/antimatter/tool/MaterialTool.java b/common/src/main/java/muramasa/antimatter/tool/MaterialTool.java index ed76b8373..6d25aa42f 100644 --- a/common/src/main/java/muramasa/antimatter/tool/MaterialTool.java +++ b/common/src/main/java/muramasa/antimatter/tool/MaterialTool.java @@ -136,29 +136,6 @@ public boolean doesSneakBypassUse(ItemStack stack, LevelReader world, BlockPos p return Utils.doesStackHaveToolTypes(stack, AntimatterDefaultTools.WRENCH, AntimatterDefaultTools.SCREWDRIVER, AntimatterDefaultTools.CROWBAR, AntimatterDefaultTools.WIRE_CUTTER); // ??? } - public boolean isCorrectToolForDrops(ItemStack stack, BlockState state) { - AntimatterToolType type = this.getAntimatterToolType(); - if (type.getEffectiveMaterials().contains(state.getMaterial())) { - return true; - } - if (type.getEffectiveBlocks().contains(state.getBlock())) { - return true; - } - for (TagKey effectiveBlockTag : type.getEffectiveBlockTags()) { - if (state.is(effectiveBlockTag)){ - return true; - } - } - boolean isType = false; - for (TagKey toolType : getAntimatterToolType().getToolTypes()) { - if (state.is(toolType)){ - isType = true; - break; - } - } - return isType && ToolUtils.isCorrectTierForDrops(getTier(stack), state); - } - //fabric method public boolean isSuitableFor(ItemStack stack, BlockState state) { return this.isCorrectToolForDrops(stack, state); @@ -224,7 +201,7 @@ public boolean hurtEnemy(ItemStack stack, LivingEntity target, LivingEntity atta @Override public float getDestroySpeed(ItemStack stack, BlockState state) { - float destroySpeed = state.getBlock() == Blocks.AIR || isCorrectToolForDrops(stack, state) ? getDefaultMiningSpeed(stack) : 1.0F; + float destroySpeed = isCorrectToolForDrops(stack, state) ? getDefaultMiningSpeed(stack) : 1.0F; if (type.isPowered() && getCurrentEnergy(stack) == 0){ destroySpeed = 0.0f; }