Solved Giving player items in delayed task.

Discussion in 'Plugin Help/Development/Requests' started by oceantheskatr, Mar 26, 2015.

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

    oceantheskatr

    Hi, I'm making a plugin to give a player a diamond sword 3 seconds after the join event. This is a plugin I'm making to work on delayed tasks, but I'm having a problem properly fetching the player.

    Here is the error I get when it tries to run the code after 3 seconds: http://pastebin.com/VHuKBR5L

    Any help with this issue is welcome!

    Code:
    package me.themineshack;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Wait extends JavaPlugin implements Listener {
    
        @Override
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
       
        @Override
        public void onDisable() {
           
        }
       
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
        this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
              public void run() {
                  Player player = getServer().getPlayer(getName());
                  player.sendMessage(ChatColor.GREEN + "Your inventory has been updated.");
                  player.getInventory().addItem(new ItemStack(Material.DIAMOND_SWORD));
              }
            }, 60L);
       
        }
    }
     
  2. Offline

    nverdier

    @oceantheskatr So you're trying to get the Player with the name that getName() returns... You can just set the player to PlayerJoinEvent#getPlayer().
     
    oceantheskatr likes this.
  3. Offline

    oceantheskatr

    That seems to be exactly what I need, however I'm not exactly sure how I would do that, I tried changing "Player player = getServer().getPlayer(getName());" to "Player player = PlayerJoinEvent#getPlayer();" but that doesn't seem to be it.

    Would you mind elaborating a but for me?
     
  4. Offline

    nverdier

    @oceantheskatr Well you'd have to replace the "PlayerJoinEvent" with the actual instance, so 'e', and the '#' with a '.'.
     
    oceantheskatr likes this.
  5. Offline

    oceantheskatr

    Alright, so when I changed it to "Player player = e.getPlayer();" it gave me a red underline on the "e" part, and when I moused over it it said "Change modifier of 'e' to final." so I clicked on that and "public void onJoin(PlayerJoinEvent e) {" changed to "public void onJoin(final PlayerJoinEvent e) {".

    I compiled the plugin and ran my server, and it seems to have worked!

    Thank you so much for your assistance! :)
     
    nverdier likes this.
  6. Offline

    nverdier

    oceantheskatr likes this.
Thread Status:
Not open for further replies.

Share This Page