use of net.glowstone.GlowServer in project Glowstone by GlowstoneMC.
the class GameServerTest method setUp.
@BeforeEach
public void setUp() throws IllegalAccessException {
GlowServer.logger.addHandler(handler);
glowServer = mock(GlowServer.class, Answers.RETURNS_SMART_NULLS);
CountDownLatch latch = new CountDownLatch(1);
ProtocolProvider protocolProvider = new ProtocolProvider(mock(HandshakeProtocol.class), mock(StatusProtocol.class), mock(LoginProtocol.class), mock(PlayProtocol.class));
gameServer = new GameServer(glowServer, protocolProvider, latch);
latch.countDown();
}
use of net.glowstone.GlowServer in project Glowstone by GlowstoneMC.
the class StatusRequestHandler method handle.
@Override
@SuppressWarnings("unchecked")
public void handle(GlowSession session, StatusRequestMessage message) {
// create and call the event
GlowServer server = session.getServer();
StatusEvent event = new StatusEvent(new GlowStatusClient(session), server.getMotd(), server.getOnlinePlayers().size(), server.getMaxPlayers(), GlowServer.GAME_VERSION, GlowServer.PROTOCOL_VERSION, server.getServerIcon());
event.serverType = server.getServerType();
event.clientModsAllowed = server.getAllowClientMods();
choosePlayerSample(server, event);
EventFactory.getInstance().callEvent(event);
// Disconnect immediately if event is cancelled
if (event.isCancelled()) {
session.getChannel().close();
return;
}
// build the json
JSONObject json = new JSONObject();
JSONObject version = new JSONObject();
version.put("name", event.getVersion());
version.put("protocol", event.getProtocolVersion());
json.put("version", version);
if (!event.shouldHidePlayers()) {
JSONObject players = new JSONObject();
players.put("max", event.getMaxPlayers());
players.put("online", event.getNumPlayers());
JSONArray playersSample = new JSONArray();
for (PlayerProfile profile : event.getPlayerSample()) {
JSONObject p = new JSONObject();
p.put("name", Strings.nullToEmpty(profile.getName()));
p.put("id", UuidUtils.toString(MoreObjects.firstNonNull(profile.getId(), BLANK_UUID)));
playersSample.add(p);
}
players.put("sample", playersSample);
json.put("players", players);
}
JSONObject description = new JSONObject();
description.put("text", event.getMotd());
json.put("description", description);
if (event.getServerIcon() != null) {
json.put("favicon", event.getServerIcon().getData());
}
// Mod list must be included but can be empty
// TODO: support adding GS-ported Forge server-side mods?
JSONArray modList = new JSONArray();
JSONObject modinfo = new JSONObject();
modinfo.put("type", event.serverType);
modinfo.put("modList", modList);
if (!event.clientModsAllowed) {
modinfo.put("clientModsAllowed", false);
}
json.put("modinfo", modinfo);
// send it off
session.send(new StatusResponseMessage(json));
}
use of net.glowstone.GlowServer in project Glowstone by GlowstoneMC.
the class BanCommand method execute.
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args, CommandMessages commandMessages) {
if (!testPermission(sender, commandMessages.getPermissionMessage())) {
return true;
}
final ResourceBundle resourceBundle = commandMessages.getResourceBundle();
if (args.length > 0) {
String name = args[0];
GlowServer server = (GlowServer) ServerProvider.getServer();
// asynchronously lookup player
server.getOfflinePlayerAsync(name).whenCompleteAsync((player, ex) -> {
if (ex != null) {
new LocalizedStringImpl("ban.exception", resourceBundle).sendInColor(ChatColor.RED, sender, name, ex.getMessage());
return;
}
if (player == null) {
commandMessages.getGeneric(GenericMessage.NO_SUCH_PLAYER).sendInColor(ChatColor.RED, sender, name);
return;
}
if (args.length == 1) {
Bukkit.getBanList(BanList.Type.NAME).addBan(player.getName(), null, null, null);
} else {
StringBuilder reason = new StringBuilder();
for (int i = 1; i < args.length; i++) {
reason.append(args[i]).append(" ");
}
Bukkit.getBanList(BanList.Type.NAME).addBan(player.getName(), reason.toString(), null, null);
}
new LocalizedStringImpl("ban.done", resourceBundle).send(sender, player.getName());
});
// todo: asynchronous command callbacks?
return true;
}
sendUsageMessage(sender, commandMessages);
return false;
}
use of net.glowstone.GlowServer in project Glowstone by GlowstoneMC.
the class WhitelistCommand method execute.
@Override
public boolean execute(CommandSender sender, String label, String[] args, CommandMessages commandMessages) {
if (!testPermission(sender, commandMessages.getPermissionMessage())) {
return true;
}
if (args.length == 0) {
sendUsageMessage(sender, commandMessages);
return false;
}
String subcommand = args[0];
GlowServer server = (GlowServer) ServerProvider.getServer();
if (subcommand.equals("on")) {
sender.getServer().setWhitelist(true);
sender.sendMessage("Turned on the whitelist");
return true;
}
if (subcommand.equals("off")) {
sender.getServer().setWhitelist(false);
sender.sendMessage("Turned off the whitelist");
return true;
}
if (subcommand.equals("list")) {
Set<OfflinePlayer> whitelistedPlayers = sender.getServer().getWhitelistedPlayers();
String[] names = new String[whitelistedPlayers.size()];
int i = 0;
for (OfflinePlayer p : whitelistedPlayers) {
names[i++] = p.getName();
}
sender.sendMessage("There are " + names.length + " whitelisted players:");
sender.sendMessage(commandMessages.joinList(names));
return true;
}
if (subcommand.equals("add")) {
if (args.length != 2) {
sender.sendMessage(ChatColor.RED + "Usage: /whitelist add <player>");
return false;
}
String name = args[1];
server.getOfflinePlayerAsync(name).whenCompleteAsync((player, ex) -> {
if (ex != null) {
sender.sendMessage(ChatColor.RED + "Failed to add " + name + " to the whitelist: " + ex.getMessage());
ex.printStackTrace();
return;
}
player.setWhitelisted(true);
sender.sendMessage("Added " + player.getName() + " to the whitelist");
});
return true;
}
if (subcommand.equals("remove")) {
if (args.length != 2) {
sender.sendMessage(ChatColor.RED + "Usage: /whitelist remove <player>");
return false;
}
String name = args[1];
server.getOfflinePlayerAsync(name).whenCompleteAsync((player, ex) -> {
if (ex != null) {
sender.sendMessage(ChatColor.RED + "Failed to remove " + name + " from the whitelist: " + ex.getMessage());
ex.printStackTrace();
return;
}
player.setWhitelisted(false);
sender.sendMessage("Removed " + player.getName() + " from the whitelist");
});
return true;
}
if (subcommand.equals("reload")) {
sender.getServer().reloadWhitelist();
sender.sendMessage("Reloaded the whitelist");
return true;
}
sendUsageMessage(sender, commandMessages);
return false;
}
use of net.glowstone.GlowServer in project Glowstone by GlowstoneMC.
the class OpCommand method execute.
@Override
public boolean execute(CommandSender sender, String label, String[] args, CommandMessages commandMessages) {
if (!testPermission(sender, commandMessages.getPermissionMessage())) {
return true;
}
if (args.length != 1) {
sendUsageMessage(sender, commandMessages);
return false;
}
String name = args[0];
GlowServer server = (GlowServer) ServerProvider.getServer();
// asynchronously lookup player
server.getOfflinePlayerAsync(name).whenCompleteAsync((player, ex) -> {
if (ex != null) {
new LocalizedStringImpl("op.failed", commandMessages.getResourceBundle()).sendInColor(ChatColor.RED, sender, name, ex.getMessage());
ConsoleMessages.Error.Command.OP_FAILED.log(ex, name);
return;
}
player.setOp(true);
new LocalizedStringImpl("op.done", commandMessages.getResourceBundle()).send(sender, name);
});
// todo: asynchronous command callbacks?
return true;
}
Aggregations