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

71 lines
2.3 KiB
Java
Raw Normal View History

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;
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;
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;
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-06-13 11:53:49 +00:00
public void sendGlobalChat(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()
2021-07-30 01:16:41 +00:00
.map(ServerWrapper::getRegisteredServer)
.forEach(registeredServer -> {
ByteArrayDataOutput buf = ByteStreams.newDataOutput();
buf.writeUTF("globalchat");
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;
}
}