Solved Players Not Added to ArrayList?

Discussion in 'Plugin Development' started by krazytraynz, Nov 17, 2013.

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

    krazytraynz

    I'm not really sure what's causing this, but in my command it executes all of the code that executes under the same conditions where my name should be added to an ArrayList, but in my EventListener that checks the ArrayList, nothing happens. Any idea as to what could be causing this?

    Code:
    Code:java
    1. package com.github.KrazyTraynz;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5.  
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Material;
    8. import org.bukkit.block.Sign;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandExecutor;
    11. import org.bukkit.command.CommandSender;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.event.EventHandler;
    14. import org.bukkit.event.Listener;
    15. import org.bukkit.event.block.Action;
    16. import org.bukkit.event.player.AsyncPlayerChatEvent;
    17. import org.bukkit.event.player.PlayerInteractEvent;
    18.  
    19. public class AddSignHandler implements CommandExecutor, Listener{
    20.  
    21. List<String> add = new ArrayList<String>();
    22. List<String> del = new ArrayList<String>();
    23.  
    24. @SuppressWarnings("unused")
    25. private AdminSign as;
    26. private SignsHandler sh;
    27.  
    28. public AddSignHandler(AdminSign as){
    29. this.as = as;
    30. }
    31.  
    32. public AddSignHandler(SignsHandler sh){
    33. this.sh = sh;
    34. }
    35.  
    36. @Override
    37. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    38. if(cmd.getName().equalsIgnoreCase("adminsign")){
    39. if(sender instanceof Player){
    40. Player p = (Player)sender;
    41. if (args.length < 1) {
    42. p.sendMessage(ChatColor.RED + "[AdminSign] What do you want to do?");
    43. p.sendMessage(ChatColor.RED + "Usage: /adminsign <add/remove>");
    44. return true;
    45. }
    46. if (args.length > 1) {
    47. p.sendMessage(ChatColor.RED + "[AdminSign] Too many arguments!");
    48. return false;
    49. }
    50.  
    51. if(args[0].equalsIgnoreCase("add")){
    52. if(p.hasPermission("adminsign.add")){
    53. add.add(p.getName());
    54. p.sendMessage(ChatColor.GREEN + "[AdminSign] Right click the sign you want to make an AdminSign!");
    55. p.sendMessage(ChatColor.GREEN + "Type \"cancel\" to cancel the AdminSign creation.");
    56. return true;
    57. }
    58. }
    59. if(args[0].equalsIgnoreCase("remove")){
    60. if(p.hasPermission("adminsign.remove")){
    61. del.add(p.getName());
    62. p.sendMessage(ChatColor.GREEN + "[AdminSign] Right click the AdminSign you want to remove!");
    63. p.sendMessage(ChatColor.GREEN + "Type \"cancel\" to cancel the AdminSign deletion.");
    64. return true;
    65. }
    66. }
    67. }else{
    68. sender.sendMessage("You have to be a player to use /adminsign!");
    69. return true;
    70. }
    71. return true;
    72. }
    73. return false;
    74. }
    75.  
    76. @EventHandler
    77. public void signManage(PlayerInteractEvent e){
    78. if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
    79. if(e.getClickedBlock().getType() == Material.SIGN_POST || e.getClickedBlock().getType() == Material.WALL_SIGN || e.getClickedBlock().getType() == Material.SIGN){
    80. Player p = e.getPlayer();
    81. if(add.contains(p.getName())){
    82. Sign s = (Sign)e.getClickedBlock().getState();
    83. sh.addSign(s);
    84. add.remove(p.getName());
    85. p.sendMessage(ChatColor.GREEN + "[AdminSign] AdminSign successfully created!");
    86. }
    87. if(del.contains(p.getName())){
    88. Sign s = (Sign)e.getClickedBlock().getState();
    89. sh.removeSign(s);
    90. del.remove(p.getName());
    91. p.sendMessage(ChatColor.GREEN + "[AdminSign] AdminSign successfully removed!");
    92. }
    93. }else{
    94. e.getPlayer().sendMessage("Sign");
    95. }
    96. }else{
    97. e.getPlayer().sendMessage("ChatColor");
    98. }
    99. }
    100.  
    101. @EventHandler
    102. public void cancel(AsyncPlayerChatEvent e){
    103. if(e.getMessage().equalsIgnoreCase("cancel")){
    104. Player p = e.getPlayer();
    105. if(add.contains(p.getName())){
    106. add.remove(p.getName());
    107. p.sendMessage(ChatColor.GREEN + "[AdminSign] AdminSign creation cancelled.");
    108. e.setCancelled(true);
    109. }
    110. if(del.contains(p.getName())){
    111. del.remove(p.getName());
    112. p.sendMessage(ChatColor.GREEN + "[AdminSign] AdminSign deletion cancelled.");
    113. e.setCancelled(true);
    114. }
    115. }
    116. }
    117. }
    118.  
     
  2. Offline

    Xacero

    krazytraynz
    Are the messages being sent? (in the same statement where the sender should be added to the list)
     
  3. Offline

    jacklin213

    why do you have
    Code:java
    1. @EventHandler
    2. public void cancel(AsyncPlayerChatEvent e){
    3. if(e.getMessage().equalsIgnoreCase("cancel")){
    4. Player p = e.getPlayer();
    5. if(add.contains(p.getName())){
    6. add.remove(p.getName());
    7. p.sendMessage(ChatColor.GREEN + "[AdminSign] AdminSign creation cancelled.");
    8. e.setCancelled(true);
    9. }
    10. if(del.contains(p.getName())){
    11. del.remove(p.getName());
    12. p.sendMessage(ChatColor.GREEN + "[AdminSign] AdminSign deletion cancelled.");
    13. e.setCancelled(true);
    14. }
    15. }
    16. }
     
  4. Offline

    krazytraynz

    Yes, that's why I'm confused as to why this isn't working.

    In case the player wants to cancel creating/deleting a sign, they just say "cancel" and they'll be removed from the Lists.
     
  5. Offline

    krazytraynz

  6. Offline

    Rocoty

    Might be one of those cases where you are working with two instances of the class, thinking you are working with only one. Happens often with beginners. Check your main class if you are calling the constructor (new AddSignHandler(...)) more than once...

    Sometimes one just thinks that you could just do
    Code:java
    1. getCommand("foo").setExecutor(new CmdAndLstnr());
    2. getServer().getPluginManager().registerEvents(new CmdAndLstnr(), this);

    But right there you have two instances of the class. And since the lists are instance fields of that class, you have two instances of each of those as well.

    However, using the same instance for both command handling and event listening, is an entirely different story
    Code:java
    1. CmdAndLstnr cnl = new CmdAndLstnr();
    2. getCommand("foo").setExecutor(cnl);
    3. getServer().getPluginManager().registerEvents(cnl, this);


    Note: Any identifiers, labels and names used in above examples are arbitrary
     
  7. Offline

    krazytraynz

    Rocoty
    It's not that, that's what I've been doing since I first started making plugins.
     
  8. Offline

    Rocoty

    Post your main class...something is off, and I can't help with only that one class, sorry.
     
  9. Offline

    krazytraynz

    Rocoty
    Code:java
    1. package com.github.KrazyTraynz;
    2.  
    3. import java.io.BufferedReader;
    4. import java.io.File;
    5. import java.io.FileReader;
    6. import java.io.IOException;
    7.  
    8. import org.bukkit.Bukkit;
    9. import org.bukkit.ChatColor;
    10. import org.bukkit.Location;
    11. import org.bukkit.World;
    12. import org.bukkit.block.Sign;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.event.EventHandler;
    15. import org.bukkit.event.Listener;
    16. import org.bukkit.event.player.PlayerJoinEvent;
    17. import org.bukkit.event.player.PlayerQuitEvent;
    18. import org.bukkit.plugin.Plugin;
    19. import org.bukkit.plugin.java.JavaPlugin;
    20.  
    21. public class AdminSign extends JavaPlugin implements Listener{
    22.  
    23. private AddSignHandler ash;
    24. private SignsHandler sh;
    25.  
    26. String vs = "";
    27.  
    28. public void onEnable(){
    29. ConfigurationLoader.load(this);
    30.  
    31. File ymlFile = new File(getDataFolder(), "AdminSigns.yml");
    32.  
    33. if(!ymlFile.exists()){
    34. try{
    35. ymlFile.createNewFile();
    36. }catch(IOException e){
    37. e.printStackTrace();
    38. }
    39. }
    40.  
    41. ash = new AddSignHandler(this);
    42. getCommand("adminsign").setExecutor(ash);
    43.  
    44. getServer().getPluginManager().registerEvents(this, this);
    45. getServer().getPluginManager().registerEvents(new AddSignHandler(this), this);
    46.  
    47. boolean u = getConfig().getBoolean("Update.Check");
    48. if (u) {
    49. getLogger().info("Searching for update...");
    50.  
    51. Download.getFile("[url]https://dl.dropboxusercontent.com/u/94792067/AdminSignVersion.txt[/url]", "plugins/AdminSign/");
    52. File dir = new File("plugins/AdminSign/");
    53. dir.mkdirs();
    54. File file = new File(dir + "/AdminSignVersion.txt");
    55. try {
    56. FileReader fr = new FileReader(file);
    57. vs = br.readLine();
    58. br.close();
    59. fr.close();
    60. } catch (Exception e) {
    61. e.printStackTrace();
    62. }
    63.  
    64. if (!vs.equalsIgnoreCase(getDescription().getVersion()))
    65. {
    66. getLogger().info("New version found: " + vs);
    67. getLogger().info("Check <SITE> for more information and to download it.");
    68. }
    69. else {
    70. getLogger().info("No new update detected.");
    71. }
    72. } else {
    73. getLogger().info("Update Checker disabled!");
    74. }
    75. getLogger().info("AdminSign has been enabled!");
    76. }
    77.  
    78. public void onDisable(){
    79. getLogger().info("AdminSign has been disabled!");
    80. }
    81.  
    82. @EventHandler
    83. public void updateOnJoin(PlayerJoinEvent e){
    84. int i = 0;
    85. for(Player p : Bukkit.getServer().getOnlinePlayers()){
    86. if(p.hasPermission("adminsign.staff")){
    87. i++;
    88. }
    89. }
    90. updateSigns(this, e.getPlayer().getWorld(), i);
    91. }
    92.  
    93. @EventHandler
    94. public void updateOnLeave(PlayerQuitEvent e){
    95. int i = 0;
    96. for(Player p : Bukkit.getServer().getOnlinePlayers()){
    97. if(p.hasPermission("adminsign.staff")){
    98. i++;
    99. }
    100. }
    101. updateSigns(this, e.getPlayer().getWorld(), i);
    102. }
    103.  
    104. public void updateSigns(Plugin p, World w, int i){
    105. Location l = sh.getLocation(this);
    106. if(w.getBlockAt(l) instanceof Sign){
    107. Sign s = (Sign)w.getBlockAt(l);
    108. s.setLine(0, ChatColor.GREEN + "Staff online:");
    109. if(i == 0){
    110. s.setLine(1, ChatColor.RED + "None!");
    111. s.setLine(3, ChatColor.RED + "Please behave.");
    112. s.update();
    113. }else{
    114. s.setLine(1, ChaColor.GREEN + Integer.toString(i) + "/" + getConfig().getInt("StaffMembers"));
    115. s.update();
    116. }
    117. }
    118. }
    119. }
    120.  
     
  10. Offline

    jacklin213

    Just going to point out here, if you are going to submit a plugin like that to bukkit dev it will be rejected, Look up there guidelines for update checking. You arent allowed to update check from any site but the curse site
     
  11. Offline

    krazytraynz

    Yeah, I figured that out yesterday :p The code has been updated since then.
     
  12. @krazytraynz im not sure but it looks like that you never registered your events in the main class
     
  13. Offline

    krazytraynz

    Line 45.

    Also, it isn't that the event isn't being called, it's that I can't seem to get pass the if statement that checks whether or not a player name is in the HashMap.
     
  14. Offline

    krazytraynz

    Bump - It turns out I really need this solved, as I'm having the same exact problem in another plugin I'm working on. I can post the source code for it in addition to this if anyone thinks it'll help. I may resort to tagging people if I have to bump again.
     
  15. Offline

    Minnymin3

    Ok so what is happening is you are registering a new instance of your handler class. You should register the one that is registered as the command executor.
     
  16. Offline

    krazytraynz

    The event is being called (I used debugs), and the command is executing the way it should (The messages show up), but the event can't get past the if statement that checks if the Player is in the HashMap.
     
  17. Offline

    Minnymin3

    Like I said you have to register the same instance of the object as the command and as the event
     
  18. Offline

    krazytraynz

    I'll try it, but how would that affect one if statement in the event if the first two still work?
     
  19. Offline

    Minnymin3

    In the main class, you are creating two instances of the class. That means the command and the event are not in the same instance of the class which means the event and the command aren't accessing the same array list.

    Learning java is a good idea btw.
     
  20. Offline

    krazytraynz

    Minnymin3
    That fixed half of it (the chat event works now), although the PlayerInteractEvent isn't working. Any ideas?
     
  21. Offline

    xTrollxDudex

    krazytraynz
    Make sure to also use the same instance of AddSignHandler in AdminSign to register the SignsHandler
     
    1Rogue likes this.
  22. Offline

    krazytraynz

    xTrollxDudex
    Minnymin3 already helped me fix that, but the PlayerInteractEvent listener still isn't fully working (it doesn't get past line 81 or 87).

    Bump.

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

    1Rogue Retired Staff

    TrollDude said exactly what your issue is.
     
  24. Offline

    krazytraynz

    It turns out you guys were right, I was just getting an NPE in the PlayerInteractEvent :oops: Thanks for the help!
     
Thread Status:
Not open for further replies.

Share This Page