Navigation Menu

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);
}
}
}
}
@Nicofisi
Copy link

Nicofisi commented Jun 22, 2016

        bedPacket.getIntegers().
            write(1, loc.getBlockX()).
            write(2, loc.getBlockY() + 1).
            write(3, loc.getBlockZ());

doesn't seem to work with MC 1.10

[19:43:43 ERROR]: [ServerCore] Unhandled exception occured in onPacketReceiving(PacketEvent) for ServerCore
com.comphenix.protocol.reflect.FieldAccessException: Field index out of bounds. (Index: 1, Size: 1)

(on second line ^)
Could you give me a quick tip about how to do it now?
A site about protocol says there is now a "location" type, and I don't see any bedPacket.getLocations()
Thanks in advance!

@HeyAwesomePeople
Copy link

@Nicofisi

The location type is now new BlockLocation(x,y,z);

@ernierock
Copy link

ernierock commented Jun 26, 2016

How can I fix this?

bedPacket.getEntityModifier(p.getWorld()).write(0, p);

bedPacket.getBlockPositionModifier().write(0, new BlockPosition(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()));

This doesn't give an error but it doesn't make anything happen either. Do we need to wait for a BlockLocation implement?

@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