diff --git a/modules/base/src/main/java/io/github/fabricators_of_create/porting_lib/mixin/common/AxeItemMixin.java b/modules/base/src/main/java/io/github/fabricators_of_create/porting_lib/mixin/common/AxeItemMixin.java deleted file mode 100644 index 3ff8456a..00000000 --- a/modules/base/src/main/java/io/github/fabricators_of_create/porting_lib/mixin/common/AxeItemMixin.java +++ /dev/null @@ -1,44 +0,0 @@ -package io.github.fabricators_of_create.porting_lib.mixin.common; - -import com.llamalad7.mixinextras.injector.wrapoperation.Operation; -import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; - -import com.llamalad7.mixinextras.sugar.Share; - -import com.llamalad7.mixinextras.sugar.ref.LocalRef; - -import io.github.fabricators_of_create.porting_lib.tool.ItemAbilities; -import io.github.fabricators_of_create.porting_lib.util.PortingHooks; -import net.minecraft.world.InteractionResult; -import net.minecraft.world.item.AxeItem; - -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.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; - -import java.util.Optional; - -// This really should be in the item abilities modules fix this TODO: fix this on 1.21 -@Mixin(AxeItem.class) -public class AxeItemMixin { - @Inject(method = "useOn", at = @At("HEAD")) - private void shareUseOnContext(UseOnContext context, CallbackInfoReturnable cir, @Share("useOnContext") LocalRef sharedContext) { - sharedContext.set(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 onStripToolAction(AxeItem instance, BlockState blockState, Operation> original, @Share("useOnContext") LocalRef sharedContext) { - BlockState eventState = PortingHooks.onToolUse(blockState, sharedContext.get(), 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 onScrapeToolAction(BlockState blockState, Operation> original, @Share("useOnContext") LocalRef sharedContext) { - BlockState eventState = PortingHooks.onToolUse(blockState, sharedContext.get(), ItemAbilities.AXE_SCRAPE, false); - return eventState != blockState ? Optional.ofNullable(eventState) : original.call(blockState); - } -} diff --git a/modules/base/src/main/java/io/github/fabricators_of_create/porting_lib/util/PortingHooks.java b/modules/base/src/main/java/io/github/fabricators_of_create/porting_lib/util/PortingHooks.java index ca68be4e..c774f951 100644 --- a/modules/base/src/main/java/io/github/fabricators_of_create/porting_lib/util/PortingHooks.java +++ b/modules/base/src/main/java/io/github/fabricators_of_create/porting_lib/util/PortingHooks.java @@ -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(); - } } diff --git a/modules/base/src/main/resources/porting_lib_base.mixins.json b/modules/base/src/main/resources/porting_lib_base.mixins.json index 02986263..c6c9b145 100644 --- a/modules/base/src/main/resources/porting_lib_base.mixins.json +++ b/modules/base/src/main/resources/porting_lib_base.mixins.json @@ -12,7 +12,6 @@ "common.AbstractMinecartMixin", "common.AbstractSkeletonMixin", "common.AnvilMenuMixin", - "common.AxeItemMixin", "common.BambooStalkBlockMixin", "common.BaseRailBlockMixin", "common.BeaconBlockEntityMixin", diff --git a/modules/item_abilities/src/main/java/io/github/fabricators_of_create/porting_lib/tool/ItemAbilityHooks.java b/modules/item_abilities/src/main/java/io/github/fabricators_of_create/porting_lib/tool/ItemAbilityHooks.java index 763f1609..f4032cf5 100644 --- a/modules/item_abilities/src/main/java/io/github/fabricators_of_create/porting_lib/tool/ItemAbilityHooks.java +++ b/modules/item_abilities/src/main/java/io/github/fabricators_of_create/porting_lib/tool/ItemAbilityHooks.java @@ -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(); } } diff --git a/modules/item_abilities/src/main/java/io/github/fabricators_of_create/porting_lib/tool/events/BlockToolModificationEvent.java b/modules/item_abilities/src/main/java/io/github/fabricators_of_create/porting_lib/tool/events/BlockToolModificationEvent.java index b6dbcbba..afa9216d 100644 --- a/modules/item_abilities/src/main/java/io/github/fabricators_of_create/porting_lib/tool/events/BlockToolModificationEvent.java +++ b/modules/item_abilities/src/main/java/io/github/fabricators_of_create/porting_lib/tool/events/BlockToolModificationEvent.java @@ -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. diff --git a/modules/item_abilities/src/main/java/io/github/fabricators_of_create/porting_lib/tool/mixin/AxeItemMixin.java b/modules/item_abilities/src/main/java/io/github/fabricators_of_create/porting_lib/tool/mixin/AxeItemMixin.java index 30140ccc..304e02d8 100644 --- a/modules/item_abilities/src/main/java/io/github/fabricators_of_create/porting_lib/tool/mixin/AxeItemMixin.java +++ b/modules/item_abilities/src/main/java/io/github/fabricators_of_create/porting_lib/tool/mixin/AxeItemMixin.java @@ -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 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 onStripToolAction(AxeItem instance, BlockState blockState, Operation> 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 onScrapeToolAction(BlockState blockState, Operation> 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 cir) { + porting_lib_item_abilities$context = null; + } + + @Override + public boolean port_lib$canPerformAction(ItemStack stack, ItemAbility ability) { + return ItemAbilities.DEFAULT_AXE_ACTIONS.contains(ability); + } }