Solved Help with OnPlayerJoin Event

Discussion in 'Spigot Plugin Development' started by Yoreni, May 2, 2017.

  1. I try to do the OnPlayerJoin Event but its just not triggering heres my code
    Code (Text):

        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event)
        {
            Player player = event.getPlayer();
                if(timer > 0)
                {
                    player.sendMessage("Sample Text");
                    clock.addPlayer(player);
                    clock.show();
                }
        }
     
     
    • Like Like x 1
  2. Register the listener in your Main class
     
    • Informative Informative x 1
  3. I have added the event to my main class
     
  4. Did you implement Listener?
    Can we see your main class?
    Is it the only event which doesn't work?
     
    • Informative Informative x 1
  5. I haven't implemented listener
    Its my only event
    here is is my whole class
    Code (Text):

    package yoreni.test.main;

    import java.util.Collection;

    import org.bukkit.Bukkit;
    import org.bukkit.boss.BarColor;
    import org.bukkit.boss.BarStyle;
    import org.bukkit.boss.BossBar;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    import yoreni.FormatterAPI.main.FormatterAPI;



    public class main extends JavaPlugin
    {
        int timer = 0;
        boolean paused = false;
        boolean help = true;
        BossBar clock = Bukkit.createBossBar(formatTime(timer), BarColor.GREEN, BarStyle.SOLID);

        public String formatTime(int seconds)
        {
            String output = "";
            if (seconds > 3599 ) output ="Timer: " +  (int )Math.floor(seconds / 3600) + "h " + (int )Math.floor(seconds / 60) % 60 + "m ";
            else if (seconds > 59) output ="Timer: " +  (int )Math.floor(seconds / 60) + "m " + seconds % 60 + "s";
            else output = "Timer: " + seconds + "s ";                      
            return output;      
        }
        public void onEnable()
        {
        }

        public void onDisable()
        {
        }

        @SuppressWarnings("deprecation")
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event)
        {
            Player player = event.getPlayer();
                if(timer > 0)
                {
                    player.sendMessage("Sample Text");
                    clock.addPlayer(player);
                    clock.show();
                }
        }



        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
        {
            help = true;
            Player player = (Player)sender;
            if (((sender instanceof Player)) && (cmd.getLabel().equalsIgnoreCase("timer")))
            {
                if(args[0].equalsIgnoreCase("set"))
                {
                    try
                    {
                        if(Integer.valueOf(args[1]).intValue() > 0)
                        {
                            Bukkit.getScheduler().cancelAllTasks();
                            help = false;
                            timer = Integer.valueOf(args[1]).intValue();
                            int startTime = timer;
                            clock.hide();
                            clock.setProgress(1);
                            //clock.addPlayer((Player) Bukkit.getServer().getOnlinePlayers());
                            Collection<? extends Player> players = Bukkit.getServer().getOnlinePlayers();
                            for(Player playerName : players)
                            {
                                clock.addPlayer(playerName);

                            }
                            clock.show();
                            Bukkit.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable()
                            {
                                public void run()
                                {
                                    if(paused == false)
                                    {
                                        clock.setTitle(formatTime(timer));
                                        if(clock.getProgress() > 0.666) clock.setColor(BarColor.GREEN);
                                        else if(clock.getProgress() > 0.333) clock.setColor(BarColor.YELLOW);
                                        else clock.setColor(BarColor.RED);
                                        if (timer > -1)
                                        {              
     
                                            if (timer == 0)
                                            {
                                                player.sendMessage("Time's Up");
                                                Bukkit.getScheduler().cancelAllTasks();
                                                player.performCommand("gamemode sp");
                                                clock.hide();
                                            }          
                                            timer -= 1;
                                            clock.setProgress(((double) (timer + 1) / startTime));
                                        }
                                    }
                                }
                            }, 0L, 20L);
                        }
                        else player.sendMessage("Please type in a number above 0");
                    }
                    catch(NumberFormatException NumberFormatException)
                    {
                        player.sendMessage("Please type in a number");
                    }
                }
                if(timer > 0)
                {
                    if(args[0].equalsIgnoreCase("pause"))
                    {
                        paused = true;
                        player.sendMessage("You have paused the timer.");
                        clock.setTitle(formatTime(timer) + " *Paused*");
                        help = false;
                    }
                    else if(args[0].equalsIgnoreCase("play"))
                    {
                        paused = false;
                        player.sendMessage("You have unpaused the timer.");
                        clock.setTitle(formatTime(timer));
                        help = false;
                    }
                    else if(args[0].equalsIgnoreCase("cancel"))
                    {
                        player.sendMessage("You have canceled the timer.");
                        clock.hide();
                        Bukkit.getScheduler().cancelAllTasks();
                        timer = -1;
                        help = false;
                    }
                }
                else player.sendMessage("There is no timer to pause or unpause.");
                if(help == true)
                {
                    player.sendMessage("==============[Timer Help]==============");
                    player.sendMessage("/timer set <seconds> - Sets the timer");
                    player.sendMessage("/timer pause - Pauses the timer");
                    player.sendMessage("/timer play - Unpauses the timer");
                    player.sendMessage("/timer cancel - Cancels the timer");
                }
            }
            return false;
        }
    }
     
     
    • Informative Informative x 1
  6. I dont rely on spoon feeding i only ask spigot when iam stuck ive looked at other people codes and looked almost everywhere on the internet yet it works for them but not for me.
     
    • Like Like x 1
  7. Dude, register the listener *sigh*
    Code (Text):
    getServer().getPluginManager().registerEvents(this,this);
    And then implement listener in that class. Otherwise, the event will not be caught by your listener.
     
    • Informative Informative x 2
  8. To do plugins you need to have at least a basic knowledge in Java and have a general idea of Bukkit/Spigot API. They have been telling you the answer! All you have to do is call PluginManager and register your events in onEnable. If you don't know what that means then go learn more about the API.
     
  9. I do know basic knowlege of Spigot and java i could not find a tutorlal on it that very clear iam still learning that why i made this thread

    EDIT:anyways iam going to try what Yugikid said
     
  10. Calling events is basically the first thing you learn with the API, I don't want to argue but you need to read up on the API more.
     
  11. iam learning how to use the API it just not very clear i do try to look things up before i ask people but if its not clear and doent work for me i do ask people anyways thank you Yugkid1 for helping me and other for trying
     
    • Optimistic Optimistic x 1
  12. I feel like there aren't enough good plugin youtubers ☹️, and when their is they usally don't do them anymore or stopped midway through the series.

    EDIT: this is a bit off topic, also if you solved your issue fix your title
     
    • Like Like x 1