Solved Save location in config

Discussion in 'Plugin Development' started by EnveRuleZZ, Sep 2, 2014.

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

    EnveRuleZZ

    Hey guys, I'm trying to write a plugin, when you type /setnether your location is saved as nether, and when you type in /nether again you're gonna spawn on that location.

    My code at the moment is:

    Code:java
    1. if (cmd.getName().equalsIgnoreCase("nether")) {
    2. int x = cfg.getInt(p.getWorld().getName() + "." + p.getName() + "." + "X", p.getWorld().getSpawnLocation().getBlockX());
    3. int y = cfg.getInt(p.getWorld().getName() + "." + p.getName() + "." + "Y", p.getWorld().getSpawnLocation().getBlockY());
    4. int z = cfg.getInt(p.getWorld().getName() + "." + p.getName() + "." + "Z", p.getWorld().getSpawnLocation().getBlockZ());
    5. p.teleport(new Location(p.getWorld(), x, y, z));
    6. p.sendMessage("§aYou have been teleported to the§9 Nether");
    7. }
    8.  
    9.  
    10.  
    11. if (cmd.getName().equalsIgnoreCase("setnether")) {
    12. if(p.hasPermission("delen.setnether")) {
    13. cfg.set(p.getWorld().getName() + "." + p.getName() + "." + "X", p.getLocation().getBlockX());
    14. cfg.set(p.getWorld().getName() + "." + p.getName() + "." + "Y", p.getLocation().getBlockY());
    15. cfg.set(p.getWorld().getName() + "." + p.getName() + "." + "Y", p.getLocation().getBlockZ());
    16. saveConfig();
    17. }else{
    18. p.sendMessage("§cYou don't have the permission to do that");
    19. }
    20.  
    21. p.sendMessage("§The Nether-Spawn was set to:§c [" + p.getLocation().getBlockX() + "|" + p.getLocation().getBlockY() + "|" + p.getLocation().getBlockZ());
    22. }
    23.  



    So my problem is, when I'm writing /setnether and then /nether it works, but when, for example, my brother then writes /nether it doesn't work...


    Hope you an help me. :)
    Bye ;D
     
  2. Offline

    thepluginbros

    You are not add the Z to the config on /setnether!

    Try this:
    Code:java
    1. if (cmd.getName().equalsIgnoreCase("nether")) {
    2. int x = cfg.getInt(p.getWorld().getName() + "." + p.getName() + "." + "X");
    3. int y = cfg.getInt(p.getWorld().getName() + "." + p.getName() + "." + "Y");
    4. int z = cfg.getInt(p.getWorld().getName() + "." + p.getName() + "." + "Z");
    5. p.teleport(new Location(p.getWorld(), x, y, z));
    6. p.sendMessage("§aYou have been teleported to the§9 Nether");
    7. }
    8.  
    9.  
    10.  
    11. if (cmd.getName().equalsIgnoreCase("setnether")) {
    12. if(p.hasPermission("delen.setnether")) {
    13. cfg.set(p.getWorld().getName() + "." + p.getName() + "." + "X", p.getLocation().getBlockX());
    14. cfg.set(p.getWorld().getName() + "." + p.getName() + "." + "Y", p.getLocation().getBlockY());
    15. cfg.set(p.getWorld().getName() + "." + p.getName() + "." + "Z", p.getLocation().getBlockZ());
    16. saveConfig();
    17. }else{
    18. p.sendMessage("§cYou don't have the permission to do that");
    19. }
    20.  
    21. p.sendMessage("§The Nether-Spawn was set to:§c [" + p.getLocation().getBlockX() + "|" + p.getLocation().getBlockY() + "|" + p.getLocation().getBlockZ());
    22. }
     
  3. Offline

    EnveRuleZZ

    Yes, I added it, but I just accidentally wrote "Y" instead of "Z", but the code is p.getLocation().getBlockZ()

    thepluginbros

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  4. Offline

    EnveRuleZZ

    Oh sorry I didn't think of that, thanks :) AdamQpzm
     
    AdamQpzm likes this.
  5. Offline

    thepluginbros

    Sorry what does 'P' mean?
     
  6. izarooni likes this.
  7. Offline

    EnveRuleZZ

    Sorry, my mistake. I meant, the code is:
    Code:java
    1. p.getWorld().getSpawnLocation().getBlockZ()


    thepluginbros
     
  8. Offline

    thepluginbros

    What doesn't work right now?
     
  9. Offline

    Epicballzy

    Forget about the other posts.. What you're doing, is only allowing one person to use it because of p.getName(). You must not do that. I'll give you an example of a more efficient way..

    Code:java
    1. public void setLoc(Player p) {
    2.  
    3. String world = p.getWorld().getName();
    4. double x = p.getLocation().getX();
    5. double y = p.getLocation().getY();
    6. double z = p.getLocation().getZ();
    7. double pitch = p.getLocation().getPitch();
    8. double yaw = p.getLocation().getYaw();
    9.  
    10. getConfig().set("netherloc.world", world);
    11. getConfig().set("netherloc.x", x);
    12. getConfig().set("netherloc.y", y);
    13. getConfig().set("netherloc.z", z);
    14. getConfig().set("netherloc.pitch", pitch);
    15. getConfig().set("netherloc.yaw", yaw);
    16. saveConfig();
    17. }
    18.  
    19. public void teleport(Player p) {
    20. Location loc;
    21. World world = Bukkit.getServer().getWorld(getConaif().getString("netherloc.world"));
    22. double x = getConfig().getDouble("netherloc.x");
    23. double y = getConfig().getDouble("netherloc.y");
    24. double z = getConfig().getDouble("netherloc.z");
    25. double pitch = getConfig().getDouble("netherloc.pitch");
    26. double yaw = getConfig().getDouble("netherloc.yaw");
    27.  
    28. //Either pitch or yaw comes first..
    29. loc = new Location(world, x, y, z,(float) yaw, (float)pitch);
    30. p.teleport(loc);
    31. }


    Things may not be right in the code above, but work out what you can.
    To other coders, don't say I spoonfed him code, even though I kinda did, I just think its annoying and OP already had the idea. (I just did it in a more efficient way?)
     
  10. Offline

    EnveRuleZZ

    Only the player who set the nether location, spawns there when he types /nether. For example Bob writes /setnether and Billy writes /nether he gets teleported at a strange location...

    thepluginbros
     
  11. Offline

    thepluginbros

    I found it, when Bob writes /setnether it sets it only for Bob not for Billy! Billy has to run that command too!
     
  12. Offline

    EnveRuleZZ

Thread Status:
Not open for further replies.

Share This Page