Solved Delhome not working

Discussion in 'Plugin Development' started by nivek1212, Sep 12, 2014.

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

    nivek1212

    Hey Guys,

    I made the commands "Home", "Sethome" and "Delhome" today, but delhome isn't working. Here's the code for sethome:
    Code:java
    1. if(args.length == 1) {
    2. if(!cfg.isSet(p.getName() + "." + args[0] + ".world")) {
    3.  
    4. String World = p.getWorld().getName();
    5. double X = p.getLocation().getX();
    6. double Y = p.getLocation().getY();
    7. double Z = p.getLocation().getZ();
    8. double Yaw = p.getLocation().getYaw();
    9. double Pitch = p.getLocation().getPitch();
    10.  
    11. cfg.set(p.getName() + "." + args[0] + ".world", World);
    12. cfg.set(p.getName() + "." + args[0] + ".X", X);
    13. cfg.set(p.getName() + "." + args[0] + ".Y", Y);
    14. cfg.set(p.getName() + "." + args[0] + ".Z", Z);
    15. cfg.set(p.getName() + "." + args[0] + ".Yaw", Yaw);
    16. cfg.set(p.getName() + "." + args[0] + ".Pitch", Pitch);
    17. try {
    18. cfg.save(file);
    19. }
    20. catch (IOException e) {
    21. e.printStackTrace();
    22. }
    23. p.sendMessage(plugin.prefix + "Das Home " + ChatColor.GOLD + args[0] + ChatColor.GREEN + " wurde gesetzt!");
    24. }
    25. else {
    26. p.sendMessage("§cDas Home " + ChatColor.GOLD + args[0] + "§c existiert bereits!" );
    27. }
    28. }
    29. else {
    30. p.sendMessage(plugin.falsch + "sethome <Name>!");
    31. }


    And here for delhome:

    Code:java
    1. if(args.length == 1) {
    2. if(cfg.isSet(p.getName() + "." + args[0] + ".world")) {
    3.  
    4. cfg.set(p.getName() + args[0], null);
    5. } else {
    6. p.sendMessage("§cDas Home " + ChatColor.GOLD + args[0] + " §cexistiert nicht!");
    7. }
    8. } else {
    9. p.sendMessage(plugin.falsch + "delhome <Name>!");
    10. }


    //Update: I don't get any errors, but nothing happens
     
  2. nivek1212
    You forgot the period (or ".") between the player name and the home name on line 4 in the second code box.

    edit: also you forgot to save the config after modifying it.
     
  3. Offline

    blablubbabc

    You are probably missing a dot here: cfg.set(p.getName() + args[0], null);
    Edit: ninja'd
     
  4. Offline

    nivek1212

    Ok, now i have this:
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    2. Player p = (Player) sender;
    3.  
    4. if(args.length == 1) {
    5.  
    6. try {
    7. cfg.save(file);
    8. } catch (IOException e) {
    9. e.printStackTrace();
    10. }
    11.  
    12. if(cfg.isSet(p.getName() + "." + args[0] + ".world")) {
    13.  
    14. cfg.set(p.getName() + "." + args[0], null);
    15.  
    16. } else {
    17. p.sendMessage("§cDas Home " + ChatColor.GOLD + args[0] + " §cexistiert nicht!");
    18. }
    19. } else {
    20. p.sendMessage(plugin.falsch + "delhome <Name>!");
    21. }
    22.  
    23. return true;
    24. }


    And it works, but when i set a home, and then want to delete if without reloading, it says that it doesn't exists, but deletes it

    //For the english guys: When it says, it doesnt exists, it's this :
    Code:java
    1. p.sendMessage("§cDas Home " + ChatColor.GOLD + args[0] + " §cexistiert nicht!");
     
  5. nivek1212
    Sorry, I don't quite understand you. Try to save the file after deleting the home, the same way you save it when you add a home. You always have to save the file in order for the changes to take place.
     
  6. Offline

    nivek1212

    Ah i got it: The problem was, that I saved the config and then deleted it. I wanted to load the config and delete it, but I messed something up. So the full code is now
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    2. Player p = (Player) sender;
    3.  
    4. if(args.length == 1) {
    5.  
    6. try {
    7. cfg.load(file);
    8. } catch (IOException e) {
    9. e.printStackTrace();
    10. } catch (InvalidConfigurationException e) {
    11. // TODO Auto-generated catch block
    12. e.printStackTrace();
    13. }
    14.  
    15. if(cfg.isSet(p.getName() + "." + args[0] + ".world")) {
    16.  
    17. cfg.set(p.getName() + "." + args[0], null);
    18.  
    19. p.sendMessage(plugin.prefix + "Das Home " + ChatColor.GOLD + args[0] + " §awurde gelöscht!");
    20.  
    21. try {
    22. cfg.save(file);
    23. } catch (IOException e) {
    24. // TODO Auto-generated catch block
    25. e.printStackTrace();
    26. }
    27.  
    28. } else {
    29. p.sendMessage("§cDas Home " + ChatColor.GOLD + args[0] + " §cexistiert nicht!");
    30. }
    31. } else {
    32. p.sendMessage(plugin.falsch + "delhome <Name>!");
    33. }
    34.  
    35. return true;
    36. }


    Thanks for your help
     
Thread Status:
Not open for further replies.

Share This Page