Help: Scoreboard Timer

Discussion in 'Plugin Development' started by Hugi21, May 24, 2015.

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

    Hugi21

    Code:
    package fr.hugi21.uhc;
    
    
    
    
    import org.bukkit.Bukkit;
    
    
    import org.bukkit.entity.Player;
    import org.bukkit.scoreboard.DisplaySlot;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Score;
    import org.bukkit.scoreboard.Scoreboard;
    
    
    
    
    
    
    public class SB extends Thread {
        private boolean timer = true;
        private int minutes = 00;
        private int heures = 00;
        private int secondes = 00;
         private Scoreboard scoreboard;
            private Objective objective;
    
        private uhc c;
       
    
        @SuppressWarnings("deprecation")
        public Scoreboard Time(){
           
           
       
               
               
               
                while(timer == true){
                   
                    try{
                        sleep(1000);
                        if (secondes >= 60){
                          secondes = 00 ;
                            minutes ++;
                       } if (minutes >= 60){
                            minutes = 00;
                           heures ++;
                        
                    }}
                    catch (InterruptedException ex){
          
        
                    } secondes ++;
                     scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
               
                   
                     objective  = scoreboard.registerNewObjective("timer", "dummy");
                     objective.setDisplaySlot(DisplaySlot.SIDEBAR);
                     objective.setDisplayName(c.getConfig().getString("ScoreboardName"));
                    Score timer = objective.getScore(Bukkit.getOfflinePlayer(heures + ":" + minutes + ":" + secondes ));
                     timer.setScore(3);
                     Score player = objective.getScore(Bukkit.getOfflinePlayer("Player    " + Bukkit.getOnlinePlayers().size()));
                    player.setScore(2);
                    for (Player p : Bukkit.getOnlinePlayers()){
                        p.setScoreboard(scoreboard);
                       
                    }
           
                   
       
                       
                    }
                       
                return scoreboard;}
                   
               
               
           
       
    
    
        public void SeeScoreboard(Player p) {
                    p.setScoreboard(scoreboard);
                }
           
           
        }
       
       
    
    
    
    
    
    
    Hi.I would like to make a timer in a Scoreboard
    except that when I load the plugin on my server the scoreboard does s' not displayed here are my codes I hope you can help me
    Code:
    package fr.hugi21.uhc;
    
    import java.io.File;
    
    import java.util.Iterator;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.event.Listener;
    import org.bukkit.inventory.Recipe;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import fr.hugi21.events.PlayerDead;
    import fr.hugi21.uhc.SB;
    
    public class uhc extends JavaPlugin implements Listener{
        public File config;
     
        public void onEnable(){
      SB timer = new SB();
        timer.start();
         
            loadConfig();
            Bukkit.getPluginManager().registerEvents(new PlayerDead(), this);
    
            if(getConfig().getBoolean("Effect.Strenght") == false){
                remove(Material.BLAZE_POWDER);
            if(!(new File(getDataFolder(),"config.yml")).exists()){
                saveDefaultConfig();
        }   System.out.println("\n");
            System.out.println("+-----------------------------------+");
            System.out.println("| UltraHardCore       Hugi21 V.01   |");
            System.out.println("|                                   |");
            System.out.println("|        All rights reserved        |");
            System.out.println("|             Hugi21                |");
            System.out.println("+-----------------------------------+");
            System.out.println("Chargement                    Loading");
            System.out.println("\n");
            }
        }
     
        @SuppressWarnings("deprecation")
        public void onDisable()
     
       { SB timer = new SB();
       timer.stop();
     
        System.out.println("\n");
        System.out.println("+-----------------------------------+");
        System.out.println("| UltraHardCore       Hugi21 V.01   |");
        System.out.println("|                                   |");
        System.out.println("|        All rights reserved        |");
        System.out.println("|             Hugi21                |");
        System.out.println("+-----------------------------------+");
        System.out.println("Désactivation                Unloaded");
        System.out.println("\n");
    Bukkit.getServer().resetRecipes();
    
    }public void loadConfig() {
        getConfig().options().copyDefaults(true);
        saveConfig();
     
    }  public static void remove(Material m) {
     
        Iterator<Recipe> it = Bukkit.getServer().recipeIterator();
        Recipe recipe;
        while(it.hasNext())
        {
            recipe = it.next();
            if (recipe != null && recipe.getResult().getType() == m)
            {
                it.remove();
             
            }
        }
    }
    }
    
    Thank and sorry for my English I am French
     
    Last edited: May 24, 2015
  2. Offline

    mine-care

    Please follow naming conventions
    This can be turned to while(timer){...} it is more efficient
    You don't need disable/enable messages, bukkit has them but if you insist, you should prefer using Minecraft Logger. not System.out.println();
    This does not stop the timmer, you simply create a new object and call a method doing nothing..

    The issue that practically blocks the scoreboard being set is that you are NOT calling the Time method to start it all up, You simply call a method from Thread superclass that has no effect on your object, and then onDisable you call stop method that again does nothing to your thread. Also having an infinite loop (oh well sub-infinite) running on the same thread as bukkit will freeze everything and may cause chunk corruption, Prefer to use BukkitRunnables instead, they are a lot easyer and WAY safer.

    A few references that may help you:
    http://www.oracle.com/technetwork/java/codeconventions-135099.html
    https://docs.oracle.com/javase/tutorial/java/javaOO/
    http://wiki.bukkit.org/Scheduler_Programming
    http://wiki.bukkit.org/Plugin_Tutorial#Logging_a_message
    https://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html

    Hope that helped! :D
     
    KingFaris11 likes this.
Thread Status:
Not open for further replies.

Share This Page