Solved Wait 10 seconds before moving on through the code...?

Discussion in 'Plugin Development' started by pokekart2014, Dec 2, 2015.

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

    pokekart2014

    So, I am fairly new to Java, and I started a plugin called MobPowers.

    It basically makes the player behave like a mob configured in the plugin.
    I was on to creating the Ghast part, after successfully making the Creeper and Slime part, when I notice that I didn't know how to wait for a few seconds.

    I would like to allow the player to fly for 10 seconds, then drop down to the ground, disabling the flight.

    The code I have so far is this, but of course, I just searched the Try Catch part from Google. This seems to throw an error in the console when executed.

    How would I do what I want, and also, what is wrong with this?

    Code:
    if (getConfig().getString("mob.name").equalsIgnoreCase("ghast")) {
        p.setFlying(true);
        p.sendMessage(ChatColor.GREEN + "Ghast ability used!");
        p.sendMessage(ChatColor.GOLD.toString() + CHatColor.BOLD.toString() + "You can now fly for 10 seconds!");
        try {
            Thread.sleep(10000);
        } catch(InterruptedException ex) {
            Thread.currentThread().interrupt();
            p.setFlying(false);
    }

    Do note that I did not copy and paste this straight from the code, which means I might have some spelling mistakes. I do not have any spelling errors in the final copy, so please do not question me about spelling or capitalization here.

    Just why this isn't working, and what I should do.
     
  2. @pokekart2014 Issue time! Whooo

    1. You don't need to do .toString for the ChatColors, it will do it already that's how the ChatColors work. If you want two next to eachother just do ChatColor.BLAH + "" + ChatColor.BLAH or have a string at the start. It is just using that because it wants a String not an enum
    2. You are sleeping the MAIN thread so you will freeze the server.
    3. Why are you trying to interrupt the thread?!?!?!
     
  3. Offline

    pokekart2014

    1. That is how I learnt it when combining both color and formatting, but OK. I can stick to your option, which most people do seem to be using.
    2. How will I sleep only the function, and not the main thread?
    3. That Try/Catch code is a pure copy and paste with a little exceptions. I do not understand it. Would be nice if you could explain....
     
  4. This is wrong... don't use threads to achieve this. Im pretty sure you would use this:
    Code:
    this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable()
    {
    public void run()
    {
    // doo stuffz
    }
    }, 200L);
    
     
    bwfcwalshy likes this.
  5. Offline

    pokekart2014

    This is returning an error in the console...
    Code:
    Code:
    p.setFlying(true);
    //BLAH BLAH
    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
        public void run() {
            p.setFlying(false);
        }
    }, 200);
    Something about Cannot make player fly if getAllowFlight() is false....
    Um, what is this?
     
  6. Offline

    CraftCreeper6

  7. Offline

    Scimiguy

    Itd sure be nice to see that error
     
  8. Offline

    pokekart2014

    It would. But the thing is, I tested the code again with @CraftCreeper6 's little snippet, and I got it successfully working, which means that I can no longer reproduce that error anymore. :(

    Working code, which was the solution.
    Code:
    p.setAllowFlight(true);
    p.setFlying(true);
    //Blah blah sending messages...
    Bukkit.getServer().getScheduler.scheduleSyncDelayedTask(this, new Runnable() {
        public void run() {
            p.setFlying(false);
           //And just in case
            p.setAllowFlight(false);
        }
    }, 200)

    Uff, By the way, any way to disable fall damage only on this occasion? So, when the player gets a fall damage event from this particular flying ability, and not from all falling damages.
     
  9. Offline

    CraftCreeper6

    @pokekart2014
    Add them to an arraylist.
    Check the corresponding event.
    If the cause of damage is fall and they are in the list, cancel the damage.
     
    TheNewTao likes this.
  10. Offline

    mcdorli

    I seriously want a feature on bukkit.org, where there is a button called "often used answers" with the typical "don't blind cast the sender to player", "String functions aren't void", "learn java", etc. It would make the time I write 70% of my answers down much shorter.
     
  11. Offline

    teej107

    toString() is perfectly fine for this if you have 2 enums or Objects next to each other and if you call it on one of the adjacent enums or Object. Anything more and it gets redundant.
     
Thread Status:
Not open for further replies.

Share This Page