Files
src
build.gradle
gradle.properties
manifest.kod
settings.gradle
Code
13 711
src/main/java/com/example/limbloss/LimbLossMod.java
package com.example.limbloss;
import com.example.limbloss.component.LimbComponent;
import com.example.limbloss.component.ModComponents;
import com.example.limbloss.effect.ModEffects;
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.UseItemCallback;
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Items;
import net.minecraft.command.CommandSource;
import net.minecraft.command.argument.EntityArgumentType;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.world.World;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.arguments.StringArgumentType;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class LimbLossMod implements ModInitializer {
public static final String MOD_ID = "limbloss";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
// Track previous enchanted golden apple counts per player
private static final Map<UUID, Integer> PREV_APPLE_COUNT = new HashMap<>();
// Track players who just had a grafted limb reject
private static final Map<UUID, Integer> REJECT_COOLDOWN = new HashMap<>();
@Override
public void onInitialize() {
ModEffects.initialize();
registerDamageHandler();
registerGraftHandler();
registerServerTick();
registerCommands();
}
private void registerDamageHandler() {
ServerLivingEntityEvents.AFTER_DAMAGE.register((entity, source, baseDamage, damageTaken, blocked) -> {
if (damageTaken <= 0 || blocked) return;
if (!(entity instanceof PlayerEntity player)) return;
if (!(entity.getEntityWorld() instanceof ServerWorld)) return;
// Chance to lose a limb: increases with damage taken
// Max ~30% chance per hit, scaled by damage
float chance = Math.min(0.3f, damageTaken / 40.0f);
if (player.getRandom().nextFloat() < chance) {
LimbComponent limb = ModComponents.LIMB.get(player);
if (limb.tryLoseRandomLimb(player.getRandom())) {
// Message the player
player.sendMessage(Text.translatable("message.limbloss.lost_limb"), true);
}
}
});
}
private void registerGraftHandler() {
UseItemCallback.EVENT.register((player, world, hand) -> {
if (world.isClient()) return ActionResult.PASS;
if (!(player instanceof ServerPlayerEntity serverPlayer)) return ActionResult.PASS;
var stack = player.getStackInHand(hand);
if (!stack.isOf(Items.ROTTEN_FLESH)) return ActionResult.PASS;
if (!player.isSneaking()) return ActionResult.PASS; // allow normal eating
LimbComponent limb = ModComponents.LIMB.get(player);
if (!limb.hasMissingNonEyeLimb()) {
player.sendMessage(Text.translatable("message.limbloss.no_limb_to_graft"), true);
return ActionResult.FAIL;
}
// Graft a random missing non-eye limb
limb.graftRandomLimb(player.getRandom());
stack.decrement(1);
// Apply graft rejection effect
player.addStatusEffect(new StatusEffectInstance(
ModEffects.GRAFT_REJECTION, 900, 0, false, true, true
));
player.sendMessage(Text.translatable("message.limbloss.grafted"), true);
return ActionResult.CONSUME; // prevent eating
});
}
private void registerServerTick() {
ServerTickEvents.END_SERVER_TICK.register(server -> {
for (ServerPlayerEntity player : server.getPlayerManager().getPlayerList()) {
UUID uuid = player.getUuid();
// 1. Apply debuffs based on missing limbs
LimbComponent limb = ModComponents.LIMB.get(player);
applyLimbDebuffs(player, limb);
// 2. Check for enchanted golden apple consumption
int currentApples = countEnchantedGoldenApples(player);
int prevApples = PREV_APPLE_COUNT.getOrDefault(uuid, 0);
if (currentApples < prevApples && limb.hasMissingLimbs()) {
limb.regrowAll();
player.sendMessage(Text.translatable("message.limbloss.regrown"), true);
}
PREV_APPLE_COUNT.put(uuid, currentApples);
// 3. Grafted limb rejection tick (every ~30 seconds)
Integer cooldown = REJECT_COOLDOWN.get(uuid);
if (cooldown == null) {
REJECT_COOLDOWN.put(uuid, 600);
} else if (cooldown <= 0) {
if (limb.hasGraftedLimbs()) {
int rejectedCount = limb.tickRejection(player.getRandom());
if (rejectedCount > 0) {
player.addStatusEffect(new StatusEffectInstance(
StatusEffects.NAUSEA, 100, 0, false, true, true
));
player.sendMessage(Text.translatable("message.limbloss.rejection"), true);
}
}
REJECT_COOLDOWN.put(uuid, 600 + player.getRandom().nextInt(200));
} else {
REJECT_COOLDOWN.put(uuid, cooldown - 1);
}
}
});
}
private void applyLimbDebuffs(ServerPlayerEntity player, LimbComponent limb) {
World world = player.getEntityWorld();
// Missing arms → Mining Fatigue
if (!limb.hasArmLeft() || !limb.hasArmRight()) {
player.addStatusEffect(new StatusEffectInstance(
StatusEffects.MINING_FATIGUE, 100, 1, false, false, true
));
}
// Missing legs → Slowness
if (!limb.hasLegLeft() || !limb.hasLegRight()) {
player.addStatusEffect(new StatusEffectInstance(
StatusEffects.SLOWNESS, 100, 1, false, false, true
));
}
// Missing eyes → Blindness
if (!limb.hasEyeLeft() || !limb.hasEyeRight()) {
player.addStatusEffect(new StatusEffectInstance(
StatusEffects.BLINDNESS, 100, 0, false, false, true
));
}
}
private int countEnchantedGoldenApples(PlayerEntity player) {
int count = 0;
for (var stack : player.getInventory().getMainStacks()) {
if (stack.isOf(Items.ENCHANTED_GOLDEN_APPLE)) {
count += stack.getCount();
}
}
if (player.getOffHandStack().isOf(Items.ENCHANTED_GOLDEN_APPLE)) {
count += player.getOffHandStack().getCount();
}
return count;
}
private void registerCommands() {
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
dispatcher.register(CommandManager.literal("limbs")
.executes(context -> {
ServerPlayerEntity player = context.getSource().getPlayerOrThrow();
LimbComponent limb = ModComponents.LIMB.get(player);
sendLimbStatus(context.getSource(), player, limb);
return Command.SINGLE_SUCCESS;
})
.then(CommandManager.literal("check")
.requires(source -> source.hasPermissionLevel(2))
.then(CommandManager.argument("target", EntityArgumentType.player())
.executes(context -> {
ServerPlayerEntity target = EntityArgumentType.getPlayer(context, "target");
LimbComponent limb = ModComponents.LIMB.get(target);
sendLimbStatus(context.getSource(), target, limb);
return Command.SINGLE_SUCCESS;
})
)