Solved Afk checking with EssentialsX wont work

Discussion in 'Spigot Plugin Development' started by Ma100, Jan 11, 2020.

Thread Status:
Not open for further replies.
  1. Here is my code:
    Code (Java):
    package me.Ma100.afktp;
    import org.bukkit.plugin.java.JavaPlugin;
    import me.Ma100.afktp.commands.HelloCommand;
    import com.earth2me.essentials.Essentials;
    import org.bukkit.entity.Player;
    import org.bukkit.Bukkit;


    public class Main extends JavaPlugin {
     
        static Essentials ess = (Essentials) Bukkit.getServer().getPluginManager().getPlugin("EssentialsX");
     
        @Override
        public void onEnable() {
            new Commands(this);
            loop(this);
        }
     
        static void loop(Main main)
        {
            Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(main, new Runnable()
            {
                public void run()
                {
                    for (Player p : Bukkit.getServer().getOnlinePlayers())
                    {
                        if (ess.getUser(p).isAfk())
                        {
                            p.kickPlayer("§4AFK FOR TOO LONG");
                        }
                    }
                    loop(main);
                }
            }, 100L);
        }

    }
     
    For some reason when i go AFK and EssentialsX detects it my plugins event wont trigger. How can I fix it or how can I make an Auto-AFK code of my own? (Code samples appreciated)
    Thanks!
     
    #1 Ma100, Jan 11, 2020
    Last edited: Jan 11, 2020
  2. The reason I want to do it myself that I want to extend the existing auto-afk of essentialsX with afk-tp& stuff.
    But thanks, I will try your solution and tell how it goes!

    EDIT: I get this error:
    Code (Text):
    Description    Resource    Path    Location    Type
    The method scheduleSyncRepeatingTask(Plugin, Runnable, long, long) in the type BukkitScheduler is not applicable for the arguments (Main, new Runnable(){}, long)    Main.java    /AFKTP/src/me/Ma100/afktp    line 22    Java Problem
     
     
  3. Why have you made everything static? There is literally no reason for either the method or the field to be static at all. I imagine your problem is that you're running a delayed task, you're probably looking for a repeating task

    Edit: looking at your edit you need to pass in 2 longs instead of 1, the first is the initial delay (in ticks) and the second is the period (in ticks)
     
  4. you have to add another parameter https://hub.spigotmc.org/javadocs/s...t.plugin.Plugin-java.lang.Runnable-long-long-

    in your case - just add another 100 as the last argument... e.g. }, 100L, 100L);
     
  5. I tried that but nothing still happens when I go afk.
    In my console was many messages like this:
    Code (Text):
    [Server thread/WARN]: [AFKTP] Task #1103 for AFKTP v1.0 generated an exception
    java.lang.NullPointerException: null
            at me.Ma100.afktp.Main$1.run(Main.java:28) ~[?:?]
            at org.bukkit.craftbukkit.v1_14_R1.scheduler.CraftTask.run(CraftTask.java:81) ~[spigot-1.14.4.jar:git-Spigot-cbd1a1b-009d8af]
            at org.bukkit.craftbukkit.v1_14_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:394) [spigot-1.14.4.jar:git-Spigot-cbd1a1b-009d8af]
            at net.minecraft.server.v1_14_R1.MinecraftServer.b(MinecraftServer.java:1022) [spigot-1.14.4.jar:git-Spigot-cbd1a1b-009d8af]
            at net.minecraft.server.v1_14_R1.DedicatedServer.b(DedicatedServer.java:393) [spigot-1.14.4.jar:git-Spigot-cbd1a1b-009d8af]
            at net.minecraft.server.v1_14_R1.MinecraftServer.a(MinecraftServer.java:970) [spigot-1.14.4.jar:git-Spigot-cbd1a1b-009d8af]
            at net.minecraft.server.v1_14_R1.MinecraftServer.run(MinecraftServer.java:815) [spigot-1.14.4.jar:git-Spigot-cbd1a1b-009d8af]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_231]
    AND like this:
    Code (Text):
    [Server thread/WARN]: [AFKTP] Task #17700 for AFKTP v1.0 generated an exception
    java.lang.NullPointerException: null
     
    #6 Ma100, Jan 11, 2020
    Last edited: Jan 11, 2020
  6. Try this modification of your code - you're likely asking for the essentials plugin before it actually exists.

    You also need to declare that your plugin depends on EssentialsX by putting the following into your plugin.xml to ensure the correct loading order.
    Code (Text):
    depend: [Essentials]
    (NOT EssentialsX)


    Code (Java):
    public class Main extends JavaPlugin {
        Essentials ess;
        @Override
        public void onEnable() {
            ess = (Essentials) Bukkit.getServer().getPluginManager().getPlugin("Essentials");  // <-- NO X - essentialsX fakes being just essentials
            // new Commands(this);  // <-- why? what do you think this is doing?
            if (ess != null)
              loop();
            else
              Bukkit.getLogger().info("[[[[[[[ ESS is NULL ]]]]]]]");
        }
        void loop()
        {
            Bukkit.getLogger().info("[[[[[[[ IT STARTED ]]]]]]]");
            Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(main,() -> {  // <-- changed this to lambda style, it's functionally the same as what you had but cleaner and more modern
                Bukkit.getLogger().info("[[[[[[[ Checking for AFK players ]]]]]]]");
                for (Player p : Bukkit.getServer().getOnlinePlayers())
                {
                    if (ess.getUser(p).isAfk())
                    {
                        p.kickPlayer("§4AFK FOR TOO LONG");
                    }
                }
                // loop(main);  // <-- remove this because we're now using scheduleSyncRepeatingTask
            }, 100L, 100L);
        }
    }
    Edited: fixed EssentialsX -> Essentials
     
    • Winner Winner x 1
  7. Thanks, I'll try that!
    Code (Java):
    new Commands(this);
    is for some of the commands in the plugin.

    Edit: Thank you, it worked!!
     
    #8 Ma100, Jan 11, 2020
    Last edited: Jan 11, 2020
Thread Status:
Not open for further replies.