gpt4 book ai didi

java - Spigot-Bungeecord 插件消息传递不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 10:43:34 28 4
gpt4 key购买 nike

我正在尝试制作一个具有“全局”配置文件的插件。现在,我正在尝试使用插件消息传递通过字符串将整个配置文件发送到另一台服务器。我已按照 https://www.spigotmc.org/wiki/bukkit-bungee-plugin-messaging-channel/ 上的指南进行操作并对所发送的内容进行了我自己的一点改动。我正在尝试在 spigot 插件中发送插件消息,所以也许这就是问题所在。下面的代码是我用来发送它的代码的摘要(我取出了 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 插件发送吗?提前致谢。如果您对代码有任何疑问,请告诉我。

最佳答案

不工作,因为它是写入的(要发送到的字符串服务器,或全部发送到每个服务器(发送插件消息的服务器除外))!要使用它,您可以使用我们自己的 channel 或redis

关于java - Spigot-Bungeecord 插件消息传递不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48671819/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com