Problem with countdown made by runnable

Discussion in 'Plugin Development' started by WetriZ, Mar 1, 2020.

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

    WetriZ

    I am trying to make minigame. But I do not know what to do. I make countdown by using
    scheduleSyncRepeatingTask and runnable. There are many things in this class that is not important for this problem so I shorted the code a bit. Imagine that there are players in the arraylist queue. The problem is that the countdown is not starting. But when I replace in countdown() method queue.get().sentTitle with Bukkit.broadcastMessage(), the countdown works correctly. Any ideas why is this happening?? Thanks.

    My code:

    Code:
    import org.bukkit.*;
    import org.bukkit.entity.*;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerInteractEntityEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.scheduler.BukkitRunnable;
    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 java.util.ArrayList;
    
    import static org.bukkit.Bukkit.getServer;
    
    
    public class Entita implements Listener{
    
        public ArrayList<Player> queue = new ArrayList();
    
        private Main plugin;
        private boolean hasStarted = false;
    
        public Entita(Main plugin) {
            this.plugin = plugin;
            Bukkit.getPluginManager().registerEvents(this, plugin);
            preparGame();
            countdown();
            start();
    
        }
    
        int counter = 3;
    
        public void preparGame(){
    
            Scoreboard sco = getServer().getScoreboardManager().getNewScoreboard();
            Objective o = sco.registerNewObjective("Lifes", "", "");
            o.setDisplaySlot(DisplaySlot.SIDEBAR);
            o.setDisplayName(ChatColor.RED +"FLOP " + ChatColor.GREEN + "GAME");
            Score p1l = o.getScore(queue.get(1).getName());
            p1l.setScore(1);
            Score p2l = o.getScore(queue.get(0).getName());
            p2l.setScore(2);
            queue.get(0).setScoreboard(sco);
            queue.get(1).setScoreboard(sco);
    
            queue.get(0).teleport(new Location(Bukkit.getWorld("PM-ArcadeLand"), -30,7,-3));
            queue.get(1).teleport(new Location(Bukkit.getWorld("PM-ArcadeLand"), -24,7,-3));
    
        }
    
        public void countdown(){
    
            plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
                @Override
                public void run() {
                    if (counter != -1) {
    
    
                        if (counter != 0) {
                            queue.get(0).[COLOR=#ff0000]sendTitle[/COLOR](ChatColor.GREEN + "" + counter, "", 0, 20, 5);
                            queue.get(1).[COLOR=#ff0000]sendTitle[/COLOR](ChatColor.GREEN + "" + counter, "", 0, 20, 5);
                            counter--;
                        } else {
                            queue.get(0).[COLOR=#ff4d4d]sendTitle[/COLOR](ChatColor.GREEN + "GO!" , "", 0, 20, 5);
                            queue.get(1).[COLOR=#ff4d4d]sendTitle[/COLOR](ChatColor.GREEN + "GO!" , "", 0, 20, 5);
                            counter--;
                            queue.get(0).getInventory().addItem(new ItemStack(Material.FISHING_ROD));
                            queue.get(1).getInventory().addItem(new ItemStack(Material.FISHING_ROD));
                        }
                    }
                }
            }, 0L, 60L);
    
        }
        public void start(){
    
            queue.remove(0);
            queue.remove(1);
            hasStarted = true;
        }
    }
     
    Last edited by a moderator: Mar 1, 2020
  2. Offline

    Machine Maker

    When you say "the countdown is not starting" you mean the title isn't showing up on the screen?
     
  3. Offline

    WetriZ

    you are right, it is not showing on the screen... but when you replace queue.get().sendTitle() in else{} with Bukkit.broadcastMessage() and in if(counter != 0){} you keep queue.get().sendTitle()... it is showing but it is still repeating "3"
     
    Last edited: Mar 1, 2020
  4. Offline

    Machine Maker

    Well, here might be an issue. You aren't waiting for the countdown to be finished when you call the start() method. Try moving the call to start() into the repeating task when countdown hits 0.
     
  5. Offline

    WetriZ

    I will try it... Thank you mate.

    It worked.... I have been trying to solve this problem since morning... Thank you so much mate

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Mar 1, 2020
Thread Status:
Not open for further replies.

Share This Page