|
package mnm.plugins.serverinfo; |
|
|
|
import java.io.File; |
|
import java.io.IOException; |
|
import java.io.InputStream; |
|
import java.nio.file.Files; |
|
|
|
import net.md_5.bungee.api.config.ServerInfo; |
|
import net.md_5.bungee.api.connection.ProxiedPlayer; |
|
import net.md_5.bungee.api.event.ServerConnectedEvent; |
|
import net.md_5.bungee.api.plugin.Listener; |
|
import net.md_5.bungee.api.plugin.Plugin; |
|
import net.md_5.bungee.config.Configuration; |
|
import net.md_5.bungee.config.ConfigurationProvider; |
|
import net.md_5.bungee.config.YamlConfiguration; |
|
import net.md_5.bungee.event.EventHandler; |
|
|
|
import com.google.gson.Gson; |
|
import com.google.gson.JsonArray; |
|
import com.google.gson.JsonObject; |
|
import com.google.gson.JsonPrimitive; |
|
|
|
public class ServerInfoPlugin extends Plugin implements Listener { |
|
|
|
private static final String cfgSEND_PLAYER_LIST = "sendPlayerList"; |
|
|
|
private static final String msgCHANNEL = "serverinfo"; |
|
private static final String msgNAME = "name"; |
|
private static final String msgMOTD = "motd"; |
|
private static final String msgPLAYERS = "playerList"; |
|
|
|
private Configuration config; |
|
|
|
@Override |
|
public void onEnable() { |
|
|
|
if (!getDataFolder().exists()) |
|
getDataFolder().mkdir(); |
|
|
|
File file = new File(getDataFolder(), "config.yml"); |
|
InputStream stream = getResourceAsStream("config.yml"); |
|
|
|
try { |
|
if (!file.exists()) { |
|
Files.copy(stream, file.toPath()); |
|
} |
|
config = ConfigurationProvider.getProvider(YamlConfiguration.class).load(file); |
|
} catch (IOException e) { |
|
config = ConfigurationProvider.getProvider(YamlConfiguration.class).load(stream); |
|
} finally { |
|
try { |
|
stream.close(); |
|
} catch (IOException e) { |
|
} |
|
} |
|
|
|
this.getProxy().getPluginManager().registerListener(this, this); |
|
} |
|
|
|
@EventHandler |
|
public void onServerSwitch(ServerConnectedEvent event) { |
|
final ProxiedPlayer player = event.getPlayer(); |
|
|
|
JsonObject msg = getServerInfo(event.getServer().getInfo()); |
|
getLogger().info("Sending server info for " + msg.get(msgNAME) + " to " + player.getName()); |
|
player.sendData(msgCHANNEL, new Gson().toJson(msg).getBytes()); |
|
} |
|
|
|
private JsonObject getServerInfo(ServerInfo server) { |
|
JsonObject obj = new JsonObject(); |
|
obj.addProperty(msgNAME, server.getName()); |
|
obj.addProperty(msgMOTD, server.getMotd()); |
|
|
|
if (config.getBoolean(cfgSEND_PLAYER_LIST, false)) { |
|
|
|
JsonArray players = new JsonArray(); |
|
for (ProxiedPlayer player : server.getPlayers()) { |
|
players.add(new JsonPrimitive(player.getName())); |
|
} |
|
obj.add(msgPLAYERS, players); |
|
} |
|
return obj; |
|
} |
|
} |