Send Player Sleep Animation with ProtocolLib

Discussion in 'Plugin Development' started by Weasel_Squeezer, Jan 24, 2014.

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

    Weasel_Squeezer

    I am having trouble trying to get a player to make the sleep animation using ProtocolLib. I know it is possible, I just can't figure it out. I know the packed ID for the bed animation is 70. None of the ProtocolLib examples are for anything like this.
    Can anyone give me some hints?
     
  2. Offline

    Comphenix

    You can trigger this, but it looks like the client automatically pushes the player entity into the ground. Though, I remember counteracting this by moving the player up in the Y-direction. Here's an old example that does just that.

    In any case, here's a more recent version that doesn't rely on deprecated packets (download):
    Code:java
    1. public class ExampleMod extends JavaPlugin {
    2. private ProtocolManager manager;
    3.  
    4. // Just a way to record if a player is asleep
    5. private Set<Player> sleeping = Collections.newSetFromMap(new WeakHashMap<Player, Boolean>());
    6.  
    7. @Override
    8. public void onEnable() {
    9. manager = ProtocolLibrary.getProtocolManager();
    10. }
    11.  
    12. @Override
    13. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    14. if (sender instanceof Player) {
    15. Player player = (Player) sender;
    16.  
    17. if (sleeping.add(player)) {
    18. playSleepAnimation(player);
    19. } else {
    20. stopSleepAnimation(player);
    21. sleeping.remove(player);
    22. }
    23. }
    24. return true;
    25. }
    26.  
    27. /**
    28.   * Play the sleep animation for every nearby player.
    29.   * @param alseep - the player asleep.
    30.   */
    31. private void playSleepAnimation(Player asleep) {
    32. final PacketContainer bedPacket = manager.createPacket(PacketType.Play.Server.BED, false);
    33. final Location loc = asleep.getLocation();
    34.  
    35. // [url]http://wiki.vg/Protocol#Use_Bed[/url]
    36. bedPacket.getEntityModifier(asleep.getWorld()).
    37. write(0, asleep);
    38. bedPacket.getIntegers().
    39. write(1, loc.getBlockX()).
    40. write(2, loc.getBlockY() + 1).
    41. write(3, loc.getBlockZ());
    42.  
    43. broadcastNearby(asleep, bedPacket);
    44. }
    45.  
    46. private void stopSleepAnimation(Player sleeping) {
    47. final PacketContainer animation = manager.createPacket(PacketType.Play.Server.ANIMATION, false);
    48.  
    49. // [url]http://wiki.vg/Protocol#Animation[/url]
    50. animation.getEntityModifier(sleeping.getWorld()).
    51. write(0, sleeping);
    52. animation.getIntegers().
    53. write(1, 2);
    54.  
    55. broadcastNearby(sleeping, animation);
    56. }
    57.  
    58. private void broadcastNearby(Player asleep, PacketContainer bedPacket) {
    59. for (Player observer : manager.getEntityTrackers(asleep)) {
    60. try {
    61. manager.sendServerPacket(observer, bedPacket);
    62. throw new RuntimeException("Cannot send packet.", e);
    63. }
    64. }
    65. }
    66. }
     
  3. Offline

    Weasel_Squeezer

    Oh awesome! From the author himself. Thank you very much for this awesome API, and for your help!
     
    Comphenix likes this.
  4. Offline

    Mr_Rizzio

    I know this is a bit old but, I've been having issues with the player visually being in the ground. Also, the client doesn't show the player lying down. Other players can see him lying down. He just doesn't see himself lying down. Any help would be awesome! Thanks.

    Edit: I just sent the packet to the player and I set him to fly. Seems to have fixed it.
     
    Last edited: Feb 16, 2015
Thread Status:
Not open for further replies.

Share This Page