Help with Strings in Config

Discussion in 'Plugin Development' started by kaif21, Nov 21, 2015.

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

    kaif21

    Code:java
    1.  
    2. code
    3. code
    4. else if (cmd.getName().equalsIgnoreCase("accept") && has((Player) sender, "System.guild")) {
    5. sender.sendMessage("1");
    6. if (invitedPlayers.containsKey(sender.getName())) {
    7. sender.sendMessage("2");//If the player was invited at some point, check if the invitation has expired
    8. long inviteEnds = invitedPlayers.get(sender.getName());
    9. sender.sendMessage("3");
    10. if (inviteEnds >= System.currentTimeMillis()) {
    11. sender.sendMessage("7");
    12. //If the invitation is still valid, let him join the game
    13. List<String> players = getConfig().getStringList("Guilds." + args[0] + ".Players");
    14. players.add(sender.getName().toString());
    15. sender.sendMessage("4");
    16. getConfig().set(
    17. "Guilds." + args[1] + ".Players" ,
    18. players);
    19. saveConfig();
    20. sender.sendMessage("You have joined the clan " + args[1]);
    21. } else { //If the invitation has expired, tell the player and remove him from the invitation list
    22. invitedPlayers.remove(sender.getName());
    23. sender.sendMessage(ChatColor.RED + "You're invitation has expired!");
    24. sender.sendMessage(ChatColor.GRAY + "You'll need to get invited again to join the clan.");
    25. }
    26. } else { //If the player hasn't ever received an invite or the last one expired and was removed, tell him
    27. sender.sendMessage(ChatColor.RED + "You need to receive an invitation before you can join a clan!");
    28. }
    29. code
    30. code
    31. PART THAT DOESNT WORK:
    32. getConfig().set(
    33. "Guilds." + args[1] + ".Players" ,
    34. players);
    35. saveConfig();
    36. sender.sendMessage("You have joined the clan " + args[1]);
    37.  
    38.  

    Output:
    1 2 3 7 4
     
  2. Offline

    mcdorli

    Geez, it would be nice to know what's the problem is...

    and learn the difference between your and you're
     
  3. Offline

    DoggyCode™

    Elaborate?!
     
  4. Offline

    kaif21

    Problem i told read it wont add the player to the Guilds.GUILDNAME.Players: and i given given the Part which doesnt work in my code ill give it again
    Code:java
    1.  
    2. [LIST=1]
    3. [*]getConfig().set(
    4. [*]"Guilds." + args[1] + ".Players" ,
    5. [*]players);
    6. [*]saveConfig();
    7. [*]sender.sendMessage("You have joined the clan " + args[1]);
    8. [/LIST]
    9.  



    @DoggyCode™
    @mcdorli
     
  5. Offline

    mcdorli

    Sorry, that I couldn't see the "PART THAT DOESNT WORK:" section in the code, it was REALLY visible (*sarcasm alert for dumb people*).Are there any errors?

    BTW.: problems:

    1.: Bind casting a sender to a player is like shooting in a sandstorm, you will miss.
    2.:You didn't check if there are even arguments in the args array.
     
  6. Offline

    WinX64

    I just gave it a quick read and noted these 2 lines.
    Are you saving it in the correct place?
     
  7. Offline

    kaif21

    @WinX64
    Im Confused to the core
     
  8. Offline

    WinX64

    Putting it in a simple way, you're getting the guild's player list from one location and saving it in another one.
     
  9. Offline

    kaif21

    @WinX64
    im not adding the new player?
    if not can u tell me how?
     
  10. Offline

    WinX64

    Tell me what is the syntax of your command.
    Is it /accept <guild name>?
     
  11. Offline

    mcdorli

    what command do you enter?

    @WinX64 Ninja'd
     
  12. Offline

    kaif21

    @WinX64
    yes

    @mcdorli
    /accept <guildName>

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 21, 2015
  13. Offline

    mcdorli

    Then there is no args[1], replace it with args[0]
     
  14. Offline

    kaif21

    @mcdorli ill try

    @mcdorli @WinX64
    im so sorry guys its args[0] in my code i posted old code but still wont work

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 21, 2015
  15. Offline

    WinX64

    Can you show us the new one then? The entire of it if possible.
     
  16. Offline

    kaif21

    Code:java
    1.  
    2. @Override
    3. public boolean onCommand(final CommandSender sender, Command cmd,
    4.  
    5. String label, String[] args) {
    6.  
    7. // final Player player = (Player) sender;
    8. if (cmd.getName().equalsIgnoreCase("guild")
    9. && sender instanceof Player) {
    10. Player player = (Player) sender;
    11. if (args.length == 0) {
    12.  
    13. player.sendMessage(ChatColor.RED
    14. + "Are You Dumb use /guild help");
    15. } else {
    16. if (args.length == 2 && args[0].equalsIgnoreCase("create")) {
    17. getConfig().set("Guilds." + args[1], args[1]);
    18. getConfig().set("Guilds." + args[1] + ".Owner", player.getName());
    19. getConfig().set("Guilds." + args[1] + ".Players", player.getName());
    20. player.sendMessage(ChatColor.YELLOW + "You created a guild with name " + ChatColor.GREEN + args[1] + ChatColor.YELLOW + " You can invite people to clan using /guild invite <player>");
    21. //
    22. List<String> players = getConfig().getStringList(
    23. "Guilds." + args[1] + ".Players");
    24. players.add(sender.getName());
    25. getConfig().set(
    26. "Guilds." + args[1] + ".Players" ,
    27. players);
    28. saveConfig();
    29. }else if (args.length == 2 && args[0].equalsIgnoreCase("invite")) {
    30.  
    31. Player target = Bukkit.getPlayer(args[1]);
    32. invitePlayer(target, 60);
    33. player.sendMessage("Invited "+ args[1] + " to the clan");;
    34. }else if (args.length == 2 && args[0].equalsIgnoreCase("disband")) {
    35. if(getConfig().get("Guilds." + args[1] + ".Owner") == sender.getName()){
    36. getConfig().set("Guilds." + args[1], null);
    37. sender.sendMessage("You have disbanded this clan!");
    38. }else {
    39. sender.sendMessage("You cant disband this clan!");
    40. }
    41.  
    42. }else if (args.length == 1 && args[0].equalsIgnoreCase("help")) {
    43. player.sendMessage("/guild create <Name>");
    44. player.sendMessage("/guild invite <Name>");
    45. player.sendMessage("/guild disband <Name>");
    46. }
    47.  
    48. }
    49.  
    50. }else if (cmd.getName().equalsIgnoreCase("accept") && has((Player) sender, "System.guild")) {
    51. sender.sendMessage("1");
    52. if (invitedPlayers.containsKey(sender.getName())) {
    53. sender.sendMessage("2");//If the player was invited at some point, check if the invitation has expired
    54. long inviteEnds = invitedPlayers.get(sender.getName());
    55. sender.sendMessage("3");
    56. if (inviteEnds >= System.currentTimeMillis()) {
    57. sender.sendMessage("7");
    58. //If the invitation is still valid, let him join the game
    59. List<String> players = getConfig().getStringList("Guilds." + args[0] + ".Players");
    60. players.add(sender.getName().toString());
    61. sender.sendMessage("4");
    62. getConfig().set(
    63. "Guilds." + args[0] + ".Players" ,
    64. players);
    65. saveConfig();
    66. sender.sendMessage("You have joined the clan " + args[0]);
    67. } else { //If the invitation has expired, tell the player and remove him from the invitation list
    68. invitedPlayers.remove(sender.getName());
    69. sender.sendMessage(ChatColor.RED + "You're invitation has expired!");
    70. sender.sendMessage(ChatColor.GRAY + "You'll need to get invited again to join the clan.");
    71. }
    72. } else { //If the player hasn't ever received an invite or the last one expired and was removed, tell him
    73. sender.sendMessage(ChatColor.RED + "You need to receive an invitation before you can join a clan!");
    74. }
    75.  
    76. }
    77. return false;
    78.  
    79. }
    80.  
     
  17. Offline

    WinX64

    Tell us what you have tried. (What did you type in?)
    And what you were expecting. (What was supposed to happen? What was the output?)
     
  18. Offline

    kaif21

    @WinX64
    1.I've tried fixing some code which i thought would make it work but it didnt and i ran out of ideas then
    2.Im expecting when someone does /accept guildName then he gets added to the guilds player (guildName.Players.playerName)
     
  19. Offline

    mcdorli

    post the errors please
     
  20. Offline

    WinX64

    This ^
    Give us more info related to the issue. Which commands have you used? What did you use to check if the player was in the clan? Did the server save the data back to the config file?
     
  21. Offline

    Zombie_Striker

    This is useless. Whyt do you need this
    These all contradiction each other, and they're all in the same command

    Wow, This is unexpected.
     
  22. Offline

    kaif21

    Woah too many questions xD
    any suggestions what i should do?
    @Zombie_Striker
    @WinX64
    @mcdorli
    Im working on getting the error code hang on

    Code:php
    1.  
    2. 21/11/2015 11:18:31 PM [INFO] kaif04 issued server command: /guild create
    3. 21/11/2015 11:18:33 PM [INFO] kaif04 issued server command: /guild create RR
    4. 21/11/2015 11:18:40 PM [INFO] kaif04 issued server command: /guild invite Kaif04
    5. 21/11/2015 11:18:43 PM [INFO] kaif04 issued server command: /accept RR
    6. 21/11/2015 11:18:43 PM [SEVERE] null
    7. 21/11/2015 11:18:43 PM org.bukkit.command.CommandException: Unhandled exception executing command 'accept' in plugin System v1.0
    8. 21/11/2015 11:18:43 PM at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit_server.jar:git-Bukkit-0899683]
    9. 21/11/2015 11:18:43 PM at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[craftbukkit_server.jar:git-Bukkit-0899683]
    10. 21/11/2015 11:18:43 PM at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServer.java:624) ~[craftbukkit_server.jar:git-Bukkit-0899683]
    11. 21/11/2015 11:18:43 PM at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerConnection.java:1058) [craftbukkit_server.jar:git-Bukkit-0899683]
    12. 21/11/2015 11:18:43 PM at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:919) [craftbukkit_server.jar:git-Bukkit-0899683]
    13. 21/11/2015 11:18:43 PM at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(SourceFile:37) [craftbukkit_server.jar:git-Bukkit-0899683]
    14. 21/11/2015 11:18:43 PM at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(SourceFile:9) [craftbukkit_server.jar:git-Bukkit-0899683]
    15. 21/11/2015 11:18:43 PM at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [craftbukkit_server.jar:git-Bukkit-0899683]
    16. 21/11/2015 11:18:44 PM at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_65]
    17. 21/11/2015 11:18:44 PM at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_65]
    18. 21/11/2015 11:18:44 PM at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:643) [craftbukkit_server.jar:git-Bukkit-0899683]
    19. 21/11/2015 11:18:44 PM at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:284) [craftbukkit_server.jar:git-Bukkit-0899683]
    20. 21/11/2015 11:18:44 PM at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:598) [craftbukkit_server.jar:git-Bukkit-0899683]
    21. 21/11/2015 11:18:44 PM at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:506) [craftbukkit_server.jar:git-Bukkit-0899683]
    22. 21/11/2015 11:18:44 PM at java.lang.Thread.run(Unknown Source) [?:1.8.0_65]
    23. 21/11/2015 11:18:44 PM Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
    24. 21/11/2015 11:18:44 PM at india.kaif04.Core.onCommand(Core.java:92) ~[?:?]
    25. 21/11/2015 11:18:44 PM at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit_server.jar:git-Bukkit-0899683]
    26. 21/11/2015 11:18:44 PM ... 14 more
    27.  
    28.  

    @WinX64
    @Zombie_Striker
    @mcdorli

    Umm I checked on line 92 of Core.jar and its
    "Guilds." + args[0] + ".Players" ,


    Help me
    @WinX64
    @mcdorli
    @Zombie_Striker

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 21, 2015
  23. Offline

    Scimiguy

    @kaif21
    Stop giving us shortcuts.
    Paste your ENTIRE class, we can go from there
     
    Zombie_Striker likes this.
  24. Offline

    kaif21

    Code:java
    1.  
    2. package india.kaif04;
    3.  
    4. import java.util.HashMap;
    5. import java.util.List;
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15. public class Core extends JavaPlugin implements Listener {
    16. public static HashMap<String, Long> invitedPlayers = new HashMap<String, Long>();
    17. public static Core plugin;
    18. public void onEnable() {
    19. plugin = this;
    20. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    21. saveConfig();
    22. }
    23. public void onDisable() {
    24.  
    25.  
    26. saveConfig();
    27. plugin = null;
    28. }
    29.  
    30.  
    31.  
    32. @Override
    33. public boolean onCommand(final CommandSender sender, Command cmd,
    34.  
    35. String label, String[] args) {
    36.  
    37. // final Player player = (Player) sender;
    38. if (cmd.getName().equalsIgnoreCase("guild")
    39. && sender instanceof Player) {
    40. Player player = (Player) sender;
    41. if (args.length == 0) {
    42.  
    43. player.sendMessage(ChatColor.RED
    44. + "Are You Dumb use /guild help");
    45. } else {
    46. if (args.length == 2 && args[0].equalsIgnoreCase("create")) {
    47. getConfig().set("Guilds." + args[1], args[1]);
    48. getConfig().set("Guilds." + args[1] + ".Owner", player.getName());
    49. getConfig().set("Guilds." + args[1] + ".Players", player.getName());
    50. player.sendMessage(ChatColor.YELLOW + "You created a guild with name " + ChatColor.GREEN + args[1] + ChatColor.YELLOW + " You can invite people to clan using /guild invite <player>");
    51. //
    52. List<String> players = getConfig().getStringList(
    53. "Guilds." + args[1] + ".Players");
    54. players.add(sender.getName());
    55. getConfig().set(
    56. "Guilds." + args[1] + ".Players" ,
    57. players);
    58. saveConfig();
    59. }else if (args.length == 2 && args[0].equalsIgnoreCase("invite")) {
    60.  
    61. Player target = Bukkit.getPlayer(args[1]);
    62. invitePlayer(target, 60);
    63. player.sendMessage("Invited "+ args[1] + " to the clan");;
    64. }else if (args.length == 2 && args[0].equalsIgnoreCase("disband")) {
    65. if(getConfig().get("Guilds." + args[1] + ".Owner") == sender.getName()){
    66. getConfig().set("Guilds." + args[1], null);
    67. sender.sendMessage("You have disbanded this clan!");
    68. }else {
    69. sender.sendMessage("You cant disband this clan!");
    70. }
    71.  
    72. }else if (args.length == 1 && args[0].equalsIgnoreCase("help")) {
    73. player.sendMessage("/guild create <Name>");
    74. player.sendMessage("/guild invite <Name>");
    75. player.sendMessage("/guild disband <Name>");
    76. }
    77.  
    78. }
    79.  
    80. }else if (cmd.getName().equalsIgnoreCase("accept") && has((Player) sender, "System.guild")) {
    81. sender.sendMessage("1");
    82. if (invitedPlayers.containsKey(sender.getName())) {
    83. sender.sendMessage("2");//If the player was invited at some point, check if the invitation has expired
    84. long inviteEnds = invitedPlayers.get(sender.getName());
    85. sender.sendMessage("3");
    86. if (inviteEnds >= System.currentTimeMillis()) {
    87. sender.sendMessage("7");
    88. //If the invitation is still valid, let him join the game
    89. List<String> players = getConfig().getStringList("Guilds." + args[0] + ".Players");
    90. players.add(sender.getName().toString());
    91. sender.sendMessage("4");
    92. getConfig().set(
    93. "Guilds." + args[0] + ".Players" ,
    94. players);
    95. saveConfig();
    96. sender.sendMessage("You have joined the clan " + args[0]);
    97. } else { //If the invitation has expired, tell the player and remove him from the invitation list
    98. invitedPlayers.remove(sender.getName());
    99. sender.sendMessage(ChatColor.RED + "You're invitation has expired!");
    100. sender.sendMessage(ChatColor.GRAY + "You'll need to get invited again to join the clan.");
    101. }
    102. } else { //If the player hasn't ever received an invite or the last one expired and was removed, tell him
    103. sender.sendMessage(ChatColor.RED + "You need to receive an invitation before you can join a clan!");
    104. }
    105.  
    106. }
    107. return false;
    108.  
    109. }
    110.  
    111. public void invitePlayer(Player player, int seconds) { //Invite the playe for a specified time in seconds
    112. if (seconds > 0) {
    113. invitedPlayers.put(player.getName(), ((seconds * 1000) + System.currentTimeMillis()));
    114. player.sendMessage(ChatColor.GOLD + "You have been invited to a clan!"); //You can change this as need be
    115. player.sendMessage(ChatColor.GRAY + "" + seconds + " seconds left to type /accept <clan name> and join the clan."); //Same with this message
    116. }
    117. }
    118. public boolean has(Player player, String permission) {
    119. return player.hasPermission(permission);
    120. }
    121. }
    122.  
    123.  

    Full code everything works perfect except the part i mentioned above please help
    :D
     
  25. Offline

    Scimiguy

    Do us a favor.. take out the comments and repaste it

    Something is weird there

    Unless I'm missing something that someone else can see?
     
  26. Offline

    kaif21

  27. Offline

    mcdorli

    All of 'em. And then do Ctrl+Shift+F in eclipse or Ctrl+Shift+L in intellij
     
Thread Status:
Not open for further replies.

Share This Page