Search in sources :

Example 1 with GameRule

use of cn.nukkit.level.GameRule in project Nukkit by Nukkit.

the class GameruleCommand method execute.

@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!this.testPermission(sender)) {
        return true;
    }
    if (!sender.isPlayer()) {
        sender.sendMessage("This command can only be executed by players");
        return true;
    }
    GameRules rules = ((Player) sender).getLevel().getGameRules();
    switch(args.length) {
        case 0:
            StringJoiner rulesJoiner = new StringJoiner(", ");
            for (GameRule rule : rules.getRules()) {
                rulesJoiner.add(rule.getName().toLowerCase());
            }
            sender.sendMessage(rulesJoiner.toString());
            return true;
        case 1:
            Optional<GameRule> gameRule = GameRule.parseString(args[0]);
            if (!gameRule.isPresent() || !rules.hasRule(gameRule.get())) {
                sender.sendMessage(new TranslationContainer("commands.generic.syntax", "/gamerule", args[0]));
                return true;
            }
            sender.sendMessage(gameRule.get().getName() + " = " + rules.getString(gameRule.get()));
            return true;
        default:
            Optional<GameRule> optionalRule = GameRule.parseString(args[0]);
            if (!optionalRule.isPresent()) {
                sender.sendMessage(new TranslationContainer("commands.generic.syntax", "/gamerule ", args[0], " " + String.join(" ", Arrays.copyOfRange(args, 1, args.length))));
                return true;
            }
            try {
                rules.setGameRules(optionalRule.get(), args[1]);
                sender.sendMessage(new TranslationContainer("commands.gamerule.success", optionalRule.get().getName(), args[1]));
            } catch (IllegalArgumentException e) {
                sender.sendMessage(new TranslationContainer("commands.generic.syntax", "/gamerule " + args[0] + " ", args[1], " " + String.join(" ", Arrays.copyOfRange(args, 2, args.length))));
            }
            return true;
    }
}
Also used : GameRules(cn.nukkit.level.GameRules) TranslationContainer(cn.nukkit.lang.TranslationContainer) GameRule(cn.nukkit.level.GameRule) StringJoiner(java.util.StringJoiner)

Aggregations

TranslationContainer (cn.nukkit.lang.TranslationContainer)1 GameRule (cn.nukkit.level.GameRule)1 GameRules (cn.nukkit.level.GameRules)1 StringJoiner (java.util.StringJoiner)1