package com.mendingmana;
import com.mendingmana.network.ManaNetworking;
import com.mendingmana.network.ManaServerBridge;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.minecraft.entity.effect.StatusEffect;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import java.util.*;
public class MendingManaMod implements ModInitializer {
public static final String MOD_ID = "mendingmana";
public static final int INTERVAL_TICKS = 20 * 60 * 2;
public static final int EFFECT_DURATION_TICKS = 20 * 60 * 2;
private final Map<UUID, Integer> timers = new HashMap<>();
private final Map<UUID, List<ManaChoice>> pendingChoices = new HashMap<>();
private final Random random = new Random();
private static final List<ManaChoice> CHOICES = List.of(
new ManaChoice("swift", "MANA CEPAT", "Lari dan bergerak lebih cepat", true, StatusEffects.SPEED, 1, EFFECT_DURATION_TICKS),
new ManaChoice("iron", "MANA BESI", "Pertahanan tubuh meningkat", true, StatusEffects.RESISTANCE, 0, EFFECT_DURATION_TICKS),
new ManaChoice("vital", "MANA VITAL", "Regenerasi kesehatan", true, StatusEffects.REGENERATION, 0, EFFECT_DURATION_TICKS),
new ManaChoice("haste", "MANA CEPAT TANGAN", "Menambang lebih cepat", true, StatusEffects.HASTE, 1, EFFECT_DURATION_TICKS),
new ManaChoice("weak", "MANA LEMAH", "Seranganmu menjadi lebih lemah", false, StatusEffects.WEAKNESS, 0, EFFECT_DURATION_TICKS),
new ManaChoice("slow", "MANA LAMBAT", "Gerakanmu menjadi lebih lambat", false, StatusEffects.SLOWNESS, 0, EFFECT_DURATION_TICKS),
new ManaChoice("heavy", "MANA BERAT", "Menambang menjadi lebih lambat", false, StatusEffects.MINING_FATIGUE, 0, EFFECT_DURATION_TICKS),
new ManaChoice("blind", "MANA GELAP", "Pandanganmu menjadi gelap", false, StatusEffects.BLINDNESS, 0, 20 * 10)
);
@Override
public void onInitialize() {
ManaServerBridge.set(this);
ManaNetworking.registerCommon();
ServerTickEvents.END_SERVER_TICK.register(this::tick);
System.out.println("[Mending Mana] Loaded for Minecraft 1.21.9");
}
private void tick(MinecraftServer server) {
Set<UUID> online = new HashSet<>();
for (ServerPlayerEntity player : server.getPlayerManager().getPlayerList()) {
UUID id = player.getUuid();
online.add(id);
int elapsed = timers.getOrDefault(id, 0) + 1;
if (elapsed % 20 == 0) {
ManaNetworking.sendTimer(player, Math.max(0, (INTERVAL_TICKS - elapsed) / 20));
}
if (elapsed >= INTERVAL_TICKS) {
elapsed = 0;
openChoice(player);
ManaNetworking.sendTimer(player, INTERVAL_TICKS / 20);
}
timers.put(id, elapsed);
}
timers.keySet().removeIf(id -> !online.contains(id));
pendingChoices.keySet().removeIf(id -> !online.contains(id));
}
private void openChoice(ServerPlayerEntity player) {
List<ManaChoice> shuffled = new ArrayList<>(CHOICES);
Collections.shuffle(shuffled, random);
List<ManaChoice> selected = List.copyOf(shuffled.subList(0, 3));
pendingChoices.put(player.getUuid(), selected);
ManaNetworking.openChoice(player, selected.get(0).id(), selected.get(1).id(), selected.get(2).id());
}
public boolean choose(ServerPlayerEntity player, int index) {
List<ManaChoice> choices = pendingChoices.get(player.getUuid());
if (choices == null || index < 0 || index >= choices.size()) return false;
ManaChoice choice = choices.get(index);
player.addStatusEffect(new StatusEffectInstance(choice.effect(), choice.duration(), choice.amplifier(), false, true, true));
pendingChoices.remove(player.getUuid());
player.sendMessage(Text.literal("Mana terpilih: " + choice.title()), true);
return true;
}
public record ManaChoice(String id, String title, String description, boolean beneficial,
StatusEffect effect, int amplifier, int duration) { }
}