Edit method of screen use on serverControlManager.js

This commit is contained in:
Loïc 2025-10-24 07:25:08 +02:00
parent bed036ea2c
commit 66c9e1fa32

View File

@ -53,13 +53,26 @@ class ServerControlManager {
} }
} }
async getMinecraftScreenName() {
// Cherche la session screen qui finit par .minecraft
const result = await this.executeCommand('screen -ls | grep \.minecraft', 'Recherche session screen Minecraft');
if (result.success && result.output.trim() !== '') {
// Exemple de sortie : "1294388.minecraft (10/21/25 22:02:21) (Detached)"
const match = result.output.trim().match(/([0-9]+\.minecraft)/);
if (match) return match[1];
}
// Fallback sur le nom par défaut
return this.screenName;
}
async reloadWhitelist() { async reloadWhitelist() {
console.log('🔄 Rechargement de la whitelist...'); console.log('🔄 Rechargement de la whitelist...');
if (this.useScreen) { if (this.useScreen) {
// Méthode screen : commande directe // Utiliser le nom dynamique de la session screen
const screenSession = await this.getMinecraftScreenName();
const result = await this.executeCommand( const result = await this.executeCommand(
`sudo -u ${this.serverUser} screen -S ${this.screenName} -p 0 -X stuff "whitelist reload\n"`, `sudo -u ${this.serverUser} screen -S ${screenSession} -p 0 -X stuff "whitelist reload\n"`,
'Rechargement whitelist via commande' 'Rechargement whitelist via commande'
); );
@ -192,9 +205,11 @@ class ServerControlManager {
async stopServer() { async stopServer() {
if (this.useScreen) { if (this.useScreen) {
// Utiliser le nom dynamique de la session screen
const screenSession = await this.getMinecraftScreenName();
// Arrêter proprement avec la commande stop // Arrêter proprement avec la commande stop
const stopResult = await this.executeCommand( const stopResult = await this.executeCommand(
`sudo -u ${this.serverUser} screen -S ${this.screenName} -p 0 -X stuff "stop\n"`, `sudo -u ${this.serverUser} screen -S ${screenSession} -p 0 -X stuff "stop\n"`,
'Arrêt propre du serveur' 'Arrêt propre du serveur'
); );
@ -202,7 +217,7 @@ class ServerControlManager {
await new Promise(resolve => setTimeout(resolve, 5000)); await new Promise(resolve => setTimeout(resolve, 5000));
const killResult = await this.executeCommand( const killResult = await this.executeCommand(
`sudo -u ${this.serverUser} screen -S ${this.screenName} -X quit`, `sudo -u ${this.serverUser} screen -S ${screenSession} -X quit`,
'Fermeture session screen' 'Fermeture session screen'
); );
@ -264,9 +279,10 @@ class ServerControlManager {
async sendServerCommand(command) { async sendServerCommand(command) {
if (this.useScreen) { if (this.useScreen) {
// Envoyer directement via screen // Utiliser le nom dynamique de la session screen
const screenSession = await this.getMinecraftScreenName();
const result = await this.executeCommand( const result = await this.executeCommand(
`sudo -u ${this.serverUser} screen -S ${this.screenName} -p 0 -X stuff "${command}\n"`, `sudo -u ${this.serverUser} screen -S ${screenSession} -p 0 -X stuff "${command}\n"`,
`Envoi commande: ${command}` `Envoi commande: ${command}`
); );