Scoreboard timer, check who's in a team (scoreboard).

Discussion in 'Plugin Development' started by tovd1234, Dec 29, 2014.

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

    tovd1234

    Hello everyone. I have another problem in my coding. I am trying to make a timer on a scoreboard. I have already made a Offline player called Timer. But I just don't know how to decrease a value of 1 per second.
    Also I need to check in which team someone is. I have 2 teams, red and blue. I want this because then I can spawn them seperately.

    Code:
    Code:
    package nl.tovd1234.sw;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Sound;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.command.defaults.PlaySoundCommand;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.SignChangeEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    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;
    import org.bukkit.scoreboard.Team;
    
    public class Main extends JavaPlugin implements Listener {
       
        // Teams you create m here, but you give them a value in the onenable function! Now you can use these two team variables and you can add the players.
          
          Team red,blue;
        
            // Objectives (So I can set spawnpoints)
            ArrayList<String> ob1 = new ArrayList();
            ArrayList<String> ob2 = new ArrayList();
            ArrayList<String> ob3 = new ArrayList();
          
            // Kits
            ArrayList<String> kw1 = new ArrayList(); // Knockback Wand level 1
            ArrayList<String> kw2 = new ArrayList(); // Knockback Wand level 2
            ArrayList<String> kw3 = new ArrayList(); // Knockback Wand level 3
          
            ArrayList<String> sw1 = new ArrayList(); // Sword level 1
            ArrayList<String> sw2 = new ArrayList(); // Sword level 2
            ArrayList<String> sw3 = new ArrayList(); // Sword level 3
          
          
            ArrayList<String> fw1 = new ArrayList(); // Fire wand level 1
            ArrayList<String> fw2 = new ArrayList(); // Fire wand level 2
            ArrayList<String> fw3 = new ArrayList(); // Fire wand level 3
           
            private Scoreboard sBoard;
           
            public void setupScoreboard() {
                sBoard = Bukkit.getScoreboardManager().getNewScoreboard();
               
                red = sBoard.registerNewTeam("red");
                blue = sBoard.registerNewTeam("blue");
              
                red.setAllowFriendlyFire(false);
                blue.setAllowFriendlyFire(false);
              
                red.setCanSeeFriendlyInvisibles(true);
                blue.setCanSeeFriendlyInvisibles(true);
              
                red.setPrefix(ChatColor.RED + "");
                blue.setPrefix(ChatColor.BLUE + "");
              
              
                // Stats
                Objective stats = sBoard.registerNewObjective("stats", "playerKillCount");
                stats.setDisplaySlot(DisplaySlot.SIDEBAR);
                stats.setDisplayName("§4Statistics");
                stats.getScore(Bukkit.getOfflinePlayer("§6Time")).setScore(1200);
              
             
            }
          
            @Override
            public void onEnable() {
                getServer().getPluginManager().registerEvents(this, this);
                getServer().getLogger().info("Sky Warriors Successfully Started");
          
                setupScoreboard();      
            }
           
            public Scoreboard getScoreboard() {
                return this.sBoard;
            }
    
           
        @EventHandler
        public void playerJoin(PlayerJoinEvent e) {
            Player player = e.getPlayer();
            player.setScoreboard(getScoreboard());
            red.removePlayer(player);
            blue.removePlayer(player);
                }
       
        // Team command
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player) sender;
            Location loc = player.getLocation();
            if ((cmd.getName().equalsIgnoreCase("red"))) {
                if (sender instanceof Player) {
                ((Team)blue).removePlayer(player);
                ((Team)red).addPlayer(player);
                player.sendMessage("§cYou have joined the red team");
                player.playSound(loc, Sound.NOTE_PLING, 1, 1);
                } else {
                    player.sendMessage("§4You're not a player!");
                }
            }
           
            if ((cmd.getName().equalsIgnoreCase("blue"))) {
                if (sender instanceof Player) {
                ((Team)red).removePlayer(player);
                ((Team)blue).addPlayer(player);
                player.sendMessage("§9You have joined the blue team");
                player.playSound(loc, Sound.NOTE_PLING, 1, 1);
                } else {
                    player.sendMessage("§4You're not a player!");
                }
            }
           
            //Start command
            if ((cmd.getName().equalsIgnoreCase("start"))) {
                Bukkit.broadcastMessage("§a§lSky Warriors has begun!");
                //TODO Check whos in team red & whos in team blue. Then spawn them differently
                if () {
                   
                }
               
                if () {
                   
                }
            }
           
            return false;
        }
       
       
       
       
        // On sign creation do this.
        @EventHandler
        public void onSignCreate(SignChangeEvent e) {
            if (e.getLine(0).equalsIgnoreCase("kit")) {
                if (e.getLine(1).equalsIgnoreCase("kw1")) {
                    e.setLine(0, "§4Kit");
                    e.setLine(1, "§6Knockback");
                    e.setLine(2, "§6Wand");
                    e.setLine(3, "§6§lLevel 1");
                }
               
                if (e.getLine(1).equalsIgnoreCase("kw2")) {
                    e.setLine(0, "§4Upgrade");
                    e.setLine(1, "§6Knockback");
                    e.setLine(2, "§6Wand");
                    e.setLine(3, "§6§lLevel 2");
                }
    
                if (e.getLine(1).equalsIgnoreCase("kw3")) {
                    e.setLine(0, "§4Upgrade");
                    e.setLine(1, "§6Knockback");
                    e.setLine(2, "§6Wand");
                    e.setLine(3, "§6§lLevel 3");
                }
    
                if (e.getLine(1).equalsIgnoreCase("sw1")) {
                    e.setLine(0, "§4Kit");
                    e.setLine(1, "§6Sword");
                    e.setLine(2, "");
                    e.setLine(3, "§6§lLevel 1");
                }
    
                if (e.getLine(1).equalsIgnoreCase("sw2")) {
                    e.setLine(0, "§4Upgrade");
                    e.setLine(1, "§6Sword");
                    e.setLine(2, "");
                    e.setLine(3, "§6§lLevel 2");
                }
    
                if (e.getLine(1).equalsIgnoreCase("sw3")) {
                    e.setLine(0, "§4Upgrade");
                    e.setLine(1, "§6KSword");
                    e.setLine(2, "");
                    e.setLine(3, "§6§lLevel 3");
                }
    
                if (e.getLine(1).equalsIgnoreCase("fw1")) {
                    e.setLine(0, "§4Kit");
                    e.setLine(1, "§6Fire");
                    e.setLine(2, "§6Wand");
                    e.setLine(3, "§6§lLevel 1");
                }
    
                if (e.getLine(1).equalsIgnoreCase("fw2")) {
                    e.setLine(0, "§4Upgrade");
                    e.setLine(1, "§6Fire");
                    e.setLine(2, "§6Wand");
                    e.setLine(3, "§6§lLevel 1");
                }
    
                if (e.getLine(1).equalsIgnoreCase("fw3")) {
                    e.setLine(0, "§4Upgrade");
                    e.setLine(1, "§6Fire");
                    e.setLine(2, "§6Wand");
                    e.setLine(3, "§6§lLevel 1");
                }
               
            }
        }
    
    }
    
     
  2. Offline

    Experminator

    Excuse me, but i don't see anything about a timer. Only a score that represents a timer.
    But where is the countdown for it?

    Maybe this:
    Code:
    public void setupScoreboard() {
                sBoard = Bukkit.getScoreboardManager().getNewScoreboard();
              
                red = sBoard.registerNewTeam("red");
                blue = sBoard.registerNewTeam("blue");
            
                red.setAllowFriendlyFire(false);
                blue.setAllowFriendlyFire(false);
            
                red.setCanSeeFriendlyInvisibles(true);
                blue.setCanSeeFriendlyInvisibles(true);
            
                red.setPrefix(ChatColor.RED + "");
                blue.setPrefix(ChatColor.BLUE + "");
            
            
                // Stats
                Objective stats = sBoard.registerNewObjective("stats", "playerKillCount");
                stats.setDisplaySlot(DisplaySlot.SIDEBAR);
                stats.setDisplayName("§4Statistics");
                stats.getScore(Bukkit.getOfflinePlayer("§6Time")).setScore(1200);
            
                Bukkit.getScheduler().scheduleAsyncRepeatingTask(this, new Runnable(){
                           public void run(){
                                 int timer = stats.getScore(Bukkit.getOfflinePlayer("§6Time"));
                                  stats.getScore(Bukkit.getOfflinePlayer("§6Time")).setScore(timer);
                                 timer--;
                           }
               }, 0, 20);                     
            }
    Play with the code, it would maybe work.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 31, 2016
  3. Offline

    tovd1234

    I hadn't added the timer yet, because I didn't know how to. But thanks, I'll play with it a little. But what do these numbers represent?

    Edit- Can't I make a for loop? Then I check if the timer isn't 0, and if it isn't then it decreases 1. That process continues every 1 second. Would that be possible?
     
    Last edited: Dec 29, 2014
  4. Offline

    Experminator

  5. Offline

    tovd1234

    }, 0, 20); <-- These

    But can't I make a for loop? Then I check if the timer isn't 0, and if it isn't then it decreases 1. That process continues every 1 second. Would that be possible? (I don't know how to set the 1 second delay)
     
  6. Offline

    Experminator

    @tovd1234 The first number represents the delay in ticks. and the second number represents the time that repeating, also in ticks. 20 ticks is 1 Second.

    You can use a while loop, but a runnable is maybe better.
    Code:
    int timer = 60;
    
    while(timer != 0) {
         timer--;
    }
    Note: 60 would be 59. Because in Java starts by 0.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 31, 2016
  7. Offline

    tovd1234

    Okay, but wouldnt that make it 0 seconds. Because there is no delay defined there


    EDIT: And when I'm using the Runnable I get an error at int timer = stats.getScore(Bukkit.getOfflinePlayer("§6Time"));. The error is Type mismatch: cannot convert from Score to int.
    But setScore only accepts integers so if I change the type of timer to Score I get an error at setScore

    Code:
    Objective stats = sBoard.registerNewObjective("stats", "playerKillCount");
                stats.setDisplaySlot(DisplaySlot.SIDEBAR);
                stats.setDisplayName("§4Statistics");
                stats.getScore(Bukkit.getOfflinePlayer("§6Time")).setScore(1200);
        
                Bukkit.getScheduler().scheduleAsyncRepeatingTask(this, new Runnable(){
                           public void run(){
                                 int timer = stats.getScore(Bukkit.getOfflinePlayer("§6Time"));
                                  stats.getScore(Bukkit.getOfflinePlayer("§6Time")).setScore(timer);
                                 timer--;
                           }
               }, 0, 20);                 
            }
     
    Last edited: Dec 29, 2014
  8. Offline

    Experminator

    @tovd1234 That's the reason you must use a Runnable.
    Code:
    int id = Bukkit.getScheduler().scheduleAsyncDelayTask(this, new Runnable(){
        
        @Override
         public void run(){
               if(timer == 0){
                   Bukkit.getScheduler().cancelTask(id);
                   return;
               }
    
                timer--;
         }
    
    }, 20);
    - Create a int variable called timer. And define time you want.
     
  9. Offline

    tovd1234

    @Experminator Please check the edit in my last post :p
    And the scheduleAsyncDelayTask does not work Error: The method scheduleAsyncDelayTask(Main, new Runnable(){}, int) is undefined for the type BukkitScheduler. If I change it to scheduleAsyncRepeatingTask it will show up like scheduleAsyncRepeatingTask
     
  10. Offline

    Experminator

    @tovd1234 Play with it. You find the explanation about that.
     
  11. Offline

    tovd1234

    @Experminator Ok thnx

    @Experminator Okay, I have figured it out (I think). But the timer doesnt seem to work. Maybe because the scoreboard isn't updating but idk.

    This is the code for the Runnable timer I use:

    Code:
    this.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() {
    
                            @Override
                            public void run() {
                                if (timer != 0) {
                                    timer--;
                                } else {
                                    Bukkit.broadcastMessage("§cThe game has stopped!");
                                }
                            }
    
                        }, 0L, 20L);
            }
    Or the problem is that I have the order of them wrong. Order: TOP [int timer > scoreboard > command to activate the timer] BOTTOM

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 31, 2016
  12. Offline

    Experminator

    @tovd1234 If you want for personal help, you can private message me.
    Then i can give a quike reply
     
  13. Offline

    tovd1234

    ok :)
     
Thread Status:
Not open for further replies.

Share This Page