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 =- )
@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!
@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
@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.
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); }
@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
@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 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?
@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.
@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
@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<?>>>>'
@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