Removing player from a list (kicking him/her from guild/faction)[Add/Remove in list]

Discussion in 'Plugin Development' started by kaif21, Apr 10, 2016.

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

    kaif21

    Code:java
    1.  
    2. package india.kaif04;
    3.  
    4. import java.util.ArrayList;
    5. import java.util.HashMap;
    6. import java.util.List;
    7.  
    8. import org.bukkit.Bukkit;
    9. import org.bukkit.ChatColor;
    10. import org.bukkit.command.Command;
    11. import org.bukkit.command.CommandSender;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15.  
    16. public class Main extends JavaPlugin implements Listener{
    17.  
    18. public static HashMap<String, Long> invitedPlayers = new HashMap<String, Long>();
    19. public static Main plugin;
    20.  
    21. public void onEnable() {
    22. plugin = this;
    23. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    24. saveConfig();
    25.  
    26. }
    27. public void onDisable() {
    28.  
    29. saveConfig();
    30. plugin = null;
    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") && sender instanceof Player) {
    39. Player player = (Player) sender;
    40. if (args.length == 0) {
    41.  
    42. player.sendMessage(ChatColor.RED + "Are You Dumb use /guild help");
    43. } else {
    44. if (args.length == 2 && args[0].equalsIgnoreCase("create")) {
    45. getConfig().set("Guilds." + args[1], args[1]);
    46. getConfig().set("Guilds." + args[1] + ".Owner", player.getName());
    47. getConfig().set("Guilds." + args[1] + ".Players", player.getName());
    48. player.sendMessage(ChatColor.YELLOW + "You created a guild with name " + ChatColor.GREEN + args[1]
    49. + ChatColor.YELLOW + " You can invite people to clan using /guild invite <player>");
    50. //
    51. List<String> players = getConfig().getStringList("Guilds." + args[1] + ".Players");
    52. players.add(sender.getName());
    53. getConfig().set("Guilds." + args[1] + ".Players", players);
    54. saveConfig();
    55. } else if (args.length == 2 && args[0].equalsIgnoreCase("invite")) {
    56.  
    57. Player target = Bukkit.getPlayer(args[1]);
    58. invitePlayer(target, 60);
    59. player.sendMessage("Invited " + args[1] + " to the clan");
    60. ;
    61. } else if (args.length == 2 && args[0].equalsIgnoreCase("disband")) {
    62. if (getConfig().get("Guilds." + args[1] + ".Owner") == sender.getName()) {
    63. getConfig().set("Guilds." + args[1], null);
    64. sender.sendMessage("You have disbanded this clan!");
    65. } else {
    66. sender.sendMessage("You cant disband this clan!");
    67. }
    68.  
    69. } else if (args.length == 1 && args[0].equalsIgnoreCase("help")) {
    70. player.sendMessage("/guild create <Name>");
    71. player.sendMessage("/guild invite <Name>");
    72. player.sendMessage("/guild disband <Name>");
    73. }else if (args[0].equalsIgnoreCase("kick")) {
    74. Player target = Bukkit.getPlayer(args[1]);
    75. // /guild kick(0) kaif04(1) name{2}
    76. List<String> list = getConfig().getStringList("Guilds." + args[2] + ".Players");
    77. if(list.contains(target.getName())){
    78. if(plugin.isOwner(player, args[2])){
    79. list.remove(target.getName());
    80. getConfig().set("Guilds." + args[0] + ".Players", list);
    81. saveConfig();
    82. }else {
    83. sender.sendMessage("You are not the owner of this guild!");
    84. }
    85.  
    86.  
    87. }else {
    88. sender.sendMessage("Player isn't in the guild.");
    89. }
    90.  
    91.  
    92. }
    93.  
    94. }
    95.  
    96. } else if (cmd.getName().equalsIgnoreCase("accept") && has((Player) sender, "System.guild")
    97. && sender instanceof Player) {
    98. sender.sendMessage("1");
    99. if (invitedPlayers.containsKey(sender.getName())) {
    100. sender.sendMessage("2");// If the player was invited at some
    101. // point, check if the invitation has
    102. // expired
    103. long inviteEnds = invitedPlayers.get(sender.getName());
    104. sender.sendMessage("3");
    105. if (inviteEnds >= System.currentTimeMillis()) {
    106. Player player = (Player) sender;
    107. sender.sendMessage("7");
    108.  
    109. List<String> list = getConfig().getStringList("Guilds." + args[0] + ".Players");
    110. if(list.contains(player.getName())){
    111. sender.sendMessage("You cannot join the guild " + args[0]);
    112. return false;
    113.  
    114. }else
    115. list.add(player.getName());
    116. getConfig().set("Guilds." + args[0] + ".Players", list);
    117. saveConfig();
    118. sender.sendMessage("You have joined the clan " + args[0]);
    119.  
    120. } else { // If the invitation has expired, tell the player and
    121. // remove him from the invitation list
    122. invitedPlayers.remove(sender.getName());
    123. sender.sendMessage(ChatColor.RED + "You're invitation has expired!");
    124. sender.sendMessage(ChatColor.GRAY + "You'll need to get invited again to join the clan.");
    125. }
    126. } else { // If the player hasn't ever received an invite or the last
    127. // one expired and was removed, tell him
    128. sender.sendMessage(ChatColor.RED + "You need to receive an invitation before you can join a clan!");
    129. }
    130.  
    131. }
    132. return false;
    133.  
    134. }
    135.  
    136. public void invitePlayer(Player player, int seconds) { // Invite the playe
    137. // for a specified
    138. // time in seconds
    139. if (seconds > 0) {
    140. invitedPlayers.put(player.getName(), ((seconds * 1000) + System.currentTimeMillis()));
    141. player.sendMessage(ChatColor.GOLD + "You have been invited to a clan!"); // You
    142. // can
    143. // change
    144. // this
    145. // as
    146. // need
    147. // be
    148. player.sendMessage(
    149. ChatColor.GRAY + "" + seconds + " seconds left to type /accept <clan name> and join the clan."); // Same
    150. // with
    151. // this
    152. // message
    153. }
    154. }
    155.  
    156. public boolean has(Player player, String permission) {
    157. return player.hasPermission(permission);
    158. }
    159.  
    160. public static void getGuild(Player player){
    161. String name = player.getName();
    162. //plugin.getConfig().get'
    163. //TODO
    164.  
    165. }
    166. public boolean isOwner(Player p,String guild){
    167. if(getConfig().get("Guilds." + guild + ".Owner") == p.getName()){
    168. return true;
    169. }else
    170. return false;
    171.  
    172.  
    173.  
    174. }
    175. }
    176.  
    177.  

    Code:java
    1.  
    2.  
    3.  
    4. [20:25:01 INFO]: kaif04 issued server command: /guild kick Govindas Test [20:25:01 ERROR]: null org.bukkit.command.CommandException: Unhandled exception executing command 'guild' in plugin Guild v1.0 at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.8.8.jar:git-Spigot-6c9b0a1-de5c261] at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.8.8.jar:git-Spigot-6c9b0a1-de5c261] at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:640) ~[spigot-1.8.8.jar:git-Spigot-6c9b0a1-de5c261] at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot-1.8.8.jar:git-Spigot-6c9b0a1-de5c261] at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot-1.8.8.jar:git-Spigot-6c9b0a1-de5c261] at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.8.8.jar:git-Spigot-6c9b0a1-de5c261] at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.8.8.jar:git-Spigot-6c9b0a1-de5c261] at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.8.jar:git-Spigot-6c9b0a1-de5c261] at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.7.0_80] at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.7.0_80] at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.8.jar:git-Spigot-6c9b0a1-de5c261] at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:714) [spigot-1.8.8.jar:git-Spigot-6c9b0a1-de5c261] at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8.jar:git-Spigot-6c9b0a1-de5c261] at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:653) [spigot-1.8.8.jar:git-Spigot-6c9b0a1-de5c261] at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:556) [spigot-1.8.8.jar:git-Spigot-6c9b0a1-de5c261] at java.lang.Thread.run(Unknown Source) [?:1.7.0_80] Caused by: java.lang.NullPointerException at india.kaif04.Main.onCommand(Main.java:76) ~[?:?] at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.8.8.jar:git-Spigot-6c9b0a1-de5c261] ... 15 more
    5.  
    6.  
    7.  
    8.  


    Code and error given above can anyone tell me whats the issue and possibly a solution :)
    i needed help in getting a persons guild too...i cant figure out a way ;/
    Any help is much appreciated!!!
    -kaif21
    Error pastebin - http://pastebin.com/hDCeghbp
     
  2. Offline

    CraftCreeper6

    @kaif21
    Your stacktrace is really hard to read, is there anything saying Caused by: ?
     
  3. Offline

    kaif21

  4. Offline

    CraftCreeper6

  5. Offline

    kaif21

  6. Offline

    CraftCreeper6

    @kaif21
    The error is line 76 of Main.java
     
  7. Offline

    kaif21

  8. Offline

    CraftCreeper6

    @kaif21
    What is line 76, currently?
     
  9. Offline

    kaif21

    @CraftCreeper6
    Code:java
    1.  
    2. if(list.contains(target.getName())){
    3.  
    4. in eclipse this is line 76
    5.  
     
  10. Offline

    CraftCreeper6

    @kaif21
    Is list empty?
    Print out the name of target.
    Print out the list.
     
  11. Offline

    kaif21

    @CraftCreeper6
    list is definitely not empty.
    the target isnt online is that the problem?
    yea ill try printing those and get back to u
     
  12. Offline

    CraftCreeper6

    @kaif21
    Yes, add a check for target being null
     
  13. Offline

    kaif21

    @CraftCreeper6
    Ok will do and get back tmr as its late night here, thnx for the help so far
    ill add a targer being null check and even debugs,i'll just tag u
     
  14. Offline

    Zombie_Striker

    Code:
    Caused by: java.lang.NullPointerException
            at india.kaif04.Main.onCommand(Main.java:76) ~[?:?]
    ...
    if(list.contains(target.getName())){
    • List can be null
    • The contains method will not throw an NPE
    • Target can be null
    • The name will not be null.
    Since if list is null it would also throw an NPE at all those other locations, target is most likely null.
     
  15. Offline

    kaif21

    @Zombie_Striker Ok i will.look into it tmr morning, since both u guys r indicating that its target being null change that . Thnx :)

    @Zombie_Striker
    @CraftCreeper6
    i did fix the problem by removing args[1] from list , rather than casting it to player then getname .
    the second thing i needed help is to check if the player is in some guild or no?
    basically getting all lists in a config.

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

Share This Page