Solved [HELP] ArrayList problem

Discussion in 'Plugin Development' started by Cheesepro, Dec 6, 2014.

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

    Cheesepro

    Ok so I was adding a AFK(Away From Keyboard) feature to my plugin, which set the player no longer afk when the player moved or interacted with something, But the problem is the listener class can't get the data that I set in the ArrayList located at the cmdAFK class, and when I debug it, the size of the ArrayList is always 0 when its in the Listener class, So here is my two classes

    cmdAFK.java
    Code:java
    1. package me.cheesepro.ce.mainpack;
    2.  
    3. import me.cheesepro.ce.extra.ConsoleSender;
    4. import me.cheesepro.ce.extra.Messenger;
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandExecutor;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.potion.PotionEffect;
    11. import org.bukkit.potion.PotionEffectType;
    12.  
    13. import java.util.*;
    14.  
    15. /**
    16.  * Created by Mark on 06/12/2014.
    17.  */
    18. public class cmdAFK implements CommandExecutor{
    19.  
    20. private CEMain plugin;
    21. private final Messenger msg;
    22. // public final HashMap<String, Boolean> isAFK = new HashMap<String, Boolean>();
    23. // public List<String> isAFK = new ArrayList<String>();
    24. private ArrayList<String> isAFK;
    25.  
    26. public cmdAFK(CEMain plugin){
    27. isAFK = new ArrayList<String>();
    28. this.plugin = plugin;
    29. this.msg = new Messenger(plugin);
    30. }
    31.  
    32. @Override
    33. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    34. if(sender instanceof Player) {
    35. Player p = (Player) sender;
    36. if (label.equalsIgnoreCase("afk")) {
    37. if (isAFK.contains(p.getUniqueId().toString())/*if isAFK is true*/) {
    38. isAFK.remove(p.getUniqueId().toString());
    39. p.removePotionEffect(PotionEffectType.BLINDNESS);
    40. msg.broadcast(ChatColor.YELLOW.toString() + ChatColor.BOLD + p.getName() + ChatColor.GRAY.toString() + " is no longer AFK");
    41. } else/*if isAFK is false*/ {
    42. isAFK.add(p.getUniqueId().toString());
    43. PotionEffectType effect=PotionEffectType.BLINDNESS;
    44. p.addPotionEffect(effect.createEffect(1000000,10000));
    45. msg.broadcast(ChatColor.YELLOW.toString() + ChatColor.BOLD + p.getName() + ChatColor.GRAY.toString() + " is AFK");
    46. }
    47. }
    48. }else{
    49. ConsoleSender.noConsole();
    50. }
    51. return false;
    52. }
    53.  
    54. public ArrayList<String> getIsAFK() {
    55. return isAFK;
    56. }
    57.  
    58. }
    59.  


    onPlayerMove.java:
    Code:java
    1. package me.cheesepro.ce.listener;
    2.  
    3. import me.cheesepro.ce.mainpack.cmdAFK;
    4. import me.cheesepro.ce.extra.Messenger;
    5. import me.cheesepro.ce.mainpack.CEMain;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.player.PlayerMoveEvent;
    11. import org.bukkit.potion.PotionEffectType;
    12.  
    13. import java.util.ArrayList;
    14.  
    15. /**
    16.  * Created by Mark on 06/12/2014.
    17.  */
    18. public class onPlayerMove implements Listener{
    19.  
    20. CEMain plugin;
    21. Messenger msg;
    22. cmdAFK afkPlayer;
    23.  
    24. public onPlayerMove(CEMain main)
    25. {
    26. main.getServer().getPluginManager().registerEvents(this, main);
    27. this.plugin = main;
    28. this.msg = new Messenger(plugin);
    29. afkPlayer = new cmdAFK(plugin);
    30. }
    31.  
    32. @EventHandler
    33. public void onAFKMove(PlayerMoveEvent e){
    34. Player p = e.getPlayer();
    35. String id = p.getUniqueId().toString();
    36. if(afkPlayer.getIsAFK().contains(id)) {
    37. afkPlayer.getIsAFK().remove(id);
    38. p.removePotionEffect(PotionEffectType.BLINDNESS);
    39. msg.broadcast(ChatColor.YELLOW.toString() + ChatColor.BOLD + p.getName() + ChatColor.GRAY.toString() + " is AFK");
    40. }
    41. }
    42. }
    43.  


    So if you do know where I got wrong, PLEASE help me, I've been trying to fixing this for like 5 hours...
     
  2. Offline

    WesJD

    Cheesepro You're creating a new instance of the class, so it would always come out to be 0.
     
  3. Offline

    Cheesepro

    WesJD, thanks but how do i exactly do that
     
  4. Offline

    WesJD

  5. Offline

    teej107

    lol
    If that is true, create only one instance and register that one instance as an Event and Command.
     
  6. Offline

    Cheesepro

    WesJD teej107
    So is that mean I have to put both the listener and the command in one class?
     
  7. Offline

    WesJD

  8. Offline

    Cheesepro

    WesJD
    So, can you try to put up a example... please... My brain is overloaded...
     
  9. Offline

    teej107

    Create one instance. Not two. Not four. Five is right out!

    EDIT: Actually, make sure you register your commands. But the above still applies.
     
  10. Offline

    Cheesepro

    teej107 WesJD
    What do you mean by one instance... do i remove the afkPlayer = new cmdAFK(plugin)
     
  11. Offline

    teej107

    Cheesepro Refresh your programming terminology!
     
    Avygeil and JordyPwner like this.
  12. Offline

    JordyPwner

    Show Spoiler

    Code:java
    1.  
    2. ArrayList<Player> isafk = new ArrayList<Player>();
    3.  
    4. @Override
    5. public boolean onCommand(CommandSender sender, Command cmd, String arg2, String[] args) {
    6.  
    7. if(!(sender instanceof Player)){
    8. return true;
    9. }
    10. if(cmd.getName().equalsIgnoreCase("afk")){
    11. Player p = (Player) sender;
    12. if(!(isafk.contains(p))){
    13. Bukkit.getServer().broadcastMessage(p.getName() + " is now AFK");
    14. isafk.add(p);
    15. }else{
    16. Bukkit.getServer().broadcastMessage(p.getName() + "is no longer afk!");
    17. isafk.remove(p);
    18. }
    19. }
    20. return false;
    21. }
    22.  
    23. }

     
  13. Offline

    Cheesepro

    JordyPwner teej107
    I just wanted to know how should the code be in the onPlayerMove.java, I think the problem is here
    Code:java
    1. CEMain plugin;
    2. Messenger msg;
    3. cmdAFK afkPlayer;
    4.  
    5. public onPlayerMove(CEMain main)
    6. {
    7. main.getServer().getPluginManager().registerEvents(this, main);
    8. this.plugin = main;
    9. this.msg = new Messenger(plugin);
    10. afkPlayer = new cmdAFK(plugin);
    11. }

    and I still don't know how to make one instance... Sorry, I am just kinda confused
     
  14. Offline

    JordyPwner

    Here u go
    Show Spoiler

    Code:java
    1. ArrayList<Player> isafk = new ArrayList<Player>();
    2.  
    3. @EventHandler
    4. public void onPlayerMove(PlayerMoveEvent e){
    5. Player p = e.getPlayer();
    6. if(!(isafk.contains(p))){
    7. return;
    8. }
    9. if(isafk.contains(p)){
    10. Bukkit.getServer().broadcastMessage("unafk message here");
    11. isafk.remove(p);
    12.  
    13. }
    14. }
    15.  
    16. }

     
  15. Offline

    Cheesepro

    JordyPwner but the isafk ArrayList in the onPlayerMove.java is not linked with the ArrayList in the cmdAFK.java
     
  16. Offline

    JordyPwner

    make it public or put the event in the cmd class
     
  17. Offline

    Cheesepro

  18. Offline

    JordyPwner

    show me your code?
     
  19. Offline

    Cheesepro

    cmdAFK.java
    Code:java
    1. package me.cheesepro.ce.mainpack;
    2.  
    3. import me.cheesepro.ce.extra.ConsoleSender;
    4. import me.cheesepro.ce.extra.Messenger;
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandExecutor;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.potion.PotionEffect;
    11. import org.bukkit.potion.PotionEffectType;
    12.  
    13. import java.util.*;
    14.  
    15. /**
    16. * Created by Mark on 06/12/2014.
    17. */
    18. public class cmdAFK implements CommandExecutor{
    19.  
    20. private CEMain plugin;
    21. private final Messenger msg;
    22. // public final HashMap<String, Boolean> isAFK = new HashMap<String, Boolean>();
    23. // public List<String> isAFK = new ArrayList<String>();
    24.  
    25. public cmdAFK(CEMain plugin){
    26. this.plugin = plugin;
    27. this.msg = new Messenger(plugin);
    28. }
    29.  
    30. public ArrayList<String> isAFK = new ArrayList<String>();
    31.  
    32. @Override
    33. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    34. if(sender instanceof Player) {
    35. Player p = (Player) sender;
    36. if (label.equalsIgnoreCase("afk")) {
    37. if (isAFK.contains(p.getUniqueId().toString())/*if isAFK is true*/) {
    38. isAFK.remove(p.getUniqueId().toString());
    39. p.removePotionEffect(PotionEffectType.BLINDNESS);
    40. msg.broadcast(ChatColor.YELLOW.toString() + ChatColor.BOLD + p.getName() + ChatColor.GRAY.toString() + " is no longer AFK");
    41. } else/*if isAFK is false*/ {
    42. isAFK.add(p.getUniqueId().toString());
    43. PotionEffectType effect=PotionEffectType.BLINDNESS;
    44. p.addPotionEffect(effect.createEffect(1000000,10000));
    45. msg.broadcast(ChatColor.YELLOW.toString() + ChatColor.BOLD + p.getName() + ChatColor.GRAY.toString() + " is AFK");
    46. }
    47. }
    48. }else{
    49. ConsoleSender.noConsole();
    50. }
    51. return false;
    52. }
    53.  
    54. public ArrayList<String> getIsAFK() {
    55. return isAFK;
    56. }
    57.  
    58. }
    59.  


    onPlayerMove.java
    Code:java
    1. package me.cheesepro.ce.listener;
    2.  
    3. import me.cheesepro.ce.mainpack.cmdAFK;
    4. import me.cheesepro.ce.extra.Messenger;
    5. import me.cheesepro.ce.mainpack.CEMain;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.player.PlayerMoveEvent;
    11. import org.bukkit.potion.PotionEffectType;
    12.  
    13. import java.util.ArrayList;
    14.  
    15. /**
    16. * Created by Mark on 06/12/2014.
    17. */
    18. public class onPlayerMove implements Listener{
    19.  
    20. CEMain plugin;
    21. Messenger msg;
    22. cmdAFK afkPlayer;
    23.  
    24. public onPlayerMove(CEMain main)
    25. {
    26. main.getServer().getPluginManager().registerEvents(this, main);
    27. this.plugin = main;
    28. this.msg = new Messenger(plugin);
    29. afkPlayer = new cmdAFK(plugin);
    30. }
    31.  
    32. @EventHandler
    33. public void onAFKMove(PlayerMoveEvent e){
    34. Player p = e.getPlayer();
    35. String id = p.getUniqueId().toString();
    36. if(afkPlayer.getIsAFK().contains(id)) {
    37. afkPlayer.getIsAFK().remove(id);
    38. p.removePotionEffect(PotionEffectType.BLINDNESS);
    39. msg.broadcast(ChatColor.YELLOW.toString() + ChatColor.BOLD + p.getName() + ChatColor.GRAY.toString() + " is AFK");
    40. }
    41. }
    42. }
    43.  
     
  20. Offline

    JordyPwner

    still your old code.. i gave you some better code but u dont use it? lel then i cant help you sorry
     
  21. Offline

    Cheesepro

    JordyPwner I tried your codes, the ArrayList still cant be linked
     
  22. Offline

    JordyPwner

    intialize the arraylist. and goddammit why you use this >.< public List<String> isAFK = new ArrayList<String>();
     
  23. Offline

    Cheesepro

    I used it cause I thought u mean it here

    JordyPwner How do I initialize it? I am kinda a noobbbb

    <Edit by mrCookieSlime: Merged posts. Please don't double post. There is an Edit Button right next to the Date.>
     
  24. Offline

    JordyPwner

    why the fuck are you using a list? just use a arraylist goddammit

    private Something something; <== if you can figure this out then you really need to learn java

    <Edit by mrCookieSlime: Merged posts. Please don't double post. There is an Edit Button right next to the Date.>
     
  25. Offline

    Cheesepro

    JordyPwner Come down... It is a arraylist... ArrayList<String> isAFK = new ArrayList<String>();
     
  26. Offline

    JordyPwner

    ArrayList<Player> isafk = new ArrayList<Player>(); !!!!!
     
  27. Offline

    Cheesepro

    JordyPwner I did everything that u said... Still when I am in afk and when I move, the afk is not canceled.
     
  28. Offline

    Avygeil

    JordyPwner Still wrong, use a Set for .contains operations.
     
  29. Offline

    Skionz

    Cheesepro Initialize the ArrayList in the constructor.
     
  30. Offline

    Cheesepro

    Avygeil Do you mean a get?
    Skionz In which constructor? cmdAFK? or onPlayerMove?
     
Thread Status:
Not open for further replies.

Share This Page