Bukkit.getPlayer()抛出数字格式异常。

huangapple go评论82阅读模式
英文:

Bukkit.getPlayer() throws a number format exception

问题

我对Minecraft插件开发还是相当新手,我正在尝试制作自己的Essentials插件的简化版本。我正在努力创建一个命令,允许我设置飞行速度,以及其他玩家的飞行速度。设置速度很正常,但是我似乎无法正确处理目标其他玩家的部分。当我执行命令时,日志中出现以下内容:

[23:55:50 INFO]: swalloich执行了服务器命令:/flyspeed swalloich 2
[23:55:50 INFO]: swalloichtargeted用flySpeed命令定位了swalloich。
[23:55:50 ERROR]: null
org.bukkit.command.CommandException:在插件EssentialsLite v0.2中执行命令'flyspeed'时出现未处理的异常
	at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[craftbukkit-1.16.2.jar:git-Bukkit-43c7ff9]
	...
Caused by: java.lang.NumberFormatException:对于字符串输入:“swalloich”
	...
	at me.swall.essentialsLite.commands.FlySpeed.onCommand(FlySpeed.java:62) ~[?:?]
	...

我几乎确定问题出现在我代码的这部分:

float flySpeed = (float)(Integer.parseInt(args[1]) / 10.0);
Bukkit.getLogger().info(player.getDisplayName() + " 用flySpeed命令定位了 " + args[0] + "。");
if (Integer.parseInt(args[0]) > 10 || Integer.parseInt(args[0]) < 0)
{
    sender.sendMessage("速度参数必须是0到10之间的整数。");
}
else
{
    //将目标玩家的飞行速度设置为作为第二个参数提供的值
    Player targetPlayer = Bukkit.getPlayerExact(args[0]);
    Bukkit.getLogger().info("已初始化targetPlayer。");
    if (targetPlayer != null && player.isOnline())
    {
        targetPlayer.setFlySpeed(flySpeed);
    	Bukkit.getLogger().info("已将用户 " + player.getDisplayName() + " 的飞行速度设置为 " + args[0]);
    }
    else
    {
    	sender.sendMessage("未找到该玩家。");
    }
}

我做错了什么?

英文:

I'm pretty new to Minecraft plugin development and I'm trying to make my own lite version of the essentials plugin. I'm trying to make a command that will allow me to set my fly speed as well as the fly speed of others. setting speed works just fine, however, targeting other players is something I can't seem to manage. When I execute the command, this appears in the log:

[23:55:50 INFO]: swalloich issued server command: /flyspeed swalloich 2
[23:55:50 INFO]: swalloichtargeted swalloich with the flySpeed command.
[23:55:50 ERROR]: null
org.bukkit.command.CommandException: Unhandled exception executing command &#39;flyspeed&#39; in plugin EssentialsLite v0.2
	at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[craftbukkit-1.16.2.jar:git-Bukkit-43c7ff9]
	at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:148) ~[craftbukkit-1.16.2.jar:git-Bukkit-43c7ff9]
	at org.bukkit.craftbukkit.v1_16_R2.CraftServer.dispatchCommand(CraftServer.java:735) ~[craftbukkit-1.16.2.jar:git-Bukkit-43c7ff9]
	at net.minecraft.server.v1_16_R2.PlayerConnection.handleCommand(PlayerConnection.java:1647) ~[craftbukkit-1.16.2.jar:git-Bukkit-43c7ff9]
	at net.minecraft.server.v1_16_R2.PlayerConnection.a(PlayerConnection.java:1504) ~[craftbukkit-1.16.2.jar:git-Bukkit-43c7ff9]
	at net.minecraft.server.v1_16_R2.PacketPlayInChat.a(SourceFile:36) ~[craftbukkit-1.16.2.jar:git-Bukkit-43c7ff9]
	at net.minecraft.server.v1_16_R2.PacketPlayInChat.a(SourceFile:9) ~[craftbukkit-1.16.2.jar:git-Bukkit-43c7ff9]
	at net.minecraft.server.v1_16_R2.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) ~[craftbukkit-1.16.2.jar:git-Bukkit-43c7ff9]
	at net.minecraft.server.v1_16_R2.TickTask.run(SourceFile:18) ~[craftbukkit-1.16.2.jar:git-Bukkit-43c7ff9]
	at net.minecraft.server.v1_16_R2.IAsyncTaskHandler.executeTask(SourceFile:144) ~[craftbukkit-1.16.2.jar:git-Bukkit-43c7ff9]
	at net.minecraft.server.v1_16_R2.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) ~[craftbukkit-1.16.2.jar:git-Bukkit-43c7ff9]
	at net.minecraft.server.v1_16_R2.IAsyncTaskHandler.executeNext(SourceFile:118) ~[craftbukkit-1.16.2.jar:git-Bukkit-43c7ff9]
	at net.minecraft.server.v1_16_R2.MinecraftServer.ba(MinecraftServer.java:899) ~[craftbukkit-1.16.2.jar:git-Bukkit-43c7ff9]
	at net.minecraft.server.v1_16_R2.MinecraftServer.executeNext(MinecraftServer.java:892) ~[craftbukkit-1.16.2.jar:git-Bukkit-43c7ff9]
	at net.minecraft.server.v1_16_R2.IAsyncTaskHandler.awaitTasks(SourceFile:127) ~[craftbukkit-1.16.2.jar:git-Bukkit-43c7ff9]
	at net.minecraft.server.v1_16_R2.MinecraftServer.sleepForTick(MinecraftServer.java:876) ~[craftbukkit-1.16.2.jar:git-Bukkit-43c7ff9]
	at net.minecraft.server.v1_16_R2.MinecraftServer.w(MinecraftServer.java:815) ~[craftbukkit-1.16.2.jar:git-Bukkit-43c7ff9]
	at net.minecraft.server.v1_16_R2.MinecraftServer.lambda$0(MinecraftServer.java:155) ~[craftbukkit-1.16.2.jar:git-Bukkit-43c7ff9]
	at java.lang.Thread.run(Thread.java:832) [?:?]
Caused by: java.lang.NumberFormatException: For input string: &quot;swalloich&quot;
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:68) ~[?:?]
	at java.lang.Integer.parseInt(Integer.java:652) ~[?:?]
	at java.lang.Integer.parseInt(Integer.java:770) ~[?:?]
	at me.swall.essentialsLite.commands.FlySpeed.onCommand(FlySpeed.java:62) ~[?:?]
	at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[craftbukkit-1.16.2.jar:git-Bukkit-43c7ff9]
	... 18 more

The portion of my code that I'm almost positive that it stems from is this:

    float flySpeed = (float)(Integer.parseInt(args[1]) / 10.0);
    Bukkit.getLogger().info(player.getDisplayName() + &quot; targeted &quot; + args[0] + &quot; with the flySpeed command.&quot;);
    if (Integer.parseInt(args[0]) &gt; 10 || Integer.parseInt(args[0]) &lt; 0)
    {
        sender.sendMessage(&quot;The speed argument must be a whole number between 0 and 10.&quot;);
    }
    else
    {
        //set the target player&#39;s fly speed to the value provided as the second argument
        Player targetPlayer = Bukkit.getPlayerExact(args[0]);
        Bukkit.getLogger().info(&quot;targetPlayer was initialized.&quot;);
        if (targetPlayer != null &amp;&amp; player.isOnline())
        {
            targetPlayer.setFlySpeed(flySpeed);
        	Bukkit.getLogger().info(&quot;The fly speed for user &quot; + player.getDisplayName() 
        							+ &quot; has been set to&quot; + args[0]);
        }
        else
        {
        	sender.sendMessage(&quot;That player was not found.&quot;);
        }
    }

What am I doing wrong?

答案1

得分: 1

在代码的第一行,

你正在将字符串(args[1])除以浮点数(10.0)。

  float flySpeed = (float)(Integer.parseInt(args[1] / 10.0));

应该更改为:

  float flySpeed = (float)(Integer.parseInt(args[1]) / 10.0);

另外,如果你在代码中要多次使用解析后的整数,我建议为此创建一个整数变量。

如果这样做,代码将如下所示:

    int flySpeedArg = Integer.parseInt(args[1]);
    float flySpeed = (float)(Integer.parseInt(args[1]) / 10.0);

    Bukkit.getLogger().info(player.getDisplayName() + " targeted " + args[0] + " with the flySpeed command.");
    if (flySpeedArg > 10 || flySpeedArg < 0)
    {
        sender.sendMessage("The speed argument must be a whole number between 0 and 10.");
    }
    else
    {
        // 将目标玩家的飞行速度设置为作为第二个参数提供的值
        Player targetPlayer = Bukkit.getPlayerExact(args[0]);
        Bukkit.getLogger().info("targetPlayer was initialized.");
        if (targetPlayer != null && player.isOnline())
        {
            targetPlayer.setFlySpeed(flySpeed);
            Bukkit.getLogger().info("The fly speed for user " + player.getDisplayName() 
                                    + " has been set to " + args[0]);
        }
        else
        {
            sender.sendMessage("That player was not found.");
        }
    }
英文:

At the line 1 of the code,
You are dividing String (args[1]) against float (10.0).

  float flySpeed = (float)(Integer.parseInt(args[1] / 10.0));

It should be changed into this:

  float flySpeed = (float)(Integer.parseInt(args[1]) / 10.0);

+ if you are going to use parsed integer multiple times in the code,
I recommend creating integer variable for that.

If then, code will be look like this:

    int flySpeedArg = Integer.parseInt(args[1]);
    float flySpeed = (float)(Integer.parseInt(args[1]) / 10.0);

    Bukkit.getLogger().info(player.getDisplayName() + &quot; targeted &quot; + args[0] + &quot; with the flySpeed command.&quot;);
    if (flySpeedArg &gt; 10 || flySpeedArg &lt; 0)
    {
        sender.sendMessage(&quot;The speed argument must be a whole number between 0 and 10.&quot;);
    }
    else
    {
        //set the target player&#39;s fly speed to the value provided as the second argument
        Player targetPlayer = Bukkit.getPlayerExact(args[0]);
        Bukkit.getLogger().info(&quot;targetPlayer was initialized.&quot;);
        if (targetPlayer != null &amp;&amp; player.isOnline())
        {
            targetPlayer.setFlySpeed(flySpeed);
            Bukkit.getLogger().info(&quot;The fly speed for user &quot; + player.getDisplayName() 
                                    + &quot; has been set to&quot; + args[0]);
        }
        else
        {
            sender.sendMessage(&quot;That player was not found.&quot;);
        }
    }

huangapple
  • 本文由 发表于 2020年9月3日 14:16:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63717878.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定