Countdown goes twice faster with 2 people in the queue

Discussion in 'Plugin Development' started by StellarFX, Jun 12, 2019.

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

    StellarFX

    Hello,

    I have a big problem which I can't fix ;
    It'd probably look logic to you but for now I can't see anything

    When someone other joins the queue, it goes twice faster (with 1 player 20, 19, 18... | with 2 players it goes like 20, 18, 16, 14...)

    Here's my code (not full cause it's ~500 lines)
    Show Spoiler

    Code:
    if(cmd.getName().equalsIgnoreCase("brup")) {
                    if(args.length == 1) {
                        if(args[0].equalsIgnoreCase("join")) {
                                if(!queue.containsKey(p.getName())) {
                                    queue.put(p.getName(), 0.0);
                                        p.teleport(new Location(p.getWorld(), Integer.parseInt(queuespawn1[0]), Integer.parseInt(queuespawn1[1]), Integer.parseInt(queuespawn1[2])));
                                        NOP = NOP + 1;
                                        if(queue.size() == 1) {
                                            startTimer();
                                        }
                                       
                                } else {
                                    p.sendMessage(prefix + "You are already in the queue !");
                               
                            }   
                           
                            return true;
    
    public void startTimer() {
                     left = 21;
                    this.task = new BukkitRunnable() {
                    @SuppressWarnings("deprecation")
                    @Override
                    public void run() {
        
                        for (String s : queue.keySet()) {
                            Player pq = Bukkit.getPlayerExact(s);
                            if (left > 0) {
                                left--;
                                    switch (left) {
                                    case 20:
                                        pq.setLevel(20);
                                        pq.sendTitle(ChatColor.GOLD + "The game will start", ChatColor.YELLOW + "in 20 seconds", 20, 40, 20);
                                        pq.playSound(pq.getLocation(), Sound.BLOCK_NOTE_PLING, 1, 2);
                                       
                                        break;
                                    case 19:
                                        pq.setLevel(19);
                                        break;
                                    case 18:
                                        pq.setLevel(18);
                                        break;
                                    case 17:
                                        pq.setLevel(17);
                                        break;
                                    case 16:
                                        pq.setLevel(16);
                                        break;
                                    case 15:
                                        pq.setLevel(15);
                                        break;
                                    case 14:
                                        pq.setLevel(14);
                                        break;
                                    case 13:
                                        pq.setLevel(13);
                                        break;
                                    case 12:
                                        pq.setLevel(12);
                                        break;
                                    case 11:
                                        pq.setLevel(11);
                                        break;
                                    case 10:
                                        pq.setLevel(10);
                                        pq.sendTitle(ChatColor.GOLD + "The game will start", ChatColor.YELLOW + "in 10 seconds", 20, 40, 20);
                                        pq.playSound(pq.getLocation(), Sound.BLOCK_NOTE_PLING, 1, 2);
                                        break;
                                    case 9:
                                        pq.setLevel(9);
                                        break;
                                    case 8:
                                        pq.setLevel(8);
                                        break;
                                    case 7:
                                        pq.setLevel(7);
                                        break;
                                    case 6:
                                        pq.setLevel(6);
                                        break;
                                    case 5:
                                        pq.setLevel(5);
                                        break;
                                    case 4:
                                        pq.setLevel(4);
                                        break;
                                    case 3:
                                        pq.setLevel(3);
                                        pq.sendTitle(ChatColor.GOLD + "The game will start", ChatColor.YELLOW + "in 3 seconds", 20, 40, 20);
                                        pq.playSound(pq.getLocation(), Sound.BLOCK_NOTE_PLING, 1, 2);
                                        break;
                                    case 2:
                                        pq.setLevel(2);
                                        pq.sendTitle(ChatColor.GOLD + "The game will start", ChatColor.YELLOW + "in 2 seconds", 20, 40, 20);
                                        pq.playSound(pq.getLocation(), Sound.BLOCK_NOTE_PLING, 1, 2);
                                        break;
                                    case 1:
                                        pq.setLevel(1);
                                        pq.sendTitle(ChatColor.GOLD + "The game will start", ChatColor.YELLOW + "in 1 second", 20, 40, 20);
                                        pq.playSound(pq.getLocation(), Sound.BLOCK_NOTE_PLING, 1, 2);
                                        break;
                                }
                                } else {
                                    task.cancel();
                                    pq.sendTitle(ChatColor.GOLD + "Starting...", "", 20, 40, 20);
                                    pq.playSound(pq.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1, 2);
                                    pq.setLevel(0);
                                    pq.teleport(new Location(pq.getWorld(), Integer.parseInt(gamespawn1[0]), Integer.parseInt(gamespawn1[1]), Integer.parseInt(gamespawn1[2])));
                                    gameZone1 = pq.getLocation();
                                    gameZone2 = pq.getLocation();
                                    worldp = pq.getWorld();
                                    metanet.spigot().setUnbreakable(true);
                                    metanet.addItemFlags(ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_UNBREAKABLE);
                                    metanet.setDisplayName(ChatColor.GOLD + "Bug Net");
                                    lorenet.add("§8§oRight click on the bugs");
                                    lorenet.add("§8§oto catch them !");
                                    net.setDurability((short) 94);
                                    metanet.setLore(lorenet);
                                    net.setItemMeta(metanet);
                                    pq.getInventory().clear();
                                    pq.getInventory().setItem(4, net);
                                    startGame();
                                }
                        }
                    }
                }.runTaskTimer(Main.getPlugin(Main.class), 0L, 20L);
            }


    Please help!

    Regards,
    StellarFX
     
  2. Offline

    Kars

    I mean no offense but i can't make sense of your code. It is horribly formatted and areas seem to be missing.

    What is it you are trying to do?
     
  3. Offline

    Minesuchtiiii

    I'd add a default in the switch which just sets his lvl to the countdown, gets you rid of like 10 switch statements where you do nothing but set his level
     
  4. Offline

    timtower Administrator Administrator Moderator

    @StellarFX You probably have 1 global timer variable. You need 1 per player.
     
  5. You decrement the int per player rather than just every task run. Also, a lot of this code is really unnecessary and messy.

    @timtower Its a game countdown, he needs it for all players just decrementing each task run rather than each player iteration.
     
  6. Offline

    StellarFX

    @Kars As I said, I'm trying to make a countdown for all the players in the hashmap "queue" (I call it queue but I also use it in the game to score the player's score)

    @Minesuchtiiii Okay but it doesn't solve my problem :/

    @bwfcwalshy In other words? (Sorry I'm not English ^^')
     
  7. Offline

    Kars

    @StellarFX Oh i see.

    Your problem is that you are decreasing your timer for every player in the queue.
    Code:
    for (String s : queue.keySet()) 
    So for every player in the queue, you are decreasing the timer by one.

    Remove the countdown from the loop.
     
  8. Offline

    StellarFX

    @Kars Oh yeah good idea. I try this tomorrow and I keep you in touch. (I think this is it )
     
  9. Offline

    StellarFX

    @Kars Okay thanks it's working !
    I post here the soluce if anyone is in the same case :

    Code:
    public void gameTimer() {
            left2 = 61;
            this.task2 = new BukkitRunnable() {
            @Override
            public void run() {
                
                    if (left2 > 0) {
                        left2--;
                            switch (left2) {
                            case 120:
                                for (String s : queue.keySet()) {
                                    Player pq = Bukkit.getPlayerExact(s);
                                    pq.sendTitle(ChatColor.GOLD + "2 minutes left!", "", 20, 40, 20);
                                    pq.playSound(pq.getLocation(), Sound.BLOCK_NOTE_PLING, 1, 2);
                                }
                                break;




    I have another problem! Once everything is finished, only one player gets the final message ;
    Here's my error :
    Show Spoiler
    Code:
    [17:39:56 WARN]: [CatchST] Task #105 for CatchST v0.0.1 generated an exception
    java.util.ConcurrentModificationException: null
            at java.util.HashMap$HashIterator.nextNode(Unknown Source) ~[?:1.8.0_211]
            at java.util.HashMap$KeyIterator.next(Unknown Source) ~[?:1.8.0_211]
            at fr.stellarfx.catchst.Main$4.run(Main.java:611) ~[?:?]
            at org.bukkit.craftbukkit.v1_12_R1.scheduler.CraftTask.run(CraftTask.java:76) ~[spigot-1.12.2.jar:git-Spigot-e8ded36-acbc348]
            at org.bukkit.craftbukkit.v1_12_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:361) [spigot-1.12.2.jar:git-Spigot-e8ded36-acbc348]
            at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:739) [spigot-1.12.2.jar:git-Spigot-e8ded36-acbc348]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot-1.12.2.jar:git-Spigot-e8ded36-acbc348]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot-1.12.2.jar:git-Spigot-e8ded36-acbc348]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot-1.12.2.jar:git-Spigot-e8ded36-acbc348]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_211]


    and here's my line 611

    Show Spoiler

    Code:
    for (String s : queue.keySet()) {
                                Player pq = Bukkit.getPlayerExact(s);
                                for (Entity entity : pq.getWorld().getEntities()) {
                                    if(entity instanceof ArmorStand) {
                                        entity.remove();
                                    }
                                }
                                pq.sendTitle(ChatColor.GOLD + "Finished !", "", 20, 40, 20);
                                pq.playSound(pq.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1, 2);
                                pq.setLevel(0);
                                pq.teleport(new Location(pq.getWorld(), Integer.parseInt(gamespawn1[0]), Integer.parseInt(gamespawn1[1]), Integer.parseInt(gamespawn1[2])));
                                pq.sendMessage("§8---===+===---");
                                queue.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).limit(3).forEach(e -> {
                                    pq.sendMessage("§6" + e.getKey() + " §7- §e" + Math.round(e.getValue()));
                                    //economy.depositPlayer(e.getKey(), 100);
                                });
                                pq.sendMessage("§8---===+===---");
                                pq.getInventory().clear();
                                NOP = 0;
                                queue.clear();
                                exp = 0;
                                pq.performCommand("spawn");
                            }


    Please help! (Again :v)

    EDIT : Maybe I found why, testing out
    RE-EDIT : Nvm not working
     
    Last edited: Jun 13, 2019
  10. Offline

    Kars

    @StellarFX You are calling queue.clear() whilst you are iterating it. You can't clear the queue until after the loop.

    I'm guessing you want to remove the player from the queue?
    Code:
    queue.remove(s);
    But you wouldn't have to do that either. Calling clear after the loop is fine.
     
    Last edited: Jun 13, 2019
  11. Offline

    StellarFX

    @Kars Yup thanks I fixed it before you told me xD

    Now I have another problem (sorry for bothering you :/ )
    I try to give money to the first, second and third player but it throws me a NullPointerException at line 634
    Here's my line 634 :

    Code:
    List<Integer> points = new ArrayList<Integer>(queue.values());
                                Collections.sort(points, Collections.reverseOrder());
                                int first = points.get(0);
                                int second = points.get(1);
                                if(points.size() >= 3) {
                                    int third = points.get(2);
                                }
                                queue.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).limit(3).forEach(e -> {
                                    pq.sendMessage("§6" + e.getKey() + " §7- §e" + Math.round(e.getValue()));
                                    if(e.getValue() == first) {
                                        economy.depositPlayer(Bukkit.getPlayerExact(e.getKey()), 100); // LINE 634
                                    }
                                    if(e.getValue() == second) {
                                        economy.depositPlayer(Bukkit.getPlayerExact(e.getKey()), 50);
                                    }
                                    if(points.size() >= 3) {
                                        int third = points.get(2);
                                        if(e.getValue() == third) {
                                            economy.depositPlayer(Bukkit.getPlayerExact(e.getKey()), 25);
                                        }
                                    }
                                
                                });
     
  12. Offline

    Kars

    @StellarFX This will thow a NullPointer if there aren't 2 people in the queue, first of all.
    Other than that it's hard to say without knowing where in that method the exception is thrown. Show the stacktrace.

    Also, didn't you empty the queue before? So wouldn't it be empty?
     
  13. Offline

    StellarFX

    @Kars I empty the queue after. Here's my stack trace:

    Code:
    [CatchST] Task #37 for CatchST v0.0.1 generated an exception
    java.lang.NullPointerException: null
            at fr.stellarfx.catchst.Main$4.lambda$run$0(Main.java:634) ~[?:?]
            at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(Unknown Source) ~[?:1.8.0_211]
            at java.util.stream.SliceOps$1$1.accept(Unknown Source) ~[?:1.8.0_211]
            at java.util.stream.SortedOps$SizedRefSortingSink.end(Unknown Source) ~[?:1.8.0_211]
            at java.util.stream.AbstractPipeline.copyIntoWithCancel(Unknown Source) ~[?:1.8.0_211]
            at java.util.stream.AbstractPipeline.copyInto(Unknown Source) ~[?:1.8.0_211]
            at java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source) ~[?:1.8.0_211]
            at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(Unknown Source) ~[?:1.8.0_211]
            at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(Unknown Source) ~[?:1.8.0_211]
            at java.util.stream.AbstractPipeline.evaluate(Unknown Source) ~[?:1.8.0_211]
            at java.util.stream.ReferencePipeline.forEach(Unknown Source) ~[?:1.8.0_211]
            at fr.stellarfx.catchst.Main$4.run(Main.java:631) ~[?:?]
            at org.bukkit.craftbukkit.v1_12_R1.scheduler.CraftTask.run(CraftTask.java:76) ~[spigot-1.12.2.jar:git-Spigot-e8ded36-acbc348]
            at org.bukkit.craftbukkit.v1_12_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:361) [spigot-1.12.2.jar:git-Spigot-e8ded36-acbc348]
            at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:739) [spigot-1.12.2.jar:git-Spigot-e8ded36-acbc348]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot-1.12.2.jar:git-Spigot-e8ded36-acbc348]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot-1.12.2.jar:git-Spigot-e8ded36-acbc348]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot-1.12.2.jar:git-Spigot-e8ded36-acbc348]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_211]
     
  14. Offline

    Kars

    It's your queue.entrySet lambda that is doing it but i can't debug it for you right now.
     
  15. Offline

    StellarFX

  16. Offline

    Kars

    The foreach in your lambda is throwing the exception. I don't know where you got it but using such an advanced and nesting lamdba is never a good idea for beginners.
    Write a simple one.
    https://www.geeksforgeeks.org/iterate-map-java/
     
Thread Status:
Not open for further replies.

Share This Page