Files
src
build.gradle
settings.gradle
Code
4 261
src/main/java/com/kodari/stormweapon/StormWeaponMod.java
package com.kodari.stormweapon;
import com.mojang.brigadier.Command;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.event.player.AttackEntityCallback;
import net.fabricmc.fabric.api.event.player.UseItemCallback;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.LoreComponent;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvent;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Formatting;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.util.math.Vec3d;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import static net.minecraft.server.command.CommandManager.literal;
public final class StormWeaponMod implements ModInitializer {
private static final int MAX_PUNCH_CHARGE = 100;
private static final int LEAP_CHARGE_TICKS = 20;
private static final String WEAPON_NAME = "Stormbreaker";
private final Map<UUID, Integer> punchCharge = new HashMap<>();
private final Map<UUID, Integer> leapCharge = new HashMap<>();
private final Set<UUID> slamReady = new HashSet<>();
private final Set<UUID> slamming = new HashSet<>();
private final Set<UUID> airborneSlam = new HashSet<>();
@Override
public void onInitialize() {
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) ->
dispatcher.register(literal("stormweapon")
.requires(source -> source.hasPermissionLevel(2))
.executes(context -> {
ServerPlayerEntity player = context.getSource().getPlayer();
ItemStack weapon = createWeapon();
if (!player.getInventory().insertStack(weapon)) {
player.dropItem(weapon, false);
}
context.getSource().sendFeedback(
() -> Text.literal("You received the Stormbreaker!"), false);
return Command.SINGLE_SUCCESS;
})));
UseItemCallback.EVENT.register((player, world, hand) -> {
ItemStack stack = player.getStackInHand(hand);
if (world.isClient || hand != Hand.MAIN_HAND || !isWeapon(stack)) {
return TypedActionResult.pass(stack);
}
UUID id = player.getUuid();
if (player.isSneaking()) {
clap((ServerPlayerEntity) player);
} else if (player.isOnGround()) {
if (!leapCharge.containsKey(id) && !slamReady.contains(id) && !slamming.contains(id)) {
leapCharge.put(id, 0);
player.sendMessage(Text.literal("Charging leap... right-click in the air to slam."), true);
}
} else if (slamReady.remove(id)) {
airborneSlam.remove(id);
slamming.add(id);
player.setVelocity(0, -3.0, 0);
playSound((ServerPlayerEntity) player, SoundEvents.ENTITY_WARDEN_SONIC_BOOM);
}
return TypedActionResult.success(stack);
});
AttackEntityCallback.EVENT.register((player, world, hand, entity, hitResult) -> {
if (world.isClient || hand != Hand.MAIN_HAND || !isWeapon(player.getMainHandStack())
|| !(entity instanceof LivingEntity target)) {
return ActionResult.PASS;
}
Integer charge = punchCharge.remove(player.getUuid());
if (charge == null || charge <= 0) {
return ActionResult.PASS;
}
target.damage(player.getDamageSources().playerAttack(player), 2.0F + charge * 0.08F);
target.setVelocity(knockbackFrom((ServerPlayerEntity) player, target,
1.4 + charge / 30.0, 0.7 + charge / 100.0));
player.sendMessage(Text.literal("Mega punch! " + charge + "%"), true);
playSound((ServerPlayerEntity) player, SoundEvents.ENTITY_PLAYER_ATTACK_KNOCKBACK);
return ActionResult.SUCCESS;
});
ServerTickEvents.END_SERVER_TICK.register(server -> {
for (ServerPlayerEntity player : server.getPlayerManager().getPlayerList()) {
tickPlayer(player);
}
});
}
private void tickPlayer(ServerPlayerEntity player) {
UUID id = player.getUuid();
if (slamReady.contains(id) && !player.isOnGround()) {
airborneSlam.add(id);
} else if (airborneSlam.contains(id) && player.isOnGround()) {
airborneSlam.remove(id);
slamReady.remove(id);
}
if (player.isSneaking() && isWeapon(player.getMainHandStack())) {
punchCharge.compute(id, (key, charge) -> Math.min(MAX_PUNCH_CHARGE, (charge == null ? 0 : charge) + 1));
}
Integer charge = leapCharge.get(id);
if (charge != null) {
if (!isWeapon(player.getMainHandStack()) || !player.isOnGround()) {
leapCharge.remove(id);
} else if (charge + 1 >= LEAP_CHARGE_TICKS) {
leapCharge.remove(id);
slamReady.add(id);
player.setVelocity(0, 1.65, 0);
player.sendMessage(Text.literal("Leap charged! Right-click in the air to slam."), true);
playSound(player, SoundEvents.ENTITY_FIREWORK_ROCKET_LAUNCH);
} else {
leapCharge.put(id, charge + 1);
}
}
if (slamming.contains(id) && player.isOnGround()) {
slamming.remove(id);
slamImpact(player);
}
}
private void clap(ServerPlayerEntity player) {
for (Entity entity : player.getWorld().getOtherEntities(player, player.getBoundingBox().expand(5, 3, 5))) {
if (!(entity instanceof LivingEntity target) || target.squaredDistanceTo(player) > 25.0) {
continue;
}
target.damage(player.getDamageSources().playerAttack(player), 8.0F);
target.setVelocity(knockbackFrom(player, target, 1.5, 0.55));
}
player.sendMessage(Text.literal("Clap!"), true);
player.getServerWorld().spawnParticles(ParticleTypes.EXPLOSION, player.getX(), player.getEyeY(),
player.getZ(), 1, 0.2, 0.2, 0.2, 0.0);
playSound(player, SoundEvents.ENTITY_GENERIC_EXPLODE);
}
private void slamImpact(ServerPlayerEntity player) {
for (Entity entity : player.getWorld().getOtherEntities(player, player.getBoundingBox().expand(6, 3, 6))) {
if (!(entity instanceof LivingEntity target) || target.squaredDistanceTo(player) > 36.0) {
continue;
}
target.damage(player.getDamageSources().playerAttack(player), 10.0F);
target.setVelocity(knockbackFrom(player, target, 1.2, 0.8));
}
player.sendMessage(Text.literal("Slam!"), true);
player.getServerWorld().spawnParticles(ParticleTypes.EXPLOSION, player.getX(), player.getY(),
player.getZ(), 2, 0.5, 0.2, 0.5, 0.0);
playSound(player, SoundEvents.ENTITY_GENERIC_EXPLODE);
}
private Vec3d knockbackFrom(ServerPlayerEntity player, Entity target, double horizontal, double vertical) {
Vec3d direction = target.getPos().subtract(player.getPos()).multiply(1, 0, 1);
if (direction.lengthSquared() < 0.01) {
direction = player.getRotationVector().multiply(1, 0, 1);
}
if (direction.lengthSquared() < 0.01) {
direction = new Vec3d(0, 0, 1);
}
return direction.normalize().multiply(horizontal).add(0, vertical, 0);
}
private ItemStack createWeapon() {
ItemStack weapon = new ItemStack(Items.DIAMOND_SWORD);
weapon.set(DataComponentTypes.CUSTOM_NAME,
Text.literal(WEAPON_NAME).formatted(Formatting.AQUA, Formatting.BOLD));
weapon.set(DataComponentTypes.LORE, new LoreComponent(List.of(
Text.literal("Sneak + right-click: damaging clap").formatted(Formatting.GRAY),
Text.literal("Right-click on the ground: charge and leap").formatted(Formatting.GRAY),
Text.literal("Right-click in the air: ground slam").formatted(Formatting.GRAY),
Text.literal("Sneak to charge, then hit: mega punch").formatted(Formatting.GRAY)
)));
return weapon;
}
private boolean isWeapon(ItemStack stack) {