Chat/velocity/src/main/java/com/alttd/velocitychat/handlers/ServerHandler.java

69 lines
2.2 KiB
Java
Raw Normal View History

2021-12-11 18:35:35 +00:00
package com.alttd.velocitychat.handlers;
2021-12-11 18:35:35 +00:00
import com.alttd.velocitychat.VelocityChat;
import com.alttd.chat.config.ServerConfig;
2021-12-11 18:35:35 +00:00
import com.alttd.velocitychat.data.ServerWrapper;
2021-07-30 01:16:41 +00:00
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
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())));
}
}
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
servers.stream()
.filter(ServerWrapper::globalChat)
2021-07-30 01:16:41 +00:00
.map(ServerWrapper::getRegisteredServer)
.forEach(registeredServer -> {
ByteArrayDataOutput buf = ByteStreams.newDataOutput();
buf.writeUTF("globalchat");
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));
}
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;
}
}