Adding spawns using Config and Ints

Discussion in 'Plugin Development' started by Dubehh, Jun 7, 2014.

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

    Dubehh

    Hi!

    I am trying to figure out how to add special Team spawns to my minigame using a config file.

    Current Code
    Code:java
    1. if (cmd.getName().equalsIgnoreCase("CoreDefense")) {
    2. if (args.length == 0) {
    3. helpMessage(p);
    4.  
    5. } else if (args[0].equalsIgnoreCase("addred")) {
    6. if(!(m.getConfig().contains("RedSpawn"))){
    7. m.getConfig().set("RedSpawn.1.X", x);
    8. m.getConfig().set("RedSpawn.1.Y", y);
    9. m.getConfig().set("RedSpawn.1.Z", z);
    10. m.saveConfig();
    11. return true;
    12. }
    13. int i = m.getConfig().getInt("RedSpawn."); // Doesn't work :)
    14.  
    15. } else if (args[0].equalsIgnoreCase("addblue")) {
    16. }
    17. return true;
    18. }
    19.  
    20. return false;
    21. }


    - When the player types the command for the first time it saves the location in a list like so:
    Code:
    RedSpawn
        '1':
            X: x
            Y: y
            Z: z
    It works fine.
    But when the player types it again there must be a second location saved with, as parent '2'.

    I tried getting the int out of the path and then doing ' i++ ' but that didn't work, or I did it wrong.

    Can someone help me?

    What it should be:

    First time command:

    Code:
    RedSpawn
        '1':
            X: x
            Y: y
            Z: z
    Second time command:
    Code:
    RedSpawn
      '1':
            X: x
            Y: y
            Z: z
        '2':
            X: x
            Y: y
            Z: z
    third etc.
    Code:
    RedSpawn
      '1':
            X: x
            Y: y
            Z: z
      '2':
            X: x
            Y: y
            Z: z
        '3':
            X: x
            Y: y
            Z: z
     
  2. Offline

    Mrawesomecookie

    Dubehh likes this.
  3. Offline

    Dubehh

  4. Offline

    xTigerRebornx

    Dubehh use ConfigurationSection#getKeys() (getting your ConfigurationSection using FileConfiguration#getConfigurationSection)
    Call it (pass in false to make sure you don't get children keys), then check the Collection's size that it returns, and then add one to that, and it'll give you the next number you want to set for
     
    Dubehh likes this.
  5. Offline

    Dubehh

    xTigerRebornx

    Im stuck ;),

    I've read the jd.bukkit thing, but I can't figure this out.
    Code:java
    1. } else if (args[0].equalsIgnoreCase("addred")) {
    2. if(!(m.getConfig().contains("RedSpawn"))){
    3. m.getConfig().set("RedSpawn.1.X", x);
    4. m.getConfig().set("RedSpawn.1.Y", y);
    5. m.getConfig().set("RedSpawn.1.Z", z);
    6. m.saveConfig();
    7. return true;
    8. }
    9. int i = m.getConfig().getConfigurationSection("RedSpawn.").getInt("") + 1;
    10. p.sendMessage(i + " is a nummber"); /* Debug message */
     
  6. Offline

    TheWolfBadger

    Dubehh What are you trying to do in more specific terms?
     
    Dubehh likes this.
  7. Offline

    Dubehh

    TheWolfBadger

    Ill just recreate it for understanding :)

    - Player types /coredefense addred
    - The players location (x,y,z) gets saved into a config (so I can load them in a respawnEvent)
    - Once a player types /coredefense addred again, the location gets added into the config again.
    So if I would spam /coredefense addred there would be a infinite list of locations.


    I was trying to do it with ints but if there is a easier / better way to do it, please reply :)
     
  8. Offline

    TheWolfBadger

    Dubehh So you don't want them to repeat the command if it's set already or you want it to overwrite the last one if there already is one?
     
    Dubehh likes this.
  9. Offline

    Dubehh

    I don't have specific demands, I just want to save locations into a config (in a list)
    like so:
    Code:
    RedSpawn:
        - Location 1: //first time  '/Coredefense addred '
            - X: x
            - Y: y
            - Z: z
        - Location 2: //second time  '/Coredefense addred '
            - X: x
            - Y: y
            - Z: z
        - Location 3: //third time  '/Coredefense addred '
            - X: x
            - Y: y
            - Z: z
        - Location 4: //fourth time  '/Coredefense addred '
            - X: x
            - Y: y
            - Z: z
    TheWolfBadger

    See it as adding warps. But forget the naming of warps.

    So if I would compare this to a warp plugin
    it would be /warp instead of /warp <name>

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

    TheWolfBadger

    Dubehh Ahh so I recommend you save the locations as strings.
    Code:java
    1. String locSerialized = loc.getWorld().getName() + "," + loc.getX() + "," + loc.getY() + "," + loc.getZ() + "," + loc.getPitch() + "," + loc.getYaw());
    2. String[] locString = locSerialized.split(",");
    3.  
    4. Location fromString = new Location(Bukkit.getWorld(locString[0]), Double.parseDouble(locString[1) ... etc );
    Credit to ZeusAllMighty11 for this code on an old post! :p. Didn't feel like writing my own out ;).
     
    Dubehh and ZeusAllMighty11 like this.
  11. Offline

    Dubehh

    TheWolfBadger
    But if you have a easier / better method, feel free to share :)

    TheWolfBadger

    could u show me how to use this in my code?

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

    TheWolfBadger

    Code:java
    1. public String locToString(Location loc) {
    2. String locSerialized = loc.getWorld().getName() + "," + loc.getX() + "," + loc.getY() + "," + loc.getZ() + "," + loc.getPitch() + "," + loc.getYaw());
    3. return locSerialized;
    4. }
    5. public Location stringToLoc(String s) {
    6. String[] locString = locSerialized.split(",");
    7.  
    8. Location fromString = new Location(Bukkit.getWorld(locString[0]), Double.parseDouble(locString[1), Double.parseDouble(locString[2]), Double.parseDouble(locString[3]), Float.parseFloat(locString[4]), Float.parseFloat(locString[5]));
    9. return fromString;
    10. }
    Dubehh
     
    Dubehh likes this.
  13. Offline

    Dubehh

    TheWolfBadger

    I might sound like a total noob, but I have no clue how to use these methods with my command.
    Could u show it how to use it in my current code? (onCommand method)

    Currently tried;
    Code:java
    1. if (cmd.getName().equalsIgnoreCase("CoreDefense")) {
    2. if (args.length == 0) {
    3. helpMessage(p);
    4.  
    5. } else if (args[0].equalsIgnoreCase("addred")) {
    6. if(!(m.getConfig().contains("RedSpawn"))){
    7. m.getConfig().set("RedSpawn.1.X", x);
    8. m.getConfig().set("RedSpawn.1.Y", y);
    9. m.getConfig().set("RedSpawn.1.Z", z);
    10. m.saveConfig();
    11. return true;
    12. }
    13. List<String> redSpawn = m.getConfig().getStringList("RedSpawn");
    14. String loc = m.spawnloc.locToString(p.getLocation());
    15. redSpawn.add(loc);
    16. m.getConfig().set("RedSpawn", redSpawn);
    17.  


    I have your methods in a seperate class btw.

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

    TheWolfBadger

    Dubehh
    Code:java
    1.  
    2. if (cmd.getName().equalsIgnoreCase("CoreDefense")) {
    3. if (args.length == 0) {
    4. helpMessage(p);
    5.  
    6. } else if (args[0].equalsIgnoreCase("addred")) {
    7. if(!(m.getConfig().contains("RedSpawn"))){
    8. m.getConfig().set("RedSpawn.1", locToString(p.getLocation())); //You should be able to figure out how to //make this a list and how to utilize getting the different locations.
    9. m.saveConfig();
    10. return true;
    11. }
    12. //This should work:
    13. List<String> redSpawn = m.getConfig().getStringList("RedSpawn");
    14. String loc = m.spawnloc.locToString(p.getLocation());
    15. redSpawn.add(loc);
    16. m.getConfig().set("RedSpawn", redSpawn);
    17. public String locToString(Location loc) {
    18. String locSerialized = loc.getWorld().getName() + "," + loc.getX() + "," + loc.getY() + "," + loc.getZ() + "," + loc.getPitch() + "," + loc.getYaw());
    19. return locSerialized;
    20. }
    21. public Location stringToLoc(String s) {
    22. String[] locString = locSerialized.split(",");
    23.  
    24. Location fromString = new Location(Bukkit.getWorld(locString[0]), Double.parseDouble(locString[1), Double.parseDouble(locString[2]), Double.parseDouble(locString[3]), Float.parseFloat(locString[4]), Float.parseFloat(locString[5]));
    25. return fromString;
    26. }


    Dubehh This is what you should use for the addRed command now:
    Code:java
    1. List<String> redSpawn = m.getConfig().getStringList("RedSpawn");
    2. String loc = m.spawnloc.locToString(p.getLocation());
    3. redSpawn.add(loc);
    4. m.getConfig().set("RedSpawn", redSpawn);
    5. m.saveConfig();
    6.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 2, 2016
    Dubehh likes this.
  15. Offline

    Dubehh

    TheWolfBadger

    Code:java
    1. public Location stringToLoc(String s) {
    2. String[] locString = locSerialized.split(","); //here
    3.  
    4. Location fromString = new Location(Bukkit.getWorld(locString[0]),
    5. Double.parseDouble(locString[1]),
    6. Double.parseDouble(locString[2]),
    7. Double.parseDouble(locString[3]),
    8. Float.parseFloat(locString[4]), Float.parseFloat(locString[5]));
    9. return fromString;
    10. }


    locSerialized cannot be resolved

    TheWolfBadger

    Code:java
    1. if (cmd.getName().equalsIgnoreCase("CoreDefense")) {
    2. if (args.length == 0) {
    3. helpMessage(p);
    4.  
    5. } else if (args[0].equalsIgnoreCase("addred")) {
    6. List<String> redSpawn = m.getConfig().getStringList("RedSpawn");
    7. String loc = m.spawnloc.locToString(p.getLocation());
    8. redSpawn.add(loc);
    9. m.getConfig().set("RedSpawn", redSpawn);
    10. m.saveConfig();
    11.  
    12. } else if (args[0].equalsIgnoreCase("addblue")) {
    13. }
    14. return true;
    15. }


    Current code.

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

    ZodiacTheories

    Dubehh

    No offence but do you know java?

    Where was locSerialized declared as a variable?
     
    TheWolfBadger likes this.
  17. Offline

    TheWolfBadger

    Dubehh Looks good to me. What's the problem?
    Change locSerialized to 's'
     
  18. Offline

    Dubehh

    TheWolfBadger
    Thanks man! Sorry for all of those questions, but I've been rewriting my plugin the entire day, and I couldn't think straight :)
    Like for u!

    TheWolfBadger

    (Hopefully) last question. When I am calling a location out of it,
    I use this:
    Code:java
    1. Location loc2 = m.spawnloc.stringToLoc(m.getConfig().getString("RedSpawns."));


    But that doesn't work.

    Could u tell me how to use stringToLoc properly?
    This is seriously my last question about this topic.
    You gave me a ton of information about saving it, and it works. Only answer if you want it, since u did alot for me already, thank you. Seriously, I am grateful for the time you've put into me.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 2, 2016
    TheWolfBadger likes this.
  19. Offline

    TheWolfBadger

    Dubehh You are saving it as RedSpawn, not "RedSpawns.", so why are you fetching that instead of what you are saving?
     
  20. Offline

    Dubehh

    TheWolfBadger

    Hi, it is not working/ or im doin' it totally wrong.

    Command:
    Code:java
    1. } else {
    2. Location loc = m.spawnloc.stringToLoc(m.getConfig().getString("RedSpawn"));
    3. if(p.getLocation().equals(loc)){
    4. p.sendMessage("Confirmed");
    5. }
    6. else{
    7. p.sendMessage("denied");
    8. }



    Error:
    Code:
    org.bukkit.command.CommandException: Unhandled exception executing command 'coredefense' in plugin C
    oreDefense v0.1
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Buk
    kit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:199) ~[craftbukkit.jar
    :git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServer.java:544) ~[craftb
    ukkit.jar:git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at net.minecraft.server.v1_7_R1.PlayerConnection.handleCommand(PlayerConnection.java:932) [c
    raftbukkit.jar:git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:814) [craftbukkit.j
    ar:git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at net.minecraft.server.v1_7_R1.PacketPlayInChat.a(PacketPlayInChat.java:28) [craftbukkit.ja
    r:git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at net.minecraft.server.v1_7_R1.PacketPlayInChat.handle(PacketPlayInChat.java:47) [craftbukk
    it.jar:git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146) [craftbukkit.jar:g
    it-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukk
    it-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:655) [craftbukkit.jar
    :git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [craftbukkit.jar
    :git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [craftbukkit.jar
    :git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [craftbukkit.j
    ar:git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar
    :git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
    Caused by: java.lang.NullPointerException
            at com.dubehh.Locations.SpawnLocations.stringToLoc(SpawnLocations.java:27) ~[?:?]
            at com.dubehh.CoreDefense.MainCmd.onCommand(MainCmd.java:48) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Buk
    kit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            ... 13 more
    SpawnLocations.java:27
    Code:java
    1. public Location stringToLoc(String s) {
    2. String[] locString = s.split(","); // This one
    3.  
    4. Location fromString = new Location(Bukkit.getWorld(locString[0]),
    5. Double.parseDouble(locString[1]),
    6. Double.parseDouble(locString[2]),
    7. Double.parseDouble(locString[3]),
    8. Float.parseFloat(locString[4]), Float.parseFloat(locString[5]));
    9. return fromString;
    10. }


    Thanks
     
  21. Offline

    Dubehh

    Bump
    TheWolfBadger (Sorry for adding you, but you gave me the methods, so you might see what im doin wrong)

    TheWolfBadger

    Bump

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

    Dubehh

    TheWolfBadger
    Methods (only x, y z and World)
    Code:java
    1. public String locToString(Location loc) {
    2. String locSerialized = loc.getWorld().getName() + "," + loc.getX()
    3. + "," + loc.getY() + "," + loc.getZ();
    4. return locSerialized;
    5. }
    6.  
    7. public Location stringToLoc(String s) {
    8. String[] locString = s.split(",");
    9.  
    10. Location fromString = new Location(Bukkit.getWorld(locString[0]),
    11. Double.parseDouble(locString[1]),
    12. Double.parseDouble(locString[2]), Double.parseDouble(locString[3]));
    13. return fromString;
    14. }
    15.  


    Trying code:
    Code:java
    1. Location loc = m.spawnloc.stringToLoc(m.getConfig().getString("RedSpawns"));
    2. if(p.getLocation().equals(loc)){
    3. p.sendMessage("works");
    4. }
    5. else{
    6. p.sendMessage("doesnt work");
    7. }


    - How can I call a location if there are multiple locations saved, since they aren't numbered (or if I could call them randomly, which is even better)
    - Can't get a location out of a string

    Error

    Code:text
    1. [23:04:32 ERROR]: null
    2. org.bukkit.command.CommandException: Unhandled exception executing command 'coredefense' in plugin C
    3. oreDefense v0.1
    4. at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Buk
    5. kit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
    6. at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:199) ~[craftbukkit.jar
    7. :git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
    8. at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServer.java:544) ~[craftb
    9. ukkit.jar:git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
    10. at net.minecraft.server.v1_7_R1.PlayerConnection.handleCommand(PlayerConnection.java:932) [c
    11. raftbukkit.jar:git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
    12. at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:814) [craftbukkit.j
    13. ar:git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
    14. at net.minecraft.server.v1_7_R1.PacketPlayInChat.a(PacketPlayInChat.java:28) [craftbukkit.ja
    15. r:git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
    16. at net.minecraft.server.v1_7_R1.PacketPlayInChat.handle(PacketPlayInChat.java:47) [craftbukk
    17. it.jar:git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
    18. at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146) [craftbukkit.jar:g
    19. it-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
    20. at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukk
    21. it-1.7.2-R0.2-14-g15b04d8-b2991jnks]
    22. at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:655) [craftbukkit.jar
    23. :git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
    24. at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [craftbukkit.jar
    25. :git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
    26. at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [craftbukkit.jar
    27. :git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
    28. at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [craftbukkit.j
    29. ar:git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
    30. at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar
    31. :git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
    32. Caused by: java.lang.NumberFormatException: For input string: "-14650.596521360496]"
    33. at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source) ~[?:1.7.0_55]
    34. at java.lang.Double.parseDouble(Unknown Source) ~[?:1.7.0_55]
    35. at com.dubehh.Locations.SpawnLocations.stringToLoc(SpawnLocations.java:30) ~[?:?]
    36. at com.dubehh.CoreDefense.MainCmd.onCommand(MainCmd.java:72) ~[?:?]
    37. at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Buk
    38. kit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
    39. ... 13 more
    40. >
    41.  
    42.  
    43.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 2, 2016
Thread Status:
Not open for further replies.

Share This Page