Solved How to cast String to org.bukkit.Location?

Discussion in 'Plugin Development' started by GODofCRAFTERS, Apr 13, 2015.

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

    GODofCRAFTERS

    Okay this is going to be hard to put in words as I'm a beginner in Plugin Development, but I'll try my best.

    I want to find a way to teleport a player to a location. But the Location is in a string format as I've stored it in the Config file. So if I do player.teleport(StringName); it will throw an error. As the argument must be a Location. But when I try to do player.teleport((Location) StringName); it says it cannot cast from String to Location.

    Also, when I stored it in the Config, I stored it as a string but as getConfig().set("Name", player.getLocation());
    Please help! Any code would be helpful, but I do not encourage spoon-feeding, as I want to learn. Thanks :D
     
  2. Online

    timtower Administrator Administrator Moderator

    @GODofCRAFTERS You can't cast string to location, you need to parse it instead.
    or getConfig().get("Name"), might work, probably not.
     
  3. Offline

    GODofCRAFTERS

    @timtower
    How do I parse it? For integers I can do Integer.parseInt(String);
    But for a Location? There's no Location.parseLoc(String);
    Is there a method for that? Sorry if I'm being very noobish at this, still learning.
     
  4. Offline

    Lolmewn

    What you can do is create a SerialisableLocation object with a Location constructor which you can then use in the config after registering it - then you can use that for casting and getters/setters.
     
  5. Offline

    GODofCRAFTERS

    @Lolmewn
    I know I advised again spoon-feeding, but I barely understood what you just said. Is it possible you could link me to a post that explains how to create one of those? Or explain one yourself? Thanks.
     
  6. Offline

    mine-care

    @GODofCRAFTERS
    a sudo-like code of what you need to do
    method named "locationToString"takes parameter Location object, and returns it to string in a way that world,x,y,zyaw,putch are on a single string. (worldname,x-y-z-yaw-pitch for example.)

    method named "locationFromString" that takes String param and returns Location obj.
    You take the string as it was done by the other method and split out it's contents, and return a location based on it.


    Alternatively if you want to store them in a config, you can use ConfigurationSections that come really handy in this case, Example:
    Code:
    Locations
       LocationOne:
          World: "WorldName"
          x: 5
          y: 10
          z: 15
          yaw: 3.14
          pitch: 3.14
    
     
    GODofCRAFTERS likes this.
Thread Status:
Not open for further replies.

Share This Page