Files
src
build.gradle
gradle.properties
settings.gradle
Code
7 180
src/main/java/com/axeshieldstunner/AxeShieldStunner.java
package com.axeshieldstunner;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.player.AttackEntityCallback;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.AxeItem;
import net.minecraft.item.ItemStack;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.EntityHitResult;
import net.minecraft.world.World;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AxeShieldStunner implements ModInitializer {
public static final String MOD_ID = "axeshieldstunner";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
@Override
public void onInitialize() {
LOGGER.info("Axe Shield Stunner initialized");
AttackEntityCallback.EVENT.register((player, world, hand, entity, hitResult) -> {
if (world.isClient) {
return ActionResult.PASS;
}
if (shouldStunShield(player, entity, hand)) {
applyShieldStun((LivingEntity) entity, player, world);
}
return ActionResult.PASS;
});
}
private boolean shouldStunShield(PlayerEntity attacker, Entity target, Hand hand) {
if (!(target instanceof LivingEntity livingTarget)) {
return false;
}
ItemStack weapon = attacker.getStackInHand(hand);
if (!(weapon.getItem() instanceof AxeItem)) {
return false;
}
if (!livingTarget.isBlocking()) {
return false;
}
return true;
}
private void applyShieldStun(LivingEntity target, PlayerEntity attacker, World world) {
if (target instanceof PlayerEntity playerTarget) {
playerTarget.getItemCooldownManager().set(target.getActiveItem().getItem(), 100);
}
target.clearActiveItem();
world.sendEntityStatus(target, (byte) 30);
target.playSound(SoundEvents.ITEM_SHIELD_BREAK, 0.8F, 0.8F + world.random.nextFloat() * 0.4F);
}
}