Giving a player items on respawn!

Discussion in 'Plugin Development' started by Zeroth, Feb 28, 2011.

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

    Zeroth

    So I'm a bit confused. When a player respawns, i want a few items to be added to the inventory.

    So heres my very simple player respawn listener code:
    Code:
    public void onPlayerRespawn(PlayerRespawnEvent event) {
            if (plugin.gameIsOn()) {
                event.setRespawnLocation(plugin.getRespawnLocation(event.getPlayer()));
            }
            plugin.giveStartKit(event.getPlayer());
        }
    And here is my giveStartKit method:
    Code:
    public void giveStartKit(Player player) {
            if (!gameIsOn()) {
                player.sendMessage(ChatColor.YELLOW+"WAIT FOR THE GAME TO START, DUMMY!");
                return;
            }
            player.sendMessage("Here ya go, champ!");
            player.getInventory().addItem(new ItemStack(Material.DIAMOND_PICKAXE, 1));
            player.getInventory().addItem(new ItemStack(Material.DIAMOND_SPADE, 1));
    }
    And this doesn't work :/ Before I added in the player.sendMessage(), I figured that the items were being added before the player actually spawned, thereby having the inventory cleared. After adding in the message though, I found that you actually do get sent a message after you have respawned completely.

    I even tried scheduling a delayed task that would wait 1 second before changing the inventory. Still got the message 1 second later, but still no items. Then I tried 5 seconds to make sure there was no respawn lag (or something), but still no items.

    Anyone know how I could get items into a player's inventory on respawn?
     
  2. Offline

    flyslasher

    try this after you add items to it,
    Code:
    ((CraftPlayer)player).getHandle().activeContainer.a();
     
  3. Offline

    Zeroth

    Nope, doesn't work :/ The javadocs are down right now though, so I don't even know what that line of code does.
    --- merged: Mar 3, 2011 6:20 PM ---
    Actually I got it working! A bit roundabout, but oh well!

    Code:
    	public void giveStartKit(Player player) {
    		// gives player the starting kit.
    		if (!this.gameIsOn()) {
    			player.sendMessage(ChatColor.YELLOW+"WAIT FOR THE GAME TO START, DUMMY!");
    			return;
    		}
    		final String playerName = player.getName();
    		int giveKitID = getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    		    public void run() {
    		    	Player player = getServer().getPlayer(playerName);
    		    	player.sendMessage("Giving start kit...");
    				player.getInventory().addItem(new ItemStack(Material.DIAMOND_PICKAXE, 1));
    				player.getInventory().addItem(new ItemStack(Material.DIAMOND_SPADE, 1));
    		    }
    		}, 20L);
    	}
     
  4. Offline

    Sammy

    Try this:

    Code:
    ItemStack PlyStack = new ItemStack(matID);
    PlyStack.setAmount((qnt));
    Ply.getInventory().addItem(PlyStack);
    
     ((CraftPlayer) Ply).updateInventory();
    PS: I had problems making this all in the same line like you are doing, but this way it worked
     
  5. Offline

    Zeroth

    Well I did get it to work in a round about way (my last post), which is fine for me now.
    Thanks for the input though, I'll comment that into my code for later.
     
  6. Offline

    Sammy

    Cool,
    If you don't mind I may be using your way in the future ;)
     
  7. Offline

    Zeroth

    Of course not! I think that the problem lies in making a new player object after death (or something...) which is (maybe) why my reference to the (old?) player wasn't updating in the (new?) player:
    final String playerName = player.getName();
    (wait for player to actually respawn)
    Player player = getServer().getPlayer(playerName);
     
  8. Offline

    Chiss

    Hey man, i completed understand the issue you were having. Most of these replies don't seem to understand, and think you're just having issues spawning items. The purpose of this post is mostly to help other people in need who end up here.

    Anyway, after toying around with it for ages, i came to a similar thing as you.

    I was waiting for RespawnEvent, then waiting ~2 seconds.
    Then placing items in to inventory.
    It was going through all the give item codes, but nothing showed up.

    It turned out that i needed to run this code to RE-GET the player.
    Player newPlayer = getServer().getPlayer(player.getDisplayName());


    SOLUTION
    To give items to a player on respawn with Bukkit, you need to do two things;


    1- Delay the giving for 1-2 seconds on their respawn
    plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, Runnable Code, Delay (20 ~= 1 second))

    2- Re-getting the Player entity from the server AFTER the Respawn event.
    Player newPlayer = getServer().getPlayer(player.getDisplayName());
     
  9. Offline

    Ne0nx3r0

    It's worth mentioning there is a configurable mod that provides this functionality: NoDrop

    I use it and it's pretty sweet. You might take a peek at the source and see what he's done.
     
  10. Offline

    Chiss

    To be fair, its nothing too difficult, its just Bukkit does something really wierd with Player objects on Respawn. Once you know how to get around it, tis easy.
     
Thread Status:
Not open for further replies.

Share This Page