"Slowing down" a RepeatingTask

Discussion in 'Plugin Development' started by MordorKing78, Aug 6, 2016.

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

    MordorKing78

    I know it's not possible to change the tickrate after the Runnable has been called.
    But I'm trying to make a csgo-like animation where it slows down over time, it might not be the best code you've seen and there are loads of flaws but it works fine, it just does not slow down yet.

    Code:

    Code:
    public void openBrokenTreasure(final Player p){
            Integer amount = plugin.config.getInt(p.getUniqueId() + ".treasure.broken");
    
            //if(amount >= 1){
                itemList.add(treasureContent.Antidode());
                itemList.add(treasureContent.dnRune());
                itemList.add(treasureContent.ExperienceStone());
                itemList.add(treasureContent.MobCloak());
                itemList.add(treasureContent.StaffofCuring());
                itemList.add(treasureContent.StrengthStone());
                itemList.add(treasureContent.ZeusSword());
                itemList.add(treasureContent.TeleportScroll());
               
                final Random r = new Random();
               
                speed.put(p, 3);
               
                final Inventory s = Bukkit.createInventory(null, 9, "ยง8Broken Treasure");
               
                s.setItem(0, itemList.get(r.nextInt(itemList.size())));
                s.setItem(1, itemList.get(r.nextInt(itemList.size())));
                s.setItem(2, itemList.get(r.nextInt(itemList.size())));
                s.setItem(3, itemList.get(r.nextInt(itemList.size())));
                s.setItem(4, itemList.get(r.nextInt(itemList.size())));
                s.setItem(5, itemList.get(r.nextInt(itemList.size())));
                s.setItem(6, itemList.get(r.nextInt(itemList.size())));
                s.setItem(7, itemList.get(r.nextInt(itemList.size())));
                s.setItem(8, itemList.get(r.nextInt(itemList.size())));
    
                p.openInventory(s);
                   
                Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
                    int scroll = 50;
                    public void run() {
                        if(scroll <= 0){
                            return;
                        }else{                   
                            p.playSound(p.getLocation(), Sound.CLICK, 10, 10);
    
                            s.setItem(0, s.getItem(1));
                            s.setItem(1, s.getItem(2));
                            s.setItem(2, s.getItem(3));
                            s.setItem(3, s.getItem(4));
                            s.setItem(4, s.getItem(5));
                            s.setItem(5, s.getItem(6));
                            s.setItem(6, s.getItem(7));
                            s.setItem(7, s.getItem(8));
                            s.setItem(8, itemList.get(r.nextInt(itemList.size())));
                        }
                    }
                }, 5, 3); //2nd = changable
            /*}else{
                //send message player has not enough chest
            }*/
        }
    What would be the best way to slow the item spinner down?
     
  2. Offline

    N00BHUN73R

    @MordorKing78
    I would think you'd change one of the arguments in the scheduler, so }, 5, 3);
     
  3. Offline

    Zombie_Striker

    Use ThreadLocalRandom.current to get a Random instance. No need to use up extra memory space with multiple randoms.

    You never do anything with scroll. This will always run.

    An easy way of doing this is setting two ints called "delay" and "max delay". Both will equal 0 at the start. Each time that the task runs, add one to "delay". If it is greater than "max delay", set delay to 0 and add one to max. Heres the thing, Only scroll when delay equals 0. This will give the feeling of slowing down. Once max delay is equal to "40" (2 second delay for the task), then stop the scrolling.
     
  4. Offline

    MordorKing78

    @N00BHUN73R Once the Task has already started its not possible to change the ticks?

    @Zombie_Striker
    1. Then how would I get a random of the size of the itemList?
    2. Yup, sorry I was trying a couple of things to get it to work and I accidentely removed scroll--;.
    3. That's actually really smart, let me give this a shot.

    @Zombie_Striker
    Okay, I think I came up with some thing, might not be the most efficient but it works I guess.

    This is what I did,
    Code:
    if(scroll >= 350){
                            scrollItems(s, scroll, p, r);
                        }else if(scroll >= scrollLimit){
                            if(slowdown >= ticks){                  
                                Bukkit.broadcastMessage(Integer.toString(scroll) + " | " + Integer.toString(ticks));
                              
                                scrollItems(s, scroll, p, r);
                              
                                slowdown = 0;
                            }else{
                                slowdown++;
                            } 
                        }else{
                            ticks = ticks+1;
                            scrollLimit = scroll+-50;
                        }
    
    But when it goes from 1 to 2 ticks it looks so weird.

    EDIT: I just feel like the transissions are not natural.
     
    Last edited: Aug 7, 2016
  5. Offline

    Blockhead7360

    Could you use doubles?


    Sent from my iPhone using Tapatalk
     
  6. Offline

    MordorKing78

    @Blockhead7360 I'm not sure if double's are possible with tickrates?
     
  7. @MordorKing78
    Doubles are not possible with tickrate.

    However, a nice way to do this is, when you want things to slow down, make your code only run every other time the runnable is called.
     
  8. Offline

    MordorKing78

    @AlvinB I'm sorry if I'm misunderstanding something but isn't that what I'm currently doing?

    What I'm now trying to figure out is some kind of "formula" that makes the animation go smoothly, I don't want it so I'd have to check if the rolls are past x amount of times and then slowing it down by x amount of ticks. I want this process to be "automatic" if you get what I mean ;)

    Is there anything you would recommend for doing this? Thanks :)
     
  9. Offline

    ArsenArsen

    Schedule a task every tick, And then save a run count, increase it every tick, when it is equal to the amount of tick between the two tasks, reset it and do your task
     
Thread Status:
Not open for further replies.

Share This Page