Solved On Death cancel packet?

Discussion in 'Plugin Development' started by Xp10d3, Sep 11, 2020.

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

    Xp10d3

    Hello. So basically I was following TheSourceCode's tutorial on reading and canceling packets (which is relatively simple, actually), however I want to cancel the "You Died!" death screen and instead change the player's gamemode to spectator or something. Now, this is definitely client side, however is there a packet that handles whether the player died? The reason I'm asking about packets and not PlayerDeathEvent is because I have a KitPvP plugin that I found on Spigot that I'm using. If I make the player respawn using the Death Event and cancel it, that will conflict with the plugin and... you guessed it, not register the death and not give the players the credit required. So if I cancel the packet, the event will be called but still remove the "You Died!" screen.

    Code:
    Code:java
    1.  
    2. package eltik.no.spawn;
    3.  
    4. import io.netty.channel.*;
    5. import net.minecraft.server.v1_15_R1.PacketPlayInBlockDig;
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.player.PlayerJoinEvent;
    14. import org.bukkit.event.player.PlayerQuitEvent;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16.  
    17. public class Core extends JavaPlugin implements Listener {
    18.  
    19. @Override
    20. public void onEnable() {
    21. Bukkit.getPluginManager().registerEvents(this, this);
    22. }
    23.  
    24. @EventHandler
    25. public void onjoin(PlayerJoinEvent event){
    26. injectPlayer(event.getPlayer());
    27. }
    28.  
    29. @EventHandler
    30. public void onleave(PlayerQuitEvent event){
    31. removePlayer(event.getPlayer());
    32. }
    33. private void removePlayer(Player player) {
    34. Channel channel = ((CraftPlayer) player).getHandle().playerConnection.networkManager.channel;
    35. channel.eventLoop().submit(() -> {
    36. channel.pipeline().remove(player.getName());
    37. return null;
    38. });
    39. }
    40.  
    41. private void injectPlayer(Player player) {
    42. ChannelDuplexHandler channelDuplexHandler = new ChannelDuplexHandler() {
    43.  
    44. @Override
    45. public void channelRead(ChannelHandlerContext channelHandlerContext, Object packet) throws Exception {
    46. //Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.YELLOW + "PACKET READ: " + ChatColor.RED + packet.toString());
    47. super.channelRead(channelHandlerContext, packet);
    48. }
    49.  
    50. // What handles the packets
    51. @Override
    52. public void write(ChannelHandlerContext channelHandlerContext, Object packet, ChannelPromise channelPromise) throws Exception {
    53. //Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "PACKET WRITE: " + ChatColor.GREEN + packet.toString());
    54.  
    55. // Check if the packet is equal to some packet, then block it.
    56. if(packet instanceof PacketPlayInBlockDig){
    57. PacketPlayInBlockDig packetPlayOutBed = (PacketPlayInBlockDig) packet;
    58. Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "PACKET BLOCKED: " + ChatColor.GREEN + packetPlayOutBed.toString());
    59. return;
    60. }
    61. super.write(channelHandlerContext, packet, channelPromise);
    62. }
    63.  
    64.  
    65. };
    66.  
    67. ChannelPipeline pipeline = ((CraftPlayer) player).getHandle().playerConnection.networkManager.channel.pipeline();
    68. pipeline.addBefore("packet_handler", player.getName(), channelDuplexHandler);
    69.  
    70. }
    71. }
    72.  
     
  2. Offline

    gochi9

    Why don't you listen to EntityDamageEvent and then check if the event damage is bigger or as big as the player current health.Then you can cancel the event,and change the player gamemode to spectator

    EDIT: I haven't read about the part about giving credit to the killer.You can still listen to PlayerDeathEvent and upon death you can use
    Code:
    player.spigot().respawn()
    then teleport them to the death location and change their gamemode to spectator

    Also i feel like this should be on the "Plugin Development" section and not on "Plugin Requests"
     
    Last edited: Sep 11, 2020
  3. Offline

    Xp10d3

    oml i posted this in the wrong section im so dumb. i thought this was in plugin dev lol. and ty, ill try that out. tim can u move this xD
     
  4. Offline

    gochi9

    Also did that advice help you?
     
  5. Offline

    KarimAKL

    You can report your own post and request it to be moved. I've already done that for you. :p

    It sounds like the exact problem that is causing him to try using packets.
     
    Xp10d3 likes this.
  6. Offline

    gochi9

    Well he doesn't need to do it.He can still use PlayerDeathEvent,let the player die(so it would still count as a kill if killed by another player),forcefully revive the player, change his gamemode to spectator and teleport the player to it's death location. I thought he was concerned that the killer would not get the credit if he used PlayerDeathEvent but it should workd with what i just wrote
     
  7. Offline

    KarimAKL

    @gochi9 Oh. That's my bad. I misunderstood.

    @Xp10d3 PlayerDeathEvent isn't Cancellable, which means that you can't cancel it. I'd probably try what @gochi9 mentioned above.
     
  8. Offline

    gochi9

    Oh that's good.I thought i misunderstood his request.Well we'll have to wait for his response to see if that fixed his problem
     
  9. Offline

    Xp10d3

    @gochi9 I haven't tried that, but thank you for that info! That was extremely helpful :D I'll see how I can as you said "forcefully revive the player".
     
  10. Offline

    gochi9

    You can forcefully revive a player by using
    Code:
    player.spigot().respawn()
     
  11. Offline

    Xp10d3

    Ah I see. Thanks :) I'll take a look at this. Will let you know on any updates.
     
  12. Offline

    Xp10d3

    Alright, I messed around with this and it seems that when I do player.spigot.respawn(), it says, "Spigot is not resolved or not a type." However this is strange since I'm using Spigot 1.9 (don't have the .jar on me, and my skool laptop blocks getBukkit.org for some reason lol), not CraftBukkit.

    Code (I haven't registered the listeners yet :p):
    Code:java
    1.  
    2. package eltik.no.spawn;
    3.  
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.entity.PlayerDeathEvent;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9.  
    10. public class Core extends JavaPlugin implements Listener {
    11. public void onEnable() {
    12.  
    13. }
    14.  
    15. @EventHandler
    16. public void onDeath(PlayerDeathEvent event) {
    17. Player player = event.getEntity();
    18. player.spigot.respawn();
    19. }
    20. }
    21.  
     
  13. Offline

    gochi9

    It's player.spigot().respawn(); not player.spigot.respawn();
     
  14. Offline

    Xp10d3

    Thanks! It still seems to want to add a cast to player making it:
    Code:java
    1.  
    2. package eltik.no.spawn;
    3.  
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.entity.PlayerDeathEvent;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9.  
    10. public class Core extends JavaPlugin implements Listener {
    11. public void onEnable() {
    12.  
    13. }
    14.  
    15. @EventHandler
    16. public void onDeath(PlayerDeathEvent event) {
    17. Player player = event.getEntity();
    18. ((Object) player).spigot().respawn();
    19. }
    20. }
    21.  
    However, it keeps saying to change the cast of player, but that does absolutely nothing. Don't know what's happening :/
     
  15. Offline

    KarimAKL

    @Xp10d3 Are you using CraftBukkit or Spigot?
     
  16. Offline

    gochi9

    Yes you might not have the correct jar.I tested and it works fine for me
     
  17. Offline

    Xp10d3

    Spigot 1.9.
    @gochi9 oh huh. Sorry, I'll have to redownload the jar file then. mb

    EDIT 9/17/2020: Was going to send the .jar file to my skool laptop so I can test this during skool (which is where I code most of the time), however the email wasn't sent. Sorry for the delay.
     
    Last edited: Sep 17, 2020
  18. Offline

    Xp10d3

    Yep, that was the issue. I'll see when I can test this; thanks! It works now :D
     
Thread Status:
Not open for further replies.

Share This Page