Solved Moving GUI [Timing]

Discussion in 'Plugin Development' started by Luke_Lax, Jul 17, 2014.

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

    Luke_Lax

    Hey.. I hate having to post here but I'm definitely stuck :p

    I have a custom inventory that opens 9 slots, I have an array list filled with itemstacks. I know how to loop over the arraylist, I know how to add them items to the inventory. My problem is, is that I want one item to loop over each slot (1-9), I want all of them to do it and each time I want the time it takes one item to cross all 9 slots to decrease. Does that make sense? :3

    EDIT: Ok I removed some of the old text and that (^) small text is my original description. I'm really having no luck, either it's not possible or I'm derping really hard.

    To hopefully help, I've created this short gif which I hope explains what I want to do. I'm not asking to be spoon fed code, I just need someone to push me in the right direction :)

    [​IMG]

    ^ Hehe, it looks silly but it explains my idea pretty well. I can get the items in the GUI and I remove them but I have no clue how to loop them over like the gif shows with a small wait in-between. When I first had this idea, I thought it'll be easy, now I'm thinking it's not possible? If so please say D: Otherwise I welcome you to try it yourself.

    Thank you very much!
    ~Luke
     
  2. Offline

    TheMcScavenger

    I really suggest using BukkitScheduler, repeating task.
     
  3. Offline

    _LB

    There is almost always 20 ticks per second on a non-lagging server. Why do you need extreme accuracy? There's no way to get around a little server lag anyway, even at packet level if it's connection lag.
     
  4. Offline

    Luke_Lax


    Because to achieve the effect I want, I assume I'd have to have 2 schedulers running one inside the other which will undoubtedly ruin the effect and cause tick lag.
     
  5. Offline

    _LB

    Why would it cause lag? Surely moving items around in inventories each tick is not an expensive process?
     
  6. Offline

    Luke_Lax

    Can anyone help me with this? I'm almost certain that I cannot use Schedulers because I need to keep track of the inv number but it needs to be final within a Scheduler...

    I just have no clue how to time it all. It needs to set it, 5 seconds later change to the next one and remove the one before it all while looping over the inventory. :(
     
  7. Offline

    EcMiner

    You can use an asynchronous timer, so even when the server is lagging, the timer of this inventory won't be laggin
     
  8. Offline

    Luke_Lax

    I'm still having no luck with this. I just need to add a wait within each iteration. I can't use a delayed task inside the loop as the loop will carry on and the task will literally just run later (new thread) and I can't use a repeating task because my methods of counting each iteration do not work as the variables need to be final within the task. The only thing I could think of it getting the start time (System.mili...) and each iteration, add 4 seconds on to the time and check if the current time is equal or greater than that time, if it is, loop the next and add +4. However I have no clue how I'd do that. :(
     
  9. Offline

    LCastr0

    Create a class implementing Runnable, maybe?
     
  10. Offline

    Luke_Lax

    Just a small bump, I've edited the question and hopefully it'll explain what I want :s I hope it's possible :p
     
  11. Offline

    hintss

    I still don't see why you can't just use a repeating task
     
  12. Offline

    Luke_Lax


    I can't get it working using repeating tasks. Feel free to try yourself, if you can then I'd be shocked :p
     
  13. Offline

    chikenlitle99

    Luke_Lax
    This looks like. is not what your looking for but if you are creative you will be able to serve
    [​IMG]

    Code:java
    1. inv = Bukkit.createInventory(null, 9, "Menu");
    2.  
    3. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    4. public void run() {
    5.  
    6. if (num == 1) {
    7. nv.setItem(0, createItem(Material.TNT, 1, (short) 0, "Arcade", "Lore));
    8. num++;
    9.  
    10. } else if (num == 2){
    11. inv.setItem(0, createItem(Material.ARROW, 1, (short) 0, "Arcade", "Lore"));
    12. num++;
    13.  
    14. } else if (num == 3){
    15. inv.setItem(0, createItem(Material.BOOK, 1, (short) 0, "Arcade", "Lore"));
    16. num++;
    17.  
    18. } else if (num == 4){
    19. inv.setItem(0, createItem(Material.COOKIE, 1, (short) 0, "Arcade", "Lore"));
    20. num++;
    21.  
    22. }
    23.  
    24. num = 1;
    25.  
    26. }
    27. }, 0, 1 * 4); //Time to update the item
    28. }
    29. public ItemStack createItem(Material material, int amount, short shrt, String displayname, String lore) {
    30. ItemStack i = new ItemStack(material, amount, (short) shrt);
    31. ItemMeta im = i.getItemMeta();
    32. im.setDisplayName(displayname);
    33.  
    34. ArrayList<String> loreList = new ArrayList<String>();
    35. String[] lores = lore.split("/");
    36. loreList.addAll(Arrays.asList(lores));
    37.  
    38. im.setLore(loreList);
    39. i.setItemMeta(im);
    40. return i;
    41.  
    42.  
    43. }
     
    Luke_Lax likes this.
  14. Offline

    hintss

    I just did. Here's the code:
    Code:java
    1. final Inventory inv = Bukkit.createInventory(null, 9);
    2. inv.setItem(0, new ItemStack(Material.DIAMOND));
    3. ((Player) sender).openInventory(inv);
    4.  
    5. new BukkitRunnable() {
    6. @Override
    7. public void run() {
    8. ItemStack temp = inv.getItem(inv.getSize() - 1);
    9. for (int i = inv.getSize(); i > 0; i--) {
    10. if (i > 1) {
    11. inv.setItem(i - 1, inv.getItem(i - 2));
    12. } else {
    13. inv.setItem(i - 1, temp);
    14. }
    15. }
    16.  
    17. ((Player) sender).updateInventory();
    18. }
    19. }.runTaskTimer(this, 10, 10);


    Just stop the timer on invclose and logout and it should be good to go

    Oh, and to swap the items, just check if slot 8 is the item, and if so, replace it with the other item, and do this right before the for loop

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
    Luke_Lax likes this.
  15. Offline

    Luke_Lax

    chikenlitle99 hintss

    Thank you both very much! I think I just needed to see a way and that it was possible, it got it to work flawlessly!
    You were right hintss, I think it frustrated me and I convinced myself it wasn't going to work :p
    For anyone wondering how I personally did it (before I saw hintss code), here, roughly:

    Code:java
    1. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(main, new Runnable() {
    2. public void run() {
    3. count++;
    4. ItemStack i = YourItemStack;
    5. if(count >= 9){
    6. count = 0;
    7. }
    8. Inv.setItem(count, air);
    9. Inv.setItem(count, i);
    10.  
    11. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(main, new Runnable() {
    12. public void run() {
    13. if(count != 0){
    14. Inv.setItem((count - 1), air);
    15. } else {
    16. Inv.setItem(8, air);
    17. }
    18. }
    19. }, 5, 0);
    20.  
    21. }
    22. }, 20, 10);
     
  16. Offline

    fireblast709

    inventory editing with a async task is a no no
     
    hintss and MCForger like this.
  17. Offline

    hintss

    what's more, async task timers will still slow down if tps goes down
     
  18. Offline

    fireblast709

    hintss they run in a separate Thread, so tps lag shouldn't slow it down unless the CPU cannot handle it (which is hardly the cause)
     
  19. Offline

    EcMiner

    Never had and still don't have any problems with it, its working perfectly for me
     
  20. Offline

    fireblast709

    EcMiner until one day you will have an unexplainable problem in the code caused by disregarding thread safety :3. Asynchronous API usage is extremely unpredictable, and if I change your inventory at exactly the same time, you won't be able to predict what happens.
     
  21. Offline

    EcMiner

    I'll take my chances
     
  22. Offline

    fireblast709

    EcMiner the previous argument aside, for what purpose? In general you won't need async execution for performance.
     
  23. Offline

    EcMiner

    fireblast709 It does help me in performance, causw when there's like 200 players on the server and then I would do the effect synchronously it would lagg and that would basically make it look ugly, and I'm not the best coder out there so my code is not fully optimized for performance cause I find that pretty hard to do with some of the things so I really need the asynchronous tasks. I appreciate the tip about it being unsafe but its just necessary in my case :)
     
  24. Offline

    fireblast709

    EcMiner wouldn't it be better to learn new ways to optimise your code :p. You can pm me or create a new thread if you need help with it :3
     
  25. Offline

    EcMiner

Thread Status:
Not open for further replies.

Share This Page