Skip to content

Instantly share code, notes, and snippets.

@aadnk
Created January 25, 2014 03:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aadnk/8611384 to your computer and use it in GitHub Desktop.
Save aadnk/8611384 to your computer and use it in GitHub Desktop.
Trigger the sleeping animation with ProtocolLib.
package com.comphenix.example;
import java.lang.reflect.InvocationTargetException;
import java.util.Collections;
import java.util.Set;
import java.util.WeakHashMap;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.events.PacketContainer;
public class SleepingAnimation extends JavaPlugin {
private ProtocolManager manager;
// Just a way to record if a player is asleep
private Set<Player> sleeping = Collections.newSetFromMap(new WeakHashMap<Player, Boolean>());
@Override
public void onEnable() {
manager = ProtocolLibrary.getProtocolManager();
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if (sleeping.add(player)) {
playSleepAnimation(player);
} else {
stopSleepAnimation(player);
sleeping.remove(player);
}
}
return true;
}
/**
* Play the sleep animation for every nearby player.
* @param alseep - the player asleep.
*/
private void playSleepAnimation(Player asleep) {
final PacketContainer bedPacket = manager.createPacket(PacketType.Play.Server.BED, false);
final Location loc = asleep.getLocation();
// http://wiki.vg/Protocol#Use_Bed
bedPacket.getEntityModifier(asleep.getWorld()).
write(0, asleep);
bedPacket.getIntegers().
write(1, loc.getBlockX()).
write(2, loc.getBlockY() + 1).
write(3, loc.getBlockZ());
broadcastNearby(asleep, bedPacket);
}
private void stopSleepAnimation(Player sleeping) {
final PacketContainer animation = manager.createPacket(PacketType.Play.Server.ANIMATION, false);
// http://wiki.vg/Protocol#Animation
animation.getEntityModifier(sleeping.getWorld()).
write(0, sleeping);
animation.getIntegers().
write(1, 2);
broadcastNearby(sleeping, animation);
}
private void broadcastNearby(Player asleep, PacketContainer bedPacket) {
for (Player observer : manager.getEntityTrackers(asleep)) {
try {
manager.sendServerPacket(observer, bedPacket);
} catch (InvocationTargetException e) {
throw new RuntimeException("Cannot send packet.", e);
}
}
}
}
@dilanx
Copy link

dilanx commented Jul 17, 2016

@ernierock

I'm having the same problem.

@Jeffrey-H
Copy link

Jeffrey-H commented Feb 9, 2021

I am trying to make this work in 1.15.2. Is there anyone who made this work?

@rageyh
Copy link

rageyh commented Dec 5, 2021

Caused by: java.lang.IllegalArgumentException: Could not find packet for type BED (line 54) ??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment