Skip to content

Commit

Permalink
added some missing methods to MAterialSword
Browse files Browse the repository at this point in the history
  • Loading branch information
Trinsdar committed Dec 9, 2023
1 parent c6efec9 commit 9f9cfee
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 33 deletions.
23 changes: 23 additions & 0 deletions common/src/main/java/muramasa/antimatter/tool/IAntimatterTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Block> effectiveBlockTag : type.getEffectiveBlockTags()) {
if (state.is(effectiveBlockTag)){
return true;
}
}
boolean isType = false;
for (TagKey<Block> 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();
}
Expand Down
50 changes: 41 additions & 9 deletions common/src/main/java/muramasa/antimatter/tool/MaterialSword.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -35,6 +40,7 @@

import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.function.Consumer;

//@ParametersAreNonnullByDefault
Expand All @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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<String, IBehaviour<IAntimatterTool>> 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);
Expand All @@ -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<ItemStack> use(Level level, Player player, InteractionHand usedHand) {
InteractionResultHolder<ItemStack> 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();
Expand Down
25 changes: 1 addition & 24 deletions common/src/main/java/muramasa/antimatter/tool/MaterialTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Block> effectiveBlockTag : type.getEffectiveBlockTags()) {
if (state.is(effectiveBlockTag)){
return true;
}
}
boolean isType = false;
for (TagKey<Block> 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);
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 9f9cfee

Please sign in to comment.