Skip to content

Instantly share code, notes, and snippets.

@cyberpwnn
Created April 1, 2016 17:30
Show Gist options
  • Save cyberpwnn/6546e53faec7617bacef38e17e560a8d to your computer and use it in GitHub Desktop.
Save cyberpwnn/6546e53faec7617bacef38e17e560a8d to your computer and use it in GitHub Desktop.
Titles & Stuff
//This is the actual class you use for sending titles
//This is a cheap / quick wrapper for sending title messages
//This will work on 1.8 and 1.9
//THIS REQUIRES CRAFTBUKKIT 1.8 AND 1.9 IN YOUR CLASSPATH
package com.glacialrush.plugin;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
public class PacketUtil
{
public enum V
{
R18, R19;
}
public static V getVersion()
{
if(Bukkit.getBukkitVersion().startsWith("1.9"))
{
return V.R19;
}
return V.R18;
}
public static void sendTitle(Player player, Integer fadeIn, Integer stay, Integer fadeOut, String title, String subtitle)
{
if(getVersion().equals(V.R18))
{
PacketUtil18.sendTitle(player, fadeIn, stay, fadeOut, title, subtitle);
}
if(getVersion().equals(V.R19))
{
PacketUtil19.sendTitle(player, fadeIn, stay, fadeOut, title, subtitle);
}
}
public static void sendActionBar(Player player, String message)
{
if(getVersion().equals(V.R18))
{
PacketUtil18.sendActionBar(player, message);
}
if(getVersion().equals(V.R19))
{
PacketUtil19.sendActionBar(player, message);
}
}
}
package com.glacialrush.plugin;
import org.bukkit.ChatColor;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
import org.bukkit.entity.Player;
import net.minecraft.server.v1_8_R3.IChatBaseComponent;
import net.minecraft.server.v1_8_R3.Packet;
import net.minecraft.server.v1_8_R3.PacketPlayOutChat;
import net.minecraft.server.v1_8_R3.PacketPlayOutTitle;
import net.minecraft.server.v1_8_R3.PlayerConnection;
public class PacketUtil18
{
public static void sendTitle(Player player, Integer fadeIn, Integer stay, Integer fadeOut, String title, String subtitle)
{
PlayerConnection connection = ((CraftPlayer) player).getHandle().playerConnection;
PacketPlayOutTitle packetPlayOutTimes = new PacketPlayOutTitle(PacketPlayOutTitle.EnumTitleAction.TIMES, null, fadeIn.intValue(), stay.intValue(), fadeOut.intValue());
connection.sendPacket((Packet<?>) packetPlayOutTimes);
if(subtitle != null)
{
subtitle = subtitle.replaceAll("%player%", player.getDisplayName());
subtitle = ChatColor.translateAlternateColorCodes((char) '&', (String) subtitle);
IChatBaseComponent titleSub = IChatBaseComponent.ChatSerializer.a((String) ("{\"text\": \"" + subtitle + "\"}"));
PacketPlayOutTitle packetPlayOutSubTitle = new PacketPlayOutTitle(PacketPlayOutTitle.EnumTitleAction.SUBTITLE, titleSub);
connection.sendPacket((Packet<?>) packetPlayOutSubTitle);
}
if(title != null)
{
title = title.replaceAll("%player%", player.getDisplayName());
title = ChatColor.translateAlternateColorCodes((char) '&', (String) title);
IChatBaseComponent titleMain = IChatBaseComponent.ChatSerializer.a((String) ("{\"text\": \"" + title + "\"}"));
PacketPlayOutTitle packetPlayOutTitle = new PacketPlayOutTitle(PacketPlayOutTitle.EnumTitleAction.TITLE, titleMain);
connection.sendPacket((Packet<?>) packetPlayOutTitle);
}
}
public static void sendActionBar(Player player, String message)
{
CraftPlayer p = (CraftPlayer) player;
IChatBaseComponent cbc = IChatBaseComponent.ChatSerializer.a((String) ("{\"text\": \"" + message + "\"}"));
PacketPlayOutChat ppoc = new PacketPlayOutChat(cbc, (byte) 2);
p.getHandle().playerConnection.sendPacket((Packet<?>) ppoc);
}
}
package com.glacialrush.plugin;
import org.bukkit.ChatColor;
import org.bukkit.craftbukkit.v1_9_R1.entity.CraftPlayer;
import org.bukkit.entity.Player;
import net.minecraft.server.v1_9_R1.IChatBaseComponent;
import net.minecraft.server.v1_9_R1.Packet;
import net.minecraft.server.v1_9_R1.PacketPlayOutChat;
import net.minecraft.server.v1_9_R1.PacketPlayOutTitle;
import net.minecraft.server.v1_9_R1.PlayerConnection;
public class PacketUtil19
{
public static void sendTitle(Player player, Integer fadeIn, Integer stay, Integer fadeOut, String title, String subtitle)
{
PlayerConnection connection = ((CraftPlayer) player).getHandle().playerConnection;
PacketPlayOutTitle packetPlayOutTimes = new PacketPlayOutTitle(PacketPlayOutTitle.EnumTitleAction.TIMES, null, fadeIn.intValue(), stay.intValue(), fadeOut.intValue());
connection.sendPacket((Packet<?>) packetPlayOutTimes);
if(subtitle != null)
{
subtitle = subtitle.replaceAll("%player%", player.getDisplayName());
subtitle = ChatColor.translateAlternateColorCodes((char) '&', (String) subtitle);
IChatBaseComponent titleSub = IChatBaseComponent.ChatSerializer.a((String) ("{\"text\": \"" + subtitle + "\"}"));
PacketPlayOutTitle packetPlayOutSubTitle = new PacketPlayOutTitle(PacketPlayOutTitle.EnumTitleAction.SUBTITLE, titleSub);
connection.sendPacket((Packet<?>) packetPlayOutSubTitle);
}
if(title != null)
{
title = title.replaceAll("%player%", player.getDisplayName());
title = ChatColor.translateAlternateColorCodes((char) '&', (String) title);
IChatBaseComponent titleMain = IChatBaseComponent.ChatSerializer.a((String) ("{\"text\": \"" + title + "\"}"));
PacketPlayOutTitle packetPlayOutTitle = new PacketPlayOutTitle(PacketPlayOutTitle.EnumTitleAction.TITLE, titleMain);
connection.sendPacket((Packet<?>) packetPlayOutTitle);
}
}
public static void sendActionBar(Player player, String message)
{
CraftPlayer p = (CraftPlayer) player;
IChatBaseComponent cbc = IChatBaseComponent.ChatSerializer.a((String) ("{\"text\": \"" + message + "\"}"));
PacketPlayOutChat ppoc = new PacketPlayOutChat(cbc, (byte) 2);
p.getHandle().playerConnection.sendPacket((Packet<?>) ppoc);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment