Juan Sebastián Jaramillo Morales
  1. Channeling Mechanics: The Channeling enchantment should function whenever there is a thunderstorm, regardless of whether the sky is clear or obstructed by blocks above the entity (unlike in the Java Edition, where a block overhead prevents lightning from striking). Furthermore, it should trigger if the trident is thrown at a block or into water near an entity, rather than requiring a direct hit.
  2. Loyalty Enchantment: If a trident with the Loyalty enchantment is thrown into the void, it should return to the player after a certain amount of time.
  3. Attack Animation: The trident should display a "thrusting" animation when used for melee attacks.
  4. Damage and Water Interaction: The Impalement enchantment should deal additional damage to any creature or player that is touching water or is in the rain—not just to creatures native to aquatic environments.
Kodari AI
KodariNov 10 at 7:25 pm 6
TridentOverhaul
Overhauls trident mechanics in Minecraft Fabric. Channeling triggers during thunderstorms regardless of sky obstruction and activates when thrown at blocks or water near entities. Loyalty tridents return from the void after a delay. Adds a thrusting melee attack animation. Impalement deals bonus damage to any entity touching water or in rain, not just aquatic mobs.

Implemented the trident improvements: Channeling can strike under cover and near block or water impacts, Loyalty returns tridents from the void, melee attacks use a thrust animation, and Impaling also damages wet targets.

These changes apply to vanilla tridents and enchantments, which remain obtainable through normal survival gameplay.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
gradle.properties
settings.gradle
Code
8 215
src/main/java/dev/kodari/trident/mixin/ThrownTridentMixin.java
package dev.kodari.trident.mixin;

import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntitySpawnReason;
import net.minecraft.world.entity.EntityTypes;
import net.minecraft.world.entity.LightningBolt;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.projectile.arrow.ThrownTrident;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.EntityHitResult;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
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.CallbackInfo;

@Mixin(ThrownTrident.class)
abstract class ThrownTridentMixin {
    @Shadow private boolean dealtDamage;
    @Unique private boolean tridentImprovements$waterStrike;

    @Unique
    private boolean tridentImprovements$hasEnchantment(net.minecraft.resources.ResourceKey<net.minecraft.world.item.enchantment.Enchantment> key) {
        ThrownTrident trident = (ThrownTrident) (Object) this;
        for (var entry : trident.getWeaponItem().getEnchantments().entrySet()) {
            if (entry.getKey().is(key) && entry.getIntValue() > 0) return true;
        }
        return false;
    }

    @Unique
    private void tridentImprovements$strike(ServerLevel level, Entity target) {
        ThrownTrident trident = (ThrownTrident) (Object) this;
        LightningBolt bolt = EntityTypes.LIGHTNING_BOLT.create(level, EntitySpawnReason.EVENT);
        if (bolt != null) {
            bolt.snapTo(target.position());
            if (trident.getOwner() instanceof ServerPlayer player) bolt.setCause(player);
            level.addFreshEntity(bolt);
            trident.playSound(SoundEvents.TRIDENT_THUNDER.value(), 5.0F, 1.0F);
        }
    }

    @Unique
    private void tridentImprovements$strikeNearby(ServerLevel level) {
        ThrownTrident trident = (ThrownTrident) (Object) this;
        if (!level.isThundering() || !tridentImprovements$hasEnchantment(Enchantments.CHANNELING)) return;
        LivingEntity closest = null;
        double distance = 9.0;
        for (LivingEntity candidate : level.getEntitiesOfClass(LivingEntity.class, trident.getBoundingBox().inflate(3.0))) {
            double candidateDistance = candidate.distanceToSqr(trident);
            if (candidate.isAlive() && candidate != trident.getOwner() && candidateDistance <= distance) {
                closest = candidate;
                distance = candidateDistance;
            }
        }
        if (closest != null) {
            tridentImprovements$strike(level, closest);
            tridentImprovements$waterStrike = true;
        }
    }

    @Inject(method = "tick", at = @At("HEAD"))
    private void tridentImprovements$tick(CallbackInfo ci) {
        ThrownTrident trident = (ThrownTrident) (Object) this;
        if (!(trident.level() instanceof ServerLevel level)) return;
        if (trident.getY() < level.getMinY() - 10 && trident.tickCount >= 20
                && trident.getOwner() != null && tridentImprovements$hasEnchantment(Enchantments.LOYALTY)) {
            dealtDamage = true;
        }
        if (!dealtDamage && !tridentImprovements$waterStrike && trident.isInWater()) {
            tridentImprovements$strikeNearby(level);
        }
    }

    @Inject(method = "onHitEntity", at = @At("HEAD"))
    private void tridentImprovements$hitEntity(EntityHitResult hit, CallbackInfo ci) {
        ThrownTrident trident = (ThrownTrident) (Object) this;
        Entity target = hit.getEntity();
        if (trident.level() instanceof ServerLevel level && level.isThundering()
                && !level.canSeeSky(target.blockPosition()) && tridentImprovements$hasEnchantment(Enchantments.CHANNELING)) {
            tridentImprovements$strike(level, target);
        }
    }

    @Inject(method = "hitBlockEnchantmentEffects", at = @At("HEAD"))
    private void tridentImprovements$hitBlock(ServerLevel level, BlockHitResult hit, ItemStack weapon, CallbackInfo ci) {
        if (!(level.getBlockState(hit.getBlockPos()).getBlock() instanceof net.minecraft.world.level.block.LightningRodBlock)
                || !level.canSeeSky(hit.getBlockPos())) {
            tridentImprovements$strikeNearby(level);
        }
    }
}