Search in sources :

Example 1 with Server

use of cn.nukkit.Server in project Nukkit by Nukkit.

the class DebugPasteCommand method execute.

@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!this.testPermission(sender)) {
        return true;
    }
    Server server = Server.getInstance();
    server.getScheduler().scheduleAsyncTask(new AsyncTask() {

        @Override
        public void onRun() {
            try {
                new StatusCommand("status").execute(server.getConsoleSender(), "status", new String[] {});
                String dataPath = server.getDataPath();
                String nukkitYML = HastebinUtility.upload(new File(dataPath, "nukkit.yml"));
                String serverProperties = HastebinUtility.upload(new File(dataPath, "server.properties"));
                String latestLog = HastebinUtility.upload(new File(dataPath, "server.log"));
                String threadDump = HastebinUtility.upload(Utils.getAllThreadDumps());
                StringBuilder b = new StringBuilder();
                b.append("# Files\n");
                b.append("links.nukkit_yml: ").append(nukkitYML).append('\n');
                b.append("links.server_properties: ").append(serverProperties).append('\n');
                b.append("links.server_log: ").append(latestLog).append('\n');
                b.append("links.thread_dump: ").append(threadDump).append('\n');
                b.append("\n# Server Information\n");
                b.append("version.api: ").append(server.getApiVersion()).append('\n');
                b.append("version.nukkit: ").append(server.getNukkitVersion()).append('\n');
                b.append("version.minecraft: ").append(server.getVersion()).append('\n');
                b.append("version.protocol: ").append(ProtocolInfo.CURRENT_PROTOCOL).append('\n');
                b.append("plugins:");
                for (Plugin plugin : server.getPluginManager().getPlugins().values()) {
                    boolean enabled = plugin.isEnabled();
                    String name = plugin.getName();
                    PluginDescription desc = plugin.getDescription();
                    String version = desc.getVersion();
                    b.append("\n  ").append(name).append(":\n    ").append("version: '").append(version).append('\'').append("\n    enabled: ").append(enabled);
                }
                b.append("\n\n# Java Details\n");
                Runtime runtime = Runtime.getRuntime();
                b.append("memory.free: ").append(runtime.freeMemory()).append('\n');
                b.append("memory.max: ").append(runtime.maxMemory()).append('\n');
                b.append("cpu.runtime: ").append(ManagementFactory.getRuntimeMXBean().getUptime()).append('\n');
                b.append("cpu.processors: ").append(runtime.availableProcessors()).append('\n');
                b.append("java.specification.version: '").append(System.getProperty("java.specification.version")).append("'\n");
                b.append("java.vendor: '").append(System.getProperty("java.vendor")).append("'\n");
                b.append("java.version: '").append(System.getProperty("java.version")).append("'\n");
                b.append("os.arch: '").append(System.getProperty("os.arch")).append("'\n");
                b.append("os.name: '").append(System.getProperty("os.name")).append("'\n");
                b.append("os.version: '").append(System.getProperty("os.version")).append("'\n\n");
                b.append("\n# Create a ticket: https://github.com/NukkitX/Nukkit/issues/new");
                String link = HastebinUtility.upload(b.toString());
                sender.sendMessage(link);
            } catch (IOException e) {
                MainLogger.getLogger().logException(e);
            }
        }
    });
    return true;
}
Also used : Server(cn.nukkit.Server) AsyncTask(cn.nukkit.scheduler.AsyncTask) IOException(java.io.IOException) File(java.io.File) PluginDescription(cn.nukkit.plugin.PluginDescription) Plugin(cn.nukkit.plugin.Plugin)

Example 2 with Server

use of cn.nukkit.Server in project Nukkit by Nukkit.

the class Item method initCreativeItems.

private static void initCreativeItems() {
    clearCreativeItems();
    Server server = Server.getInstance();
    String path = server.getDataPath() + "creativeitems.json";
    if (!new File(path).exists()) {
        try {
            Utils.writeFile(path, Server.class.getClassLoader().getResourceAsStream("creativeitems.json"));
        } catch (IOException e) {
            MainLogger.getLogger().logException(e);
            return;
        }
    }
    List<Map> list = new Config(path, Config.YAML).getMapList("items");
    for (Map map : list) {
        try {
            int id = (int) map.get("id");
            int damage = (int) map.getOrDefault("damage", 0);
            String hex = (String) map.get("nbt_hex");
            byte[] nbt = hex != null ? Utils.parseHexBinary(hex) : new byte[0];
            addCreativeItem(Item.get(id, damage, 1, nbt));
        } catch (Exception e) {
            MainLogger.getLogger().logException(e);
        }
    }
}
Also used : Server(cn.nukkit.Server) Config(cn.nukkit.utils.Config) IOException(java.io.IOException) File(java.io.File) Map(java.util.Map) IOException(java.io.IOException)

Example 3 with Server

use of cn.nukkit.Server in project Nukkit by Nukkit.

the class TimingsExport method reportTimings.

/**
 * Builds a JSON timings report and sends it to Aikar's viewer
 *
 * @param sender Sender that issued the command
 */
public static void reportTimings(CommandSender sender) {
    JsonObject out = new JsonObject();
    out.addProperty("version", Server.getInstance().getVersion());
    out.addProperty("maxplayers", Server.getInstance().getMaxPlayers());
    out.addProperty("start", TimingsManager.timingStart / 1000);
    out.addProperty("end", System.currentTimeMillis() / 1000);
    out.addProperty("sampletime", (System.currentTimeMillis() - TimingsManager.timingStart) / 1000);
    if (!Timings.isPrivacy()) {
        out.addProperty("server", Server.getInstance().getName());
        out.addProperty("motd", Server.getInstance().getMotd());
        // In MCPE we have permanent offline mode.
        out.addProperty("online-mode", false);
        // "data:image/png;base64,"
        out.addProperty("icon", "");
    }
    final Runtime runtime = Runtime.getRuntime();
    RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean();
    JsonObject system = new JsonObject();
    system.addProperty("timingcost", getCost());
    system.addProperty("name", System.getProperty("os.name"));
    system.addProperty("version", System.getProperty("os.version"));
    system.addProperty("jvmversion", System.getProperty("java.version"));
    system.addProperty("arch", System.getProperty("os.arch"));
    system.addProperty("maxmem", runtime.maxMemory());
    system.addProperty("cpu", runtime.availableProcessors());
    system.addProperty("runtime", ManagementFactory.getRuntimeMXBean().getUptime());
    system.addProperty("flags", String.join(" ", runtimeBean.getInputArguments()));
    system.add("gc", JsonUtil.mapToObject(ManagementFactory.getGarbageCollectorMXBeans(), (input) -> new JsonUtil.JSONPair(input.getName(), JsonUtil.toArray(input.getCollectionCount(), input.getCollectionTime()))));
    out.add("system", system);
    TimingsHistory[] history = HISTORY.toArray(new TimingsHistory[HISTORY.size() + 1]);
    // Current snapshot
    history[HISTORY.size()] = new TimingsHistory();
    JsonObject timings = new JsonObject();
    for (TimingIdentifier.TimingGroup group : TimingIdentifier.GROUP_MAP.values()) {
        for (Timing id : group.timings.stream().toArray(Timing[]::new)) {
            if (!id.timed && !id.isSpecial()) {
                continue;
            }
            timings.add(String.valueOf(id.id), JsonUtil.toArray(group.id, id.name));
        }
    }
    JsonObject idmap = new JsonObject();
    idmap.add("groups", JsonUtil.mapToObject(TimingIdentifier.GROUP_MAP.values(), (group) -> new JsonUtil.JSONPair(group.id, group.name)));
    idmap.add("handlers", timings);
    idmap.add("worlds", JsonUtil.mapToObject(TimingsHistory.levelMap.entrySet(), (entry) -> new JsonUtil.JSONPair(entry.getValue(), entry.getKey())));
    idmap.add("tileentity", JsonUtil.mapToObject(TimingsHistory.blockEntityMap.entrySet(), (entry) -> new JsonUtil.JSONPair(entry.getKey(), entry.getValue())));
    idmap.add("entity", JsonUtil.mapToObject(TimingsHistory.entityMap.entrySet(), (entry) -> new JsonUtil.JSONPair(entry.getKey(), entry.getValue())));
    out.add("idmap", idmap);
    // Information about loaded plugins
    out.add("plugins", JsonUtil.mapToObject(Server.getInstance().getPluginManager().getPlugins().values(), (plugin) -> {
        JsonObject jsonPlugin = new JsonObject();
        jsonPlugin.addProperty("version", plugin.getDescription().getVersion());
        // Sounds legit
        jsonPlugin.addProperty("description", plugin.getDescription().getDescription());
        jsonPlugin.addProperty("website", plugin.getDescription().getWebsite());
        jsonPlugin.addProperty("authors", String.join(", ", plugin.getDescription().getAuthors()));
        return new JsonUtil.JSONPair(plugin.getName(), jsonPlugin);
    }));
    // Information on the users Config
    JsonObject config = new JsonObject();
    if (!Timings.getIgnoredConfigSections().contains("all")) {
        JsonObject nukkit = JsonUtil.toObject(Server.getInstance().getConfig().getRootSection());
        Timings.getIgnoredConfigSections().forEach(nukkit::remove);
        config.add("nukkit", nukkit);
    } else {
        config.add("nukkit", null);
    }
    out.add("config", config);
    new TimingsExport(sender, out, history).start();
}
Also used : HttpURLConnection(java.net.HttpURLConnection) JsonObject(com.google.gson.JsonObject) RuntimeMXBean(java.lang.management.RuntimeMXBean) PGZIPOutputStream(cn.nukkit.nbt.stream.PGZIPOutputStream) URL(java.net.URL) Date(java.util.Date) HISTORY(co.aikar.timings.TimingsManager.HISTORY) SimpleDateFormat(java.text.SimpleDateFormat) JsonUtil(cn.nukkit.timings.JsonUtil) Deflater(java.util.zip.Deflater) Server(cn.nukkit.Server) CommandSender(cn.nukkit.command.CommandSender) GsonBuilder(com.google.gson.GsonBuilder) ConsoleCommandSender(cn.nukkit.command.ConsoleCommandSender) TranslationContainer(cn.nukkit.lang.TranslationContainer) InetAddress(java.net.InetAddress) RemoteConsoleCommandSender(cn.nukkit.command.RemoteConsoleCommandSender) java.io(java.io) TextFormat(cn.nukkit.utils.TextFormat) Gson(com.google.gson.Gson) ManagementFactory(java.lang.management.ManagementFactory) JsonObject(com.google.gson.JsonObject) RuntimeMXBean(java.lang.management.RuntimeMXBean) JsonUtil(cn.nukkit.timings.JsonUtil)

Example 4 with Server

use of cn.nukkit.Server in project Nukkit by Nukkit.

the class StatusCommand method execute.

@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!this.testPermission(sender)) {
        return true;
    }
    Server server = sender.getServer();
    sender.sendMessage(TextFormat.GREEN + "---- " + TextFormat.WHITE + "Server status" + TextFormat.GREEN + " ----");
    long time = (System.currentTimeMillis() - Nukkit.START_TIME) / 1000;
    int seconds = NukkitMath.floorDouble(time % 60);
    int minutes = NukkitMath.floorDouble((time % 3600) / 60);
    int hours = NukkitMath.floorDouble(time % (3600 * 24) / 3600);
    int days = NukkitMath.floorDouble(time / (3600 * 24));
    String upTimeString = TextFormat.RED + "" + days + TextFormat.GOLD + " days " + TextFormat.RED + hours + TextFormat.GOLD + " hours " + TextFormat.RED + minutes + TextFormat.GOLD + " minutes " + TextFormat.RED + seconds + TextFormat.GOLD + " seconds";
    sender.sendMessage(TextFormat.GOLD + "Uptime: " + upTimeString);
    TextFormat tpsColor = TextFormat.GREEN;
    float tps = server.getTicksPerSecond();
    if (tps < 17) {
        tpsColor = TextFormat.GOLD;
    } else if (tps < 12) {
        tpsColor = TextFormat.RED;
    }
    sender.sendMessage(TextFormat.GOLD + "Current TPS: " + tpsColor + NukkitMath.round(tps, 2));
    sender.sendMessage(TextFormat.GOLD + "Load: " + tpsColor + server.getTickUsage() + "%");
    sender.sendMessage(TextFormat.GOLD + "Network upload: " + TextFormat.GREEN + NukkitMath.round((server.getNetwork().getUpload() / 1024 * 1000), 2) + " kB/s");
    sender.sendMessage(TextFormat.GOLD + "Network download: " + TextFormat.GREEN + NukkitMath.round((server.getNetwork().getDownload() / 1024 * 1000), 2) + " kB/s");
    sender.sendMessage(TextFormat.GOLD + "Thread count: " + TextFormat.GREEN + Thread.getAllStackTraces().size());
    Runtime runtime = Runtime.getRuntime();
    double totalMB = NukkitMath.round(((double) runtime.totalMemory()) / 1024 / 1024, 2);
    double usedMB = NukkitMath.round((double) (runtime.totalMemory() - runtime.freeMemory()) / 1024 / 1024, 2);
    double maxMB = NukkitMath.round(((double) runtime.maxMemory()) / 1024 / 1024, 2);
    double usage = usedMB / maxMB * 100;
    TextFormat usageColor = TextFormat.GREEN;
    if (usage > 85) {
        usageColor = TextFormat.GOLD;
    }
    sender.sendMessage(TextFormat.GOLD + "Used memory: " + usageColor + usedMB + " MB. (" + NukkitMath.round(usage, 2) + "%)");
    sender.sendMessage(TextFormat.GOLD + "Total memory: " + TextFormat.RED + totalMB + " MB.");
    sender.sendMessage(TextFormat.GOLD + "Maximum VM memory: " + TextFormat.RED + maxMB + " MB.");
    sender.sendMessage(TextFormat.GOLD + "Available processors: " + TextFormat.GREEN + runtime.availableProcessors());
    TextFormat playerColor = TextFormat.GREEN;
    if (((float) server.getOnlinePlayers().size() / (float) server.getMaxPlayers()) > 0.85) {
        playerColor = TextFormat.GOLD;
    }
    sender.sendMessage(TextFormat.GOLD + "Players: " + playerColor + server.getOnlinePlayers().size() + TextFormat.GREEN + " online, " + TextFormat.RED + server.getMaxPlayers() + TextFormat.GREEN + " max. ");
    for (Level level : server.getLevels().values()) {
        sender.sendMessage(TextFormat.GOLD + "World \"" + level.getFolderName() + "\"" + (!Objects.equals(level.getFolderName(), level.getName()) ? " (" + level.getName() + ")" : "") + ": " + TextFormat.RED + level.getChunks().size() + TextFormat.GREEN + " chunks, " + TextFormat.RED + level.getEntities().length + TextFormat.GREEN + " entities, " + TextFormat.RED + level.getBlockEntities().size() + TextFormat.GREEN + " blockEntities." + " Time " + ((level.getTickRate() > 1 || level.getTickRateTime() > 40) ? TextFormat.RED : TextFormat.YELLOW) + NukkitMath.round(level.getTickRateTime(), 2) + "ms" + (level.getTickRate() > 1 ? " (tick rate " + level.getTickRate() + ")" : ""));
    }
    return true;
}
Also used : Server(cn.nukkit.Server) TextFormat(cn.nukkit.utils.TextFormat) Level(cn.nukkit.level.Level)

Aggregations

Server (cn.nukkit.Server)4 TextFormat (cn.nukkit.utils.TextFormat)2 File (java.io.File)2 IOException (java.io.IOException)2 CommandSender (cn.nukkit.command.CommandSender)1 ConsoleCommandSender (cn.nukkit.command.ConsoleCommandSender)1 RemoteConsoleCommandSender (cn.nukkit.command.RemoteConsoleCommandSender)1 TranslationContainer (cn.nukkit.lang.TranslationContainer)1 Level (cn.nukkit.level.Level)1 PGZIPOutputStream (cn.nukkit.nbt.stream.PGZIPOutputStream)1 Plugin (cn.nukkit.plugin.Plugin)1 PluginDescription (cn.nukkit.plugin.PluginDescription)1 AsyncTask (cn.nukkit.scheduler.AsyncTask)1 JsonUtil (cn.nukkit.timings.JsonUtil)1 Config (cn.nukkit.utils.Config)1 HISTORY (co.aikar.timings.TimingsManager.HISTORY)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 JsonObject (com.google.gson.JsonObject)1 java.io (java.io)1