Java Code Examples for cn.nukkit.command.Command#broadcastCommandMessage()

The following examples show how to use cn.nukkit.command.Command#broadcastCommandMessage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OpCommand.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!this.testPermission(sender)) {
        return true;
    }
    if (args.length == 0) {
        sender.sendMessage(new TranslationContainer("commands.op.usage", this.usageMessage));
        return false;
    }

    String name = args[0];
    IPlayer player = sender.getServer().getOfflinePlayer(name);

    Command.broadcastCommandMessage(sender, new TranslationContainer("commands.op.success", player.getName()));
    if (player instanceof Player) {
        ((Player) player).sendMessage(new TranslationContainer(TextFormat.GRAY + "%commands.op.message"));
    }

    player.setOp(true);

    return true;
}
 
Example 2
Source File: PardonIpCommand.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!this.testPermission(sender)) {
        return true;
    }

    if (args.length != 1) {
        sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));

        return false;
    }

    String value = args[0];

    if (Pattern.matches("^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$", value)) {
        sender.getServer().getIPBans().remove(value);

        Command.broadcastCommandMessage(sender, new TranslationContainer("commands.unbanip.success", value));
    } else {

        sender.sendMessage(new TranslationContainer("commands.unbanip.invalid"));
    }

    return true;
}
 
Example 3
Source File: PardonCommand.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!this.testPermission(sender)) {
        return true;
    }

    if (args.length != 1) {
        sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));

        return false;
    }

    sender.getServer().getNameBans().remove(args[0]);

    Command.broadcastCommandMessage(sender, new TranslationContainer("%commands.unban.success", args[0]));

    return true;
}
 
Example 4
Source File: SaveCommand.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!this.testPermission(sender)) {
        return true;
    }

    Command.broadcastCommandMessage(sender, new TranslationContainer("commands.save.start"));

    for (Player player : sender.getServer().getOnlinePlayers().values()) {
        player.save();
    }

    for (Level level : sender.getServer().getLevels().values()) {
        level.save(true);
    }

    Command.broadcastCommandMessage(sender, new TranslationContainer("commands.save.success"));
    return true;
}
 
Example 5
Source File: PardonCommand.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!this.testPermission(sender)) {
        return true;
    }

    if (args.length != 1) {
        sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));

        return false;
    }

    sender.getServer().getNameBans().remove(args[0]);

    Command.broadcastCommandMessage(sender, new TranslationContainer("%commands.unban.success", args[0]));

    return true;
}
 
Example 6
Source File: ReloadCommand.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!this.testPermission(sender)) {
        return true;
    }

    Command.broadcastCommandMessage(sender, new TranslationContainer(TextFormat.YELLOW + "%nukkit.command.reload.reloading" + TextFormat.WHITE));

    sender.getServer().reload();

    Command.broadcastCommandMessage(sender, new TranslationContainer(TextFormat.YELLOW + "%nukkit.command.reload.reloaded" + TextFormat.WHITE));

    return true;
}
 
Example 7
Source File: DifficultyCommand.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!this.testPermission(sender)) {
        return true;
    }

    if (args.length != 1) {
        sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
        return false;
    }

    int difficulty = Server.getDifficultyFromString(args[0]);

    if (sender.getServer().isHardcore()) {
        difficulty = 3;
    }

    if (difficulty != -1) {
        sender.getServer().setPropertyInt("difficulty", difficulty);

        SetDifficultyPacket pk = new SetDifficultyPacket();
        pk.difficulty = sender.getServer().getDifficulty();
        Server.broadcastPacket(new ArrayList<>(sender.getServer().getOnlinePlayers().values()), pk);

        Command.broadcastCommandMessage(sender, new TranslationContainer("commands.difficulty.success", String.valueOf(difficulty)));
    } else {
        sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));

        return false;
    }

    return true;
}
 
Example 8
Source File: SaveOnCommand.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!this.testPermission(sender)) {
        return true;
    }
    sender.getServer().setAutoSave(true);
    Command.broadcastCommandMessage(sender, new TranslationContainer("commands.save.enabled"));
    return true;
}
 
Example 9
Source File: BanCommand.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!this.testPermission(sender)) {
        return true;
    }

    if (args.length == 0) {
        sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));

        return false;
    }

    String name = args[0];
    String reason = "";
    for (int i = 1; i < args.length; i++) {
        reason += args[i] + " ";
    }

    if (reason.length() > 0) {
        reason = reason.substring(0, reason.length() - 1);
    }

    sender.getServer().getNameBans().addBan(name, reason, null, sender.getName());

    Player player = sender.getServer().getPlayerExact(name);
    if (player != null) {
        player.kick(PlayerKickEvent.Reason.NAME_BANNED, !reason.isEmpty() ? "Banned by admin. Reason: " + reason : "Banned by admin");
    }

    Command.broadcastCommandMessage(sender, new TranslationContainer("%commands.ban.success", player != null ? player.getName() : name));

    return true;
}
 
Example 10
Source File: EnchantCommand.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!this.testPermission(sender)) {
        return true;
    }
    if (args.length < 2) {
        sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
        return true;
    }
    Player player = sender.getServer().getPlayer(args[0]);
    if (player == null) {
        sender.sendMessage(new TranslationContainer(TextFormat.RED + "%commands.generic.player.notFound"));
        return true;
    }
    int enchantId;
    int enchantLevel;
    try {
        enchantId = getIdByName(args[1]);
        enchantLevel = args.length == 3 ? Integer.parseInt(args[2]) : 1;
    } catch (NumberFormatException e) {
        sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
        return true;
    }
    Enchantment enchantment = Enchantment.getEnchantment(enchantId);
    if (enchantment == null) {
        sender.sendMessage(new TranslationContainer("commands.enchant.notFound", String.valueOf(enchantId)));
        return true;
    }
    enchantment.setLevel(enchantLevel);
    Item item = player.getInventory().getItemInHand();
    if (item.getId() <= 0) {
        sender.sendMessage(new TranslationContainer("commands.enchant.noItem"));
        return true;
    }
    item.addEnchantment(enchantment);
    player.getInventory().setItemInHand(item);
    Command.broadcastCommandMessage(sender, new TranslationContainer("%commands.enchant.success"));
    return true;
}
 
Example 11
Source File: EnchantCommand.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!this.testPermission(sender)) {
        return true;
    }
    if (args.length < 2) {
        sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
        return true;
    }
    Player player = sender.getServer().getPlayer(args[0]);
    if (player == null) {
        sender.sendMessage(new TranslationContainer(TextFormat.RED + "%commands.generic.player.notFound"));
        return true;
    }
    int enchantId;
    int enchantLevel;
    try {
        enchantId = getIdByName(args[1]);
        enchantLevel = args.length == 3 ? Integer.parseInt(args[2]) : 1;
    } catch (NumberFormatException e) {
        sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
        return true;
    }
    Enchantment enchantment = Enchantment.getEnchantment(enchantId);
    if (enchantment == null) {
        sender.sendMessage(new TranslationContainer("commands.enchant.notFound", String.valueOf(enchantId)));
        return true;
    }
    enchantment.setLevel(enchantLevel);
    Item item = player.getInventory().getItemInHand();
    if (item.getId() <= 0) {
        sender.sendMessage(new TranslationContainer("commands.enchant.noItem"));
        return true;
    }
    item.addEnchantment(enchantment);
    player.getInventory().setItemInHand(item);
    Command.broadcastCommandMessage(sender, new TranslationContainer("%commands.enchant.success"));
    return true;
}
 
Example 12
Source File: SaveOffCommand.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!this.testPermission(sender)) {
        return true;
    }
    sender.getServer().setAutoSave(false);
    Command.broadcastCommandMessage(sender, new TranslationContainer("commands.save.disabled"));
    return true;
}
 
Example 13
Source File: SaveOnCommand.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!this.testPermission(sender)) {
        return true;
    }
    sender.getServer().setAutoSave(true);
    Command.broadcastCommandMessage(sender, new TranslationContainer("commands.save.enabled"));
    return true;
}
 
Example 14
Source File: SaveOnCommand.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!this.testPermission(sender)) {
        return true;
    }
    sender.getServer().setAutoSave(true);
    Command.broadcastCommandMessage(sender, new TranslationContainer("commands.save.enabled"));
    return true;
}
 
Example 15
Source File: DifficultyCommand.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!this.testPermission(sender)) {
        return true;
    }

    if (args.length != 1) {
        sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
        return false;
    }

    int difficulty = Server.getDifficultyFromString(args[0]);

    if (sender.getServer().isHardcore()) {
        difficulty = 3;
    }

    if (difficulty != -1) {
        sender.getServer().setPropertyInt("difficulty", difficulty);

        SetDifficultyPacket pk = new SetDifficultyPacket();
        pk.difficulty = sender.getServer().getDifficulty();
        Server.broadcastPacket(new ArrayList<>(sender.getServer().getOnlinePlayers().values()), pk);

        Command.broadcastCommandMessage(sender, new TranslationContainer("commands.difficulty.success", String.valueOf(difficulty)));
    } else {
        sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));

        return false;
    }

    return true;
}
 
Example 16
Source File: ReloadCommand.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!this.testPermission(sender)) {
        return true;
    }

    Command.broadcastCommandMessage(sender, new TranslationContainer(TextFormat.YELLOW + "%nukkit.command.reload.reloading" + TextFormat.WHITE));

    sender.getServer().reload();

    Command.broadcastCommandMessage(sender, new TranslationContainer(TextFormat.YELLOW + "%nukkit.command.reload.reloaded" + TextFormat.WHITE));

    return true;
}
 
Example 17
Source File: GamemodeCommand.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
@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;
}
 
Example 18
Source File: GamemodeCommand.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
@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("" + target.getName() + "のゲームモード変更に失敗しました。");
    } 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;
}
 
Example 19
Source File: WhitelistCommand.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!this.testPermission(sender)) {
        return true;
    }

    if (args.length == 0 || args.length > 2) {
        sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
        return true;
    }

    if (args.length == 1) {
        if (this.badPerm(sender, args[0].toLowerCase())) {
            return false;
        }
        switch (args[0].toLowerCase()) {
            case "reload":
                sender.getServer().reloadWhitelist();
                Command.broadcastCommandMessage(sender, new TranslationContainer("commands.whitelist.reloaded"));

                return true;
            case "on":
                sender.getServer().setPropertyBoolean("white-list", true);
                Command.broadcastCommandMessage(sender, new TranslationContainer("commands.whitelist.enabled"));

                return true;
            case "off":
                sender.getServer().setPropertyBoolean("white-list", false);
                Command.broadcastCommandMessage(sender, new TranslationContainer("commands.whitelist.disabled"));

                return true;
            case "list":
                String result = "";
                int count = 0;
                for (String player : sender.getServer().getWhitelist().getAll().keySet()) {
                    result += player + ", ";
                    ++count;
                }
                sender.sendMessage(new TranslationContainer("commands.whitelist.list", new String[]{String.valueOf(count), String.valueOf(count)}));
                sender.sendMessage(result.length() > 0 ? result.substring(0, result.length() - 2) : "");

                return true;

            case "add":
                sender.sendMessage(new TranslationContainer("commands.generic.usage", "%commands.whitelist.add.usage"));
                return true;

            case "remove":
                sender.sendMessage(new TranslationContainer("commands.generic.usage", "%commands.whitelist.remove.usage"));
                return true;
        }
    } else if (args.length == 2) {
        if (this.badPerm(sender, args[0].toLowerCase())) {
            return false;
        }
        switch (args[0].toLowerCase()) {
            case "add":
                sender.getServer().getOfflinePlayer(args[1]).setWhitelisted(true);
                Command.broadcastCommandMessage(sender, new TranslationContainer("commands.whitelist.add.success", args[1]));

                return true;
            case "remove":
                sender.getServer().getOfflinePlayer(args[1]).setWhitelisted(false);
                Command.broadcastCommandMessage(sender, new TranslationContainer("commands.whitelist.remove.success", args[1]));

                return true;
        }
    }

    return true;
}
 
Example 20
Source File: WeatherCommand.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!this.testPermission(sender)) {
        return true;
    }
    if (args.length == 0 || args.length > 2) {
        sender.sendMessage(new TranslationContainer("commands.weather.usage", this.usageMessage));
        return false;
    }

    String weather = args[0];
    Level level;
    int seconds;
    if (args.length > 1) {
        try {
            seconds = Integer.parseInt(args[1]);
        } catch (Exception e) {
            sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
            return true;
        }
    } else {
        seconds = 600 * 20;
    }

    if (sender instanceof Player) {
        level = ((Player) sender).getLevel();
    } else {
        level = sender.getServer().getDefaultLevel();
    }

    switch (weather) {
        case "clear":
            level.setRaining(false);
            level.setThundering(false);
            level.setRainTime(seconds * 20);
            level.setThunderTime(seconds * 20);
            Command.broadcastCommandMessage(sender,
                    new TranslationContainer("commands.weather.clear"));
            return true;
        case "rain":
            level.setRaining(true);
            level.setRainTime(seconds * 20);
            Command.broadcastCommandMessage(sender,
                    new TranslationContainer("commands.weather.rain"));
            return true;
        case "thunder":
            level.setThundering(true);
            level.setRainTime(seconds * 20);
            level.setThunderTime(seconds * 20);
            Command.broadcastCommandMessage(sender,
                    new TranslationContainer("commands.weather.thunder"));
            return true;
        default:
            sender.sendMessage(new TranslationContainer("commands.weather.usage", this.usageMessage));
            return false;
    }

}