Skip "Respawn" "Title Screen" dialog on death

Discussion in 'Plugin Development' started by potatofarms, Aug 10, 2012.

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

    potatofarms

    Hello, is it possible to skip the screen that appears when you die (with the "Respawn" and "Title Screen" buttons).
    I'm making a plugin for a server that I'm running where I want it to kick the player out of the server when they die and have them respawn, but when it does that and I log back in, the screen with the "Respawn" and "Title Screen" buttons appears.

    I'm using the PlayerDeathEvent to listen for when a player dies. Then use "Player player = event.getEntity();" to get the player that died, then I teleport them with "player.teleport(loc);" then kick them with "player.kickPlayer();".
     
  2. Offline

    travja

    Cancel the PlayerDeathEvent and clear their info then teleport them to spawn or where ever then kick them.
     
    potatofarms likes this.
  3. Offline

    potatofarms

    Ah, I did not think of doing this.
    Thanks man for the fast reply and good advice.
     
  4. Offline

    travja

    Any time!
     
  5. Offline

    potatofarms

    I seem to be having an issue with cancelling the event. The PlayerDeathEvent doesn't seem to be cancellable.

    "The method setCancelled(boolean) is undefined for the type PlayerDeathEvent"
     
  6. Offline

    travja

    You can't do event.setCancelled(true);??? that is really weird.... I would have to ask some people...
     
  7. Offline

    potatofarms

    Nope, it every time underlines it in red and says that setCancelled is undefined for PlayerDeathEvent.
    I also tried it with EntityDeathEvent and it seems to say the exact same thing.
     
  8. Offline

    travja

    ok, try PlayerDamageEvent and every time get how much damage is going to be done and if it drop their health past 0 cancel it then tele and kick them.
     
    potatofarms likes this.
  9. Offline

    potatofarms

    Alright, PlayerDamageEvent doesn't exist, so I used EntityDamageEvent, then checked if the entity is a player, converted it to a player and ran the code.

    Here's the code:
    Code:
    public void onDeath(EntityDamageEvent event) {
            Entity player = event.getEntity();
            if (player instanceof Player) {
                Player p = (Player) player;
                Integer damage = event.getDamage();
                Integer pHealth = p.getHealth();
                if (pHealth - damage <= 0) {
                    event.setCancelled(true);
                    Location loc = new Location(player.getWorld(), -492.18296, 69,
                            482.25593);
                    p.teleport(loc);
                    p.getInventory().clear();
                    p.setHealth(10);
                    p.setFoodLevel(10);
                    p.kickPlayer("You Died!");
                }
     
            }
        }
     
  10. Offline

    travja

    That should work except, they don't drop any items and will still be wearing their armor. You can use p.dropItemStack I think........ emphasis on think... and to clear their armor use p.getInventory().setHelmet(null) and do the same for chestplate pants an boots.
     
  11. Offline

    potatofarms

    I tested it out on my server, it works except it puts their health and hunger at half, so I fixed this by updating the setHealth and setFoodLevel to 20, I will have to test if it removes armor and all items. I will comment again to let you know what happens.
     
  12. Offline

    travja

    You need to tell it to remove their armor and to drop the items as they don't actually die.... so you would have to drop their armor and their inv. Good luck and good night....
     
    potatofarms likes this.
  13. Much more simple:
    Code:java
    1. public void autoRespawn(PlayerDeathEvent event)
    2. {
    3. final Player player = event.getPlayer();
    4. getServer().getScheduler().scheduleSyncDelayedTask(new Runnable(){ public void run() {
    5. if(player.isDeath())
    6. player.setHealth(20);
    7. }});
    8. }

    Setting the health while the player is death will remove the screen.
     
  14. Offline

    potatofarms

    Actually, the "p.getInventory().clear();" line works for clearing their entire inventory, however you are right that their armor doesn't get cleared. I used the code that you provided from your previous code to fix this, and it now 100% treats it like a death, but without that pesky screen!

    Thank you so much for your help in all of this, I wouldn't have been able to get this to work without you. :)
     
  15. Offline

    travja

    Code:java
    1.  
    2. Didn't know that, That would work too!
    3.  
    4. [quote="potatofarms, post: 1273498, member: 90710683"]Actually, the "p.getInventory().clear();" line works for clearing their entire inventory, however you are right that their armor doesn't get cleared. I used the code that you provided from your previous code to fix this, and it now 100% treats it like a death, but without that pesky screen!
    5.  
    6. Thank you so much for your help in all of this, I wouldn't have been able to get this to work without you. :)[/quote]
    7. The one thing is, it doesn't drop their items, just deletes them........
    8.  
    9. EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  16. Offline

    potatofarms

    I will have to try this out on my server, this looks interesting.
     
  17. Offline

    travja

    Also, his way would drop all items and xp naturally unlike the way you are using now as yours only deletes the items.
     
  18. Offline

    potatofarms

    This is the only problem with the code that I have encountered.
    "The method getServer() is undefined for the type playerOnDeathListener"

    "playerOnDeathListener" is the name of my listener class where it's:
    "public class playerOnDeathListener implements Listener"
     
  19. Yea, you basically give the server time to handle the death (so drop items, ...) and then you revive the player. The only missing bit is teleporting the player to the spawn, cause that happens when the player clicks on respawn, not when he dies (when he dies he's just invisible to other players, that's why you see your death spot behind the screen).

    either use plugin.getServer() (where plugin is a reference to your plugins instance) or Bukkit (no need for getServer() then, as Bukkit has getScheduler(), too).

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  20. Offline

    potatofarms

    Yeah, I changed it to Bukkit.getSchedualer(), however a new error appeared, this time underlining the "scheduleSyncDelayedTask" part.

    The error is this:
    "The method scheduleSyncDelayedTask(Plugin, Runnable) in the type BukkitScheduler is not applicable for the arguments (new Runnable(){})"

    the code is this:
    Code:
    final Player player = event.getEntity();
        Bukkit.getScheduler().scheduleSyncDelayedTask(new Runnable(){ public void run() {
        if(player.isDead())
        player.setHealth(20);
        }});
        }
    (Your exact code, except for Bukkit in place of getServer())
     
  21. okay, so you need a reference to your plugins instance and then use:
    Code:java
    1. final Player player = event.getEntity();
    2. plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){ public void run() {
    3. if(player.isDead())
    4. player.setHealth(20);
    5. }});
     
  22. Offline

    potatofarms

    The error is gone, I probably would have been able to figure out that easy error had I ever used the Scheduler before.
    I will now test on my server and see if it works.

    Alright, I ran it and died in the game and got this:
    Code:
    01:53:49 [SEVERE] Could not pass event PlayerDeathEvent to mrowsurvive
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:332)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62)
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:477)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:462)
            at org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerDeathEvent(C
    raftEventFactory.java:322)
            at net.minecraft.server.EntityPlayer.die(EntityPlayer.java:307)
            at net.minecraft.server.EntityLiving.damageEntity(EntityLiving.java:663)
     
            at net.minecraft.server.EntityHuman.damageEntity(EntityHuman.java:595)
            at net.minecraft.server.EntityPlayer.damageEntity(EntityPlayer.java:349)
     
            at net.minecraft.server.BlockCactus.a(BlockCactus.java:96)
            at net.minecraft.server.Entity.D(Entity.java:695)
            at net.minecraft.server.Entity.move(Entity.java:647)
            at net.minecraft.server.EntityLiving.e(EntityLiving.java:953)
            at net.minecraft.server.EntityHuman.e(EntityHuman.java:1073)
            at net.minecraft.server.EntityLiving.d(EntityLiving.java:1124)
            at net.minecraft.server.EntityHuman.d(EntityHuman.java:305)
            at net.minecraft.server.EntityLiving.h_(EntityLiving.java:447)
            at net.minecraft.server.EntityHuman.h_(EntityHuman.java:158)
            at net.minecraft.server.EntityPlayer.g(EntityPlayer.java:203)
            at net.minecraft.server.NetServerHandler.d(NetServerHandler.java:128)
            at net.minecraft.server.ServerConnection.b(SourceFile:35)
            at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
            at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:583)
            at net.minecraft.server.DedicatedServer.q(DedicatedServer.java:212)
            at net.minecraft.server.MinecraftServer.p(MinecraftServer.java:476)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:408)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)
    Caused by: java.lang.NullPointerException
            at me.gmail.potatofarms.mrowsurvive.playerOnDeathListener.autoRespawn(pl
    ayerOnDeathListener.java:16)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:330)
            ... 26 more
    Looking at this line:
    "Caused by: java.lang.NullPointerException
    at me.gmail.potatofarms.mrowsurvive.playerOnDeathListener.autoRespawn(pl
    ayerOnDeathListener.java:16)"
    Says it's on line 16 of the code which is this:
    "plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){ public void run() {"

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  23. You're sure plugin is a reference to your plugin and not null (post the whole class) ?
     
  24. Offline

    potatofarms

    I actually went back and fixed my previous code so it would do the same thing.
    Made it drop their entire inventory by doing a for loop, and then drop and remove their helmet, legs, boots, and chestplate.

    Code:
        public void onDeath(EntityDamageEvent event) {
            Entity player = event.getEntity();
            if (player instanceof Player) {
                Player p = (Player) player;
                Integer damage = event.getDamage();
                Integer pHealth = p.getHealth();
                if (pHealth - damage <= 0) {
                    event.setCancelled(true);
                   
                    for (ItemStack i : p.getInventory().getContents()) {
                        if (i != null) {
                            p.getWorld().dropItemNaturally(p.getLocation(), i);
                            p.getInventory().remove(i);
                        }
                    }
                    p.getWorld().dropItemNaturally(p.getLocation(),
                            p.getInventory().getHelmet());
                    p.getWorld().dropItemNaturally(p.getLocation(),
                            p.getInventory().getBoots());
                    p.getWorld().dropItemNaturally(p.getLocation(),
                            p.getInventory().getLeggings());
                    p.getWorld().dropItemNaturally(p.getLocation(),
                            p.getInventory().getChestplate());
                    p.getInventory().setHelmet(null);
                    p.getInventory().setBoots(null);
                    p.getInventory().setLeggings(null);
                    p.getInventory().setChestplate(null);
                   
                    Location loc = new Location(player.getWorld(), -492.18296, 69,
                            482.25593);
                    p.teleport(loc);
                   
                    p.setHealth(20);
                    p.setFireTicks(0);
                    p.setFoodLevel(20);
                }
     
            }
        }
    The code referencing the plugin was:
    Code:
    final mrowsurvive plugin = new mrowbeast();
    
    mrowsurvive is the name of the plugin class (mrowsurvive.java)
     
  25. Whis is(/was) wrong. You have to use the instance bukkit created for you, not a new one.
    Right:
    Code:java
    1.  
    2. final mrowsurvive plugin; // Global variable.
    3.  
    4. public playerOnDeathListener(mrowsurvive plugin) // Create a constructor for the class
    5. {
    6. this.plugin = plugin; // Attach the given reference to the global reference
    7. }

    then, in your main class you have to give the instance of it to the listener while creating the instance, so change
    new playerOnDeathListener();
    to
    new playerOnDeathListener(this);

    Using constructors to give variables/references to a class is very common at Java/plugin development, so learn how to do it. :)
     
  26. There was recently a thread about the exact same topic. It was also suggested to cancel the death and try to reset everything by hand. That's just stupid, though, the player never ACTUALLY dies on the server, breaking other stuff (oh, and "player health - damage < 0" doesn't consider armor, by the way).

    So I've did some testing, and the solution was quite simple: Simply fake the respawn packet that the client would normally send. You call one method and the rest is automatically done for you: the server respawns the client and the client just ignores the death screen!
    See my post here: http://forums.bukkit.org/posts/1248547/
    (you have to build against CraftBukkit)
     
  27. Offline

    Napkin222

    Guys, there's a packet to be used on the server. Packet9Respawn. Send this to the player and they will automatically respawn.
     
  28. Offline

    DrTURTLE2


    Bumping it from 2012? Sigh..
     
  29. Offline

    redytedy

    DrTURTLE2
    Exactly... That is his only post too.
     
  30. Offline

    Napkin222

    I was just searching for a different thread on Google and I thought this was still unanswered. Sorry.
     
Thread Status:
Not open for further replies.

Share This Page