Skip to content

Commit

Permalink
Go Fish crash fixed (ty Aqua)
Browse files Browse the repository at this point in the history
  • Loading branch information
MysticKoko committed Apr 18, 2024
1 parent ba770ff commit 317a249
Showing 1 changed file with 20 additions and 27 deletions.
47 changes: 20 additions & 27 deletions src/main/java/dev/hybridlabs/aquatic/mixin/FishingRodItemMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,46 @@
import dev.hybridlabs.aquatic.access.CustomFishingBobberEntityData;
import dev.hybridlabs.aquatic.tag.HybridAquaticItemTags;
import dev.hybridlabs.aquatic.utils.HandUtils;
import net.minecraft.entity.Entity;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.projectile.FishingBobberEntity;
import net.minecraft.item.FishingRodItem;
import net.minecraft.item.ItemStack;
import net.minecraft.stat.Stats;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.world.World;
import net.minecraft.world.event.GameEvent;
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.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(FishingRodItem.class)
public abstract class FishingRodItemMixin {
@Unique
private PlayerEntity usedPlayer;

@Unique
private Hand usedHand;

// Gets all the required objects
@Inject(method = "use", at = @At("HEAD"))
private void playerGetter(World world, PlayerEntity user, Hand hand, CallbackInfoReturnable<TypedActionResult<ItemStack>> cir) {
this.usedPlayer = user;
this.usedHand = hand;
}

// If item in the opposite hand has lure item, it gets put in the fishing rod
@Redirect(
@Inject(
method = "use",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/world/World;spawnEntity(Lnet/minecraft/entity/Entity;)Z"
)
),
cancellable = true
)
private boolean spawnEntityRedirect(World world, Entity entity) {
FishingBobberEntity bobber = (FishingBobberEntity) entity;
Hand opposingHand = HandUtils.getOpposingHand(usedHand);
ItemStack opposingHandItemStack = usedPlayer.getStackInHand(opposingHand);
private void redirectFix(World world, PlayerEntity user, Hand hand, CallbackInfoReturnable<TypedActionResult<ItemStack>> cir) {
ItemStack mainHandItemStack = user.getStackInHand(hand);
ItemStack opposingHandItemStack = user.getStackInHand(HandUtils.getOpposingHand(hand));

if (opposingHandItemStack.isIn(HybridAquaticItemTags.INSTANCE.getLURE_ITEMS())) {
((CustomFishingBobberEntityData) bobber).hybrid_aquatic$setLureItem(opposingHandItemStack.copyAndEmpty());
}
int lureLevel = EnchantmentHelper.getLure(mainHandItemStack);
int luckLevel = EnchantmentHelper.getLuckOfTheSea(mainHandItemStack);
FishingBobberEntity customBobber = new FishingBobberEntity(user, world, lureLevel, luckLevel);

((CustomFishingBobberEntityData) customBobber).hybrid_aquatic$setLureItem(opposingHandItemStack.copyAndEmpty());
world.spawnEntity(customBobber);

return world.spawnEntity(bobber);
user.incrementStat(Stats.USED.getOrCreateStat(((FishingRodItem) (Object) this)));
user.emitGameEvent(GameEvent.ITEM_INTERACT_START);
cir.setReturnValue(TypedActionResult.success(mainHandItemStack, world.isClient()));
}
}
}
}

0 comments on commit 317a249

Please sign in to comment.