wait(); or thread.sleep();

Discussion in 'Plugin Development' started by Reoss, Jul 21, 2014.

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

    Reoss

    hello i want to ask if it is possible to use wait(); or thread.sleep(); for a event but as example pickupevent. Not to delay to pickup a item onyl to delay the functions in the event is it possible if its so than tell me pls thanks.
     
  2. Offline

    xmarinusx

    You can better never use that, sleeping the main thread will freeze the entire server.

    You can however use BukkitRunnables to delay tasks, find more about it here: http://wiki.bukkit.org/Scheduler_Programming

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

    MCMastery

    xmarinusx is right.
    But next time xmarinusx please exit your original post, and not make another if its in a row ;)
     
  4. Offline

    Reoss

    can you help me to use sheduler i never use it before.
    Code:java
    1. @EventHandler
    2. public void take(PlayerPickupItemEvent e, Player p) throws IOException, InterruptedException{
    3.  
    4.  
    5. final PlayerPickupItemEvent finalEvent = e;
    6. Bukkit.getScheduler().scheduleSyncDelayedTask(pl, new Runnable() {
    7. public void run() {
    8.  
    9. File lol = new File("plugins/SaveSlots/Players/", finalEvent.getPlayer().getUniqueId() + ".yml");
    10. FileConfiguration lol2 = YamlConfiguration.loadConfiguration(lol);
    11.  
    12. if(lol2.getBoolean("Slot1.inuse", true)) {
    13. lol2.set("Slot1.Items", finalEvent.getPlayer().getInventory().getContents());
    14. lol2.set("Slot1.Armor", finalEvent.getPlayer().getInventory().getArmorContents());
    15. try {
    16. lol2.save(lol);
    17. } catch (IOException e1) {
    18. // TODO Auto-generated catch block
    19. e1.printStackTrace();
    20. }
    21. }
    22. else if(lol2.getBoolean("Slot2.inuse", true)) {
    23. lol2.set("Slot2.Items", finalEvent.getPlayer().getInventory().getContents());
    24. lol2.set("Slot2.Armor", finalEvent.getPlayer().getInventory().getArmorContents());
    25. try {
    26. lol2.save(lol);
    27. } catch (IOException e1) {
    28. // TODO Auto-generated catch block
    29. e1.printStackTrace();
    30. }
    31. }
    32. else if(lol2.getBoolean("Slot3.inuse", true)) {
    33. lol2.set("Slot3.Items", finalEvent.getPlayer().getInventory().getContents());
    34. lol2.set("Slot3.Armor", finalEvent.getPlayer().getInventory().getArmorContents());
    35. try {
    36. lol2.save(lol);
    37. } catch (IOException e1) {
    38. // TODO Auto-generated catch block
    39. e1.printStackTrace();
    40. }
    41.  
    42. }
    43. }
    44. }
    45.  
    46. , 2*20L);
    47.  
    48.  
    49.  
    50. }

    this is not working.
     
  5. Offline

    Zupsub

    Just use the function runTaskLater.
    Then, please rename your variables, to describe their function.
     
  6. Offline

    Reoss

    not working either.
     
  7. Offline

    fireblast709

    Reoss please elaborate
     
  8. Offline

    Cirno

    For future reference, wait() is used in synchronization; it's primary purpose is for multithreading. If used correctly, the current thread pauses and waits for whatever you called wait() on to have it's notify() method called. It's a bit more advanced and I suggest you don't read up on it until you feel your plugin needs to use multiple threads.


    tl;dr wait() probably doesn't do what want to do currently.
     
  9. Offline

    Reoss

    private Plugin p;
    Code:java
    1. Bukkit.getServer().getScheduler().runTaskLater(p, new Runnable()

    i get a error in this line.
     
  10. Offline

    daavko

    Reoss And what does that error say?
     
  11. Offline

    Reoss

    NullPointerException
     
  12. Offline

    daavko

    Reoss Does "p" have any value?
     
  13. Offline

    Reoss

    its from import org.bukkit.plugin.Plugin;
     
  14. Offline

    Saladoc

    thats a type. Not a value.
     
  15. Offline

    Reoss

    I never said its a type or value but Private Plugin P; is bound to import org.bukkit.plugin.Plugin;
     
  16. Reoss Please learn the basics of Java - you don't really understand the terminology we're using and therefore cannot really been helped. You never assign a value for p. And you told us about the import because you had no idea what you were doing :)
     
  17. Offline

    Reoss

    i never used scheduler before. And i tested some other ways.
     
  18. Offline

    Drew1080

    Reoss
    Assigning a value to a variable has nothing todo whether you used a scheduler before or not. It's the very basic's of java. Like said above please go learn java has you have no idea of the terminology being used here.
     
    Zupsub likes this.
  19. Offline

    daavko

    Drew1080 Damn that bug in your signature...also, I agree with you.
     
  20. Offline

    Reoss

    aha you know i testes other ways before and this way was only one from them i like to say "Expérience passe science."
    also i testet some other ways but this way is only one from 100 examples.
     
  21. Reoss I don't understand your point.
     
  22. Offline

    Reoss

    ehm sry my english is not the best.
    ehm ok i have this problem since yesterday and of course i testes more methods to fix it but no one works you know? and what i post here was my last option. becouse eclipse automatic error fix do it automatic so i testet it.
     
  23. Offline

    AoH_Ruthless

    Well we can't help you with basic Java. You need to stop what you are doing, and go learn some Java first because you have little idea about what you are doing.
     
  24. Offline

    Reoss

    im 100% sure what im doing i want to save the items and armor to a .yml file and the yml file is names the uuid ffrom the player if the player pickup a item.
     
  25. Reoss Knowing what you want to do and knowing how to do it are two completely different things. It doesn't seem like you know how to assign a value to a variable, which should be obvious otherwise you definitely do not have enough knowledge to make plugins.
     
  26. Offline

    Reoss

    i already complete the plugin only this is missing ;)
     
  27. Offline

    1Rogue

    Saying "learn java" and not even providing any type of help for the person to actually learn it is on par with sitting there and pouting. At least point them in the right direction.

    Reoss

    You should try to gain a better understanding of things like classes and instances first:
    http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
    http://docs.oracle.com/javase/tutorial/reflect/member/ctorInstance.html

    If you want, from there you can start the tutorial from the beginning (which I recommend):
    http://docs.oracle.com/javase/tutorial/getStarted/

    It starts very basic but gets into more raw material fast.
     
  28. Offline

    Reoss

    1Rogue
    maybe they dont know it how to fix it and thanks for the tutorials i will take a look :)
    The First usefull answer !!
     
  29. 1Rogue There's a point at which they clearly either have never tried learning for themselves or something has clearly gone horribly wrong that giving direction wouldn't be all that helpful. Not knowing how to assign a variable a value, or what even constitutes a value, is one of these times.
     
    Zupsub likes this.
  30. Offline

    1Rogue


    Not knowing something constitutes at the very least linking them to a tutorial (preferrably oracle's). Or at the very least show them how to find it. This is used very much as a support forum, if you don't feel like offering anything useful to a topic, why bother posting? There are situations where I have certainly felt that way, so I don't post in them.
     
    AdamQpzm likes this.
Thread Status:
Not open for further replies.

Share This Page