简体   繁体   中英

Pitch and Yaw - Location and Teleportation

The yaw and pitch are double values, but in new Location Object, they ask for float and I can't do (Float) pitch , because it shows an error that says I can't do it.

Here is my code:

float yaw = (Float) getConfig().get("location.Yaw");
float pitch = (Float) getConfig().get("location.Pitch");

Location teleport = new Location(w, getConfig().getDouble("location.X"), getConfig().getDouble("location.Y"), getConfig().getDouble("location.Z"), yaw, pitch);

In my config, the yaw and pitch are double values, the coordinates aren't a problem so let's ignore them, the problem is just the yaw and pitch

The new Location object initialization arguments are:

Location teleport = new Location(World world, Double x, Double y, Double z, Float yaw, Float pitch);

Assuming there's no getFloat method, use:

float yaw = (float) getConfig().getDouble("location.Yaw");

You cannot cast a String to a Float . Reference casts never convert objects to other objects - they only allow you to have different types of references that refer to the same object. Since get returns a String object, and String s are not also Float s, you can't cast a String to a Float .

The distinction between float and Float is also relevant. Apart from boxing/unboxing conversions, you can never cast between primitives and references.

You could use Float.parseFloat() to convert a string into a float. For example:

String str = "13.715";
float value = Float.parseFloat(str); //value is now equal to 13.715f

To get a value from the config and parse a float, you could use:

String valueFromConfig = getConfig().getString("my.path");
float myFloat = Float.parseFloat(valueFromConfig);

So, this is what your code could look like:

float yaw = Float.parseFloat(getConfig().get("location.Yaw"));
float pitch = Float.parseFloat(getConfig().get("location.Pitch"));

Location teleport = new Location(w, getConfig().getDouble("location.X"), getConfig().getDouble("location.Y"), getConfig().getDouble("location.Z"), yaw, pitch);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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