Récupérer un fichier yml sur un autre plugin

LEZIKO

Architecte en herbe
2 Novembre 2021
93
2
69
19
Bonjour/Bonsoir,

Je reviens ici pour savoir si il est possible de récupérer un fichier yml d'un plugin à un autre.
Je m'explique j'ai créer un plugin bungeecord et j'y ai créé un fichier yml pour stocker les joueurs bannis et je souhaitais récupérer le fichier via un plugin spigot (je précise que ce sont mes deux plugins et non des plugins pris) . Sauriez-vous si cela est possible ? je vous remercie,

LEZIKO
 

ShE3py

Enbogueuse
Support
26 Septembre 2015
4 135
162
463
247
21
Mìlhüsa
Bonsoir,

Depuis 2012 il y a un protocole officiel pour envoyer des messages à un client depuis un serveur, donc l'idée est juste d'en envoyer un à n'importe quel joueur et de l'intercepter dans le proxy BungeeCord :
Java:
public class SpigotPlugin extends JavaPlugin implements Listener, PluginMessageListener {
    public static final String CHANNEL_NAME = "mapatate:channel";
    private static final Map<UUID, CompletableFuture<Boolean>> PENDING_BAN_CHECK = new HashMap<>();
    
    @Override
    public void onEnable() {
        Server server = this.getServer();
        Messenger messenger = server.getMessenger();
        PluginManager pm = server.getPluginManager();
        
        if(server.spigot().getConfig().getConfigurationSection("settings").getBoolean("settings.bungeecord")) {
            messenger.registerOutgoingPluginChannel(this, CHANNEL_NAME);
            messenger.registerIncomingPluginChannel(this, CHANNEL_NAME, this);
        }
        else {
            this.getLogger().severe("BungeeCord is disabled in `spigot.yml`");
            pm.disablePlugin(this);
        }
        
        pm.registerEvents(this, this);
    }
    
    @Override
    public void onDisable() {
        Messenger messenger = this.getServer().getMessenger();
        
        messenger.unregisterOutgoingPluginChannel(this);
        messenger.unregisterIncomingPluginChannel(this);
    }
    
    @EventHandler
    public void onPlayerLogin(PlayerLoginEvent e) {
        if(e.getResult() != PlayerLoginEvent.Result.ALLOWED) {
            return;
        }
        
        ByteArrayDataOutput out = ByteStreams.newDataOutput();
        out.writeUTF("PlayerCheck");
        out.writeUTF("IsBanned");
        
        Player p = e.getPlayer();
        UUID playerId = p.getUniqueId();
        
        ByteBuffer uuid = ByteBuffer.wrap(new byte[16]);
        uuid.putLong(playerId.getMostSignificantBits());
        uuid.putLong(playerId.getLeastSignificantBits());
        out.write(uuid.array());
        
        CompletableFuture<Boolean> promise = new CompletableFuture<>();
        PENDING_BAN_CHECK.put(playerId, promise);
        p.sendPluginMessage(this, CHANNEL_NAME, out.toByteArray());
        
        try {
            boolean isBanned = promise.get(1, TimeUnit.SECONDS);
            
            if(isBanned) {
                e.setResult(PlayerLoginEvent.Result.KICK_BANNED);
            }
        }
        catch(ExecutionException | InterruptedException | TimeoutException ex) {
            e.setResult(PlayerLoginEvent.Result.KICK_OTHER);
            ex.printStackTrace();
        }
        
        promise.cancel(true);
        PENDING_BAN_CHECK.remove(playerId);
    }
    
    @Override
    public void onPluginMessageReceived(String channel, Player player, byte[] message) {
        if(!channel.equals(CHANNEL_NAME)) {
            return;
        }
        
        ByteArrayDataInput in = ByteStreams.newDataInput(message);
        String subchannel = in.readUTF();
        if(subchannel.equals("PlayerCheck")) {
            String action = in.readUTF();
            
            byte[] buf = new byte[16];
            in.readFully(buf);
            
            ByteBuffer bb = ByteBuffer.wrap(buf);
            long msb = bb.getLong();
            long lsb = bb.getLong();
            UUID playerId = new UUID(msb, lsb);
            
            if(action.equals("IsBanned")) {
                boolean result = in.readBoolean();
                
                CompletableFuture<Boolean> promise = PENDING_BAN_CHECK.get(playerId);
                if(promise != null) promise.complete(result);
            }
        }
    }
}
Java:
public class BungeeCordPlugin extends Plugin implements Listener {
    public static final String CHANNEL_NAME = "mapatate:channel";
    
    @Override
    public void onEnable() {
        this.getProxy().registerChannel(CHANNEL_NAME);
        this.getProxy().getPluginManager().registerListener(this, this);
    }
    
    @Override
    public void onDisable() {
        this.getProxy().unregisterChannel(CHANNEL_NAME);
    }
    
    @EventHandler
    public void onPluginMessage(PluginMessageEvent e) {
        if(!e.getTag().equals(CHANNEL_NAME)) {
            return;
        }
        
        // on ne veut pas qu'un client puisse envoyer des messages sur le canal de notre plugin,
        // ni que nos messages atteignent les joueurs
        e.setCancelled(true);
        
        if(!(e.getSender() instanceof Server) {
            return;
        }
        
        ByteArrayDataInput in = ByteStreams.newDataInput(message);
        String subchannel = in.readUTF();
        if(subchannel.equals("PlayerCheck")) {
            String action = in.readUTF();
            
            byte[] uuid = new byte[16];
            in.readFully(uuid);
            
            ByteBuffer bb = ByteBuffer.wrap(uuid);
            long msb = bb.getLong();
            long lsb = bb.getLong();
            UUID playerId = new UUID(msb, lsb);
            
            if(action.equals("IsBanned")) {
                //                 TODO
                boolean isBanned = isPlayerBanned(playerId);
                
                ByteArrayDataOutput out = ByteStreams.newDataOutput();
                out.writeUTF("PlayerCheckResult");
                out.writeUTF("IsBanned");
                out.write(uuid);
                out.writeBoolean(isBanned);
                
                ((Server) e.getSender()).sendData(CHANNEL_NAME, out.toByteArray());
            }
        }
    }
}

Sinon tu peux aussi passer par une base de données.

Cordialement,
ShE3py