Player Disconnection on onPlayerRespawn

Discussion in 'Plugin Development' started by videege, Apr 4, 2011.

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

    videege

    Hey guys,

    I've been writing a plugin for bukkit and I'm trying to give players some items whenever they die. Here's the relevant code below:

    Code:
    @Override
    public void onPlayerRespawn(PlayerRespawnEvent event)
    {
        Player p = event.getPlayer();
        if (this.plugin.playerController.isActive(p)) {
            System.out.println("Giving player kit: " + p.getDisplayName());
            p.getInventory().addItem(new ItemStack(Material.BOW, 1););
            p.getInventory().addItem(new ItemStack(Material.ARROW, 1));
        }
    }
    The event is registered on the PLAYER_RESPAWN event at the lowest priority, but I've also played around with various priorities with no obvious effects.

    When a player dies, their client crashes (1.4) and the server spits out this message:

    Code:
    20:36:38 [SEVERE] java.net.SocketException: Connection reset
    20:36:38 [SEVERE]       at java.net.SocketInputStream.read(Unknown Source)
    20:36:38 [SEVERE]       at java.net.SocketInputStream.read(Unknown Source)
    20:36:38 [SEVERE]       at java.net.SocketInputStream.read(Unknown Source)
    20:36:38 [SEVERE]       at java.io.FilterInputStream.read(Unknown Source)
    20:36:38 [SEVERE]       at net.minecraft.server.Packet.b(Packet.java:73)
    20:36:38 [SEVERE]       at net.minecraft.server.NetworkManager.f(NetworkManager.java:130)
    20:36:38 [SEVERE]       at net.minecraft.server.NetworkManager.c(NetworkManager.java:229)
    20:36:38 [SEVERE]       at net.minecraft.server.NetworkReaderThread.run(SourceFile:68)
    20:36:38 [INFO] videege lost connection: disconnect.genericReason
    
    The console output ("Giving player kit...") is never displayed, but I know for a fact that the isActive function is perfectly working (basically just a boolean check as to whether or not the player object is in a list).

    My test server is a 64-bit Ubuntu 9.10 linux box running CB 612. Thanks for any assistance!
     
  2. Offline

    Zeroth

    I have a solution for you! I had this problem and found a roundabout way of solving it.

    I have this method in my main method which is called from the respawn event. The method starts a timer for 10 server ticks (half a second I think) which then gives the player the items. Its the only solution I could find to the problem.

    Make sure if you use any variables inside the run() {} that were declared outside of it that they are declared as final. The last line of code is the delay in server ticks.
    Code:
    public void giveStartKit(Player player) {
      final String playerName = player.getName();
            int giveKitID = getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                public void run() {
                    Player player = getServer().getPlayer(playerName);
                    player.sendMessage(ChatColor.GRAY+"Giving start kit...");
                    player.getInventory().addItem(new ItemStack(Material.DIAMOND_PICKAXE, 1));
                    player.getInventory().addItem(new ItemStack(Material.DIAMOND_SPADE, 1));
                    getTeam(player).giveHat(player);
                }
            }, 10L);
     
  3. Offline

    videege

    Zeroth, thanks for the reply. I was going to update this post but sort of forgot :D. I found that using a delayed task still occasionally crashes the client if the player hasn't respawned (i.e., clicked the respawn button) by the time the task goes off. I fixed it by adding dead players to a list and then checking during the player movement event to see if they were in that list, then giving them the items. This ensures they are up and moving by the time the items are given.

    Thanks again for your reply!
     
  4. Offline

    desmin88

    Semicolon bolded and made red. I'm pretty sure it should not be there.
    Code:
    @Override
    public void onPlayerRespawn(PlayerRespawnEvent event)
    {
        Player p = event.getPlayer();
        if (this.plugin.playerController.isActive(p)) {
            System.out.println("Giving player kit: " + p.getDisplayName());
            p.getInventory().addItem(new ItemStack(Material.BOW, 1)[COLOR=rgb(255, 0, 0)][B];[/B][/COLOR]);
            p.getInventory().addItem(new ItemStack(Material.ARROW, 1));
        }
     
Thread Status:
Not open for further replies.

Share This Page