Problem with adding a delay to PlayerRespawnEvent

Discussion in 'Plugin Development' started by Madenman, Mar 10, 2021.

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

    Madenman

    So im trying to code it that when you respawn you get an effect, the problem with this is that while respawning a player cant get an effect because the player itself hasn't spawned in the world yet. To solve this i tried adding a delay to the effect. I just started with coding around 2 Weeks ago, so adding delays is something new for me.
    I looked it up multiple times, searched on forums etc. But i just cant get it to work.
    When you reply please try keeping it simple, so i can understand because i'm new to coding.


    This is my code: (One of my attempts to get it working, i also changed the event here to test it easier, in this case it just sends the first message "testv1" but nothing else.)
    Code:
        @EventHandler
            public void onPlayerMove(PlayerMoveEvent event){
                Player player = event.getPlayer();
                player.sendMessage("test v1");
                Bukkit.getScheduler().scheduleSyncDelayedTask((Plugin) event, new Runnable() {
                    public void run() {
                        Bukkit.broadcastMessage("This message is shown after one second");
                        player.sendMessage("test");
                        player.addPotionEffect(new PotionEffect(PotionEffectType.HEALTH_BOOST, Integer.MAX_VALUE, 4, false));                            
    
                    }
                }, 20L); //20 Tick (1 Second) delay before run() is called
            }
    edit: some mod edited my post and put his problem here, removed it but his attached image is still there. Dont care to remove it
     

    Attached Files:

    Last edited: Mar 14, 2021
  2. Offline

    CraftCreeper6

    @Madenman
    You are casting your event to Plugin? What?

    What's your main class? Pass that into the scheduler instead of (Plugin) event.
     
  3. Offline

    Madenman

    So I did this:
    Code:
        @EventHandler
        public void onPlayerMove(PlayerMoveEvent event){
            Player player = event.getPlayer();
            player.sendMessage("test v1");
        Bukkit.getScheduler().scheduleSyncDelayedTask((multishot) event, new Runnable() {
                public void run() {
                    Bukkit.broadcastMessage("This message is shown after one second");
                    player.sendMessage("test");
                    player.addPotionEffect(new PotionEffect(PotionEffectType.HEALTH_BOOST, Integer.MAX_VALUE, 4, false));
    
                }
            }, 20L); //20 Tick (1 Second) delay before run() is called
        }
    Changed it to my main class (its where you register the events right?)
    But now its telling me this:
    Inconvertible types; cannot cast 'org.bukkit.event.player.PlayerMoveEvent' to 'com.Madenman.Multishot.multishot'
    What now?

    Edit:
    Alr so at the Scheudler i added a bracket that didnt belong there:
    Code:
    ((multishot) event, new Runnable() { 
    and changed it to
    Code:
    (multishot) event, new Runnable() { 
    But its still not working. Its telling me this:
    ";" expected
    not a statement

    Im new to coding, but Im pretty sure the ; doesnt belong there.
     
    Last edited: Mar 10, 2021
  4. Offline

    CraftCreeper6

    @Madenman
    Not what I meant.

    Your main class is your 'Plugin'. You need to pass the instance of your main class into the scheduler.

    So instead of (multishot) event, you need to pass the instance of your main class into it.

    An ideal way of doing this is creating a constructor in your listener class that takes in your main class (you can use the keyword 'this' to pass the instance to the registerEvents method) and use that instance in your runnable.
     
  5. Offline

    Madenman

    So like this:
    Code:
    final int test = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    ? Still d0oesnt work tho, also had to ad the int test thing at the start, dunno if its suppoed to be f"declare final" or declare var type.
    am new so there are just some parts i dont understand
     
  6. Offline

    CraftCreeper6

    @Madenman
    I suggest you learn some Java before trying to use the Bukkit API.

    Is the scheduler in your main class?
     
  7. Offline

    Madenman

    Im assuming you mean if the code i showed you is in my main class, it is not. The code is in a class I made called "playervents", while the main class is called "multishot".
     
  8. Offline

    davidclue

    I'm going to just spoonfeed you this answer because it will help you understand more about java and help you learn when you see it your code should look something like this

    Code:
    imports here
    
    public class playerevents implements Listener {
    
        private Main plugin; //if your main class is not called Main then change this to whatever it's called
    
        public playerevents(Main plugin) {
            this.plugin = plugin;
        }
    
        @EventHandler
        public void onPlayerMove(PlayerMoveEvent event) {
            Player player = event.getPlayer();
            player.sendMessage("test v1");
            Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                public void run() {
                    Bukkit.broadcastMessage("This message is shown after one second");
                    player.sendMessage("test");
                    player.addPotionEffect(new PotionEffect(PotionEffectType.HEALTH_BOOST, Integer.MAX_VALUE, 4, false));
    
                }
            }, 20L); //20 Tick (1 Second) delay before run() is called
        }
    }
    This is what your class should look like and now I will explain

    Code:
    private Main plugin;
    
        public playerevents(Main plugin) {
            this.plugin = plugin;
        }
    This part of the code is where you declare what values you want inside your class it is a method that you can call from any other class or even inside this class to create a new object (instance). So for example to create a new instance of this class you can use
    Code:
    playerevents object = new playerevents(plugin);
    
    or if you aren't going to use this object for anything else but this line then use:
    
    new playerevents(plugin);
    Think of it like your main class is a cookie jar and what you are doing in that method is asking for a cookie every time in that bracket, when you create a new instance of your playerevent class, you are taking a cookie from that jar and giving it to your playerevent class and with that cookie you can call methods that require a JavaPlugin such as the delayed task method.
     
  9. Offline

    Madenman

    Alright, I added your code and modified my main class. Its working! Thank you so much for helping me, i have been trying to get this working for the past 5 days. Thank you aswell for explaining it, that helped alot aswell!
    You are my Hero, thank you so much!


    To anyone else having this or a simillair problem, take the code davidclue send in, replace the "main" with your main class, then in your main class change the eventRegister from this:
    Code:
    getServer().getPluginManager().registerEvents(new yourclass(), this);
    to this:
    Code:
    getServer().getPluginManager().registerEvents(new yourclass(this), this);
    Then your delay will work! :)

    Again thank you so much for helping me, I don't know what I would have done without your help!
     
    Last edited: Mar 12, 2021
  10. Offline

    davidclue

    Np, happy to hear it helped! Also a much more efficient way of registering the events is adding this instead for your class
    Code:
    public playerevents(Main plugin) {
            this.plugin = plugin;
           
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
    And then in your main class for the onEnable() method all you need to do is add this
    Code:
    new playerevents(this);
     
Thread Status:
Not open for further replies.

Share This Page