Search in sources :

Example 1 with CommandSender

use of cn.nukkit.command.CommandSender in project Nukkit by Nukkit.

the class TeleportCommand method execute.

@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!this.testPermission(sender)) {
        return true;
    }
    if (args.length < 1 || args.length > 6) {
        sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
        return true;
    }
    CommandSender target;
    CommandSender origin = sender;
    if (args.length == 1 || args.length == 3) {
        if (sender instanceof Player) {
            target = sender;
        } else {
            sender.sendMessage(new TranslationContainer("commands.generic.ingame"));
            return true;
        }
        if (args.length == 1) {
            target = sender.getServer().getPlayer(args[0]);
            if (target == null) {
                sender.sendMessage(TextFormat.RED + "Can't find player " + args[0]);
                return true;
            }
        }
    } else {
        target = sender.getServer().getPlayer(args[0]);
        if (target == null) {
            sender.sendMessage(TextFormat.RED + "Can't find player " + args[0]);
            return true;
        }
        if (args.length == 2) {
            origin = target;
            target = sender.getServer().getPlayer(args[1]);
            if (target == null) {
                sender.sendMessage(TextFormat.RED + "Can't find player " + args[1]);
                return true;
            }
        }
    }
    if (args.length < 3) {
        ((Player) origin).teleport((Player) target, PlayerTeleportEvent.TeleportCause.COMMAND);
        Command.broadcastCommandMessage(sender, new TranslationContainer("commands.tp.success", new String[] { origin.getName(), target.getName() }));
        return true;
    } else if (((Player) target).getLevel() != null) {
        int pos;
        if (args.length == 4 || args.length == 6) {
            pos = 1;
        } else {
            pos = 0;
        }
        double x;
        double y;
        double z;
        double yaw;
        double pitch;
        try {
            x = Double.parseDouble(args[pos++]);
            y = Double.parseDouble(args[pos++]);
            z = Double.parseDouble(args[pos++]);
            yaw = ((Player) target).getYaw();
            pitch = ((Player) target).getPitch();
        } catch (NumberFormatException e1) {
            sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
            return true;
        }
        if (y < 0)
            y = 0;
        if (y > 256)
            y = 256;
        if (args.length == 6 || (args.length == 5 && pos == 3)) {
            yaw = Integer.parseInt(args[pos++]);
            pitch = Integer.parseInt(args[pos++]);
        }
        ((Player) target).teleport(new Location(x, y, z, yaw, pitch, ((Player) target).getLevel()), PlayerTeleportEvent.TeleportCause.COMMAND);
        Command.broadcastCommandMessage(sender, new TranslationContainer("commands.tp.success.coordinates", new String[] { target.getName(), String.valueOf(NukkitMath.round(x, 2)), String.valueOf(NukkitMath.round(y, 2)), String.valueOf(NukkitMath.round(z, 2)) }));
        return true;
    }
    sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
    return true;
}
Also used : Player(cn.nukkit.Player) TranslationContainer(cn.nukkit.lang.TranslationContainer) CommandSender(cn.nukkit.command.CommandSender) Location(cn.nukkit.level.Location)

Example 2 with CommandSender

use of cn.nukkit.command.CommandSender in project LuckPerms by lucko.

the class NukkitCommandExecutor method onCommand.

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    Sender lpSender = this.plugin.getSenderFactory().wrap(sender);
    onCommand(lpSender, label, new ArrayList<>(Arrays.asList(args)));
    return true;
}
Also used : Sender(me.lucko.luckperms.common.sender.Sender) CommandSender(cn.nukkit.command.CommandSender)

Example 3 with CommandSender

use of cn.nukkit.command.CommandSender 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 CommandSender

use of cn.nukkit.command.CommandSender in project Nukkit by Nukkit.

the class GamemodeCommand method execute.

@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (args.length == 0) {
        sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
        return false;
    }
    int gameMode = Server.getGamemodeFromString(args[0]);
    if (gameMode == -1) {
        sender.sendMessage("Unknown game mode");
        return true;
    }
    CommandSender target = sender;
    if (args.length > 1) {
        if (sender.hasPermission("nukkit.command.gamemode.other")) {
            target = sender.getServer().getPlayer(args[1]);
            if (target == null) {
                sender.sendMessage(new TranslationContainer(TextFormat.RED + "%commands.generic.player.notFound"));
                return true;
            }
        } else {
            sender.sendMessage(new TranslationContainer(TextFormat.RED + "%commands.generic.permission"));
            return true;
        }
    } else if (!(sender instanceof Player)) {
        sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
        return true;
    }
    if ((gameMode == 0 && !sender.hasPermission("nukkit.command.gamemode.survival")) || (gameMode == 1 && !sender.hasPermission("nukkit.command.gamemode.creative")) || (gameMode == 2 && !sender.hasPermission("nukkit.command.gamemode.adventure")) || (gameMode == 3 && !sender.hasPermission("nukkit.command.gamemode.spectator"))) {
        sender.sendMessage(new TranslationContainer(TextFormat.RED + "%commands.generic.permission"));
        return true;
    }
    if (!((Player) target).setGamemode(gameMode)) {
        sender.sendMessage("Game mode update for " + target.getName() + " failed");
    } else {
        if (target.equals(sender)) {
            Command.broadcastCommandMessage(sender, new TranslationContainer("commands.gamemode.success.self", Server.getGamemodeString(gameMode)));
        } else {
            target.sendMessage(new TranslationContainer("gameMode.changed"));
            Command.broadcastCommandMessage(sender, new TranslationContainer("commands.gamemode.success.other", new String[] { target.getName(), Server.getGamemodeString(gameMode) }));
        }
    }
    return true;
}
Also used : Player(cn.nukkit.Player) TranslationContainer(cn.nukkit.lang.TranslationContainer) CommandSender(cn.nukkit.command.CommandSender)

Aggregations

CommandSender (cn.nukkit.command.CommandSender)4 TranslationContainer (cn.nukkit.lang.TranslationContainer)3 Player (cn.nukkit.Player)2 Server (cn.nukkit.Server)1 ConsoleCommandSender (cn.nukkit.command.ConsoleCommandSender)1 RemoteConsoleCommandSender (cn.nukkit.command.RemoteConsoleCommandSender)1 Location (cn.nukkit.level.Location)1 PGZIPOutputStream (cn.nukkit.nbt.stream.PGZIPOutputStream)1 JsonUtil (cn.nukkit.timings.JsonUtil)1 TextFormat (cn.nukkit.utils.TextFormat)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 ManagementFactory (java.lang.management.ManagementFactory)1 RuntimeMXBean (java.lang.management.RuntimeMXBean)1 HttpURLConnection (java.net.HttpURLConnection)1 InetAddress (java.net.InetAddress)1 URL (java.net.URL)1