2021-05-13 18:27:20 +00:00
|
|
|
package com.alttd.chat.handlers;
|
|
|
|
|
|
|
|
|
|
import com.alttd.chat.VelocityChat;
|
|
|
|
|
import com.alttd.chat.config.ServerConfig;
|
|
|
|
|
import com.alttd.chat.data.ServerWrapper;
|
2021-07-30 01:16:41 +00:00
|
|
|
import com.google.common.io.ByteArrayDataOutput;
|
|
|
|
|
import com.google.common.io.ByteStreams;
|
2021-06-06 19:32:13 +00:00
|
|
|
import com.velocitypowered.api.proxy.Player;
|
2021-05-13 18:27:20 +00:00
|
|
|
import com.velocitypowered.api.proxy.server.RegisteredServer;
|
2021-05-22 18:34:32 +00:00
|
|
|
import net.kyori.adventure.text.Component;
|
2021-06-13 11:53:49 +00:00
|
|
|
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
2021-05-13 18:27:20 +00:00
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.List;
|
2021-06-06 19:32:13 +00:00
|
|
|
import java.util.stream.Collectors;
|
2021-05-13 18:27:20 +00:00
|
|
|
|
|
|
|
|
public class ServerHandler {
|
|
|
|
|
|
|
|
|
|
private VelocityChat plugin;
|
|
|
|
|
|
|
|
|
|
private static List<ServerWrapper> servers;
|
|
|
|
|
|
|
|
|
|
public ServerHandler() {
|
|
|
|
|
plugin = VelocityChat.getPlugin();
|
|
|
|
|
initialize();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void cleanup() { // for use on /reload?
|
|
|
|
|
servers.clear();
|
|
|
|
|
initialize();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void initialize() {
|
|
|
|
|
servers = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
for (RegisteredServer registeredServer : plugin.getProxy().getAllServers()) {
|
|
|
|
|
servers.add(new ServerWrapper(registeredServer, new ServerConfig(registeredServer.getServerInfo().getName())));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-31 01:46:50 +00:00
|
|
|
public void sendGlobalChat(String uuid, String message) {
|
2021-07-30 01:16:41 +00:00
|
|
|
// Component component = GsonComponentSerializer.gson().deserialize(message);
|
2021-06-13 11:53:49 +00:00
|
|
|
|
2021-05-13 18:27:20 +00:00
|
|
|
servers.stream()
|
2021-07-30 01:16:41 +00:00
|
|
|
.map(ServerWrapper::getRegisteredServer)
|
|
|
|
|
.forEach(registeredServer -> {
|
|
|
|
|
ByteArrayDataOutput buf = ByteStreams.newDataOutput();
|
|
|
|
|
buf.writeUTF("globalchat");
|
2021-07-31 01:46:50 +00:00
|
|
|
buf.writeUTF(uuid);
|
2021-07-30 01:16:41 +00:00
|
|
|
buf.writeUTF(message);
|
|
|
|
|
registeredServer.sendPluginMessage(VelocityChat.getPlugin().getChannelIdentifier(), buf.toByteArray());
|
|
|
|
|
});
|
|
|
|
|
// .filter(serverWrapper -> serverWrapper.globalChat())
|
|
|
|
|
// .forEach(serverWrapper -> serverWrapper.getRegisteredServer().sendMessage(component));
|
2021-05-13 18:27:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ServerWrapper> getServers()
|
|
|
|
|
{
|
|
|
|
|
return Collections.unmodifiableList(servers);
|
|
|
|
|
}
|
2021-05-15 10:08:29 +00:00
|
|
|
|
|
|
|
|
public ServerWrapper getWrapper(String serverName) {
|
2021-05-15 11:26:31 +00:00
|
|
|
for(ServerWrapper wrapper : servers) {
|
2021-05-15 10:08:29 +00:00
|
|
|
if(wrapper.serverName().equalsIgnoreCase(serverName)) {
|
|
|
|
|
return wrapper;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2021-05-13 18:27:20 +00:00
|
|
|
}
|