Skip to content

Commit 25e47d0

Browse files
committed
Use Share for FishingRodItemMixin
1 parent af8f99e commit 25e47d0

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

src/mixins/java/org/spongepowered/common/mixin/core/world/item/FishingRodItemMixin.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
*/
2525
package org.spongepowered.common.mixin.core.world.item;
2626

27+
import com.llamalad7.mixinextras.sugar.Share;
28+
import com.llamalad7.mixinextras.sugar.ref.LocalRef;
2729
import net.minecraft.server.level.ServerLevel;
2830
import net.minecraft.world.InteractionHand;
2931
import net.minecraft.world.InteractionResult;
@@ -35,8 +37,8 @@
3537
import net.minecraft.world.item.ItemStack;
3638
import net.minecraft.world.item.enchantment.EnchantmentHelper;
3739
import net.minecraft.world.level.Level;
38-
import org.checkerframework.checker.nullness.qual.Nullable;
3940
import org.spongepowered.api.entity.projectile.FishingBobber;
41+
import org.spongepowered.api.event.CauseStackManager;
4042
import org.spongepowered.api.event.SpongeEventFactory;
4143
import org.spongepowered.asm.mixin.Mixin;
4244
import org.spongepowered.asm.mixin.injection.At;
@@ -49,11 +51,9 @@
4951
@Mixin(FishingRodItem.class)
5052
public abstract class FishingRodItemMixin {
5153

52-
@Nullable private FishingHook impl$fishHook;
53-
5454
@Inject(method = "use", at = @At(value = "INVOKE", shift = At.Shift.AFTER,
5555
target = "Lnet/minecraft/world/entity/projectile/FishingHook;retrieve(Lnet/minecraft/world/item/ItemStack;)I"), cancellable = true)
56-
private void cancelHookRetraction(Level world, Player player, InteractionHand hand, CallbackInfoReturnable<InteractionResultHolder<ItemStack>> cir) {
56+
private void impl$cancelHookRetraction(Level world, Player player, InteractionHand hand, CallbackInfoReturnable<InteractionResultHolder<ItemStack>> cir) {
5757
if (player.fishing != null) {
5858
// Event was cancelled
5959
cir.setReturnValue(new InteractionResultHolder<>(InteractionResult.SUCCESS, player.getItemInHand(hand)));
@@ -63,7 +63,8 @@ private void cancelHookRetraction(Level world, Player player, InteractionHand ha
6363
@Inject(method = "use", at = @At(value = "INVOKE",
6464
target = "Lnet/minecraft/world/level/Level;playSound(Lnet/minecraft/world/entity/player/Player;DDDLnet/minecraft/sounds/SoundEvent;Lnet/minecraft/sounds/SoundSource;FF)V", ordinal = 1),
6565
cancellable = true)
66-
private void onThrowEvent(Level level, Player player, InteractionHand hand, CallbackInfoReturnable<InteractionResultHolder<ItemStack>> cir) {
66+
private void impl$onThrowEvent(Level level, Player player, InteractionHand hand, CallbackInfoReturnable<InteractionResultHolder<ItemStack>> cir,
67+
@Share("entity") LocalRef<FishingHook> entityRef) {
6768
if (level.isClientSide) {
6869
// Only fire event on server-side to avoid crash on client
6970
return;
@@ -75,23 +76,23 @@ private void onThrowEvent(Level level, Player player, InteractionHand hand, Call
7576
int $$7 = EnchantmentHelper.getFishingLuckBonus(serverLevel, itemstack, player);
7677
FishingHook fishHook = new FishingHook(player, level, $$7, $$6);
7778

78-
PhaseTracker.getInstance().pushCause(player);
79-
if (SpongeCommon.post(SpongeEventFactory.createFishingEventStart(PhaseTracker.getInstance().currentCause(), (FishingBobber) fishHook))) {
80-
fishHook.remove(Entity.RemovalReason.DISCARDED); // Bye
81-
cir.setReturnValue(new InteractionResultHolder<>(InteractionResult.SUCCESS, player.getItemInHand(hand)));
82-
} else {
83-
this.impl$fishHook = fishHook;
79+
try (final CauseStackManager.StackFrame frame = PhaseTracker.getInstance().pushCauseFrame()) {
80+
frame.pushCause(player);
81+
if (SpongeCommon.post(SpongeEventFactory.createFishingEventStart(PhaseTracker.getInstance().currentCause(), (FishingBobber) fishHook))) {
82+
fishHook.remove(Entity.RemovalReason.DISCARDED); // Bye
83+
cir.setReturnValue(new InteractionResultHolder<>(InteractionResult.SUCCESS, player.getItemInHand(hand)));
84+
} else {
85+
entityRef.set(fishHook);
86+
}
8487
}
85-
PhaseTracker.getInstance().popCause();
8688
}
8789
}
8890

8991
@Redirect(method = "use", at = @At(value = "NEW", target = "net/minecraft/world/entity/projectile/FishingHook"))
90-
private FishingHook onNewEntityFishHook(Player p_i50220_1_, Level p_i50220_2_, int p_i50220_3_, int p_i50220_4_) {
92+
private FishingHook impl$onNewEntityFishHook(Player p_i50220_1_, Level p_i50220_2_, int p_i50220_3_, int p_i50220_4_,
93+
@Share("entity") LocalRef<FishingHook> entityRef) {
9194
// Use the fish hook we created for the event
92-
FishingHook fishHook = this.impl$fishHook;
93-
this.impl$fishHook = null;
94-
return fishHook;
95+
return entityRef.get();
9596
}
9697

9798
}

0 commit comments

Comments
 (0)