Mod GastinelClaims : /claim, /unclaim, /friend (NeoForge 1.21.1, serveur seul)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Cette révision appartient à :
Theo GastineletClaude Opus 5.5 a révisé 2026-10-08 11:28:00 +02:00
révision f7ea72ed99
20 fichiers modifiés
+1267

Aucun fichier ne correspond à votre recherche

+197
Voir le fichier
@@ -0,0 +1,197 @@
package com.gastinelclaims;
import com.gastinelclaims.core.ChunkKey;
import com.gastinelclaims.core.ClaimRegistry;
import com.mojang.authlib.GameProfile;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.SharedSuggestionProvider;
import net.minecraft.network.chat.Component;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.neoforge.event.RegisterCommandsEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
/** /claim, /claim info, /unclaim, /friend [add|unadd <pseudo>] */
public final class ClaimCommands {
@SubscribeEvent
public static void onRegister(RegisterCommandsEvent event) {
CommandDispatcher<CommandSourceStack> d = event.getDispatcher();
d.register(Commands.literal("claim")
.executes(ClaimCommands::claim)
.then(Commands.literal("info").executes(ClaimCommands::info)));
d.register(Commands.literal("unclaim").executes(ClaimCommands::unclaim));
d.register(Commands.literal("friend")
.executes(ClaimCommands::listFriends)
.then(Commands.literal("add")
.then(Commands.argument("pseudo", StringArgumentType.word())
.suggests((ctx, b) -> SharedSuggestionProvider.suggest(
ctx.getSource().getServer().getPlayerNames(), b))
.executes(ctx -> addFriend(ctx, StringArgumentType.getString(ctx, "pseudo")))))
.then(Commands.literal("unadd")
.then(Commands.argument("pseudo", StringArgumentType.word())
.suggests((ctx, b) -> SharedSuggestionProvider.suggest(friendNames(ctx), b))
.executes(ctx -> removeFriend(ctx, StringArgumentType.getString(ctx, "pseudo"))))));
}
// --- Claims ---
private static int claim(CommandContext<CommandSourceStack> ctx) throws CommandSyntaxException {
ServerPlayer p = ctx.getSource().getPlayerOrException();
ClaimsData data = ClaimsData.get(p.server);
ChunkKey key = ClaimsData.key(p.level(), p.blockPosition());
int max = ClaimsConfig.maxClaims();
ClaimRegistry r = data.registry();
switch (r.claim(p.getUUID(), key, max)) {
case OK -> {
data.setDirty();
ok(ctx, "Chunk [" + key.x() + ", " + key.z() + "] protégé. Claims : "
+ r.countOf(p.getUUID()) + "/" + max + ".");
return 1;
}
case ALREADY_YOURS -> fail(ctx, "Ce chunk est déjà à toi.");
case CLAIMED_BY_OTHER -> fail(ctx, "Ce chunk est déjà claim par " + nameOf(p.server, r.ownerOf(key)) + ".");
case LIMIT_REACHED -> fail(ctx, "Limite atteinte (" + max + " chunks). Fais /unclaim ailleurs d'abord.");
}
return 0;
}
private static int unclaim(CommandContext<CommandSourceStack> ctx) throws CommandSyntaxException {
ServerPlayer p = ctx.getSource().getPlayerOrException();
ClaimsData data = ClaimsData.get(p.server);
ChunkKey key = ClaimsData.key(p.level(), p.blockPosition());
switch (data.registry().unclaim(p.getUUID(), key)) {
case OK -> {
data.setDirty();
ok(ctx, "Chunk [" + key.x() + ", " + key.z() + "] libéré.");
return 1;
}
case NOT_CLAIMED -> fail(ctx, "Ce chunk n'est pas claim.");
case NOT_OWNER -> fail(ctx, "Ce chunk appartient à " + nameOf(p.server, data.registry().ownerOf(key)) + ".");
}
return 0;
}
private static int info(CommandContext<CommandSourceStack> ctx) throws CommandSyntaxException {
ServerPlayer p = ctx.getSource().getPlayerOrException();
ClaimRegistry r = ClaimsData.get(p.server).registry();
ChunkKey key = ClaimsData.key(p.level(), p.blockPosition());
UUID owner = r.ownerOf(key);
int max = ClaimsConfig.maxClaims();
int left = Math.max(0, max - r.countOf(p.getUUID()));
String who = owner == null ? "libre" : "à " + nameOf(p.server, owner);
ok(ctx, "Chunk [" + key.x() + ", " + key.z() + "] : " + who + ". Il te reste " + left + "/" + max + " claims.");
return 1;
}
// --- Amis ---
private static int listFriends(CommandContext<CommandSourceStack> ctx) throws CommandSyntaxException {
ServerPlayer p = ctx.getSource().getPlayerOrException();
var friends = ClaimsData.get(p.server).registry().friendsOf(p.getUUID());
if (friends.isEmpty()) {
ok(ctx, "Aucun ami. Ajoute-en avec /friend add <pseudo>.");
} else {
List<String> names = new ArrayList<>();
for (UUID u : friends) names.add(nameOf(p.server, u));
ok(ctx, "Amis (" + names.size() + ") : " + String.join(", ", names));
}
return 1;
}
private static int addFriend(CommandContext<CommandSourceStack> ctx, String name) throws CommandSyntaxException {
ServerPlayer p = ctx.getSource().getPlayerOrException();
Optional<GameProfile> target = resolve(p.server, name);
if (target.isEmpty()) {
fail(ctx, "Joueur inconnu du serveur : " + name + ".");
return 0;
}
ClaimsData data = ClaimsData.get(p.server);
switch (data.registry().addFriend(p.getUUID(), target.get().getId())) {
case OK -> {
data.setDirty();
ok(ctx, target.get().getName() + " a maintenant accès à tous tes claims.");
return 1;
}
case SELF -> fail(ctx, "Tu ne peux pas t'ajouter toi-même.");
case ALREADY -> fail(ctx, target.get().getName() + " est déjà ton ami.");
default -> { }
}
return 0;
}
private static int removeFriend(CommandContext<CommandSourceStack> ctx, String name) throws CommandSyntaxException {
ServerPlayer p = ctx.getSource().getPlayerOrException();
ClaimsData data = ClaimsData.get(p.server);
// On cherche d'abord dans la liste d'amis (marche même si le cache de profils a oublié le pseudo)
UUID friend = null;
for (UUID u : data.registry().friendsOf(p.getUUID())) {
if (nameOf(p.server, u).equalsIgnoreCase(name)) { friend = u; break; }
}
if (friend == null) {
friend = resolve(p.server, name).map(GameProfile::getId).orElse(null);
}
if (friend == null || data.registry().removeFriend(p.getUUID(), friend) != ClaimRegistry.FriendResult.OK) {
fail(ctx, name + " n'est pas dans ta liste d'amis.");
return 0;
}
data.setDirty();
ok(ctx, name + " n'a plus accès à tes claims.");
return 1;
}
// --- Utilitaires ---
/** Pseudo -> profil : joueur en ligne, sinon cache de profils du serveur (joueurs déjà venus). */
private static Optional<GameProfile> resolve(MinecraftServer server, String name) {
ServerPlayer online = server.getPlayerList().getPlayerByName(name);
if (online != null) return Optional.of(online.getGameProfile());
if (server.getProfileCache() == null) return Optional.empty();
return server.getProfileCache().get(name);
}
public static String nameOf(MinecraftServer server, UUID uuid) {
if (uuid == null) return "?";
ServerPlayer online = server.getPlayerList().getPlayer(uuid);
if (online != null) return online.getGameProfile().getName();
if (server.getProfileCache() != null) {
Optional<GameProfile> prof = server.getProfileCache().get(uuid);
if (prof.isPresent()) return prof.get().getName();
}
return uuid.toString().substring(0, 8);
}
private static List<String> friendNames(CommandContext<CommandSourceStack> ctx) {
ServerPlayer p = ctx.getSource().getPlayer();
if (p == null) return List.of();
List<String> names = new ArrayList<>();
for (UUID u : ClaimsData.get(p.server).registry().friendsOf(p.getUUID())) names.add(nameOf(p.server, u));
return names;
}
private static void ok(CommandContext<CommandSourceStack> ctx, String msg) {
ctx.getSource().sendSuccess(() -> Component.literal(msg), false);
}
private static void fail(CommandContext<CommandSourceStack> ctx, String msg) {
ctx.getSource().sendFailure(Component.literal(msg));
}
private ClaimCommands() {}
}