Created
January 25, 2014 03:24
-
-
Save aadnk/8611384 to your computer and use it in GitHub Desktop.
Trigger the sleeping animation with ProtocolLib.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} | |
} |
The location type is now new BlockLocation(x,y,z);
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?
I'm having the same problem.
I am trying to make this work in 1.15.2. Is there anyone who made this work?
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
doesn't seem to work with MC 1.10
(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!