Solved getConfig().set does not work!

Discussion in 'Plugin Development' started by Sean0402, Jul 20, 2014.

Thread Status:
Not open for further replies.
  1. Hello I am having a error on the getconfig().set here:
    Code:java
    1. if (cmd.getName().equalsIgnoreCase("setspawn")) {
    2. getConfig().set("spawn.world", p.getLocation().getWorld().getName());
    3. getConfig().set("spawn.x", p.getLocation().getX());
    4. getConfig().set("spawn.y", p.getLocation().getY());
    5. getConfig().set("spawn.z", p.getLocation().getZ());
    6. saveConfig();
    7. p.sendMessage(ChatColor.GREEN + "Spawn set!");
    8. return true;
    9. }

    I cant seem to find what's wrong with it..
     
  2. Offline

    Traks

    What is the stack trace/problem?
     
  3. Offline

    Zettelkasten

    @Sean0402 Can we see the saveConfig() method?​
     
  4. Offline

    xmarinusx

    It's probably in the main class, saveConfig() is part of JavaPlugin...
     
  5. Offline

    Traks

    A double isn't an object, try setting the value of the locations in the configuration to p.getLocation().getX() + "" and y and z accordingly. Or use the constructor for Double to create a new Double object
     
  6. Offline

    fireblast709

    Traks wat, that should still compile fine
     
  7. Offline

    Zettelkasten

    Traks fireblast709 Yeah, Java auto-converts? simple-types into objects. That's why you can do Map<String, Integer>.put("Hey", 31415); without needing a String object and an Integer object
     
  8. Traks Well it works but when I do /setspawn is sets the spawn but then when I type /spawn I go into the void?

    My /spawn code:
    Code:java
    1. if (cmd.getName().equalsIgnoreCase("spawn")) {
    2. if (getConfig().getConfigurationSection("spawn") == null) {
    3. p.sendMessage(ChatColor.RED + "The spawn has not yet been set!");
    4. return true;
    5. }
    6. World w = Bukkit.getServer().getWorld(getConfig().getString("spawn.world"));
    7. double x = getConfig().getDouble("spawn.x");
    8. double y = getConfig().getDouble("spawn.y");
    9. double z = getConfig().getDouble("spawn.z");
    10. p.teleport(new Location(w, x, y, z));
    11. p.sendMessage(ChatColor.GREEN + "Welcome to the spawn!");
    12. }


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

    Traks

    fireblast709 Zettelkasten It was the only logical reason I could come up with. Perhaps the OP is using on old Java version to compile with?
     
  10. Offline

    fireblast709

    Traks the error does suggest it. Nonetheless I have never seen it happen before.

    Sean0402 when does that error occur? Also, can you provide a bigger screenshot / Java version
     
  11. fireblast709 My Main Class:
    Code:java
    1. package me.sean0402.teleport;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Location;
    6. import org.bukkit.World;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class Teleport extends JavaPlugin {
    13.  
    14. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    15.  
    16. if (!(sender instanceof Player)) {
    17. sender.sendMessage(ChatColor.RED + "This plugin is for players only!");
    18. return true;
    19. }
    20.  
    21. Player p = (Player) sender;
    22.  
    23. if (cmd.getName().equalsIgnoreCase("tp")) {
    24. if (args.length == 0) {
    25. p.sendMessage(ChatColor.RED + "Please specify a player.");
    26. return true;
    27. }
    28. Player target = Bukkit.getServer().getPlayer(args[0]);
    29. if (target == null) {
    30. p.sendMessage(ChatColor.RED + "Could not find player " + args[0] + "!");
    31. return true;
    32. }
    33. p.teleport(target.getLocation());
    34. return true;
    35. }
    36.  
    37. if (cmd.getName().equalsIgnoreCase("setspawn")) {
    38. getConfig().set("spawn.world", p.getLocation().getWorld().getName());
    39. getConfig().set("spawn.x", p.getLocation().getX() + "spawn.x");
    40. getConfig().set("spawn.y", p.getLocation().getY() + "spawn.y");
    41. getConfig().set("spawn.z", p.getLocation().getZ() + "spawn.z");
    42. saveConfig();
    43. p.sendMessage(ChatColor.GREEN + "Spawn set!");
    44. return true;
    45. }
    46.  
    47. if (cmd.getName().equalsIgnoreCase("spawn")) {
    48. if (getConfig().getConfigurationSection("spawn") == null) {
    49. p.sendMessage(ChatColor.RED + "The spawn has not yet been set!");
    50. return true;
    51. }
    52. World w = Bukkit.getServer().getWorld(getConfig().getString("spawn.world"));
    53. double x = getConfig().getDouble("spawn.x");
    54. double y = getConfig().getDouble("spawn.y");
    55. double z = getConfig().getDouble("spawn.z");
    56. p.teleport(new Location(w, x, y, z));
    57. p.sendMessage(ChatColor.GREEN + "Welcome to the spawn!");
    58. }
    59. return true;
    60. }
    61. }
     
  12. Offline

    fireblast709

    Sean0402 well that + "spawn.x" part causes the coords to be 0,0,0 when using getDouble
     
  13. Offline

    fireblast709

    Sean0402 you should remove the whole String part, it causes the double to be a String which isn't a valid double
     
  14. fireblast709 Explain what you mean? And where to move what?
     
  15. Offline

    fireblast709

    Sean0402
    Code:java
    1. p.getLocation().getY() + "spawn.y"
    to
    Code:java
    1. p.getLocation().getY()
    etc
     
  16. fireblast709 But now I got the same error I got from the beginning doing that.. The getConfig().set is not working
     
  17. Offline

    fireblast709

    Sean0402 once more, where exactly do you get the error, and are there actual stacktraces involved (no, just referring me to the previous screenshot will not suffice :p)
     
  18. Offline

    Traks

    What's the Java version used for compiling set to? In Eclipse it can be found at Preferences > Java > Compiler > Compiler compliance level
     
  19. Traks 1.4

    fireblast709 this part is the error:
    Code:java
    1. getConfig().set("spawn.x", p.getLocation().getX());
    2. getConfig().set("spawn.y", p.getLocation().getY());
    3. getConfig().set("spawn.z", p.getLocation().getZ());


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

    Traks

    Change it to 1.6, 1.7 or 1.8 depending on what Java version you have installed for executing Java applications
     
  21. Traks Thanks! Works perfect now!
     
Thread Status:
Not open for further replies.

Share This Page