[Resource] Simple yet useful getLocation from config

Discussion in 'Resources' started by RizzelDazz, Feb 27, 2014.

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

    RizzelDazz

    This is a simple thing to do but it is very useful when trying to prevent long unneeded code.

    Method:
    Code:java
    1. private Location getLoc(String configString, Main PLUGIN) {
    2. String stringYaw = PLUGIN.getConfig().getString(configString + ".yaw");
    3. String stringPitch = PLUGIN.getConfig().getString(configString + ".pitch");
    4. String stringX = PLUGIN.getConfig().getString(configString + ".X");
    5. String stringY = PLUGIN.getConfig().getString(configString + ".Y");
    6. String stringZ = PLUGIN.getConfig().getString(configString + ".Z");
    7. World world = Bukkit.getWorld(PLUGIN.getConfig().getString(configString + ".World"));
    8.  
    9. float yaw = Float.parseFloat(stringYaw);
    10. float pitch = Float.parseFloat(stringPitch);
    11. float X = Float.parseFloat(stringX);
    12. float Y = Float.parseFloat(stringY);
    13. float Z = Float.parseFloat(stringZ);
    14.  
    15. Location doneLocation = new Location(world, X, Y, Z, yaw, pitch);
    16. return doneLocation;
    17.  
    18. }
    19.  



    Example Usage:
    Code:java
    1. Location location = getLoc("Location.redSpawn", plugin);
    2.  
     
  2. Offline

    DarkBladee12

    RizzelDazz why not just serialize the location to a single string and deserialize that?
     
    CraftThatBlock likes this.
  3. Offline

    CraftThatBlock

  4. Offline

    RizzelDazz


    For when using a configuration that looks like this:

    Code:
    red:
      World: world
      X: 347.30000001192093
      Y: 66.0
      Z: 275.84611179198475
      yaw: 0.0
      pitch: 0.0
    
     
  5. Offline

    DarkBladee12

    RizzelDazz does that matter? I think if you store a location you don't want it to be modified manually...
     
  6. Offline

    RizzelDazz

    I have 3 people who I have made plugins for that want to edit it manually
     
  7. Offline

    L33m4n123

    Locations does not implement any sort of Serialization. thus it cannot be "just serialized"
     
  8. Offline

    DarkBladee12

    L33m4n123 well I meant that you can easily serialize it by yourself...
     
  9. Offline

    xTrollxDudex

    RizzelDazz
    Too much IO. Not good for performance plugins. Use a single string read instead multiple file interactions.
     
  10. Offline

    callum2904

    Is this any good? This is what I tend to use.

    Code:java
    1. public String shortLoc(Location loc) {
    2. return loc.getWorld() + "," + loc.getBlockX() + "," + loc.getBlockY() + ","
    3. + loc.getBlockZ() + "," + loc.getPitch() + "," + loc.getYaw();
    4. }
    5.  
    6. public Location longLoc(String string) {
    7. String args[] = string.split(",");
    8. return new Location(Bukkit.getWorld(args[0]), Double.parseDouble(args[1]),
    9. Double.parseDouble(args[2]), Double.parseDouble(args[3]),
    10. Float.parseFloat(args[4]), Float.parseFloat(args[5]));
    11. }
     
Thread Status:
Not open for further replies.

Share This Page