Advertisement
Phe0X

Sleep Animation Snippet Skript (EN)

Jun 18th, 2020
4,419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. option ver:
  2.     get:
  3.         return console.getServer().getClass().getPackage().getName().split("\.")[3] #Get the version of the server (for NMS)
  4. import:
  5.     java.util.Optional
  6.     net.minecraft.server.{@ver}.BlockPosition #Required for the location of the bed
  7.     net.minecraft.server.{@ver}.EntityPose #The pose (here we'll use Sleeping)
  8.     com.comphenix.protocol.wrappers.WrappedDataWatcher #Required to edit the player's metadata using protocollib (and packets)
  9.     com.comphenix.protocol.wrappers.WrappedDataWatcher$Registry as DataWatcherRegistry
  10.     com.comphenix.protocol.wrappers.WrappedDataWatcher$WrappedDataWatcherObject as DataWatcherObject
  11. effect (make|force) [the] %players% [to] sleep at %location% for %players% [to not be stuck in bed %-boolean%]:
  12.     trigger:
  13.         set {_stuck} to expression-4 ? false #Set to false if "to not be.." is not set
  14.         set {_PacketMetadata} to new play_server_entity_metadata packet
  15.         set {_OptionalBlockPos} to Optional.of(new BlockPosition(x location of expression-2, (y location of expression-2) - 0.5 and z location of expression-2)) #The location of the ""fake"" bed (where the player will sleep)
  16.         set {_WDW} to new WrappedDataWatcher() #Creating the datawatcher/watchablecollection
  17.         set {_Serializer} to DataWatcherRegistry.getBlockPositionSerializer(true)
  18.         set {_DWO} to new DataWatcherObject(13 and {_Serializer})
  19.         {_WDW}.setObject({_DWO} and {_OptionalBlockPos}) #Object 13 is the location
  20.         {_WDW}.setObject(6, DataWatcherRegistry.get(EntityPose) and EntityPose.SLEEPING) #Object 6 is the EntityPose, here the sleeping one
  21.         set watchable collection field 0 of {_PacketMetadata} to {_WDW}
  22.         loop expressions-1:
  23.             set int pnum 0 of {_PacketMetadata} to (loop-value-1).getEntityId() #Send the packet with the right entity Id
  24.             if {_stuck} is false:
  25.                 loop expressions-3:
  26.                     send packet {_PacketMetadata} to loop-value-2
  27.                     set {IsSleeping::%(loop-value-1).getEntityId()%::%loop-value-2%} to true #Useful to make the player sleep again when he comes in a visible range
  28.                 wait 2 ticks #Avoid some bugs you may have if you try to make multiple players sleep at once
  29.             else:
  30.                 loop expressions-3:
  31.                     loop-value-1 is not loop-value-2 #If the player doesn't receive his packets, he isn't stuck
  32.                     send packet {_PacketMetadata} to loop-value-2
  33.                     set {IsSleeping::%(loop-value-1).getEntityId()%::%loop-value-2%} to true
  34. effect wake [the][up] %players% [up] for %players%:
  35.     trigger:
  36.         set {_PacketMetadata} to new play_server_entity_metadata packet
  37.         set {_OptionalBlockPos} to Optional.empty() #The same thing happen when you click "leave bed"
  38.         set {_WDW} to new WrappedDataWatcher()
  39.         set {_Serializer} to DataWatcherRegistry.getBlockPositionSerializer(true)
  40.         set {_DWO} to new DataWatcherObject(13 and {_Serializer})
  41.         {_WDW}.setObject({_DWO} and {_OptionalBlockPos})       
  42.         {_WDW}.setObject(6, DataWatcherRegistry.get(EntityPose) and EntityPose.STANDING)
  43.         set watchable collection field 0 of {_PacketMetadata} to {_WDW}
  44.         loop expressions-1:
  45.             set int pnum 0 of {_PacketMetadata} to (loop-value-1).getEntityId()
  46.             loop expressions-2:
  47.                 clear {IsSleeping::%(loop-value-1).getEntityId()%::%loop-value-2%}
  48.                 send packet {_PacketMetadata} to loop-value-2
  49. on packet event play_server_entity_metadata: #Prevent the player from changing position (by sneaking for instance)
  50.     {IsSleeping::%int pnum 0 of event-packet%::%player%} is true
  51.     set {_WC} to watchable collection field 0 of event-packet
  52.     "%{_WC}.getObject(6)%" is not "SLEEPING" or "<none>"
  53.     cancel event #I have to cancel here, because setting it can cause weird visual glitches
  54. on packet event play_server_named_entity_spawn: #Make the player sleep when he comes in a visible range
  55.     {IsSleeping::%int pnum 0 of event-packet%::%player%} is true
  56.     make {Id::%int pnum 0 of event-packet%} sleep at {Id::%int pnum 0 of event-packet%}'s location for player
  57. on quit: #The player won't be asleep when he'll log back
  58.     clear {IsSleeping::%player.getEntityId()%::*}
  59. on join: #Must be used in order to convert an entityid into a player without looping all players
  60.     set {Id::%player.getEntityId()%} to player
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement