[HELP] Waiting about 4 seconds

Discussion in 'Plugin Development' started by x86cam, Jun 13, 2012.

  1. Offline

    x86cam

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    So I am trying to make it so that you first spawn, and wait 4 seconds, then the messages will greet you, how would I wait for that long, I tried wait(80); surrounded by try/catch, that didn't work.

    Here is my code, I want it to execute before the greetings:
    PHP:
            @EventHandler
            
    public void onJoinLogin(PlayerJoinEvent e)
            {
                
    Player player e.getPlayer();
                
    // Check if it's their first join.
                
    World w player.getWorld();
                
    File file = new File(w.getName()
                        + 
    "/players/" player.getName() + ".dat");
                
    boolean exists file.exists();
                if (!
    exists) {
                    
    teleportToFirstSpawn(player);
                    
    Bukkit.broadcastMessage(ChatColor.DARK_PURPLE player.getDisplayName() + " has joined the server for the first time!");
                }else{
                    
    Bukkit.broadcastMessage(ChatColor.YELLOW player.getDisplayName() + ", welcome again!");
                }
               
            }
  2. Offline

    r0306

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
  3. Online

    LucasEmanuel

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Freeze the player with one the methods in the other thread on this forum and make a delayed task that unfreezes him after 80 ticks. Never, ever, ever, ever, ever freeze the main CraftBukkit thread! Ever.
  4. Online

    desht

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    You can also use player.hasPlayedBefore() (javadocs) to see if the player has been on your server before. No need to check for physical file locations.
  5. Offline

    x86cam

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I am trying to do a FUS RO DAH! chat command, and I want the explosions to wait ~5 ticks before exploding so they do not explode at the exact same time. This is my event:
    PHP:
            @EventHandler
            
    public void onPlayerChatting(final PlayerChatEvent e)
            {
                if(
    e.getMessage().equals("FUS RO DAH!")){
                 
                    final 
    World world e.getPlayer().getWorld();
                    final 
    Location location e.getPlayer().getLocation();
                    final 
    Location location1 location.add(444);
                    final 
    Location location2 location1.add(232);
                    final 
    Location location3 location2.add(452);
                    final 
    Location location4 location3.add(243);
                    final 
    Location location5 location4.add(444);
                 
                    
    getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {public void run() {world.createExplosion(location0F);}}, 5L);
                    
    getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {public void run() {world.createExplosion(location10F);}}, 5L);
                    
    getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {public void run() {world.createExplosion(location20F);}}, 5L);
                    
    getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {public void run() {world.createExplosion(location30F);}}, 5L);
                    
    getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {public void run() {world.createExplosion(location40F);}}, 5L);
                    
    getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {public void run() {world.createExplosion(location50F);}}, 5L);
                 
                }
            }
    I am getting an error, any ideas?

    This post has been edited 1 time. It was last edited by x86cam Jun 14, 2012.
  6. Offline

    bartboy8

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Why do you have final in front of all your locations?
  7. Online

    desht

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Since you don't say what error you're getting, it's kind of hard to know. But I can see one problem: your usage of .add() on those Locations is incorrect. Unfortunately, Location objects are mutable (poor design!), and .add() modifies the object in-place (as well as returning the modified object, not a new object). So location1 = location.add(4, 4, 4) ends up setting both location1 and location to the same thing. All of your locations are going to end up pointing to the same place. The simplest way around this is to do location1 = location.clone().add(4,4,4).

    (Also, if you want the explosion not to happen at the same time, you probably want a different delay for each delayed task you schedule)

    That's essential if you're passing an object into a Runnable like that. Since it's code that will be executed in the future, the compiler needs to know that the object isn't going to change in the meantime.
  8. Offline

    ferrybig

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I recommend doing like this:
    Code:java
    1.  
    2. @EventHandler
    3. public void onPlayerChatting(final PlayerChatEvent e)
    4. {
    5. if(e.getMessage().equals("FUS RO DAH!")){
    6.  
    7. final World world = e.getPlayer().getWorld();
    8. final Location location = e.getPlayer().getLocation();
    9. final Location location1 = location.clone().add(4, 4, 4);
    10. final Location location2 = location1.clone().add(2, 3, 2);
    11. final Location location3 = location2.clone().add(4, 5, 2);
    12. final Location location4 = location3.clone().add(2, 4, 3);
    13. final Location location5 = location4.clone().add(4, 4, 4);
    14. Schedular sd = getServer().getScheduler();
    15. sd.scheduleSyncDelayedTask(plugin, new ExplotionTask(location), 5L);
    16. sd.scheduleSyncDelayedTask(plugin, new ExplotionTask(location1), 10L);
    17. sd.scheduleSyncDelayedTask(plugin, new ExplotionTask(location2), 15L);
    18. sd.scheduleSyncDelayedTask(plugin, new ExplotionTask(location3), 20L);
    19. sd.scheduleSyncDelayedTask(plugin, new ExplotionTask(location4), 25L);
    20. sd.scheduleSyncDelayedTask(plugin, new ExplotionTask(location5), 30L);
    21. }
    22. } private class ExplotionTask implements Runnable
    23. {Location loc;ExplotionTask(Location loc){this.loc = loc;}
    24. public void run(){} loc.getWorld().createExplosion(loc, 0F);
    25. }
    ^^ may contain bugs

    This post has been edited 1 time. It was last edited by ferrybig Jun 14, 2012.
  9. Offline

    x86cam

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I figured it out after I posted it.

Share This Page