是否有 simple/complicated 方法使用套接字创建 requestData() 方法

Is there a simple/complicated way to create requestData() method using Sockets

正如标题所说,我需要一种方法(不管它是否复杂)来创建一个 getData() 方法,该方法将请求数据包发送到服务器 > 接收消息我已经有系统设置但是我有一个问题,我只在 PluginMessageReceiveEvent 中得到结果这是我的代码和解释:

public String requestData(String path) {
    SocketUtils.sendData("REQUEST|" + p.getPlayer().getUniqueId() + "|" + path, "playerconfig", "BUNGEE");
    return /*Need to get data from the plugin message to here*/;
}

@EventHandler
public void onPluginMessageReceive(PluginMessageReceiveEvent e) {
    if (e.getChannel().equalsIgnoreCase("playerconfig")) {
        String[] args = e.getMessage().split("\|");
        String uuid = args[0];
        String path = args[1];//Maybe a HashMap<Path, Data> but that would make the requestData() result return null because you don't get the data instantly.
        String data = args[2].replace("_", " ");
        if (uuid.equals(p.getPlayer().getUniqueId() + "")) {
            return data; //I need to get this result on request data method.
        }
    }
}

一个简单的解决方案是 waitrequestData 中锁定并在 onPluginMessageReceive 中通知该锁定。像这样:

 synchronized(this) {
    wait();
 }

并且在您的接收方法中:

 synchronized(this) {
    notifyAll();
 }

使数据成为class的成员字段。

注意异常处理和语法错误。