Plugin not working and no errors?

Discussion in 'Spigot Plugin Development' started by ZevoGaems, May 6, 2017.

  1. I have a punishment plugin and the ban doesn't currently work and all it does is kick them and it doesn't add them to the config. Can someone please help me I don't understand why its not working.
    Code (Text):
    package pw.LoopTurn.punishment;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerLoginEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    public class main extends JavaPlugin implements Listener {
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
       
        @EventHandler
        public void onPlayerLogin(PlayerLoginEvent e) {
            Player p = e.getPlayer();
            if(getConfig().getString("banned." + p.getUniqueId()) == null) {
                e.allow();
            } else {
                String reason2 = getConfig().getString("banned." + p.getUniqueId() + ".issuer");
                String issuer2 = getConfig().getString("banned." + p.getUniqueId() + ".issuer");
                p.kickPlayer(ChatColor.GRAY + "Kicked whilst connecting: you are currently banned" + "\n"
                        + ChatColor.RED + "Banned by, " + issuer2 + "\n"
                            + ChatColor.RED + "Reason, " + reason2);
            }
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("kick")) {
                if(!sender.hasPermission("minegrid.kick")) {
                    sender.sendMessage(ChatColor.RED + "Requested Permission Level \n TRAINEE");
                }
                if(sender.hasPermission("minegrid.kick")) {
                    if(!(args.length > 1)) {
                        sender.sendMessage(ChatColor.RED + "Usgae, /kick (player) (reason)");
                    }
                    if(args.length > 1) {
                        String PName = args[0];
                        final Player target = Bukkit.getPlayerExact(PName);
                        if(target == null) {
                            sender.sendMessage(ChatColor.RED + "Error, player couldn't be identified");
                        }
                        if(target != null) {
                            StringBuilder message = new StringBuilder();

                            for (int i = 1; i < args.length; i++) {
                                message.append(args[i] + " ");
                            }
                               
                                target.kickPlayer(ChatColor.RED + "You have been kicked from the server" + "\n"
                                        + ChatColor.RED + "Kicked By: " + sender.getName() + "\n"
                                            + ChatColor.RED + "Reason: " + message);
                               
                                sender.sendMessage(ChatColor.RED + "You have kicked, " + target.getName() + " for, " + message);
                    }
                }
            }
        }    else if(cmd.getName().equalsIgnoreCase("ban")) {
            if(!sender.hasPermission("minegrid.ban")) {
                sender.sendMessage(ChatColor.RED + "Requested Permission Level \n MODERATOR");
            }
            if(sender.hasPermission("minegrid.ban")) {
                if(!(args.length > 1)) {
                    sender.sendMessage(ChatColor.RED + "Usgae, /ban (player) (reason)");
                }
                if(args.length > 1) {
                    String PName1 = args[0];
                    final Player target1 = Bukkit.getPlayerExact(PName1);
                    if(target1 == null) {
                        sender.sendMessage(ChatColor.RED + "Error, player couldn't be identified");
                    }
                    if(target1 != null) {
                        StringBuilder message1 = new StringBuilder();

                        for (int i = 1; i < args.length; i++) {
                            message1.append(args[i] + " ");
                        }
                           
                            target1.kickPlayer(ChatColor.WHITE + "Internal Exception: java.io.IOException: An existing connection was forcibly closed by remote host");
                            getConfig().set("banned." + target1.getUniqueId() + ".reason", message1);
                            getConfig().set("banned." + target1.getUniqueId() + ".issuer", sender.getName());
                            saveConfig(); reloadConfig();
                            sender.sendMessage(ChatColor.RED + "You have permanently banned, " + target1.getName() + " for, " + message1);
                }
            }
        }
    }
            return true;
    }
    }
     
  2. You never really set anything banned right?
     
  3. I did try to ban a player but they could just join back if that is what you mean.
     
  4. Try listening to PlayerJoinEvent instead of PlayerLoginEvent...
     
  5. That didn't work D:
     
  6. Why you use cmd.getName().equalsIgnoreCase("kick") not using cmdlable.equals("kick")? that only work on console?
     
  7. -What is up with the naming conventions...?
    -Did you register the command and listener?
    -I believe you are missing a bunch of return statements.
     
    • Agree Agree x 1
  8. Hurmm, I mean you never ban the player in your code
     
  9. I understand what youre trying to accomplish with this plugin I believe, a custom banning and kicking etc.. plugin right? If so, there are many others already made that are efficient and provide loads of nice features. For example, LiteBans, has a nice web feature in it as well as all the features you could want. Also, as a side note, if you are trying to kick a player when they login, use PlayerPreLoginEvent, this gets executed before the player logs in... self explanatory. This stops the annoying 'Player has joined the server' 'Player has left the server', as kicking them in PlayerJoinEvent would do that. ;p
     
  10. Along with that, there are tons of slight issues and errors with this code that can be fixed, such as return statements, using getPlayerExact when you enter in a player (not recommended in this case), plus a few other slight issues...
     
  11. I didn't see yet anybody saying that the event he shall use is the AsyncPlayerPreLoginEvent... Use it https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/AsyncPlayerPreLoginEvent.html
     
  12. expires - date for the ban's expiration (unban), or null to imply forever, it does support tempbans.
     
  13. It's AsyncPlayerPreLoginEvent (it's required to be Async because 2 players can't join at the same time to avoid server confussion)
     
  14. I tried changin it to AsyncPlayerPreLoginEvent. It didn't work. I don't have anything in the template for the config. Here is my code that I currently have.
    Code (Text):
    package pw.LoopTurn.punishment;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    public class main extends JavaPlugin implements Listener {
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
       
        @EventHandler
        public void AsyncPlayerPreLoginEvent(PlayerJoinEvent e) {
            Player p = e.getPlayer();
            if(getConfig().getString("banned." + p.getUniqueId()) == null) {
                p.sendMessage("");
            } else {
                String reason2 = getConfig().getString("banned." + p.getUniqueId() + ".issuer");
                String issuer2 = getConfig().getString("banned." + p.getUniqueId() + ".issuer");
                p.kickPlayer(ChatColor.GRAY + "Kicked whilst connecting: you are currently banned" + "\n"
                        + ChatColor.RED + "Banned by, " + issuer2 + "\n"
                            + ChatColor.RED + "Reason, " + reason2);
            }
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("kick")) {
                if(!sender.hasPermission("minegrid.kick")) {
                    sender.sendMessage(ChatColor.RED + "Requested Permission Level \n TRAINEE");
                }
                if(sender.hasPermission("minegrid.kick")) {
                    if(!(args.length > 1)) {
                        sender.sendMessage(ChatColor.RED + "Usgae, /kick (player) (reason)");
                    }
                    if(args.length > 1) {
                        String PName = args[0];
                        final Player target = Bukkit.getPlayerExact(PName);
                        if(target == null) {
                            sender.sendMessage(ChatColor.RED + "Error, player couldn't be identified");
                        }
                        if(target != null) {
                            StringBuilder message = new StringBuilder();

                            for (int i = 1; i < args.length; i++) {
                                message.append(args[i] + " ");
                            }
                               
                                target.kickPlayer(ChatColor.RED + "You have been kicked from the server" + "\n"
                                        + ChatColor.RED + "Kicked By: " + sender.getName() + "\n"
                                            + ChatColor.RED + "Reason: " + message);
                               
                                sender.sendMessage(ChatColor.RED + "You have kicked, " + target.getName() + " for, " + message);
                    }
                }
            }
        }    else if(cmd.getName().equalsIgnoreCase("ban")) {
            if(!sender.hasPermission("minegrid.ban")) {
                sender.sendMessage(ChatColor.RED + "Requested Permission Level \n MODERATOR");
            }
            if(sender.hasPermission("minegrid.ban")) {
                if(!(args.length > 1)) {
                    sender.sendMessage(ChatColor.RED + "Usgae, /ban (player) (reason)");
                }
                if(args.length > 1) {
                    String PName1 = args[0];
                    final Player target1 = Bukkit.getPlayerExact(PName1);
                    if(target1 == null) {
                        sender.sendMessage(ChatColor.RED + "Error, player couldn't be identified");
                    }
                    if(target1 != null) {
                        StringBuilder message1 = new StringBuilder();

                        for (int i = 1; i < args.length; i++) {
                            message1.append(args[i] + " ");
                        }
                           
                            target1.kickPlayer(ChatColor.WHITE + "Internal Exception: java.io.IOException: An existing connection was forcibly closed by remote host");
                            getConfig().set("banned." + target1.getUniqueId() + ".reason", message1);
                            getConfig().set("banned." + target1.getUniqueId() + ".issuer", sender.getName());
                            saveConfig(); reloadConfig();
                            sender.sendMessage(ChatColor.RED + "You have permanently banned, " + target1.getName() + " for, " + message1);
                }
            }
        }
    }
            return true;
    }
    }
     
  15. That's really bad, you are not creating an event xD
    @EventHandler
    public void AsyncPlayerPreLoginEvent(PlayerJoinEvent e) {

    What you have to change is the PlayerJoinEvent. Here's an example:

    @EventHandler
    public void loginOnBanned(final AsyncPlayerPreLoginEvent e) {
     
  16. So something like this?
    Code (Text):
        @EventHandler
        public void LogOnBanned(final AsyncPlayerPreLoginEvent e) {
            Player p = ((Player) e).getPlayer();
            if(getConfig().getString("banned." + p.getUniqueId()) == null) {
                p.sendMessage("");
            } else {
                String reason2 = getConfig().getString("banned." + p.getUniqueId() + ".issuer");
                String issuer2 = getConfig().getString("banned." + p.getUniqueId() + ".issuer");
                p.kickPlayer(ChatColor.GRAY + "Kicked whilst connecting: you are currently banned" + "\n"
                        + ChatColor.RED + "Banned by, " + issuer2 + "\n"
                            + ChatColor.RED + "Reason, " + reason2);
            }
        }
     
  17. Player p = ((Player) e).getPlayer();
    Only players can login, irrelevant casting

    p.sendMessage("");
    This method doesn't work due to he didn't login yet, irrelevant and much better to use a ! before the statement

    String reason2 = getConfig().getString("banned." + p.getUniqueId() + ".issuer");
    String issuer2 = getConfig().getString("banned." + p.getUniqueId() + ".issuer");
    Define twice the same content, irrelevant

    And you need the e.setLoginResult() method