Mod GastinelTpa : /tpa, /tpahere, /tpaccept, /tpadeny (expiration 1 min)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Cette révision appartient à :
révision
3b34a46376
15 fichiers modifiés
+928
Aucun fichier ne correspond à votre recherche
@@ -0,0 +1,242 @@
|
||||
package com.gastineltpa;
|
||||
|
||||
import com.gastineltpa.core.TpaRegistry;
|
||||
import com.gastineltpa.core.TpaRegistry.Request;
|
||||
import com.gastineltpa.core.TpaRegistry.Type;
|
||||
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.ChatFormatting;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import net.minecraft.commands.Commands;
|
||||
import net.minecraft.commands.SharedSuggestionProvider;
|
||||
import net.minecraft.network.chat.ClickEvent;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.network.chat.HoverEvent;
|
||||
import net.minecraft.network.chat.MutableComponent;
|
||||
import net.minecraft.network.chat.Style;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.neoforged.bus.api.SubscribeEvent;
|
||||
import net.neoforged.neoforge.event.RegisterCommandsEvent;
|
||||
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
|
||||
import net.neoforged.neoforge.event.tick.ServerTickEvent;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
/** /tpa, /tpahere, /tpaccept, /tpadeny */
|
||||
public final class TpaCommands {
|
||||
|
||||
private static final TpaRegistry REGISTRE = new TpaRegistry(System::currentTimeMillis);
|
||||
|
||||
@SubscribeEvent
|
||||
public static void onRegister(RegisterCommandsEvent event) {
|
||||
CommandDispatcher<CommandSourceStack> d = event.getDispatcher();
|
||||
|
||||
d.register(Commands.literal("tpa")
|
||||
.then(Commands.argument("joueur", StringArgumentType.word())
|
||||
.suggests((ctx, b) -> SharedSuggestionProvider.suggest(
|
||||
ctx.getSource().getServer().getPlayerNames(), b))
|
||||
.executes(ctx -> demander(ctx, Type.TPA))));
|
||||
|
||||
d.register(Commands.literal("tpahere")
|
||||
.then(Commands.argument("joueur", StringArgumentType.word())
|
||||
.suggests((ctx, b) -> SharedSuggestionProvider.suggest(
|
||||
ctx.getSource().getServer().getPlayerNames(), b))
|
||||
.executes(ctx -> demander(ctx, Type.TPAHERE))));
|
||||
|
||||
d.register(Commands.literal("tpaccept")
|
||||
.executes(ctx -> repondre(ctx, null, true))
|
||||
.then(Commands.argument("joueur", StringArgumentType.word())
|
||||
.suggests(TpaCommands::suggererDemandeurs)
|
||||
.executes(ctx -> repondre(ctx, StringArgumentType.getString(ctx, "joueur"), true))));
|
||||
|
||||
d.register(Commands.literal("tpadeny")
|
||||
.executes(ctx -> repondre(ctx, null, false))
|
||||
.then(Commands.argument("joueur", StringArgumentType.word())
|
||||
.suggests(TpaCommands::suggererDemandeurs)
|
||||
.executes(ctx -> repondre(ctx, StringArgumentType.getString(ctx, "joueur"), false))));
|
||||
}
|
||||
|
||||
// --- Événements ---
|
||||
|
||||
/** Une fois par seconde : prévient les deux joueurs des invitations expirées. */
|
||||
@SubscribeEvent
|
||||
public static void onTick(ServerTickEvent.Post event) {
|
||||
MinecraftServer server = event.getServer();
|
||||
if (server.getTickCount() % 20 == 0) {
|
||||
nettoyerExpirees(server);
|
||||
}
|
||||
}
|
||||
|
||||
/** Un joueur qui se déconnecte annule toutes ses invitations (envoyées et reçues). */
|
||||
@SubscribeEvent
|
||||
public static void onLogout(PlayerEvent.PlayerLoggedOutEvent event) {
|
||||
if (!(event.getEntity() instanceof ServerPlayer parti)) return;
|
||||
MinecraftServer server = parti.server;
|
||||
for (Request r : REGISTRE.removeInvolving(parti.getUUID())) {
|
||||
UUID autreId = r.from().equals(parti.getUUID()) ? r.to() : r.from();
|
||||
ServerPlayer autre = server.getPlayerList().getPlayer(autreId);
|
||||
if (autre != null) {
|
||||
autre.sendSystemMessage(info("Demande de téléportation annulée : "
|
||||
+ parti.getGameProfile().getName() + " s'est déconnecté."));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- Commandes ---
|
||||
|
||||
private static int demander(CommandContext<CommandSourceStack> ctx, Type type) throws CommandSyntaxException {
|
||||
ServerPlayer moi = ctx.getSource().getPlayerOrException();
|
||||
MinecraftServer server = moi.server;
|
||||
nettoyerExpirees(server);
|
||||
|
||||
String nom = StringArgumentType.getString(ctx, "joueur");
|
||||
ServerPlayer cible = server.getPlayerList().getPlayerByName(nom);
|
||||
if (cible == null) {
|
||||
return echec(ctx, "Joueur introuvable ou hors ligne : " + nom);
|
||||
}
|
||||
if (cible.getUUID().equals(moi.getUUID())) {
|
||||
return echec(ctx, "Tu ne peux pas t'envoyer une demande à toi-même.");
|
||||
}
|
||||
|
||||
REGISTRE.add(moi.getUUID(), cible.getUUID(), type);
|
||||
String nomCible = cible.getGameProfile().getName();
|
||||
String nomMoi = moi.getGameProfile().getName();
|
||||
|
||||
ctx.getSource().sendSuccess(() -> info(type == Type.TPA
|
||||
? "Demande envoyée à " + nomCible + " : tu iras le rejoindre s'il accepte (60 s)."
|
||||
: "Demande envoyée à " + nomCible + " : il viendra te rejoindre s'il accepte (60 s)."), false);
|
||||
|
||||
MutableComponent msg = Component.literal("[TPA] ").withStyle(ChatFormatting.GOLD)
|
||||
.append(Component.literal(type == Type.TPA
|
||||
? nomMoi + " souhaite se téléporter vers toi. "
|
||||
: nomMoi + " souhaite que tu te téléportes vers lui. ").withStyle(ChatFormatting.YELLOW))
|
||||
.append(bouton("[Accepter]", "/tpaccept " + nomMoi, ChatFormatting.GREEN, "Accepter la demande"))
|
||||
.append(Component.literal(" "))
|
||||
.append(bouton("[Refuser]", "/tpadeny " + nomMoi, ChatFormatting.RED, "Refuser la demande"))
|
||||
.append(Component.literal(" (expire dans 60 s)").withStyle(ChatFormatting.GRAY));
|
||||
cible.sendSystemMessage(msg);
|
||||
return 1;
|
||||
}
|
||||
|
||||
private static int repondre(CommandContext<CommandSourceStack> ctx, String nomDemandeur, boolean accepter)
|
||||
throws CommandSyntaxException {
|
||||
ServerPlayer moi = ctx.getSource().getPlayerOrException();
|
||||
MinecraftServer server = moi.server;
|
||||
// On prévient d'abord les expirées, pour que le message d'expiration passe avant tout.
|
||||
nettoyerExpirees(server);
|
||||
|
||||
Optional<Request> trouvee;
|
||||
if (nomDemandeur == null) {
|
||||
trouvee = REGISTRE.latest(moi.getUUID());
|
||||
if (trouvee.isEmpty()) {
|
||||
return echec(ctx, "Aucune demande de téléportation en attente.");
|
||||
}
|
||||
} else {
|
||||
ServerPlayer demandeur = server.getPlayerList().getPlayerByName(nomDemandeur);
|
||||
if (demandeur == null) {
|
||||
return echec(ctx, "Joueur introuvable ou hors ligne : " + nomDemandeur);
|
||||
}
|
||||
trouvee = REGISTRE.find(moi.getUUID(), demandeur.getUUID());
|
||||
if (trouvee.isEmpty()) {
|
||||
return echec(ctx, "Aucune demande en attente de la part de "
|
||||
+ demandeur.getGameProfile().getName() + " (peut-être expirée).");
|
||||
}
|
||||
}
|
||||
|
||||
Request r = trouvee.get();
|
||||
REGISTRE.remove(r);
|
||||
ServerPlayer demandeur = server.getPlayerList().getPlayer(r.from());
|
||||
if (demandeur == null) {
|
||||
return echec(ctx, "Le demandeur s'est déconnecté, demande annulée.");
|
||||
}
|
||||
String nomDem = demandeur.getGameProfile().getName();
|
||||
String nomMoi = moi.getGameProfile().getName();
|
||||
|
||||
if (!accepter) {
|
||||
ctx.getSource().sendSuccess(() -> info("Demande de " + nomDem + " refusée."), false);
|
||||
demandeur.sendSystemMessage(erreur(nomMoi + " a refusé ta demande de téléportation."));
|
||||
return 1;
|
||||
}
|
||||
|
||||
// TPA : le demandeur va vers moi. TPAHERE : je vais vers le demandeur.
|
||||
ServerPlayer voyageur = r.type() == Type.TPA ? demandeur : moi;
|
||||
ServerPlayer destination = r.type() == Type.TPA ? moi : demandeur;
|
||||
teleporter(voyageur, destination);
|
||||
|
||||
ctx.getSource().sendSuccess(() -> succes("Demande de " + nomDem + " acceptée."), false);
|
||||
demandeur.sendSystemMessage(succes(nomMoi + " a accepté ta demande de téléportation."));
|
||||
return 1;
|
||||
}
|
||||
|
||||
// --- Utilitaires ---
|
||||
|
||||
/** Téléporte vers la position de la destination (même dimension ou non), en gardant le regard du voyageur. */
|
||||
private static void teleporter(ServerPlayer voyageur, ServerPlayer destination) {
|
||||
ServerLevel niveau = destination.serverLevel();
|
||||
voyageur.stopRiding();
|
||||
voyageur.teleportTo(niveau, destination.getX(), destination.getY(), destination.getZ(),
|
||||
voyageur.getYRot(), voyageur.getXRot());
|
||||
}
|
||||
|
||||
private static void nettoyerExpirees(MinecraftServer server) {
|
||||
for (Request r : REGISTRE.purgeExpired()) {
|
||||
ServerPlayer de = server.getPlayerList().getPlayer(r.from());
|
||||
ServerPlayer vers = server.getPlayerList().getPlayer(r.to());
|
||||
String nomDe = de != null ? de.getGameProfile().getName() : "?";
|
||||
String nomVers = vers != null ? vers.getGameProfile().getName() : "?";
|
||||
if (de != null) {
|
||||
de.sendSystemMessage(erreur("Ta demande de téléportation à " + nomVers + " a expiré."));
|
||||
}
|
||||
if (vers != null) {
|
||||
vers.sendSystemMessage(erreur("La demande de téléportation de " + nomDe + " a expiré."));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static java.util.concurrent.CompletableFuture<com.mojang.brigadier.suggestion.Suggestions>
|
||||
suggererDemandeurs(CommandContext<CommandSourceStack> ctx, com.mojang.brigadier.suggestion.SuggestionsBuilder b)
|
||||
throws CommandSyntaxException {
|
||||
ServerPlayer moi = ctx.getSource().getPlayerOrException();
|
||||
List<String> noms = new ArrayList<>();
|
||||
for (Request r : REGISTRE.pendingFor(moi.getUUID())) {
|
||||
ServerPlayer p = moi.server.getPlayerList().getPlayer(r.from());
|
||||
if (p != null) noms.add(p.getGameProfile().getName());
|
||||
}
|
||||
return SharedSuggestionProvider.suggest(noms, b);
|
||||
}
|
||||
|
||||
private static MutableComponent bouton(String texte, String commande, ChatFormatting couleur, String survol) {
|
||||
return Component.literal(texte).withStyle(Style.EMPTY
|
||||
.withColor(couleur)
|
||||
.withBold(true)
|
||||
.withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, commande))
|
||||
.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Component.literal(survol))));
|
||||
}
|
||||
|
||||
private static int echec(CommandContext<CommandSourceStack> ctx, String message) {
|
||||
ctx.getSource().sendFailure(Component.literal(message));
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static Component info(String s) {
|
||||
return Component.literal("[TPA] ").withStyle(ChatFormatting.GOLD)
|
||||
.append(Component.literal(s).withStyle(ChatFormatting.YELLOW));
|
||||
}
|
||||
|
||||
private static Component succes(String s) {
|
||||
return Component.literal("[TPA] ").withStyle(ChatFormatting.GOLD)
|
||||
.append(Component.literal(s).withStyle(ChatFormatting.GREEN));
|
||||
}
|
||||
|
||||
private static Component erreur(String s) {
|
||||
return Component.literal("[TPA] ").withStyle(ChatFormatting.GOLD)
|
||||
.append(Component.literal(s).withStyle(ChatFormatting.RED));
|
||||
}
|
||||
}
|
||||
Référencer dans un nouveau ticket
Bloquer un utilisateur