Zones configurables (config/gastinelclaims-zones.json, /zoneprotegee liste|ici|recharger), zone « spawn » x 3284..3320 y 185..204 z -615..-566. Non-ops : ni casse, ni pose, ni seau/briquet, interactions limitées à une liste. Explosions, pistons, mobs, liquides venus de l'extérieur et feu (mixins FlowingFluid/FireBlock) n'y modifient rien. /claim inchangé. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
178 lignes
8.6 KiB
Java
178 lignes
8.6 KiB
Java
package com.gastinelclaims;
|
|
|
|
import com.gastinelclaims.core.ZonesConfig;
|
|
import com.gastinelclaims.core.ZonesConfig.Zone;
|
|
import com.mojang.brigadier.CommandDispatcher;
|
|
import net.minecraft.ChatFormatting;
|
|
import net.minecraft.commands.CommandSourceStack;
|
|
import net.minecraft.commands.Commands;
|
|
import net.minecraft.core.BlockPos;
|
|
import net.minecraft.core.registries.BuiltInRegistries;
|
|
import net.minecraft.core.registries.Registries;
|
|
import net.minecraft.network.chat.Component;
|
|
import net.minecraft.resources.ResourceLocation;
|
|
import net.minecraft.server.MinecraftServer;
|
|
import net.minecraft.server.level.ServerPlayer;
|
|
import net.minecraft.tags.TagKey;
|
|
import net.minecraft.world.entity.Entity;
|
|
import net.minecraft.world.entity.player.Player;
|
|
import net.minecraft.world.level.BlockGetter;
|
|
import net.minecraft.world.level.Level;
|
|
import net.minecraft.world.level.block.Block;
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
import net.neoforged.bus.api.SubscribeEvent;
|
|
import net.neoforged.neoforge.common.util.FakePlayer;
|
|
import net.neoforged.neoforge.event.RegisterCommandsEvent;
|
|
import net.neoforged.neoforge.event.server.ServerStartedEvent;
|
|
import net.neoforged.neoforge.event.server.ServerStoppedEvent;
|
|
|
|
import java.io.InputStream;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* Zones protégées (spawn incassable…). État global lu depuis config/gastinelclaims-zones.json
|
|
* (créé depuis la version intégrée au jar s'il manque). Utilisé par {@link ZoneEvents} et les mixins.
|
|
* Indépendant des claims (/claim) : un claim et une zone se cumulent, la zone gagne.
|
|
*/
|
|
public final class ZonesProtegees {
|
|
|
|
static final String FICHIER = "config/gastinelclaims-zones.json";
|
|
|
|
private static volatile ZonesConfig config = ZonesConfig.VIDE;
|
|
private static volatile List<TagKey<Block>> tagsAutorises = List.of();
|
|
private static volatile List<Block> blocsAutorises = List.of();
|
|
|
|
private ZonesProtegees() {}
|
|
|
|
public static ZonesConfig config() {
|
|
return config;
|
|
}
|
|
|
|
/** Le bloc est-il dans une zone protégée ? (appelé aussi par les mixins, doit rester rapide). */
|
|
public static boolean protege(BlockGetter getter, BlockPos pos) {
|
|
ZonesConfig c = config;
|
|
if (c.zones.isEmpty() || !(getter instanceof Level level) || level.isClientSide()) return false;
|
|
return c.zoneDe(level.dimension().location().toString(), pos.getX(), pos.getY(), pos.getZ()) != null;
|
|
}
|
|
|
|
public static Zone zoneDe(Level level, BlockPos pos) {
|
|
ZonesConfig c = config;
|
|
if (c.zones.isEmpty() || level.isClientSide()) return null;
|
|
return c.zoneDe(level.dimension().location().toString(), pos.getX(), pos.getY(), pos.getZ());
|
|
}
|
|
|
|
/** Entité à moins de {@code marge} blocs d'une zone (pour les mobs qui cassent autour d'eux). */
|
|
public static boolean proche(Entity e, double marge) {
|
|
ZonesConfig c = config;
|
|
if (c.zones.isEmpty() || e.level().isClientSide()) return false;
|
|
return c.proche(e.level().dimension().location().toString(), e.getX(), e.getY(), e.getZ(), marge);
|
|
}
|
|
|
|
/** Joueur qui ignore la protection : op du niveau configuré (jamais une machine / FakePlayer). */
|
|
public static boolean ignore(Player p) {
|
|
return p != null && !(p instanceof FakePlayer) && p.hasPermissions(config.niveauOp);
|
|
}
|
|
|
|
public static boolean interactionAutorisee(BlockState s) {
|
|
for (Block b : blocsAutorises) if (s.is(b)) return true;
|
|
for (TagKey<Block> t : tagsAutorises) if (s.is(t)) return true;
|
|
return false;
|
|
}
|
|
|
|
/** (Re)lit le fichier. Renvoie un message de résultat. En cas d'erreur, l'ancienne config est gardée. */
|
|
static String charger(MinecraftServer server) {
|
|
Path f = server.getServerDirectory().resolve(FICHIER);
|
|
try {
|
|
if (!Files.exists(f)) {
|
|
try (InputStream in = ZonesProtegees.class.getResourceAsStream("/gastinelclaims/zones-defaut.json")) {
|
|
if (in == null) throw new IllegalStateException("zones-defaut.json absent du jar");
|
|
Files.createDirectories(f.getParent());
|
|
Files.copy(in, f);
|
|
GastinelClaims.LOGGER.info("[GastinelClaims] Fichier de zones protégées créé : {}", f);
|
|
}
|
|
}
|
|
ZonesConfig c = ZonesConfig.parse(Files.readString(f, StandardCharsets.UTF_8));
|
|
List<TagKey<Block>> tags = new ArrayList<>();
|
|
List<Block> blocs = new ArrayList<>();
|
|
for (String id : c.interactionsAutorisees) {
|
|
if (id.startsWith("#")) {
|
|
tags.add(TagKey.create(Registries.BLOCK, ResourceLocation.parse(id.substring(1))));
|
|
} else {
|
|
ResourceLocation rl = ResourceLocation.parse(id);
|
|
if (BuiltInRegistries.BLOCK.containsKey(rl)) blocs.add(BuiltInRegistries.BLOCK.get(rl));
|
|
else GastinelClaims.LOGGER.warn("[GastinelClaims] Bloc inconnu dans interactions_autorisees : {}", id);
|
|
}
|
|
}
|
|
tagsAutorises = List.copyOf(tags);
|
|
blocsAutorises = List.copyOf(blocs);
|
|
config = c;
|
|
for (Zone z : c.zones) {
|
|
GastinelClaims.LOGGER.info("[GastinelClaims] Zone protégée active : {} {} x {}..{} y {}..{} z {}..{}",
|
|
z.nom(), z.dimension(), z.minX(), z.maxX(), z.minY(), z.maxY(), z.minZ(), z.maxZ());
|
|
}
|
|
return c.zones.size() + " zone(s) protégée(s) chargée(s).";
|
|
} catch (Exception e) {
|
|
GastinelClaims.LOGGER.error("[GastinelClaims] Zones protégées illisibles ({}) : {} — ancienne config gardée.", f, e.toString());
|
|
return "Erreur de lecture de " + FICHIER + " : " + e.getMessage() + " (ancienne config gardée).";
|
|
}
|
|
}
|
|
|
|
@SubscribeEvent
|
|
public static void onStarted(ServerStartedEvent event) {
|
|
charger(event.getServer());
|
|
// Vérification utile : le spawn du monde est-il dans une zone ?
|
|
var ow = event.getServer().overworld();
|
|
BlockPos s = ow.getSharedSpawnPos();
|
|
Zone z = zoneDe(ow, s);
|
|
GastinelClaims.LOGGER.info("[GastinelClaims] Spawn du monde {} {} {} : {}", s.getX(), s.getY(), s.getZ(),
|
|
z == null ? "HORS de toute zone protégée" : "dans la zone protégée « " + z.nom() + " »");
|
|
}
|
|
|
|
@SubscribeEvent
|
|
public static void onStopped(ServerStoppedEvent event) {
|
|
config = ZonesConfig.VIDE;
|
|
}
|
|
|
|
@SubscribeEvent
|
|
public static void onRegister(RegisterCommandsEvent event) {
|
|
CommandDispatcher<CommandSourceStack> d = event.getDispatcher();
|
|
d.register(Commands.literal("zoneprotegee")
|
|
.requires(s -> s.hasPermission(2))
|
|
.then(Commands.literal("recharger").executes(ctx -> {
|
|
String m = charger(ctx.getSource().getServer());
|
|
ctx.getSource().sendSuccess(() -> Component.literal(m), true);
|
|
return config.zones.size();
|
|
}))
|
|
.then(Commands.literal("liste").executes(ctx -> {
|
|
if (config.zones.isEmpty()) {
|
|
ctx.getSource().sendSuccess(() -> Component.literal("Aucune zone protégée."), false);
|
|
}
|
|
for (Zone z : config.zones) {
|
|
ctx.getSource().sendSuccess(() -> Component.literal("- " + z.nom() + " (" + z.dimension() + ") x "
|
|
+ z.minX() + ".." + z.maxX() + ", y " + z.minY() + ".." + z.maxY() + ", z "
|
|
+ z.minZ() + ".." + z.maxZ()), false);
|
|
}
|
|
return config.zones.size();
|
|
}))
|
|
.then(Commands.literal("ici").executes(ctx -> {
|
|
CommandSourceStack src = ctx.getSource();
|
|
BlockPos p = BlockPos.containing(src.getPosition());
|
|
Zone z = zoneDe(src.getLevel(), p);
|
|
src.sendSuccess(() -> Component.literal(z == null ? "Hors zone protégée." : "Dans la zone protégée « " + z.nom() + " ».")
|
|
.withStyle(z == null ? ChatFormatting.GRAY : ChatFormatting.GOLD), false);
|
|
return z == null ? 0 : 1;
|
|
})));
|
|
}
|
|
|
|
static void prevenir(Player p) {
|
|
if (p instanceof ServerPlayer sp && !(p instanceof FakePlayer)) {
|
|
sp.displayClientMessage(Component.literal("Zone protégée : impossible de modifier ici.")
|
|
.withStyle(ChatFormatting.RED), true);
|
|
}
|
|
}
|
|
}
|