如果玩家键入/launch< notaninteger>或/launch string,我如何让我的代码向他们返回“invalid number”消息

dgjrabp2  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(405)
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class launch implements CommandExecutor {
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (label.equalsIgnoreCase("launch") || label.equalsIgnoreCase("l")) {
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&7[&dHi&bMessage&7]  &4Invalid Entity"));
                return true;
            }
            Player player = (Player) sender;
            //Does /launch or /launch <amplifier>
            if (args.length == 0) {
                player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&7[&dHi&bMessage&7]  &2Launching in &a3"));
                try {
                    Thread.sleep(1000);
                    player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&7[&dHi&bMessage&7]  &2Launching in &62"));
                } catch (InterruptedException e) {
                    System.err.println(e.getMessage());
                }
                try {
                    Thread.sleep(1000);
                    player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&7[&dHi&bMessage&7]  &2Launching in &c1"));
                } catch (InterruptedException e) {
                    System.err.println(e.getMessage());
                }
                player.setVelocity(player.getLocation().getDirection().multiply(2).setY(1.5));
                return true;
            }

            //Launch but with args
            player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&7[&dHi&bMessage&7]  &2Launching in &a3"));
            try {
                Thread.sleep(1000);
                player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&7[&dHi&bMessage&7]  &2Launching in &62"));
            } catch (InterruptedException e) {
                System.err.println(e.getMessage());
            }
            try {
                Thread.sleep(1000);
                player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&7[&dHi&bMessage&7]  &2Launching in &c1"));
            } catch (InterruptedException e) {
                System.err.println(e.getMessage());
            }
            player.setVelocity(player.getLocation().getDirection().multiply(Integer.parseInt(args[0])).setY(1.5));
            player.setHealth(20.0);

            return true;
        }
    return false;
    }}

也许我可以加一个if语句或者一个带有非整数异常的catch?请一定要有耐心,因为我是一个新的年轻的开发人员,我已经看到人们如何感到不安/愤怒,如果有人不知道如何格式的问题正确。我的问题是:如果玩家键入/launch或/launch字符串,如何使代码向他们返回“invalid number”消息

iklwldmw

iklwldmw1#

在“/”启动后,但使用args“您必须检查 args[0] 是一个整数。我建议定义一个这样的方法:

public boolean isInteger(String arg) {
        try {
            Integer.parseInt(arg);
        } catch (NumberFormatException e) {
            return false;
        }
        return true;
}

然后在开始倒计时之前,您可以添加如下内容:

if (!isInteger(args[0])) {
    player.sendMessage("Invalid number");
    return true;
}

相关问题