Solved Afk plugin doesn't seem to work

Discussion in 'Plugin Development' started by KarimAKL, Jun 13, 2018.

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

    KarimAKL

    I'm trying to make a AFK plugin using a HashMap<UUID, Integer> but when i try using this:
    Code:Java
    1.  
    2. public class RepeatRun {
    3.  
    4. @SuppressWarnings("unused")
    5. private Main plugin;
    6.  
    7. public RepeatRun(Main plugin) {
    8. this.plugin = plugin;
    9. HashMap<UUID, Integer> isAfk = new AfkCheck().isAfk;
    10. new BukkitRunnable() {
    11. @Override
    12. public void run() {
    13. if (!plugin.getServer().getOnlinePlayers().isEmpty()) {
    14. for (Player op : Bukkit.getOnlinePlayers()) {
    15. if (op.isOnline()) {
    16. if (isAfk.containsKey(op.getUniqueId())) {
    17. isAfk.put(op.getUniqueId(), isAfk.get(op.getUniqueId())+1);
    18. if (isAfk.get(op.getUniqueId()) >= 20*60*5) {
    19. WitherSkull skull = (WitherSkull) op.getWorld().spawn(op.getLocation().add(0, +1, 0), WitherSkull.class);
    20. skull.setDirection(new Vector(0, 0, 0));
    21. skull.setVelocity(new Vector(0, 0, 0));
    22. Entity barrier = op.getWorld().dropItem(op.getLocation().add(0, +2, 0), new ItemStack(Material.BARRIER));
    23. skull.setPassenger(barrier);
    24. new BukkitRunnable() {
    25. @Override
    26. public void run() {
    27. if (isAfk.get(op.getUniqueId()) < 20*60*5) {
    28. skull.remove();
    29. barrier.remove();
    30. cancel();
    31. }
    32. }
    33. }.runTaskTimer(plugin, 0, 20);
    34. }
    35. } else {
    36. isAfk.put(op.getUniqueId(), 1);
    37. }
    38. }
    39. }
    40. }
    41. }
    42. }.runTaskTimer(plugin, 0, 20);
    43. }
    44. }
    45.  

    The count doesn't seem to go up, i have made a "/getafk" command to test if it does go up like this:
    Code:Java
    1.  
    2. public class CommandGetAfk implements CommandExecutor {
    3.  
    4. @SuppressWarnings("unused")
    5. private Main plugin;
    6.  
    7. public CommandGetAfk(Main plugin) {
    8. this.plugin = plugin;
    9. plugin.getCommand("getafk").setExecutor(this);
    10. }
    11.  
    12. private HashMap<UUID, Integer> isAfk = new AfkCheck().isAfk;
    13.  
    14. @Override
    15. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    16. if (cmd.getName().equalsIgnoreCase("getafk")) {
    17. if (sender instanceof Player) {
    18. Player p = (Player) sender;
    19. if (isAfk.containsKey(p.getUniqueId())) {
    20. sender.sendMessage("Afk time: "+isAfk.get(p.getUniqueId()));
    21. return true;
    22. } else {
    23. isAfk.put(p.getUniqueId(), 0);
    24. sender.sendMessage("Afk time: "+isAfk.get(p.getUniqueId()));
    25. return true;
    26. }
    27. } else {
    28. if (args.length == 0) {
    29. sender.sendMessage("Only players may execute this command!");
    30. return true;
    31. } else {
    32. for (Player op : Bukkit.getOnlinePlayers()) {
    33. if (op.getName().equalsIgnoreCase(args[0])) {
    34. if (isAfk.containsKey(op.getUniqueId())) {
    35. sender.sendMessage("Afk time: "+isAfk.get(op.getUniqueId()));
    36. return true;
    37. } else {
    38. isAfk.put(op.getUniqueId(), 0);
    39. sender.sendMessage("Afk time: "+isAfk.get(op.getUniqueId()));
    40. return true;
    41. }
    42. }
    43. }
    44. }
    45. }
    46. }
    47. return false;
    48. }
    49. }
    50.  

    And it always returns "Afk time: 0", i've tried to find something that keeps setting it to 0 and i've also tried finding something that makes it so it doesn't go up but i've had no luck. :/
    Here are some classes to set the time to 0 if you move/interact with something, i might've made a mistake in one of them but i can't seem to find it, maybe you can see what's wrong.
    Code:Java
    1.  
    2. public class AsyncPlayerChat implements Listener {
    3.  
    4. @SuppressWarnings("unused")
    5. private Main plugin;
    6.  
    7. public AsyncPlayerChat(Main plugin) {
    8. this.plugin = plugin;
    9. Bukkit.getPluginManager().registerEvents(this, plugin);
    10. }
    11.  
    12. private HashMap<UUID, Integer> isAfk = new AfkCheck().isAfk;
    13.  
    14. @EventHandler
    15. public void onChat(AsyncPlayerChatEvent e) {
    16. Player p = e.getPlayer();
    17. isAfk.put(p.getUniqueId(), 0);
    18. if (isAfk.get(p.getUniqueId()) >= 20*60*5) {
    19. p.sendMessage("You are no longer afk.");
    20. }
    21. }
    22. }
    23.  

    Code:Java
    1.  
    2. public class CommandPreprocess implements Listener {
    3.  
    4. @SuppressWarnings("unused")
    5. private Main plugin;
    6.  
    7. public CommandPreprocess(Main plugin) {
    8. this.plugin = plugin;
    9. Bukkit.getPluginManager().registerEvents(this, plugin);
    10. }
    11.  
    12. private HashMap<UUID, Integer> isAfk = new AfkCheck().isAfk;
    13.  
    14. @EventHandler
    15. public void onCommand(PlayerCommandPreprocessEvent e) {
    16. if (!e.getMessage().equalsIgnoreCase("/afk") || !e.getMessage().equalsIgnoreCase("/getafk")) {
    17. Player p = e.getPlayer();
    18. isAfk.put(p.getUniqueId(), 0);
    19. if (isAfk.get(p.getUniqueId()) >= 20*60*5) {
    20. p.sendMessage("You are no longer afk.");
    21. }
    22. }
    23. }
    24. }
    25.  

    Code:Java
    1.  
    2. public class PlayerInteract implements Listener {
    3.  
    4. @SuppressWarnings("unused")
    5. private Main plugin;
    6.  
    7. public PlayerInteract(Main plugin) {
    8. this.plugin = plugin;
    9. Bukkit.getPluginManager().registerEvents(this, plugin);
    10. }
    11.  
    12. private HashMap<UUID, Integer> isAfk = new AfkCheck().isAfk;
    13.  
    14. @EventHandler
    15. public void onInteract(PlayerInteractEvent e) {
    16. Player p = e.getPlayer();
    17. isAfk.put(p.getUniqueId(), 0);
    18. if (isAfk.get(p.getUniqueId()) >= 20*60*5) {
    19. p.sendMessage("You are no longer afk.");
    20. }
    21. }
    22. }
    23.  

    Code:Java
    1.  
    2. public class PlayerMove implements Listener {
    3.  
    4. @SuppressWarnings("unused")
    5. private Main plugin;
    6.  
    7. public PlayerMove(Main plugin) {
    8. this.plugin = plugin;
    9. Bukkit.getPluginManager().registerEvents(this, plugin);
    10. }
    11.  
    12. private HashMap<UUID, Integer> isAfk = new AfkCheck().isAfk;
    13.  
    14. @EventHandler
    15. public void onMove(PlayerMoveEvent e) {
    16. Player p = e.getPlayer();
    17. isAfk.put(p.getUniqueId(), 0);
    18. if (isAfk.get(p.getUniqueId()) >= 20*60*5) {
    19. p.sendMessage("You are no longer afk.");
    20. }
    21. }
    22. }
    23.  

    Code:Java
    1.  
    2. public class PlayerQuit implements Listener {
    3.  
    4. @SuppressWarnings("unused")
    5. private Main plugin;
    6.  
    7. public PlayerQuit(Main plugin) {
    8. this.plugin = plugin;
    9. Bukkit.getPluginManager().registerEvents(this, plugin);
    10. }
    11.  
    12. private HashMap<UUID, Integer> isAfk = new AfkCheck().isAfk;
    13.  
    14. @EventHandler
    15. public void onQuit(PlayerQuitEvent e) {
    16. Player p = e.getPlayer();
    17. isAfk.put(p.getUniqueId(), 0);
    18. }
    19. }
    20.  

    EDIT: Seems i forgot to post a "/afk" command class(Just in case that is doing something):
    Code:Java
    1.  
    2. public class CommandAfk implements CommandExecutor {
    3.  
    4. @SuppressWarnings("unused")
    5. private Main plugin;
    6.  
    7. public CommandAfk(Main plugin) {
    8. this.plugin = plugin;
    9. plugin.getCommand("afk").setExecutor(this);
    10. }
    11.  
    12. private HashMap<UUID, Integer> isAfk = new AfkCheck().isAfk;
    13.  
    14. @Override
    15. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    16. if (cmd.getName().equalsIgnoreCase("afk")) {
    17. if (sender instanceof Player) {
    18. Player p = (Player) sender;
    19. if (isAfk.containsKey(p.getUniqueId())) {
    20. if (isAfk.get(p.getUniqueId()) < 20*60*5) {
    21. isAfk.put(p.getUniqueId(), 20*60*5);
    22. sender.sendMessage("You are now afk.");
    23. return true;
    24. } else {
    25. isAfk.put(p.getUniqueId(), 0);
    26. sender.sendMessage("You are no longer afk.");
    27. return true;
    28. }
    29. } else {
    30. isAfk.put(p.getUniqueId(), 20*60*5);
    31. sender.sendMessage("You are now afk.");
    32. return true;
    33. }
    34. } else {
    35. sender.sendMessage("Only players may execute this command!");
    36. return true;
    37. }
    38. }
    39. return false;
    40. }
    41. }
    42.  

    Btw i have no errors or anything.
     
  2. Online

    timtower Ninja on the waves Moderator

    @KarimAKL You are making new instances of AfkCheck every time.
    Make one in the onEnable, pass it along to the other classes.
     
  3. Offline

    MightyOne

    A few hours ago you ran the BukkitRunnable every tick, now changed it to once per second. You probably have to change some numbers inside it doesn't take 100 minutes to become afk xD. The second runnable looks a bit unnecessary. Create listeners for movement and stuff and remove afk scull there
     
  4. Offline

    KarimAKL

    How? I tried moving this:
    Code:Java
    1.  
    2. public HashMap<UUID, Integer> isAfk = new HashMap<UUID, Integer>();
    3.  

    to the main class and do this:
    Code:Java
    1.  
    2. HashMap<UUID, Integer> isAfk = plugin.isAfk;
    3.  

    to get it but that comes with a NullPointerException at these lines:
    In onEnable:
    Code:Java
    1.  
    2. new CommandAfk(this);
    3.  

    In CommandAfk:
    Code:Java
    1.  
    2. HashMap<UUID, Integer> isAfk = plugin.isAfk;
    3.  

    I'm sorry but i don't understand what you mean. :/

    I don't know what you are talking about at first..? :7 But about the second runnable i don't know how i would remove the skull in another class(probably pretty easy when i think about it) but i wanna make the rest work before i fix that, anyway thanks for the tip. :p
     
  5. Offline

    MightyOne

    Before calling new CommandAfk(this); you have to initialize your hashmap isAfk.
    Maybe you can show us your whole Main class.

    A clean code also barely contains any public variables. Create a getter to make it look more professional.
     
  6. Offline

    KarimAKL

    @MightyOne Here you go:
    Code:Java
    1.  
    2. public class Main extends JavaPlugin {
    3.  
    4. public HashMap<UUID, Integer> isAfk = new HashMap<UUID, Integer>();
    5.  
    6. public void onEnable() {
    7. new CommandAfk(this);
    8. new CommandGetAfk(this);
    9. new AsyncPlayerChat(this);
    10. new CommandPreprocess(this);
    11. new PlayerInteract(this);
    12. new PlayerMove(this);
    13. new PlayerQuit(this);
    14. new RepeatRun(this);
    15. }
    16. }
    17.  

    This is my current main class. :p
    About creating a getter, i don't know what it is but from a quick google search i think it looks like this?:
    Code:Java
    1.  
    2. public void whatever() { //Can add something in the ()
    3. //Do something
    4. }
    5.  

    Not sure if that's a getter tho and if it is i would just get it like this, right?:
    Code:Java
    1.  
    2. plugin.whatever();
    3.  

    From what i read about them it seems they are used so no one can update them without you knowing?
    EDIT: If this is a getter then i've been using it a few times without knowing the name for it. :p

    Anyway, what do you mean "initialize your hashmap"? I tried looking it up on google and the forum but had no luck. :/ (Sorry, this might be a dumb question)
     
  7. Offline

    MightyOne

    Ok I am really impatient at this point. You don't ask dumb questions. It's just like you created a not just basic code and you don't know how to fix it. But if you don't know how to fix it you shouldn't create it.

    Everything I can tell you is try to reimplement all of your RepeatRun in steps that small that you can always fix mistakes right away. You will learn a lot by doing that yourself.

    Before doing all that you should look up the terms
    declaration of a variable
    initialization of one
    getter methods and
    setter methods

    That is elementary, you will always need it in java, and without java you can forget bukkit. If anyone else can fix the problem woooh happy end :/
     
  8. Offline

    KarimAKL

    @MightyOne I'm sorry but thank you for the terms, i'll look them up and try and learn from what i find. :)
    EDIT: I changed some things and it seems to work now. Thank you for the help MightyOne. :)
     
    Last edited: Jun 15, 2018
Thread Status:
Not open for further replies.

Share This Page