GastinelGarde 0.1.0 : phase 0 et 1 (coeur pur, adaptateurs NeoForge, 62 tests)
Coeur pur : points d'infraction avec decroissance, paliers, exemptions, killaura (angle, cibles multiples), cadence et regularite, timer, enregistreur de traces JSONL, tolerance riptide reprise de GastinelClaims. Adaptateurs : configuration rechargeable, commandes /garde, mixins de lecture des paquets de mouvement. Tout demarre en observe. Jamais de bannissement automatique. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Cette révision appartient à :
révision
b07c6b56f2
44 fichiers modifiés
+2999
Aucun fichier ne correspond à votre recherche
@@ -0,0 +1,331 @@
|
||||
package com.gastinelgarde;
|
||||
|
||||
import com.gastinelgarde.core.EtatJoueur;
|
||||
import com.gastinelgarde.core.GardeConfig;
|
||||
import com.gastinelgarde.core.Moteur;
|
||||
import com.gastinelgarde.core.Sanction;
|
||||
import com.gastinelgarde.core.Suivi;
|
||||
import com.gastinelgarde.core.TraceFichier;
|
||||
import com.gastinelgarde.core.TraceLigne;
|
||||
import com.gastinelgarde.core.Verdict;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.time.Instant;
|
||||
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 net.minecraft.ChatFormatting;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.world.effect.MobEffects;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.item.UseAnim;
|
||||
import net.minecraft.world.item.Items;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.item.enchantment.EnchantmentHelper;
|
||||
import net.neoforged.fml.loading.FMLPaths;
|
||||
|
||||
/**
|
||||
* État d'exécution du mod (une seule instance par serveur) et pont entre Minecraft et le moteur pur.
|
||||
* Toute la logique de décision est dans {@code com.gastinelgarde.core} ; ici on ne fait que lire l'état du
|
||||
* serveur, appeler le moteur et appliquer le résultat.
|
||||
*/
|
||||
public final class Garde {
|
||||
|
||||
/** Marques propres à un joueur, côté adaptateur (ticks du serveur). */
|
||||
private static final class Marques {
|
||||
int teleportTick = Integer.MIN_VALUE;
|
||||
int reculTick = Integer.MIN_VALUE;
|
||||
int dernierVerdictTick = Integer.MIN_VALUE;
|
||||
int dernierTraceTick = Integer.MIN_VALUE;
|
||||
int dernierRiptideTick = Integer.MIN_VALUE;
|
||||
long exemptJusquaMs = 0;
|
||||
double dernierX, dernierY, dernierZ;
|
||||
boolean aDernierePosition;
|
||||
}
|
||||
|
||||
private static final GardeConfig CONFIG = new GardeConfig();
|
||||
private static final Moteur MOTEUR = new Moteur(CONFIG);
|
||||
private static final Map<UUID, Marques> MARQUES = new HashMap<>();
|
||||
private static final Set<UUID> ALERTES_COUPEES = new HashSet<>();
|
||||
|
||||
private static TraceFichier traces;
|
||||
private static Path journal;
|
||||
private static boolean tracesEnCours;
|
||||
private static int gelTick = Integer.MIN_VALUE;
|
||||
private static double tpsEstime = 20.0;
|
||||
|
||||
private Garde() { }
|
||||
|
||||
public static GardeConfig config() {
|
||||
return CONFIG;
|
||||
}
|
||||
|
||||
public static Moteur moteur() {
|
||||
return MOTEUR;
|
||||
}
|
||||
|
||||
// --- Cycle de vie ---
|
||||
|
||||
public static void surConfigChargee() {
|
||||
GastinelGarde.LOGGER.info("[GastinelGarde] Configuration appliquée.");
|
||||
}
|
||||
|
||||
public static synchronized void demarrer() {
|
||||
GardeSpec.appliquer(CONFIG);
|
||||
Path jeu = FMLPaths.GAMEDIR.get();
|
||||
journal = jeu.resolve("logs").resolve("gastinelgarde.log");
|
||||
try {
|
||||
Files.createDirectories(journal.getParent());
|
||||
traces = new TraceFichier(jeu.resolve("gastinelgarde-traces"), GardeSpec.tracesJours(), GardeSpec.tracesTailleOctets());
|
||||
tracesEnCours = GardeSpec.tracesActives();
|
||||
} catch (IOException e) {
|
||||
GastinelGarde.LOGGER.error("[GastinelGarde] Impossible d'ouvrir le dossier de traces", e);
|
||||
traces = null;
|
||||
tracesEnCours = false;
|
||||
}
|
||||
}
|
||||
|
||||
public static synchronized void arreter() {
|
||||
if (traces != null) {
|
||||
try {
|
||||
traces.close();
|
||||
} catch (IOException ignored) {
|
||||
// fermeture du serveur : rien à faire de plus
|
||||
}
|
||||
traces = null;
|
||||
}
|
||||
MARQUES.clear();
|
||||
}
|
||||
|
||||
public static synchronized void flushTraces() {
|
||||
if (traces == null) return;
|
||||
try {
|
||||
traces.flush();
|
||||
} catch (IOException e) {
|
||||
GastinelGarde.LOGGER.warn("[GastinelGarde] flush des traces : {}", e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean tracesEnCours() {
|
||||
return tracesEnCours && traces != null;
|
||||
}
|
||||
|
||||
public static void definirTraces(boolean actif) {
|
||||
tracesEnCours = actif && traces != null;
|
||||
}
|
||||
|
||||
public static void joueurParti(UUID id) {
|
||||
MOTEUR.retirer(id);
|
||||
MARQUES.remove(id);
|
||||
ALERTES_COUPEES.remove(id);
|
||||
}
|
||||
|
||||
// --- Faits collectés ---
|
||||
|
||||
public static void noterTeleport(ServerPlayer p) {
|
||||
marques(p.getUUID()).teleportTick = p.server.getTickCount();
|
||||
}
|
||||
|
||||
public static void noterRecul(ServerPlayer p) {
|
||||
marques(p.getUUID()).reculTick = p.server.getTickCount();
|
||||
}
|
||||
|
||||
public static void noterTickServeur(MinecraftServer server, long dureeNs) {
|
||||
double ms = dureeNs / 1_000_000.0;
|
||||
if (ms > 250.0) gelTick = server.getTickCount();
|
||||
double moyenne = server.getAverageTickTimeNanos() / 1_000_000.0;
|
||||
tpsEstime = moyenne <= 50.0 ? 20.0 : 1000.0 / moyenne;
|
||||
}
|
||||
|
||||
public static void exempter(UUID id, long dureeMs) {
|
||||
marques(id).exemptJusquaMs = System.currentTimeMillis() + dureeMs;
|
||||
}
|
||||
|
||||
public static boolean estExempteManuellement(UUID id) {
|
||||
Marques m = MARQUES.get(id);
|
||||
return m != null && m.exemptJusquaMs > System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public static boolean alertesActivesPour(UUID id) {
|
||||
return !ALERTES_COUPEES.contains(id);
|
||||
}
|
||||
|
||||
public static void definirAlertes(UUID id, boolean actif) {
|
||||
if (actif) ALERTES_COUPEES.remove(id);
|
||||
else ALERTES_COUPEES.add(id);
|
||||
}
|
||||
|
||||
private static Marques marques(UUID id) {
|
||||
return MARQUES.computeIfAbsent(id, k -> new Marques());
|
||||
}
|
||||
|
||||
// --- État d'un joueur pour le moteur ---
|
||||
|
||||
public static EtatJoueur etat(ServerPlayer p, boolean chercherContraption) {
|
||||
int tick = p.server.getTickCount();
|
||||
Marques m = marques(p.getUUID());
|
||||
|
||||
boolean riptide = p.isAutoSpinAttack() || tridentImpulsion(p.getMainHandItem(), p) || tridentImpulsion(p.getOffhandItem(), p);
|
||||
if (riptide) m.dernierRiptideTick = tick;
|
||||
boolean riptideRecent = m.dernierRiptideTick != Integer.MIN_VALUE
|
||||
&& tick - m.dernierRiptideTick <= com.gastinelgarde.core.RiptideTolerance.GRACE_TICKS;
|
||||
|
||||
boolean mange = false;
|
||||
if (p.isUsingItem()) {
|
||||
UseAnim anim = p.getUseItem().getUseAnimation();
|
||||
mange = anim == UseAnim.EAT || anim == UseAnim.DRINK;
|
||||
}
|
||||
|
||||
return EtatJoueur.builder()
|
||||
.creatif(p.isCreative())
|
||||
.spectateur(p.isSpectator())
|
||||
.peutVoler(p.getAbilities().mayfly)
|
||||
.riptide(riptideRecent)
|
||||
.elytres(p.isFallFlying())
|
||||
.levitation(p.hasEffect(MobEffects.LEVITATION))
|
||||
.chuteLente(p.hasEffect(MobEffects.SLOW_FALLING))
|
||||
.enVehicule(p.isPassenger())
|
||||
.pocheContraption(chercherContraption && pocheContraption(p))
|
||||
.utiliseObjet(mange)
|
||||
.ticksDepuisTeleport(depuis(tick, m.teleportTick))
|
||||
.ticksDepuisRecul(depuis(tick, m.reculTick))
|
||||
.ticksDepuisGel(depuis(tick, gelTick))
|
||||
.tps(tpsEstime)
|
||||
.pingMs(p.connection.latency())
|
||||
.build();
|
||||
}
|
||||
|
||||
private static int depuis(int tick, int marque) {
|
||||
if (marque == Integer.MIN_VALUE) return EtatJoueur.JAMAIS;
|
||||
return Math.max(0, tick - marque);
|
||||
}
|
||||
|
||||
private static boolean tridentImpulsion(ItemStack stack, ServerPlayer joueur) {
|
||||
return stack.is(Items.TRIDENT) && EnchantmentHelper.getTridentSpinAttackStrength(stack, joueur) > 0.0F;
|
||||
}
|
||||
|
||||
/** Une entité de contraption (Create, Aeronautics…) est-elle toute proche ? Détection par nom de classe. */
|
||||
private static boolean pocheContraption(Player p) {
|
||||
int dist = GardeSpec.distanceContraption();
|
||||
if (dist <= 0) return false;
|
||||
String[] prefixes = GardeSpec.prefixesContraption().split(",");
|
||||
List<Entity> proches = p.level().getEntities(p, p.getBoundingBox().inflate(dist), e -> {
|
||||
String nom = e.getClass().getName();
|
||||
for (String pre : prefixes) {
|
||||
String t = pre.trim();
|
||||
if (!t.isEmpty() && nom.startsWith(t)) return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
return !proches.isEmpty();
|
||||
}
|
||||
|
||||
// --- Réaction aux verdicts ---
|
||||
|
||||
/**
|
||||
* Journalise, prévient les ops et renvoie la pire sanction applicable. Ne modifie pas le joueur : l'appelant
|
||||
* décide de l'action (annuler le coup, retour arrière, expulsion).
|
||||
*/
|
||||
public static Sanction traiter(ServerPlayer p, List<Verdict> verdicts) {
|
||||
if (verdicts.isEmpty()) return Sanction.AUCUNE;
|
||||
marques(p.getUUID()).dernierVerdictTick = p.server.getTickCount();
|
||||
for (Verdict v : verdicts) {
|
||||
String ligne = String.format("%s %s mode=%s vl=%.1f (+%.1f) palier=%s applique=%s %s exemptions=%s",
|
||||
p.getGameProfile().getName(), v.controle().id(), v.mode(), v.vl(), v.points(),
|
||||
v.palier(), v.applicable(), v.details(), v.exemptions());
|
||||
ecrireJournal(ligne);
|
||||
if (v.notifier()) alerterOps(p, v);
|
||||
}
|
||||
return Verdict.pire(verdicts);
|
||||
}
|
||||
|
||||
private static void alerterOps(ServerPlayer cible, Verdict v) {
|
||||
Component msg = Component.literal(String.format("[Garde] %s : %s VL=%.1f (%s, %s)",
|
||||
cible.getGameProfile().getName(), v.controle().id(), v.vl(), v.mode(), v.applicable()))
|
||||
.withStyle(ChatFormatting.GOLD);
|
||||
for (ServerPlayer op : cible.server.getPlayerList().getPlayers()) {
|
||||
if (op.hasPermissions(2) && alertesActivesPour(op.getUUID())) op.sendSystemMessage(msg);
|
||||
}
|
||||
}
|
||||
|
||||
private static synchronized void ecrireJournal(String ligne) {
|
||||
GastinelGarde.LOGGER.warn("[GastinelGarde] {}", ligne);
|
||||
if (journal == null) return;
|
||||
try {
|
||||
Files.writeString(journal, Instant.now() + " " + ligne + System.lineSeparator(), StandardCharsets.UTF_8,
|
||||
StandardOpenOption.CREATE, StandardOpenOption.APPEND);
|
||||
} catch (IOException e) {
|
||||
GastinelGarde.LOGGER.warn("[GastinelGarde] journal : {}", e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
// --- Traces ---
|
||||
|
||||
public static synchronized void tracer(TraceLigne l) {
|
||||
if (!tracesEnCours()) return;
|
||||
try {
|
||||
traces.ecrire(System.currentTimeMillis(), l.json());
|
||||
} catch (IOException e) {
|
||||
GastinelGarde.LOGGER.warn("[GastinelGarde] trace : {}", e.toString());
|
||||
tracesEnCours = false;
|
||||
}
|
||||
}
|
||||
|
||||
// --- Paquet de mouvement (appelé par le mixin) ---
|
||||
|
||||
/** Début du traitement d'un paquet de mouvement : contrôle du débit. */
|
||||
public static void avantMouvement(ServerPlayer p) {
|
||||
if (estExempteManuellement(p.getUUID())) return;
|
||||
EtatJoueur etat = etat(p, false);
|
||||
List<Verdict> v = MOTEUR.paquetMouvement(p.getUUID(), System.currentTimeMillis(), etat);
|
||||
Sanction s = traiter(p, v);
|
||||
if (s.ordinal() >= Sanction.SETBACK.ordinal()) appliquerSanctionMouvement(p, s, v);
|
||||
}
|
||||
|
||||
/** Fin du traitement d'un paquet accepté : mémorise la position valide et trace. */
|
||||
public static void apresMouvement(ServerPlayer p) {
|
||||
int tick = p.server.getTickCount();
|
||||
Marques m = marques(p.getUUID());
|
||||
if (m.dernierVerdictTick != tick) {
|
||||
MOTEUR.suivi(p.getUUID()).memoriserPosition(p.getX(), p.getY(), p.getZ(), p.getYRot(), p.getXRot(),
|
||||
p.level().dimension().location().toString());
|
||||
}
|
||||
if (tracesEnCours() && m.dernierTraceTick != tick) {
|
||||
m.dernierTraceTick = tick;
|
||||
double vx = m.aDernierePosition ? p.getX() - m.dernierX : 0;
|
||||
double vy = m.aDernierePosition ? p.getY() - m.dernierY : 0;
|
||||
double vz = m.aDernierePosition ? p.getZ() - m.dernierZ : 0;
|
||||
m.dernierX = p.getX();
|
||||
m.dernierY = p.getY();
|
||||
m.dernierZ = p.getZ();
|
||||
m.aDernierePosition = true;
|
||||
boolean solServeur = !p.level().noCollision(p, p.getBoundingBox().move(0.0, -0.05, 0.0));
|
||||
tracer(new TraceLigne(tick, p.getGameProfile().getName(), "move", p.getX(), p.getY(), p.getZ(),
|
||||
vx, vy, vz, p.onGround(), solServeur, TraceLigne.masque(etat(p, false)), -1, Double.NaN, null));
|
||||
}
|
||||
}
|
||||
|
||||
public static void appliquerSanctionMouvement(ServerPlayer p, Sanction s, List<Verdict> verdicts) {
|
||||
String raison = verdicts.isEmpty() ? "anti-triche" : verdicts.get(0).controle().id();
|
||||
if (s == Sanction.EXPULSION) {
|
||||
ecrireJournal(p.getGameProfile().getName() + " EXPULSE (" + raison + ")");
|
||||
p.connection.disconnect(Component.literal("Expulsé par l'anti-triche (" + raison + ")."));
|
||||
} else if (s == Sanction.SETBACK) {
|
||||
Suivi sv = MOTEUR.suivi(p.getUUID());
|
||||
String dim = p.level().dimension().location().toString();
|
||||
if (sv.aPositionValide() && sv.dimension().equals(dim)) {
|
||||
p.connection.teleport(sv.x(), sv.y(), sv.z(), sv.yaw(), sv.pitch());
|
||||
ecrireJournal(p.getGameProfile().getName() + " SETBACK (" + raison + ")");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Référencer dans un nouveau ticket
Bloquer un utilisateur