Skip to content

Commit

Permalink
Extend Paxels from AxeItem to allow Axe enchantments (#7903)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChampionAsh5357 authored Nov 14, 2023
1 parent 8224c0b commit e145b90
Showing 1 changed file with 43 additions and 53 deletions.
96 changes: 43 additions & 53 deletions src/tools/java/mekanism/tools/common/item/ItemMekanismPaxel.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.AxeItem;
import net.minecraft.world.item.DiggerItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
Expand All @@ -37,26 +36,27 @@
import net.minecraft.world.level.block.CampfireBlock;
import net.minecraft.world.level.block.LevelEvent;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.common.TierSortingRegistry;
import net.minecraftforge.common.ToolAction;
import net.minecraftforge.common.ToolActions;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class ItemMekanismPaxel extends DiggerItem implements IHasRepairType, IAttributeRefresher {
public class ItemMekanismPaxel extends AxeItem implements IHasRepairType, IAttributeRefresher {

private static final ToolAction PAXEL_DIG = ToolAction.get("paxel_dig");

private final IPaxelMaterial material;
private final AttributeCache attributeCache;

public ItemMekanismPaxel(MaterialCreator material, Item.Properties properties) {
super(material.getPaxelDamage(), material.getPaxelAtkSpeed(), material, ToolsTags.Blocks.MINEABLE_WITH_PAXEL, properties);
super(material, material.getPaxelDamage(), material.getPaxelAtkSpeed(), properties);
this.material = material;
this.attributeCache = new AttributeCache(this, material.attackDamage, material.paxelDamage, material.paxelAtkSpeed);
}

public ItemMekanismPaxel(VanillaPaxelMaterialCreator material, Item.Properties properties) {
super(material.getPaxelDamage(), material.getPaxelAtkSpeed(), material.getVanillaTier(), ToolsTags.Blocks.MINEABLE_WITH_PAXEL, properties);
super(material.getVanillaTier(), material.getPaxelDamage(), material.getPaxelAtkSpeed(), properties);
this.material = material;
//Don't add the material's damage as a listener as the vanilla component is not configurable
this.attributeCache = new AttributeCache(this, material.paxelDamage, material.paxelAtkSpeed);
Expand All @@ -75,13 +75,13 @@ public float getAttackDamage() {

@Override
public boolean canPerformAction(ItemStack stack, ToolAction action) {
return action == PAXEL_DIG || ToolActions.DEFAULT_AXE_ACTIONS.contains(action) || ToolActions.DEFAULT_PICKAXE_ACTIONS.contains(action) ||
ToolActions.DEFAULT_SHOVEL_ACTIONS.contains(action);
return action == PAXEL_DIG || ToolActions.DEFAULT_PICKAXE_ACTIONS.contains(action) ||
ToolActions.DEFAULT_SHOVEL_ACTIONS.contains(action) || super.canPerformAction(stack, action);
}

@Override
public float getDestroySpeed(@NotNull ItemStack stack, @NotNull BlockState state) {
return super.getDestroySpeed(stack, state) == 1 ? 1 : material.getPaxelEfficiency();
return state.is(ToolsTags.Blocks.MINEABLE_WITH_PAXEL) ? material.getPaxelEfficiency() : 1;
}

/**
Expand All @@ -92,32 +92,36 @@ public float getDestroySpeed(@NotNull ItemStack stack, @NotNull BlockState state
@NotNull
@Override
public InteractionResult useOn(UseOnContext context) {
// Attempt to use the paxel as an axe
InteractionResult axeResult = super.useOn(context);
if (axeResult != InteractionResult.PASS) {
return axeResult;
}

Level world = context.getLevel();
BlockPos blockpos = context.getClickedPos();
Player player = context.getPlayer();
BlockState blockstate = world.getBlockState(blockpos);
BlockState resultToSet = useAsAxe(blockstate, context);
if (resultToSet == null) {
//We cannot strip the item that was right-clicked, so attempt to use the paxel as a shovel
if (context.getClickedFace() == Direction.DOWN) {
return InteractionResult.PASS;
}
BlockState foundResult = blockstate.getToolModifiedState(context, ToolActions.SHOVEL_FLATTEN, false);
if (foundResult != null && world.isEmptyBlock(blockpos.above())) {
//We can flatten the item as a shovel
world.playSound(player, blockpos, SoundEvents.SHOVEL_FLATTEN, SoundSource.BLOCKS, 1.0F, 1.0F);
resultToSet = foundResult;
} else if (blockstate.getBlock() instanceof CampfireBlock && blockstate.getValue(CampfireBlock.LIT)) {
//We can use the paxel as a shovel to extinguish a campfire
if (!world.isClientSide) {
world.levelEvent(null, LevelEvent.SOUND_EXTINGUISH_FIRE, blockpos, 0);
}
CampfireBlock.dowse(player, world, blockpos, blockstate);
resultToSet = blockstate.setValue(CampfireBlock.LIT, false);
}
if (resultToSet == null) {
return InteractionResult.PASS;
BlockState resultToSet = null;
//We cannot strip the item that was right-clicked, so attempt to use the paxel as a shovel
if (context.getClickedFace() == Direction.DOWN) {
return InteractionResult.PASS;
}
BlockState foundResult = blockstate.getToolModifiedState(context, ToolActions.SHOVEL_FLATTEN, false);
if (foundResult != null && world.isEmptyBlock(blockpos.above())) {
//We can flatten the item as a shovel
world.playSound(player, blockpos, SoundEvents.SHOVEL_FLATTEN, SoundSource.BLOCKS, 1.0F, 1.0F);
resultToSet = foundResult;
} else if (blockstate.getBlock() instanceof CampfireBlock && blockstate.getValue(CampfireBlock.LIT)) {
//We can use the paxel as a shovel to extinguish a campfire
if (!world.isClientSide) {
world.levelEvent(null, LevelEvent.SOUND_EXTINGUISH_FIRE, blockpos, 0);
}
CampfireBlock.dowse(player, world, blockpos, blockstate);
resultToSet = blockstate.setValue(CampfireBlock.LIT, false);
}
if (resultToSet == null) {
return InteractionResult.PASS;
}
if (!world.isClientSide) {
ItemStack stack = context.getItemInHand();
Expand All @@ -132,31 +136,6 @@ public InteractionResult useOn(UseOnContext context) {
return InteractionResult.sidedSuccess(world.isClientSide);
}

@Nullable
private BlockState useAsAxe(BlockState state, UseOnContext context) {
Level world = context.getLevel();
BlockPos blockpos = context.getClickedPos();
Player player = context.getPlayer();
BlockState resultToSet = state.getToolModifiedState(context, ToolActions.AXE_STRIP, false);
if (resultToSet != null) {
world.playSound(player, blockpos, SoundEvents.AXE_STRIP, SoundSource.BLOCKS, 1.0F, 1.0F);
return resultToSet;
}
resultToSet = state.getToolModifiedState(context, ToolActions.AXE_SCRAPE, false);
if (resultToSet != null) {
world.playSound(player, blockpos, SoundEvents.AXE_SCRAPE, SoundSource.BLOCKS, 1.0F, 1.0F);
world.levelEvent(player, LevelEvent.PARTICLES_SCRAPE, blockpos, 0);
return resultToSet;
}
resultToSet = state.getToolModifiedState(context, ToolActions.AXE_WAX_OFF, false);
if (resultToSet != null) {
world.playSound(player, blockpos, SoundEvents.AXE_WAX_OFF, SoundSource.BLOCKS, 1.0F, 1.0F);
world.levelEvent(player, LevelEvent.PARTICLES_WAX_OFF, blockpos, 0);
return resultToSet;
}
return null;
}

@Override
public int getEnchantmentValue() {
return material.getPaxelEnchantability();
Expand Down Expand Up @@ -189,4 +168,15 @@ public void addToBuilder(ImmutableMultimap.Builder<Attribute, AttributeModifier>
builder.put(Attributes.ATTACK_DAMAGE, new AttributeModifier(BASE_ATTACK_DAMAGE_UUID, "Tool modifier", getAttackDamage(), Operation.ADDITION));
builder.put(Attributes.ATTACK_SPEED, new AttributeModifier(BASE_ATTACK_SPEED_UUID, "Tool modifier", material.getPaxelAtkSpeed(), Operation.ADDITION));
}

// Need to override both method as DiggerItem performs two different behaviors
@Override
public boolean isCorrectToolForDrops(BlockState state) {
return state.is(ToolsTags.Blocks.MINEABLE_WITH_PAXEL) && TierSortingRegistry.isCorrectTierForDrops(getTier(), state);
}

@Override
public boolean isCorrectToolForDrops(ItemStack stack, BlockState state) {
return state.is(ToolsTags.Blocks.MINEABLE_WITH_PAXEL) && TierSortingRegistry.isCorrectTierForDrops(getTier(), state);
}
}

0 comments on commit e145b90

Please sign in to comment.