1.12.2 PlayerJoinEvent help

Discussion in 'Spigot Plugin Development' started by Nico12, Jan 19, 2020.

  1. Hello everyone, I had a doubt and that is if doing a Bukkit Runnable every time a player enters the server that is, PlayerJoinEvent could cause a collapse.
    My Code:
    Code (Text):
    package animated;

    import java.io.File;
    import java.util.ArrayList;
    import java.util.List;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.FileConfiguration;
    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;
    import org.bukkit.scheduler.BukkitScheduler;
    import org.bukkit.scoreboard.DisplaySlot;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Score;
    import org.bukkit.scoreboard.Scoreboard;
    import org.bukkit.scoreboard.ScoreboardManager;

    public class Animated extends JavaPlugin implements Listener{
        @Override
        public void onEnable(){
            registerConfig();
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
           
        }

        public void registerConfig() {
            File config = new File(this.getDataFolder(), "config.yml");
            if(!config.exists()){
                getConfig().options().copyDefaults(true);
                saveConfig();
            }
        }
       
        public void ScoreboardAnimada(Player player){
            int ticks = this.getConfig().getInt("Ticks");
            List<String> list = getConfig().getStringList("Lista");
            final ScoreboardManager m = Bukkit.getScoreboardManager();
            final Scoreboard s = m.getNewScoreboard();
           
            Objective o = s.registerNewObjective("Sccoreboard", "dummy");
           
            o.setDisplaySlot(DisplaySlot.SIDEBAR);
           
            BukkitScheduler sc = Bukkit.getServer().getScheduler();
            sc.scheduleSyncRepeatingTask(this, new Runnable(){
                int i = 0;
                @Override
                public void run() {
                   
                    if(i < list.size()-1){
                        i++;
                        }else{
                        i=0;
                    }
                    o.setDisplayName(ChatColor.translateAlternateColorCodes('&', list.get(i)));
                }
               
            }, 0, ticks);
            Score s1 = o.getScore("Hola");
            s1.setScore(1);
            player.setScoreboard(s);
        }
        @EventHandler
        public void PlayerJoin(PlayerJoinEvent e){
            Player p = e.getPlayer();
            this.ScoreboardAnimada(p);
        }
    }
     
     
  2. why do you need to make 1 of everything for each player? why not just make 1 of everything and send that 1 to every player
     
  3. and how would he do that?
     
  4. Hey!

    I would suggest creating a one runnable and looping through a list of players with scoreboards and updating their scoreboards that way!

    So it would look something like this...
    Code (Java):
    public class ScoreboardUpdater extends BukkitRunnable {

        public ScoreboardUpdater() {
            this.getScheduler().scheduleSyncRepeatingTask(plugin, this, 0, ticks);
        }

        @Override
        public void run() {
            for (Player player : players) {
                // Do your scoreboard stuff here!
            }
        }
    }
    Note: You will have to insert the plugin, ticks, and players variable by yourself.
     
  5. take your scoreboard declarations out of the method. since you cant create them properly until the plugin enables, split the declaration into the onenable. after that, create one task that loops through all players and uses the same scoreboard objects for every player. you can give each player the scoreboard when they join. then just have the task edit the scores as needed.
     
  6. It has a lot of coherence what it says, but, I must call the method that contains the task and the scoreboard in the onEnable method of the main class of my plugin, but for that I would need to obtain Player to be able to declare it in the main, how would I do that?