Search in sources :

Example 1 with ServerException

use of cn.nukkit.utils.ServerException 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 2 with ServerException

use of cn.nukkit.utils.ServerException in project Nukkit by Nukkit.

the class MetadataStore method setMetadata.

public void setMetadata(Object subject, String metadataKey, MetadataValue newMetadataValue) {
    if (newMetadataValue == null) {
        throw new ServerException("Value cannot be null");
    }
    Plugin owningPlugin = newMetadataValue.getOwningPlugin();
    if (owningPlugin == null) {
        throw new PluginException("Plugin cannot be null");
    }
    String key = this.disambiguate((Metadatable) subject, metadataKey);
    Map<Plugin, MetadataValue> entry = this.metadataMap.computeIfAbsent(key, k -> new WeakHashMap<>(1));
    entry.put(owningPlugin, newMetadataValue);
}
Also used : ServerException(cn.nukkit.utils.ServerException) PluginException(cn.nukkit.utils.PluginException) Plugin(cn.nukkit.plugin.Plugin)

Aggregations

ServerException (cn.nukkit.utils.ServerException)2 Player (cn.nukkit.Player)1 TranslationContainer (cn.nukkit.lang.TranslationContainer)1 Plugin (cn.nukkit.plugin.Plugin)1 Effect (cn.nukkit.potion.Effect)1 InstantEffect (cn.nukkit.potion.InstantEffect)1 PluginException (cn.nukkit.utils.PluginException)1