Files
src
build.gradle
gradle.properties
settings.gradle
Code
8 267
src/main/java/com/tridentbeam/TridentBeamMod.java
package com.tridentbeam;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.player.AttackBlockCallback;
import net.fabricmc.fabric.api.event.player.AttackEntityCallback;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.NbtComponent;
import net.minecraft.item.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Formatting;
import net.minecraft.util.Hand;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
public class TridentBeamMod implements ModInitializer {
public static final String MOD_ID = "tridentbeam";
// Players whose next trident throw should produce a beam
public static final Set<UUID> pendingChargedThrows = ConcurrentHashMap.newKeySet();
// Active TridentEntity UUIDs currently producing a beam
public static final Set<UUID> activeBreamTridents = ConcurrentHashMap.newKeySet();
@Override
public void onInitialize() {
AttackBlockCallback.EVENT.register((player, world, hand, pos, direction) -> {
if (!world.isClient() && hand == Hand.MAIN_HAND && player.isSneaking()) {
tryChargeHeldTrident(player.getMainHandStack(), player);
}
return ActionResult.PASS;
});
AttackEntityCallback.EVENT.register((player, world, hand, entity, hitResult) -> {
if (!world.isClient() && hand == Hand.MAIN_HAND && player.isSneaking()) {
tryChargeHeldTrident(player.getMainHandStack(), player);
}
return ActionResult.PASS;
});
}
private static void tryChargeHeldTrident(ItemStack stack, net.minecraft.entity.player.PlayerEntity player) {
if (!stack.isOf(Items.TRIDENT)) return;
NbtComponent existing = stack.get(DataComponentTypes.CUSTOM_DATA);
if (existing != null && existing.copyNbt().contains("tridentBeamCharged")) return;
NbtCompound nbt = existing != null ? existing.copyNbt() : new NbtCompound();
nbt.putBoolean("tridentBeamCharged", true);
stack.set(DataComponentTypes.CUSTOM_DATA, NbtComponent.of(nbt));
player.sendMessage(Text.literal("✦ Trident charged!").formatted(Formatting.RED), true);
}
public static boolean consumeChargeFlag(ItemStack stack) {
NbtComponent comp = stack.get(DataComponentTypes.CUSTOM_DATA);
if (comp == null || !comp.copyNbt().contains("tridentBeamCharged")) return false;
NbtCompound nbt = comp.copyNbt();
nbt.remove("tridentBeamCharged");
if (nbt.isEmpty()) {
stack.remove(DataComponentTypes.CUSTOM_DATA);
} else {
stack.set(DataComponentTypes.CUSTOM_DATA, NbtComponent.of(nbt));
}
return true;
}
}