Get NBT tag as double.

Discussion in 'Plugin Development' started by NukerFall, Apr 20, 2020.

Thread Status:
Not open for further replies.
  1. Offline

    NukerFall

    [​IMG]
    How can i get this three doubles in playerdata file?
    NBTTagCompound pos = (NBTTagCompound) nbt.get("Pos");
    NBTTagCompound rotation = (NBTTagCompound) nbt.get("Rotation");
    Double x = pos....;
    Double y = pos....;
    Double z = pos....;
    Float yaw = rotation....;
    Float pitch = rotation....;
     
  2. @NukerFall I think something like this should do it:
    Code:Java
    1. // If "Pos" is an instance of NBTTagCompound
    2. NBTTagCompound pos = (NBTTagCompound) nbt.get("Pos");
    3. for (String key : pos.getKeys()) {
    4. // Get the value as a double
    5. double value = Double.parseDouble(key);
    6.  
    7. // Do something with the value
    8. }
    9.  
    10. // If "Pos" is an instance of NBTTagList
    11. NBTTagList pos = (NBTTagList) nbt.get("Pos");
    12. for (int i = 0; i < pos.size(); i++) {
    13. // Get the value as a double
    14. double value = ((NBTTagDouble) pos.get(i)).asDouble();
    15.  
    16. // Do something with the value
    17. }
     
  3. Offline

    NukerFall

    Found own solution, but thanks anyway.
    Code:
    NBTTagCompound nbt = NBTCompressedStreamTools.a((InputStream) (new FileInputStream(f)));
    NBTTagList pos = (NBTTagList) nbt.get("Pos");
    NBTTagList rotation = (NBTTagList) nbt.get("Rotation");
    Double x,y,z = 0.0;
    Float yaw, pitch = 0.0f;
    Location loc = new Location(Bukkit.getWorlds().get(0), 0, 0, 0);
    if (!pos.isEmpty() && !rotation.isEmpty()) {
      x = Double.valueOf(pos.get(0).asString());
      y = Double.valueOf(pos.get(1).asString());
      z = Double.valueOf(pos.get(2).asString());
      yaw = Float.valueOf(rotation.get(0).asString());
      pitch = Float.valueOf(rotation.get(1).asString());
      loc = new Location(Bukkit.getWorlds().get(0), x, y, z, yaw, pitch);
    }
    
     
    KarimAKL likes this.
Thread Status:
Not open for further replies.

Share This Page