Skip to content

Commit

Permalink
Merge pull request #154 from MerchantPug/fix/axe-item-ability
Browse files Browse the repository at this point in the history
Fix Axe Item Ability Crashes
  • Loading branch information
AlphaMode authored Jan 16, 2025
2 parents 9c87b8e + 42453b8 commit 1715c12
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 63 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,4 @@ public static boolean onGrindstoneTake(Container inputSlots, ContainerLevelAcces
});
return true;
}

@Nullable
public static BlockState onToolUse(BlockState originalState, UseOnContext context, ItemAbility itemAbility, boolean simulate) {
BlockToolModificationEvent event = new BlockToolModificationEvent(originalState, context, itemAbility, simulate);
return event.post() ? null : event.getFinalState();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"common.AbstractMinecartMixin",
"common.AbstractSkeletonMixin",
"common.AnvilMenuMixin",
"common.AxeItemMixin",
"common.BambooStalkBlockMixin",
"common.BaseRailBlockMixin",
"common.BeaconBlockEntityMixin",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public class ItemAbilityHooks {
@Nullable
public static BlockState onToolUse(BlockState originalState, UseOnContext context, ItemAbility itemAbility, boolean simulate) {
BlockToolModificationEvent event = new BlockToolModificationEvent(originalState, context, itemAbility, simulate);
BlockToolModificationEvent event = new BlockToolModificationEvent(originalState, context, itemAbility, simulate);
return event.post() ? null : event.getFinalState();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ public class BlockToolModificationEvent extends BlockEvent implements Cancellabl
private final boolean simulate;
private BlockState state;

public BlockToolModificationEvent(BlockState originalState, UseOnContext context, ItemAbility itemAbility, boolean simulate) {
super(context.getLevel(), context.getClickedPos(), originalState);
this.context = context;
this.state = originalState;
this.itemAbility = itemAbility;
this.simulate = simulate;
}
public BlockToolModificationEvent(BlockState originalState, UseOnContext context, ItemAbility itemAbility, boolean simulate) {
super(context.getLevel(), context.getClickedPos(), originalState);
this.context = context;
this.state = originalState;
this.itemAbility = itemAbility;
this.simulate = simulate;
}

/**
* @return the player using the tool.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,55 @@
package io.github.fabricators_of_create.porting_lib.tool.mixin;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import io.github.fabricators_of_create.porting_lib.tool.ItemAbility;
import io.github.fabricators_of_create.porting_lib.tool.ItemAbilities;
import io.github.fabricators_of_create.porting_lib.tool.ItemAbilityHooks;
import io.github.fabricators_of_create.porting_lib.tool.extensions.VanillaItemAbilityItem;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.item.AxeItem;

import net.minecraft.world.item.ItemStack;

import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.block.state.BlockState;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.Optional;

@Mixin(AxeItem.class)
public class AxeItemMixin implements VanillaItemAbilityItem {
@Override
public boolean port_lib$canPerformAction(ItemStack stack, ItemAbility ability) {
return ItemAbilities.DEFAULT_AXE_ACTIONS.contains(ability);
}
@Unique
private UseOnContext porting_lib_item_abilities$context;

@Inject(method = "useOn", at = @At("HEAD"))
private void shareUseOnContext(UseOnContext context, CallbackInfoReturnable<InteractionResult> cir) {
porting_lib_item_abilities$context = context;
}

@WrapOperation(method = "evaluateNewBlockState", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/item/AxeItem;getStripped(Lnet/minecraft/world/level/block/state/BlockState;)Ljava/util/Optional;"))
private Optional<BlockState> onStripToolAction(AxeItem instance, BlockState blockState, Operation<Optional<BlockState>> original) {
BlockState eventState = ItemAbilityHooks.onToolUse(blockState, porting_lib_item_abilities$context, ItemAbilities.AXE_STRIP, false);
return eventState != blockState ? Optional.ofNullable(eventState) : original.call(instance, blockState);
}

@WrapOperation(method = "evaluateNewBlockState", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/block/WeatheringCopper;getPrevious(Lnet/minecraft/world/level/block/state/BlockState;)Ljava/util/Optional;"))
private Optional<BlockState> onScrapeToolAction(BlockState blockState, Operation<Optional<BlockState>> original) {
BlockState eventState = ItemAbilityHooks.onToolUse(blockState, porting_lib_item_abilities$context, ItemAbilities.AXE_SCRAPE, false);
return eventState != blockState ? Optional.ofNullable(eventState) : original.call(blockState);
}

@Inject(method = "useOn", at = @At("RETURN"))
private void clearUseOnContext(UseOnContext context, CallbackInfoReturnable<InteractionResult> cir) {
porting_lib_item_abilities$context = null;
}

@Override
public boolean port_lib$canPerformAction(ItemStack stack, ItemAbility ability) {
return ItemAbilities.DEFAULT_AXE_ACTIONS.contains(ability);
}
}

0 comments on commit 1715c12

Please sign in to comment.