Solved PlayerJoinEvent help

Discussion in 'Spigot Plugin Development' started by UnlimitedNinjas, Jul 7, 2018.

  1. Everything except the join event is working here.
    I get kicked and stuff everything is fine but when try to relog, it doesnt kick me again?
    I think its due to the arraylist ressetting or?
    Code (Text):
    package me.thesilentpro.net.commands;

    import java.util.ArrayList;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.PlayerJoinEvent;

    public class Denyaccess implements CommandExecutor {

        ArrayList<String> access = new ArrayList<String>();
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String lable, String args[]) {
            if (sender instanceof Player) {
                Player p = (Player) sender;
                if (p.hasPermission("upg.denyaccess")) {
                    if (args.length == 1) {
                        if (access.contains(args[0])) {
                            access.remove(args[0]);
                            Bukkit.getServer().broadcastMessage(ChatColor.RED + p.getName() + " - Enabling server access for player " + args[0]);
                        }else {
                            Player target = Bukkit.getServer().getPlayer(args[0]);
                            target.getWorld().strikeLightning(target.getLocation());
                            target.getWorld().strikeLightning(target.getLocation());
                            access.add(target.getName());
                            Bukkit.getServer().broadcastMessage(ChatColor.RED + p.getName() + " - Denying server access for player " + target.getName());
                            target.kickPlayer(ChatColor.DARK_RED + "You access to UPGamers has been denied by " + p.getName());
                        }
                    }
                }
            }
            return true;
        }
     
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            if (access.contains(e.getPlayer().getName())) {
                e.getPlayer().kickPlayer(ChatColor.DARK_RED + "Access denied!");
                Bukkit.getServer().broadcastMessage(ChatColor.RED + "Kicking " + e.getPlayer().getName() + " Reason: " + ChatColor.YELLOW + " Access denied!");
            }
        }

    }
     
     
    #1 UnlimitedNinjas, Jul 7, 2018
    Last edited: Jul 9, 2018
  2. You aren't implementing listener!
    Make sure to also register the listener in your onEnable.
     
  3. Added Bukkit.getServer().getPluginManager().registerEvents(this, (Plugin) new Denyaccess() ); to on enable, still doesnt work.
    also now onDisable gives a error
    Code (Text):
    :[16:51:15 ERROR]: Error occurred while disabling UPGamers v2.2 (Is it up to date?)java.lang.ClassCastException: me.thesilentpro.net.commands.Denyaccess cannot be cast to org.bukkit.plugin.Plugin
        at me.thesilentpro.net.Main.onDisable(Main.java:85)
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:266)
        at org.bukkit.plugin.java.JavaPluginLoader.disablePlugin(JavaPluginLoader.java:361)
        at org.bukkit.plugin.SimplePluginManager.disablePlugin(SimplePluginManager.java:422)
        at com.rylinaux.plugman.util.PluginUtil.unload(PluginUtil.java:419)
        at com.rylinaux.plugman.util.PluginUtil.reload(PluginUtil.java:379)
        at com.rylinaux.plugman.command.ReloadCommand.execute(ReloadCommand.java:122)
        at com.rylinaux.plugman.PlugManCommandHandler.onCommand(PlugManCommandHandler.java:95)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141)
        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(CraftServer.java:648)
        at net.minecraft.server.v1_12_R1.PlayerConnection.handleCommand(PlayerConnection.java:1397)
        at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1232)
        at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:45)
        at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:1)
        at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46)
        at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:748)
        at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406)
        at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679)
        at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577)
        at java.lang.Thread.run(Thread.java:748)
     
     
  4. Do not do new DenyAccess() do ‘this’. For the plugin instance you need to reference it, eg Main.getInstance();
     
  5. If its in his main class, that shouldn't be necessary, "this" will suffice. It appears that he simply got the variables backward, and when it spit out a red line he just cast the class.
     
  6. Oh yea he’s not making his listener in his main class ;-;
     
  7. changed them around, still not working and heres the error
    Code (Text):
    :[17:16:57 ERROR]: Error occurred while disabling UPGamers v2.2 (Is it up to date?)java.lang.ClassCastException: me.thesilentpro.net.commands.Denyaccess cannot be cast to org.bukkit.plugin.Plugin
        at me.thesilentpro.net.Main.onDisable(Main.java:85)
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:266)
        at org.bukkit.plugin.java.JavaPluginLoader.disablePlugin(JavaPluginLoader.java:361)
        at org.bukkit.plugin.SimplePluginManager.disablePlugin(SimplePluginManager.java:422)
        at com.rylinaux.plugman.util.PluginUtil.unload(PluginUtil.java:419)
        at com.rylinaux.plugman.util.PluginUtil.reload(PluginUtil.java:379)
        at com.rylinaux.plugman.command.ReloadCommand.execute(ReloadCommand.java:122)
        at com.rylinaux.plugman.PlugManCommandHandler.onCommand(PlugManCommandHandler.java:95)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141)
        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(CraftServer.java:648)
        at me.totalfreedom.bukkittelnet.session.ClientSession$1.run(ClientSession.java:212)
        at org.bukkit.craftbukkit.v1_12_R1.scheduler.CraftTask.run(CraftTask.java:76)
        at org.bukkit.craftbukkit.v1_12_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:353)
        at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:739)
        at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406)
        at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679)
        at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577)
        at java.lang.Thread.run(Thread.java:748)
     
     
  8. Show your onEnable and onDisable?
     
  9. Its a class cast exception, you're still casting something you shouldn't be.
     
  10. Yea, it should be Bukkit.getServer().getPluginManager().registerEvents(new DenyAccess(), this); or the other way around, i can’t remember lmao
     
    • Like Like x 1
  11. Benz56

    Moderator Supporter

    It is the other way around.

    @UnlimitedNinjas remember simply casting won’t magically make your code work. If you’re not registering your Events in your main class (which is perfectly fine and preferred according to the Single Responsibility Principle) you should do as @JustRayz said; just the other way around.
     
  12. Code (Text):
        public static Main plugin;
       
        public void onEnable() {
            getConfig().options().copyDefaults(true);
            saveDefaultConfig();
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            plugin = this;
            getCommand("spawn").setExecutor(new Spawn());
            getCommand("setspawn").setExecutor(new Setspawn());
            getCommand("ip").setExecutor(new IP());
            getCommand("admininfo").setExecutor(new Admininfo());
            getCommand("discordinvite").setExecutor(new Discord());
            getCommand("site").setExecutor(new Site());
            getCommand("filter").setExecutor(new Itemfilter());
            getCommand("getid").setExecutor(new Getid());
            getCommand("cookie").setExecutor(new Cookie());
            getCommand("fakegtfo").setExecutor(new Fakegtfo());
            getCommand("fakedoom").setExecutor(new Fakedoom());
            getCommand("fakesmite").setExecutor(new Fakesmite());
            getCommand("changelog").setExecutor(new Changelog());
            getCommand("adminhelp").setExecutor(new Adminhelp());
            getCommand("adminmessage").setExecutor(new Adminmessage());
            getCommand("launch").setExecutor(new Launch());
            getCommand("nodrops").setExecutor(new Nodrops());
            getCommand("blind").setExecutor(new Blind());
            getCommand("denyaccess").setExecutor(new Denyaccess());
           
            Bukkit.getServer().getPluginManager().registerEvents(new Denyaccess(), this);
        }
       
        public void onDisable() {
            getLogger().info("UPGamers - Disabled!");
        }
     
  13. Benz56

    Moderator Supporter

    Code (Java):
    Bukkit.getServer().getPluginManager().registerEvents(new Denyaccess(), this);
    Should be:
    Code (Java):
    Bukkit.getServer().getPluginManager().registerEvents(this, new Denyaccess());
    Edit:
    First one is correct. My mistake.
     
    #13 Benz56, Jul 7, 2018
    Last edited: Jul 7, 2018
  14. Thats what it was like the first time....
    and i had to like add a ((Plugin)) thing
     
  15. Benz56

    Moderator Supporter

    You're right, sorry :)
    You should not cast it, however, you should implement Listener in the Denyaccess class.
     
  16. So just remove that from onenable but have listener in denyaccess class?
     
  17. Benz56

    Moderator Supporter

    If you don't have any Events in your main class then yes. You can implement Listener in any amount of classes you want as long as you register them.
     
  18. I have alot of events in my main class.
     
  19. Then don't.
     
    • Agree Agree x 1
  20. how do i fix this then? Can i make the event in the main class but i would need to get the arraylist and the player AND the target somehow?