Spigot-Bungeecord插件消息不起作用

时间:2018-02-07 19:28:40

标签: java

我正在尝试制作一个包含“全局”配置文件的插件。现在,我正在尝试使用Plugin Messaging将整个配置文件通过字符串发送到另一台服务器。我已经按照https://www.spigotmc.org/wiki/bukkit-bungee-plugin-messaging-channel/的指南进行了操作,并对发送的内容进行了一些改动。我正在尝试在插件插件中发送插件消息,所以这可能就是问题所在。这里的代码是我用来发送它的代码的摘要(我拿出了readFile(),clearFile()和writeFile(),让我知道你是否想要那些):

public class Main extends JavaPlugin implements PluginMessageListener {
public void onEnable() {
    this.getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
    this.getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", this);
}

public void onDisable() {}

public void updateConfig() {
    String updateConfig = "";
    for (String s : readFile(this.getDataFolder() + "/config.yml")) {
        if (updateConfig.equals("")) {
            updateConfig = s;
        } else {
            updateConfig = updateConfig + " |n| " + s;
        }
    }
    Bukkit.getLogger().info("Sending config update...");
    sendUpdateconfig(updateConfig);
}

public void sendUpdateconfig(String update) {
    ByteArrayDataOutput out = ByteStreams.newDataOutput();
    try {

        out.writeUTF("Forward");
        out.writeUTF("ALL");
        out.writeUTF("FooServer");

        ByteArrayOutputStream msgbytes = new ByteArrayOutputStream();
        DataOutputStream msgout = new DataOutputStream(msgbytes);
        msgout.writeUTF(update);
        msgout.writeShort(295);

        out.writeShort(msgbytes.toByteArray().length);
        out.write(msgbytes.toByteArray());

        Player player = Iterables.getLast(Bukkit.getOnlinePlayers());
        player.getServer().sendPluginMessage(this, "BungeeCord", out.toByteArray());
        Bukkit.getLogger().info("Sent " + update);
        Bukkit.getLogger().info("Short sent: 295");
        Bukkit.getLogger().info("Sent through player " + player.getName());

    } catch (IOException e) {
        e.printStackTrace();
    }
}

@Override
public void onPluginMessageReceived(String channel, Player player, byte[] message) {
    Bukkit.getLogger().info("Recieved message...");
    if (!channel.equals("BungeeCord")) {
        return;
    }
    try {
        Bukkit.getLogger().info("Recieved message...");
        ByteArrayDataInput in = ByteStreams.newDataInput(message);
        String subChannel = in.readUTF();
        if (!subChannel.equals("FooServer")) {
            Bukkit.getLogger().info("Loading message....");
            short len = in.readShort();
            byte[] msgbytes = new byte[len];
            in.readFully(msgbytes);

            DataInputStream msgin = new DataInputStream(new ByteArrayInputStream(msgbytes));
            String somedata = msgin.readUTF();
            short somenumber = msgin.readShort();

            if (somenumber == 295) {
                Bukkit.getLogger().info("Updating config...");
                String[] toWrite = somedata.split(" |n| ");
                String path = (this.getDataFolder() + "/config.yml");
                clearFile(path);
                for (String s : toWrite) {
                    writeFile(path, s);
                }
                Bukkit.getLogger().info("Config updated!");
            }
        } else {
            Bukkit.getLogger().info("Message sent by this plugin.");
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

我发送消息的方式就是调用updateConfig();调用它时,onPluginMessageReceived永远不会运行。 有什么我做错了吗?插件邮件只能通过bungeecord插件发送吗?提前致谢。如果您对代码有任何疑问,请与我们联系。

1 个答案:

答案 0 :(得分:0)

不要工作,因为它写了(要发送到的字符串服务器,或者要发送到每个服务器的所有服务器(发送插件消息的服务器除外))!使用它你可以使用我们自己的频道或redis