Problem with animations

Discussion in 'Spigot Plugin Development' started by Fighter-HUN, Jan 8, 2020.

  1. Hello everyone! I trying to make animation, but I don't know why doesn't work for me :/
    Code (Java):
    public static String placeholder(String pch) {
         
            for(String anim:plugin.getConfig().getConfigurationSection("Animation").getKeys(false)) {
         
                BukkitScheduler scheduler = Bukkit.getScheduler();
             
                int interval = plugin.getConfig().getInt("Animation."+anim+".interval");
             
                scheduler.runTaskTimer(plugin, new Runnable() {

                    int i = plugin.getConfig().getStringList("Animation."+anim+".text").size();
                 
                    @Override
                    public void run() {
                 
                     
                        if(i >= plugin.getConfig().getStringList("Animation."+anim+".text").size()) {
                         
                            i = 0;
                        }
                        if(i < plugin.getConfig().getStringList("Animation."+anim+".text").size()) {
                         
                            plugin.getConfig().getStringList("Animation."+anim+".text").get(i);
                        }
                     
                        i++;
                    }
                 
                }, 0, interval);
             
                List<String> string = plugin.getConfig().getStringList("Animation."+anim+".text");
             
                for(String replaced:string) {
             
                    pch = pch.replace("%Animation:"+anim+"%", replaced);
                }
            }
         
            return pch;
         
        }
     
  2. Please provide what you expect to see and what you actually see, and (if applicable) the stack trace
     
    • Agree Agree x 1
  3. in config:
    Code (YAML):
    Animation:
      elso
    :
        text
    :
        - 'H'
        - 'He'
        - 'Hel'
        - 'Hell'
        - 'Hello'
        interval
    : 20
    I only see the first line of list on TabList
     
  4. Without actually trying, I think that I might be defined in the wrong scope for that to work.
    I believe that you need to at least put that on the class level. Might be wrong thou
     
  5. What? I don't understand you
     
  6. I want to make animated placeholders, but it doesn't work for me, and I don't know why
     
  7. ah autocorrect mad i an I.
    Try making i a private class variable
     
  8. A lot of your current code is doing nothing useful. The only relevant thing that is being done here is that you loop through the animation lines, then you replace the some placeholder in your text with the animation (this happens on every iteration - hence on the first iteration, the placeholder gets replaced with the first line). Iterations after the first one will look for the placeholder, but since it has already been replaced, it won't find them. Then your string gets returned. What you end up with is a string that only has the proper text for the first animation and then nothing after.

    What you want to do on a high level is to change the text every x ticks. This scheduling of the x ticks needs to happen first and then you can change the text to a new line of animation each time the scheduler gets ran. You already have the scheduler in place: now think about how you would get the next line of the animation each time the code in the scheduler runs again.
     
    • Agree Agree x 1
  9. I made a new code, but the animation "works" but really buggy, and idk why :/

    Code (Java):
    static Main plugin = Main.getPlugin(Main.class);
        @SuppressWarnings("unused")
        private static int Interval;
        private static String rep = "";
        private static int i = 0;

        public static String placeholder(String pch) {
            AnimationCreate.rep = pch;
            for (String anim : plugin.getConfig().getConfigurationSection("Animation").getKeys(false)) {

                BukkitScheduler scheduler = Bukkit.getScheduler();

                int interval = plugin.getConfig().getInt("Animation." + anim + ".interval");

                scheduler.runTaskTimer(plugin, new Runnable() {

                    List<String> animation = plugin.getConfig().getStringList("Animation." + anim + ".text");

                    @Override
                    public void run() {

                        if (i >= animation.size()) {

                            i += 0;

                        }
                        if (i <= animation.size()) {

                            plugin.getConfig().getStringList("Animation." + anim + ".text").get(i);
                        }
                        i++;
                     
                        if(i == animation.size()) {
                            i = 0;
                        }

                    }

                }, 0, interval * System.currentTimeMillis());

                pch = rep.replace("%Animation:" + anim + "%",
                        plugin.getConfig().getStringList("Animation." + anim + ".text").get(i));
            }

            return pch;

        }
    The runnable method doesn't use the refresh interval time, the runnable method use the tablist refresh interval :/
     
  10. Could you more elaborately describe how your current method is "buggy"?
     
  11. The characters are not written individually on the tablist, because the interval unused(I think) The animated placeholders are use the tablist refresh interval...
     
  12. You're multiplying the interval by the System.currentTimeMillis, however this is likely not what you want. System.currentTimeMillis returns the amount of milliseconds that have passed since January 1, 1970 until now. For me that's at this moment about 1,578,601,765. If you multiply that by the interval, which in the config.yml you posted is 20, you get 31,572,035,300. This means that the runnable runs once immediately and then it will take about 50 years 8 days 8 hours 29 minutes 25 seconds to run again. The interval you should specify should be in ticks, where 20 ticks is equal to one second. How fast do you actually want the animation to go? The delay between each different text should be the amount you set that value to.
     
  13. Thanks, but the animations still using the tablist interval and not the placeholder refresh interval :/
    I'm confused
    This the first time, that I want to make animated placeholders :/