Search in sources :

Example 6 with TranslationContainer

use of cn.nukkit.lang.TranslationContainer in project Nukkit by Nukkit.

the class DifficultyCommand method execute.

@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;
}
Also used : SetDifficultyPacket(cn.nukkit.network.protocol.SetDifficultyPacket) TranslationContainer(cn.nukkit.lang.TranslationContainer)

Example 7 with TranslationContainer

use of cn.nukkit.lang.TranslationContainer in project Nukkit by Nukkit.

the class EffectCommand method execute.

@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;
    }
    if (args[1].equalsIgnoreCase("clear")) {
        for (Effect effect : player.getEffects().values()) {
            player.removeEffect(effect.getId());
        }
        sender.sendMessage(new TranslationContainer("commands.effect.success.removed.all", player.getDisplayName()));
        return true;
    }
    Effect effect;
    try {
        effect = Effect.getEffect(Integer.parseInt(args[1]));
    } catch (NumberFormatException | ServerException a) {
        try {
            effect = Effect.getEffectByName(args[1]);
        } catch (Exception e) {
            sender.sendMessage(new TranslationContainer("commands.effect.notFound", args[1]));
            return true;
        }
    }
    int duration = 300;
    int amplification = 0;
    if (args.length >= 3) {
        try {
            duration = Integer.valueOf(args[2]);
        } catch (NumberFormatException a) {
            sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
            return true;
        }
        if (!(effect instanceof InstantEffect)) {
            duration *= 20;
        }
    } else if (effect instanceof InstantEffect) {
        duration = 1;
    }
    if (args.length >= 4) {
        try {
            amplification = Integer.valueOf(args[3]);
        } catch (NumberFormatException a) {
            sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
            return true;
        }
    }
    if (args.length >= 5) {
        String v = args[4].toLowerCase();
        if (v.matches("(?i)|on|true|t|1")) {
            effect.setVisible(false);
        }
    }
    if (duration == 0) {
        if (!player.hasEffect(effect.getId())) {
            if (player.getEffects().size() == 0) {
                sender.sendMessage(new TranslationContainer("commands.effect.failure.notActive.all", player.getDisplayName()));
            } else {
                sender.sendMessage(new TranslationContainer("commands.effect.failure.notActive", new String[] { effect.getName(), player.getDisplayName() }));
            }
            return true;
        }
        player.removeEffect(effect.getId());
        sender.sendMessage(new TranslationContainer("commands.effect.success.removed", new String[] { effect.getName(), player.getDisplayName() }));
    } else {
        effect.setDuration(duration).setAmplifier(amplification);
        player.addEffect(effect);
        Command.broadcastCommandMessage(sender, new TranslationContainer("%commands.effect.success", new String[] { effect.getName(), String.valueOf(effect.getId()), String.valueOf(effect.getAmplifier()), player.getDisplayName(), String.valueOf(effect.getDuration() / 20) }));
    }
    return true;
}
Also used : InstantEffect(cn.nukkit.potion.InstantEffect) Player(cn.nukkit.Player) ServerException(cn.nukkit.utils.ServerException) TranslationContainer(cn.nukkit.lang.TranslationContainer) Effect(cn.nukkit.potion.Effect) InstantEffect(cn.nukkit.potion.InstantEffect) ServerException(cn.nukkit.utils.ServerException)

Example 8 with TranslationContainer

use of cn.nukkit.lang.TranslationContainer in project Nukkit by Nukkit.

the class EnchantCommand method execute.

@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;
}
Also used : Item(cn.nukkit.item.Item) Player(cn.nukkit.Player) TranslationContainer(cn.nukkit.lang.TranslationContainer) Enchantment(cn.nukkit.item.enchantment.Enchantment)

Example 9 with TranslationContainer

use of cn.nukkit.lang.TranslationContainer 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)

Example 10 with TranslationContainer

use of cn.nukkit.lang.TranslationContainer 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)

Aggregations

TranslationContainer (cn.nukkit.lang.TranslationContainer)49 Player (cn.nukkit.Player)24 Level (cn.nukkit.level.Level)6 BlockEntity (cn.nukkit.blockentity.BlockEntity)3 CommandSender (cn.nukkit.command.CommandSender)3 ConsoleCommandSender (cn.nukkit.command.ConsoleCommandSender)3 EntityDamageEvent (cn.nukkit.event.entity.EntityDamageEvent)3 Plugin (cn.nukkit.plugin.Plugin)3 IPlayer (cn.nukkit.IPlayer)2 Command (cn.nukkit.command.Command)2 RemoteConsoleCommandSender (cn.nukkit.command.RemoteConsoleCommandSender)2 EntityDamageByBlockEvent (cn.nukkit.event.entity.EntityDamageByBlockEvent)2 EntityDamageByEntityEvent (cn.nukkit.event.entity.EntityDamageByEntityEvent)2 Item (cn.nukkit.item.Item)2 Enchantment (cn.nukkit.item.enchantment.Enchantment)2 Effect (cn.nukkit.potion.Effect)2 DecimalFormat (java.text.DecimalFormat)2 Type (cn.nukkit.AdventureSettings.Type)1 cn.nukkit.block (cn.nukkit.block)1 BlockEntityItemFrame (cn.nukkit.blockentity.BlockEntityItemFrame)1