Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Axe Item Ability Crashes #154

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

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);
}
}
Loading