PlayerJoinEvent not working??

Discussion in 'Plugin Development' started by CheesyFreezy, Jan 11, 2015.

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

    CheesyFreezy

    Hi developers,

    I made a plugin where you can ban people, the /ban command works fine. It saves the player's name and reason why he/she is banned to a file. I'm trying to make that if the player joins and he/she is banned than the player will be kicked again from the server. I don't want to use player.setBanned(true); because i can't set my own message.

    Code:
    Code:
        @EventHandler(priority = EventPriority.HIGH)
        public void onPlayerJoin(final PlayerLoginEvent event) {
            final Player player = event.getPlayer();
           
            File file = new File("plugins/GalacticEssentials/", "banned_players.yml");
            final YamlConfiguration fileConfig = YamlConfiguration.loadConfiguration(file);
           
            if(fileConfig.isSet(player.getName())) {
                event.disallow(PlayerLoginEvent.Result.KICK_OTHER, "Banned!! \n " + ChatColor.GRAY + "Reason" + ChatColor.DARK_GRAY + ": " + ChatColor.AQUA + message + " \n \n" + ChatColor.GRAY + "Unfairly banned? \n" + ChatColor.GRAY + "Skype" + ChatColor.DARK_GRAY + ": " + ChatColor.AQUA + "GalacticPvPServer");
            }
        }
     
  2. Offline

    Noahz123

    Make sure to register your Events and to make sure your class with any events extends Listener.
     
  3. Offline

    CheesyFreezy

    @Noahz123, i checked it and i did that, but it's still not working
     
  4. Offline

    Noahz123

    May I see your entire main plugin class and entire class that contains the event? Did you make sure the event wasn't running? (Add a print statement to the event to see if it is actually running).
     
  5. Offline

    ChipDev

    PlayerLoginEvent != PlayerJoinEvent
     
  6. Offline

    CheesyFreezy

    the command and event are in the main class:

    Code:
    Code:
    public class Core extends JavaPlugin implements Listener {  
    
    public void onEnable() {
            saveConfig();
          
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }  
    
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            String command = cmd.getName();      
                    if(command.equalsIgnoreCase("ban")) {
                if(sender instanceof Player) {
                    Player player = (Player)sender;
                  
                    if(player.hasPermission("core.ban") || player.isOp()) {
                        if(args.length == 0) {
                            player.sendMessage(prefix + ChatColor.AQUA + "Usage: /ban [player] [reason]");
                        } else if(args.length == 1) {
                            Player target = Bukkit.getServer().getPlayer(args[0]);
                          
                            if(target instanceof Player) {
                                if(target.isOnline()) {
                                    player.sendMessage(prefix + ChatColor.AQUA + "Usage: /ban " + ChatColor.AQUA + target.getName() + ChatColor.AQUA + " [reason]");
                                } else {
                                    player.sendMessage(prefix + ChatColor.AQUA + args[0] + ChatColor.GRAY + " does not exist or is not online!");
                                }
                            } else {
                                player.sendMessage(prefix + ChatColor.AQUA + args[0] + ChatColor.GRAY + " does not exist or is not online!");
                            }
                        } else if(args.length > 1) {
                            final Player target = Bukkit.getServer().getPlayer(args[0]);
                          
                            if(target instanceof Player) {
                                if(target.isOnline()) {
                                    File file = new File("plugins/GalacticEssentials/", "banned_players.yml");
                                    YamlConfiguration fileConfig = YamlConfiguration.loadConfiguration(file);
                                  
                                    if(!fileConfig.isSet(target.getName())) {
                                        for(int i=1;i<args.length;i++) {
                                            message = message + args[i] + " ";
                                        }
                                          
                                        Bukkit.broadcastMessage(prefix + ChatColor.AQUA + target.getName() + ChatColor.GRAY + " has been banned!" + ChatColor.AQUA + " Reason" + ChatColor.DARK_GRAY + ": " + ChatColor.GRAY + message);
                                        target.kickPlayer(ChatColor.AQUA + "Banned" + ChatColor.GRAY + "! \n" + ChatColor.GRAY + "Reason" + ChatColor.DARK_GRAY + ": " + ChatColor.AQUA + message + " \n \n" + ChatColor.GRAY + "Unfairly banned? \n" + ChatColor.GRAY + "Skype" + ChatColor.DARK_GRAY + ": " + ChatColor.AQUA + "GalacticPvPServer");
                                      
                                        fileConfig.set(target.getName(), ChatColor.translateAlternateColorCodes('&', message));
                                        try {
                                            fileConfig.save(file);
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        }
                                      
                                        message = "";
                                    } else {
                                        player.sendMessage(prefix + ChatColor.GRAY + "This player is already banned!");
                                    }
                                } else {
                                    player.sendMessage(prefix + ChatColor.AQUA + args[0] + ChatColor.GRAY + " does not exist or is not online!");
                                }
                            } else {
                                player.sendMessage(prefix + ChatColor.AQUA + args[0] + ChatColor.GRAY + " does not exist or is not online!");
                            }
                        }
                    } else {
                        player.sendMessage(prefix + ChatColor.GRAY + "You do not have access to that command!");
                    }
                } else {
                    logger.info("[Core] Only players can send this command!");
                }
            }
            if(command.equalsIgnoreCase("unban")) {
                if(sender instanceof Player) {
                    Player player = (Player)sender;
                  
                    if(player.hasPermission("core.unban") || player.isOp()) {
                        if(args.length == 0) {
                            player.sendMessage(prefix + ChatColor.AQUA + "Usage: /unban [player]");
                        } else if(args.length == 1) {
                            OfflinePlayer target = Bukkit.getServer().getOfflinePlayer(args[0]);
                      
                            File file = new File("plugins/GalacticEssentials/", "banned_players.yml");
                            YamlConfiguration fileConfig = YamlConfiguration.loadConfiguration(file);
                          
                            if(fileConfig.isSet(target.getName())) {
                                player.sendMessage(prefix + ChatColor.AQUA + target.getName() + ChatColor.GRAY + " has been unbanned!");
                              
                                fileConfig.set(target.getName(), null);
                                try {
                                    fileConfig.save(file);
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            } else {
                                player.sendMessage(prefix + ChatColor.GRAY + target.getName() + " was never banned!");
                            }
                        } else {
                            player.sendMessage(prefix + ChatColor.GRAY + "Too many arguments!");
                        }
                    } else {
                        player.sendMessage(prefix + ChatColor.GRAY + "You do not have access to that command!");
                    }
                } else if(sender instanceof ConsoleCommandSender) {
                    if(args.length == 0) {
                        logger.info(prefix + "Usage: /unban [player]");
                    } else if(args.length == 1) {
                        OfflinePlayer target = Bukkit.getServer().getOfflinePlayer(args[0]);
                      
                        File file = new File("plugins/GalacticEssentials/", "banned_players.yml");
                        YamlConfiguration fileConfig = YamlConfiguration.loadConfiguration(file);
                      
                        if(fileConfig.isSet(target.getName())) {
                            logger.info(prefix + ChatColor.AQUA + target.getName() + ChatColor.GRAY + " has been unbanned!");
                          
                            fileConfig.set(target.getName(), null);
                            try {
                                fileConfig.save(file);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        } else {
                            logger.info(prefix + ChatColor.AQUA + args[0] + ChatColor.GRAY + " does not exist or is not online!");
                        }
                    } else {
                        logger.info(prefix + ChatColor.GRAY + "Too many arguments!");
                    }
                } else {
                    return true;
                }
            }
    
        @EventHandler(priority = EventPriority.HIGH)
        public void onPlayerJoin(final PlayerLoginEvent event) {
            final Player player = event.getPlayer();
          
            File file = new File("plugins/GalacticEssentials/", "banned_players.yml");
            final YamlConfiguration fileConfig = YamlConfiguration.loadConfiguration(file);
          
            if(fileConfig.isSet(player.getName())) {
                event.disallow(PlayerLoginEvent.Result.KICK_OTHER, "Banned!! \n " + ChatColor.GRAY + "Reason" + ChatColor.DARK_GRAY + ": " + ChatColor.AQUA + message + " \n \n \n \n \n \n \n" + ChatColor.GRAY + "Unfairly banned? \n" + ChatColor.GRAY + "Skype" + ChatColor.DARK_GRAY + ": " + ChatColor.AQUA + "GalacticPvPServer");
            }
    }
        }
     
  7. Offline

    SuperOriginal

    Why are you creating several File and YamlConfiguration objects? You only need one.
     
  8. Offline

    CheesyFreezy

    @SuperOriginal , thank you for responding but i don't know what you mean by that :/
     
  9. Offline

    Noahz123

    So is the problem that your ban plugin isn't banning people, or that the PlayerLoginEvent is not being called?
     
  10. Offline

    SuperOriginal

    @CheesyFreezy You instantiate a File and YamlConfiguration object (new file(...)) every time you need it. You only need to do it once, then you can reference it when you need it instead of creating a new one.

    As for your main problem, you need to elaborate on "not working"
     
  11. Offline

    CheesyFreezy

  12. Offline

    SuperOriginal

    @CheesyFreezy define the File and YamlConfiguration variables at the top of your class (do not assign values), then in your onEnable(), assign the correct values. Then when you need the config object, you can just access the object already created instead of making a new one.

    Code:java
    1.  
    2. public class MyClass extends JavaPlugin{
    3.  
    4. private File file;
    5. private YamlConfiguration config;
    6.  
    7. @Override
    8. public void onEnable(){
    9. file = new File(...);
    10. config = YamlConfiguration.loadConfiguration(...);
    11. }
    12.  
    13. public void exampleMethodToAccessConfig(){
    14. config.set(...); //This is an example of how you'd access the file. You can do this anywhere in your class
    15. }
    16. }
     
  13. Offline

    teej107

  14. Offline

    CheesyFreezy

    @SuperOriginal, thank you, hopefully this will work
    @teej107, i'm not using setBanned() because than i can't set my own ban message (the message that shows when you join the server while you're banned)
     
  15. Offline

    teej107

    @CheesyFreezy Well take a look at the link I posted. It gives an alternative to use which I think you will like.
     
Thread Status:
Not open for further replies.

Share This Page