Util Auto respawn player!

Discussion in 'Resources' started by mine-care, Mar 24, 2015.

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

    mine-care

    Hello bukkit comunity!
    Who does not like minigames... few people dont, because there are so many out there to chose from! Some of them like war or kit pvp pligins may reqire all users to be playing and not being in the state where they are asked to click respawn. So there is actually a really easy method to do this by just using the following method:
    Code:
        public static void autoRespawnPlayer(Player who) throws ReflectiveOperationException {
            String bukkitversion = Bukkit.getServer().getClass()
                    .getPackage().getName().substring(23);
            Class<?> cp = Class.forName("org.bukkit.craftbukkit."
                    + bukkitversion + ".entity.CraftPlayer");
            Class<?> clientcmd = Class.forName("net.minecraft.server."
                    + bukkitversion + ".PacketPlayInClientCommand");
            Class enumClientCMD = Class.forName("net.minecraft.server."
                    + bukkitversion + ".EnumClientCommand");
            Method handle = cp.getDeclaredMethod("getHandle", null);
            Object entityPlayer = handle.invoke(who, null);
            Constructor<?> packetconstr = clientcmd
                    .getDeclaredConstructor(enumClientCMD);
            Enum<?> num = Enum.valueOf(enumClientCMD, "PERFORM_RESPAWN");
            Object packet = packetconstr.newInstance(num);
            Object playerconnection = entityPlayer.getClass()
                    .getDeclaredField("playerConnection").get(entityPlayer);
            Method send = playerconnection.getClass().getDeclaredMethod("a",
                    clientcmd);
            send.invoke(playerconnection, packet);
        }
    
    Another method provided by @Konato_K is this:
    Code:
    public void respawnPlayer(Player paramPlayer) {
      if (paramPlayer.isDead())
      ((CraftServer)Bukkit.getServer()).getHandle().moveToWorld(((CraftPlayer)paramPlayer).getHandle(), 0, false);
    }
    Hope you like it!
    i would love to hear your feedback!
    Thanks =- )
     
    Last edited: Mar 27, 2015
  2. Offline

    sgavster

    @mine-care I love this code, I actually didn't know you made it (found it in a post, used it for a private plugin) this is awesome though, thanks!
     
  3. Offline

    mine-care

    @sgavster i didnt actually made it, i got it in the form of normal NMS code and parsed it into reflection for further convinience, i had it laying arround in my codebase and got it out now :p
     
    sgavster likes this.
  4. @mine-care Awesome utility. Although, I have a method that does this in less code (not trying to take the spotlight):
    Code:
    @EventHandler
        public void onPlayerDeath(final PlayerDeathEvent event) {
            final Player player = event.getEntity();
            final CraftPlayer craftPlayer = (CraftPlayer) player;
            plugin.getServer().getScheduler()
                    .scheduleSyncDelayedTask(plugin, new Runnable() {
                        public void run() {
                            if (player.isDead()) {
                                craftPlayer.getHandle().playerConnection
                                        .a(new PacketPlayInClientCommand(
                                                EnumClientCommand.PERFORM_RESPAWN));
                            }
                        }
                    });
        }
    Just thought that I'd share this. :)
     
  5. Offline

    Konato_K

    Am I the only one who prefers this?

    Code:
    public void respawnPlayer(Player paramPlayer) {
      if (paramPlayer.isDead())
      ((CraftServer)Bukkit.getServer()).getHandle().moveToWorld(((CraftPlayer)paramPlayer).getHandle(), 0, false);
    }
    
     
  6. Offline

    mine-care

    @CodePlaysMinecraft @Konato_K its good that you guys shared your code but the one above is version independed whereas yours will work only with the compile version. The one above uses reflection that can explain its size
    in the case of @Konato_K its an interesting way.... i might add it to the post if thats alright :)
     
    CodePlaysMinecraft likes this.
  7. Offline

    Konato_K

    @mine-care Yeah mine it's version dependant, but it's kinda shorter, I usually write interfaces for my NMS codes and an implementation for each version so I never worry about unsupported versions and that.

    I found that code by digging in the NMS of the server and stuff, so you're free to use it
     
    mine-care likes this.
  8. Offline

    mine-care

    @Konato_K thanks, will look on that when I go back home!
     
  9. Offline

    guitargun

    @mine-care can this also work to delay any respawns? like I die and I have to wait 10 seconds before respawning or do I need to cancel any player actions for it?
     
  10. Offline

    mine-care

    @guitargun ofcaurse it will work, the only thinkg you have to do is schedule it :)
     
  11. Offline

    ChipDev

    mine-care likes this.
  12. Offline

    teej107

    @ChipDev Hey you're back!

    @mine-care Good resource. You should store the Classes/Methods/etc so you don't have to look them up everytime you want to do a respawn.
     
    mine-care and ChipDev like this.
  13. Offline

    mine-care

    @ChipDev thx!!

    @teej107 that would be a good idea.... i was aiming mainly to make it compact as a method without outer variables but efficiency-wise youre right, ill update it asap
     
    Last edited: Mar 28, 2015
  14. Offline

    hubeb

    @mine-care This line errors (any ideas):
    "Enum<?> num =Enum.valueOf(enumClientCMD, "PERFORM_RESPAWN");"
    Error:
    Inferred type 'capture<T>' for type 'T' is not within its bound; should extend 'java.lang.Enum<capture<? extends java lang.Enum<capture<?>>>>'
     
    Last edited: Mar 30, 2015
  15. Offline

    mine-care

    @hubeb remove <?> from the enum. i am not sure, i cant check it now because i am on phone D: but give it a try
     
  16. Offline

    hubeb

    @mine-care nope then it says Identifier Expected 'Enum<>'
     
  17. Offline

    mine-care

    @hubeb Hmmm what java version are you on?
     
  18. Offline

    hubeb

    @mine-care 1.8.0_40-b26 (bukkit 1.8.3 latest)
     
  19. Offline

    mine-care

    @hubeb ahh mabe it is java :( try with java version 1.7.X
     
  20. Offline

    hubeb

    @mine-care ok will do, could it be an issue with bukkit 1.8.3?
     
  21. Offline

    mine-care

    @hubeb not sure, I have t experienced with it at all. I don't even have it :3
     
Thread Status:
Not open for further replies.

Share This Page