Skip to content

Instantly share code, notes, and snippets.

@aadnk
Created February 21, 2014 21:38
Show Gist options
  • Save aadnk/9144073 to your computer and use it in GitHub Desktop.
Save aadnk/9144073 to your computer and use it in GitHub Desktop.
Display floating text. Thanks to http://www.youtube.com/watch?v=q1B19JvX5TE for the amazing trick. Uses PacketWrapper and ProtocolLib, but could be rewritten to avoid them.
package com.comphenix.example;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import com.comphenix.protocol.wrappers.WrappedDataWatcher;
public class DisplayFloatingText extends JavaPlugin implements Listener {
private static int entityId = Short.MAX_VALUE;
private static final int WITHER_SKULL = 66;
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
spawnNametag(player, player.getLocation(), 0.5, "Hello World");
}
return true;
}
public static void spawnNametag(Player p, Location loc, double dy, String message) {
WrapperPlayServerSpawnEntityLiving horse = new WrapperPlayServerSpawnEntityLiving();
WrapperPlayServerSpawnEntity skull = new WrapperPlayServerSpawnEntity();
WrapperPlayServerAttachEntity attach = new WrapperPlayServerAttachEntity();
// Initialize horse packet
horse.setEntityID(entityId++);
horse.setType(EntityType.HORSE);
setLocation(horse, dy, loc);
WrappedDataWatcher wdw = new WrappedDataWatcher();
wdw.setObject(10, message);
wdw.setObject(11, (byte) 1);
wdw.setObject(12, -1700000);
horse.setMetadata(wdw);
// Initialize skull packet
skull.setEntityID(entityId++);
skull.setType(WITHER_SKULL);
setLocation(skull, dy, loc);
// The horse is riding on the skull
attach.setEntityId(horse.getEntityID());
attach.setVehicleId(skull.getEntityID());
horse.sendPacket(p);
skull.sendPacket(p);
attach.sendPacket(p);
}
private static void setLocation(WrapperPlayServerSpawnEntity living, double dy, Location location) {
living.setX(location.getX());
living.setY(location.getY() + dy + 55);
living.setZ(location.getZ());
}
private static void setLocation(WrapperPlayServerSpawnEntityLiving living, double dy, Location location) {
living.setX(location.getX());
living.setY(location.getY() + dy + 55);
living.setZ(location.getZ());
}
}
@97WaterPolo
Copy link

How would I destroy the name plate that is created? Also, how would I save certain ones so they will stay after restarts?

@rubik-cube-man
Copy link

Its so simple! Love it!!!! Keep up the great work!

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