简体   繁体   English

Bukkit-为什么要铸造成易损件?

[英]Bukkit - Why does casting to Damageable work?

From Bukkit 1.6.4 on there was another way to handle to players life, the life is stored as double from now on. 从Bukkit 1.6.4开始,还有另一种处理玩家生命的方法,从现在开始,生命被存储为两倍。 To be compatible to older plugins the Player.getHealth() method is ambigious, there are: 为了与较旧的插件兼容, Player.getHealth()方法模棱两可,其中包括:

@Deprecated
public int getHealth();
public double getHealth();

I was trying to use Player.getHealth() and ended up with an error. 我试图使用Player.getHealth()并最终出现错误。 I searched for a solution, but things like double d = (double) player.getHealth() or double d = new Double(player.getHealth()); 我在寻找解决方案,但是类似double d = (double) player.getHealth()double d = new Double(player.getHealth()); still throw the error The method getHealth() of Player is ambigious (I am using Eclipse btw.). 仍然抛出错误The method getHealth() of Player is ambigious (我正在使用Eclipse btw。)。 But after some search I also found this: 但是经过一番搜索,我也发现了这一点:

Damageable d = player; //because player is a Damageable
double health = d.getHealth();
//the method called is the "new one", which returns a double

I don't understand why this works, because I took a look at Damageable and the two ambigious methods are already defined in Damageable. 我不明白为什么这行得通,因为我研究了Damageable,并且在Damageable中已经定义了两种模棱两可的方法。 The error described above should also occur here. 上述错误也应该在这里发生。 Where am I wrong? 我哪里错了? What am I missing? 我想念什么?

Thanks in advance. 提前致谢。

This is because in Minecraft 1.6.4 and up, the health is now stored in a float. 这是因为在Minecraft 1.6.4及更高版本中,运行状况现在存储在浮动对象中。 Bukkit decided to use a double to protect against a future change from float to double. Bukkit决定使用double,以防止将来从float变为double。 If you don't use NMS anywhere, use only bukkit , and not craftbukkit , as this will make it so that you can just do double d = player.getHealth(); 如果您在任何地方都不使用NMS ,请仅使用bukkit ,而不要使用craftbukkit ,因为这样做可以使您可以将double d = player.getHealth(); . Otherwise if you need craftbukkit , you could put bukkit higher than craftbukkit in your build hierarchy. 否则,如果您需要craftbukkit ,你可以把bukkit高于craftbukkit在构建层次结构。

Damageable.getHealth() is a method from Bukkit, so it is not deprecated, while Player.getHealth() is a method in CraftBukkit (CraftBukkit uses code mostly from the original Minecraft server, (called NMS), so It IS deprecated. Although if you were to remove CraftBukkit from your build path, or put Bukkit above it, then Player.getHealth() would work. Damageable.getHealth()是从Bukkit的方法,所以它不会被弃用,而Player.getHealth()是在CraftBukkit(方法CraftBukkit使用代码大多从原来的Minecraft服务器(称为NMS),所以它不推荐使用。虽然如果你要删除CraftBukkit从您的构建路径,或将Bukkit它上面,然后Player.getHealth()会工作。

Bukkit deprecated the .getHealth() methods that use integers, although they still work, but they will be removed soon. Bukkit已弃用使用整数的.getHealth()方法,尽管它们仍然可以使用,但很快将被删除。 So, at the moment, you need to use double health = ((Damageable) player).getHealth(); 因此,目前,您需要使用double health = ((Damageable) player).getHealth();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM