Solved Updating Scoreboard

Discussion in 'Plugin Help/Development/Requests' started by bubblefat_, May 29, 2015.

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

    bubblefat_

    I want to make it so that my scoreboard will update every 10 seconds.

    Code:
    package me.bubblefat.JoinQuit;
    
    import java.lang.reflect.Field;
    import java.util.ArrayList;
    import java.util.List;
    
    import me.bubblefat.Main;
    import net.minecraft.server.v1_8_R1.ChatSerializer;
    import net.minecraft.server.v1_8_R1.IChatBaseComponent;
    import net.minecraft.server.v1_8_R1.PacketPlayOutPlayerListHeaderFooter;
    import net.minecraft.server.v1_8_R1.PlayerConnection;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    import org.bukkit.Material;
    import org.bukkit.craftbukkit.v1_8_R1.entity.CraftPlayer;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.potion.PotionEffect;
    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 Messages implements Listener {
       
    
        public Messages(Main plugin) {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);   
        }
       
        @EventHandler(priority=EventPriority.HIGHEST)
        public void onPlayerJoin(PlayerJoinEvent event) {
            Player player = event.getPlayer();
            CraftPlayer p = (CraftPlayer) player;
           
           
            player.getInventory().clear();
           
            if(player.hasPermission("ct.staffjoin")) {
            event.setJoinMessage(ChatColor.GOLD + "Join> " + ChatColor.GRAY + player.getName() + " has joined Creative Times swiftly and silently.");
            
            player.sendMessage(ChatColor.GOLD + "§l––––––––––––––––––––––––––––––––");
             player.sendMessage(ChatColor.GRAY + "Welcome " + ChatColor.GOLD + player.getName() + " §7to Creative Times!");
             player.sendMessage(ChatColor.GRAY + "We truly appriciate your work here :)");
             player.sendMessage(ChatColor.GRAY + "To chat with other staff members do" + ChatColor.RED + " /admin <message> " + ChatColor.GRAY + "or" + ChatColor.RED + " /a " + ChatColor.GRAY + "for short!");
             player.sendMessage("");
             player.sendMessage(ChatColor.GRAY + "The server is currently in" + ChatColor.GREEN + " Alpha version 0.0.3" + ChatColor.GRAY + ", report any bugs to QA");
             player.sendMessage(ChatColor.GOLD + "§lGAMES:");
             player.sendMessage(ChatColor.GRAY + "Kit PvP" + ChatColor.DARK_GRAY + "(Click Me)");
             player.sendMessage(ChatColor.GOLD + "§l––––––––––––––––––––––––––––––––");
           
           
            } else {
                player.getInventory().clear();
                event.setJoinMessage(ChatColor.GOLD + "Join> " + ChatColor.GRAY + player.getName() + " has joined Creative Times");
                player.setGameMode(GameMode.SURVIVAL);
               
                 player.sendMessage(ChatColor.GOLD + "§l––––––––––––––––––––––––––––––––");
                 player.sendMessage(ChatColor.GRAY + "Welcome " + ChatColor.GOLD + player.getName() + " §7to Creative Times!");
                 player.sendMessage("");
                 player.sendMessage(ChatColor.GRAY + "The server is currently in" + ChatColor.GREEN + " Alpha version 0.0.3" + ChatColor.GRAY + ", report any bugs to QA");
                 player.sendMessage(ChatColor.GOLD + "§lGAMES:");
                 player.sendMessage(ChatColor.GRAY + "Kit PvP" + ChatColor.DARK_GRAY + "(Click Me)");
                 player.sendMessage(ChatColor.GOLD + "§l––––––––––––––––––––––––––––––––");
               
            }
    
            if (p.hasPlayedBefore() == false) {
                event.setJoinMessage(ChatColor.GOLD + "Join> " + ChatColor.GRAY + player.getName() + " has joined Creative Times for the first time!");
               
                 player.sendMessage(ChatColor.GOLD + "§l––––––––––––––––––––––––––––––––");
                 player.sendMessage(ChatColor.GRAY + "Welcome " + ChatColor.GOLD + player.getName() + " §7to Creative Times!");
                 player.sendMessage("");
                 player.sendMessage(ChatColor.GRAY + "The server is currently in" + ChatColor.GREEN + " Alpha version 0.0.3" + ChatColor.GRAY + ", report any bugs to QA");
                 player.sendMessage(ChatColor.GOLD + "§lGAMES:");
                 player.sendMessage(ChatColor.GRAY + "Kit PvP" + ChatColor.DARK_GRAY + "(Click Me)");
                 player.sendMessage(ChatColor.GOLD + "§l––––––––––––––––––––––––––––––––");
               
            }
           
            PlayerConnection connection = p.getHandle().playerConnection;
            IChatBaseComponent tabTitle = ChatSerializer.a("{text:'§6§lCreative Times'}");
            IChatBaseComponent tabFoot = ChatSerializer.a("{text:'Go to §c§nwww.creativetimesmc.com§r for Forums, Shop, and more!'}");
            PacketPlayOutPlayerListHeaderFooter headerPacket = new PacketPlayOutPlayerListHeaderFooter(tabTitle);
           
           
            try {
               
                Field field = headerPacket.getClass().getDeclaredField("b");
                field.setAccessible(true);
                field.set(headerPacket, tabFoot);
               
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                connection.sendPacket(headerPacket);
            }
           
             Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "title " + p.getName() + " subtitle {text:'Alpha 0.0.3'}");
             Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "title " + p.getName() + " times 50 100 60");
             Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "title " + p.getName() + " title {text:'Creative Times',color:'gold',bold:'true'}");
    
            
             ItemStack item = new ItemStack(Material.NETHER_STAR, 1);
             ItemMeta meta = item.getItemMeta();
             meta.setDisplayName(ChatColor.GOLD + "Kit Selector §7(Right Click)");
             List<String> lore = new ArrayList<String>();
             lore.add("§7Right Click this item to select your kit!");
             meta.setLore(lore);
             item.setItemMeta(meta);
             player.getInventory().addItem(item);
            
            
             for(PotionEffect effect : player.getActivePotionEffects())
             {
                 player.removePotionEffect(effect.getType());
             }
                
             Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "minecraft:tp " + player.getName() + " 718.2053 206.0000 -693.0194");
            
             for(Player all : Bukkit.getServer().getOnlinePlayers()) {
             setupScoreboard(all);
             }
            
    
             if(!Main.config.contains(player.getName())){
                Main.config.set(player.getName() + ".Currency", 250);
                Main.saveFile();
            }
            
        }
       
       
       
    //        SCORE BOARD   
       
       
    
        @SuppressWarnings("deprecation")
        private void setupScoreboard(Player player) {
            ScoreboardManager sm = Bukkit.getScoreboardManager();
            Scoreboard sb = sm.getNewScoreboard();
            Objective obj = sb.registerNewObjective("dash", "dummy");
           
            obj.setDisplaySlot(DisplaySlot.SIDEBAR);
            obj.setDisplayName(ChatColor.GOLD + "§lCreative Times");
           
            Score space = null;
            Score space2 = null;
            Score space3 = null;
           
            Score online = null;
            Score online2 = null;
           
            Score rank = null;
            Score rank2 = null;
           
            Score currency = null;
            Score currency2 = null;
           
           
            try {
               
                space = obj.getScore(ChatColor.BLACK + "  ");
                space.setScore(9);
               
                online = obj.getScore(ChatColor.BLUE + "§lOnline");
                online.setScore(8);
               
                online2 = obj.getScore(Bukkit.getOnlinePlayers().length + "/100");
                online2.setScore(7);
               
                space2 = obj.getScore(ChatColor.BLACK + "  ");
                space2.setScore(6);
               
                rank = obj.getScore(ChatColor.RED + "§lRank");
                rank.setScore(5);
               
                if(player.isOp()) {
                rank2 = obj.getScore("Owner");
                rank2.setScore(4);
                } else if(player.hasPermission("ct.admin") && !player.isOp()){
                    rank2 = obj.getScore("Admin");
                    rank2.setScore(4);
                }else if(player.hasPermission("ct.mod") && !player.isOp()){
                    rank2 = obj.getScore("Mod");
                    rank2.setScore(4);
                }else if(player.hasPermission("ct.helper") && !player.isOp()){
                    rank2 = obj.getScore("Helper");
                    rank2.setScore(4);
                }else if(player.hasPermission("ct.builder") && !player.isOp()){
                    rank2 = obj.getScore("Builder");
                    rank2.setScore(4);
                }else {
                    rank2 = obj.getScore("Player");
                    rank2.setScore(4);
                }
               
                space3 = obj.getScore(ChatColor.BLACK + " ");
                space3.setScore(3);
               
                currency = obj.getScore(ChatColor.GREEN + "§lCurrency");
                currency.setScore(2);
               
                currency2 = obj.getScore(Main.config.getString(player.getName() + ".Currency"));
                currency2.setScore(1);
               
                player.setScoreboard(sb);
               
            } catch (Exception ex) {
                System.out.println(ex);
            }
           
        }
       
       
       
    //         QUIT MESSAGES   
    
        @EventHandler
        public void onPlayerQuit(PlayerQuitEvent event) {
            Player player = event.getPlayer();
            event.setQuitMessage(ChatColor.GOLD + "Quit> " + ChatColor.GRAY + player.getName() + " has left Creative Times");
           
            for(Player all : Bukkit.getServer().getOnlinePlayers()) {
                 setupScoreboard(all);
                 }
        }
    
    }
    
     
  2. Offline

    Zombie_Striker

    @bubblefat_
    I have no idea why you would even post your code, since you do not have anything that can try to update it. Even if it did, we would not need to see the entire class if you are just asking how you would do that.

    Create a schedular and create a repeating task. Set the wait value to 10*20L (20L is one second), and in the public void run(), update your scoreboard instance.
     
    ElliottOlson likes this.
  3. Offline

    bwfcwalshy Retired Staff

    Moved to Bukkit Alternates.
     
  4. Offline

    mine-care

    Please don't compare boo leans with == there is no need, you can do if(Boolean){} and use ! To check for false
     
Thread Status:
Not open for further replies.

Share This Page