Skip to content

Instantly share code, notes, and snippets.

@Sheigutn
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save Sheigutn/ea1a1d641ffeafa7c0cd to your computer and use it in GitHub Desktop.

Select an option

Save Sheigutn/ea1a1d641ffeafa7c0cd to your computer and use it in GitHub Desktop.
Packets 1.8
package me.sheigutn.Packet1_8;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.reflect.accessors.Accessors;
import net.minecraft.server.v1_7_R4.EnumProtocol;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import java.util.Map;
import static com.comphenix.protocol.ProtocolLibrary.*;
import static com.comphenix.protocol.PacketType.UNKNOWN_PACKET;
public class PacketAPI {
private static final int CAMERA = 0x43;
private static final int WORLD_BORDER = 0x44;
private static final int TITLE = 0x45;
private static final int TABLIST = 0x47;
private static final PacketType cameraPacket = new PacketType(PacketType.Protocol.PLAY, PacketType.Sender.SERVER, CAMERA, UNKNOWN_PACKET);
private static final PacketType borderPacket = new PacketType(PacketType.Protocol.PLAY, PacketType.Sender.SERVER, WORLD_BORDER, UNKNOWN_PACKET);
private static final PacketType titlePacket = new PacketType(PacketType.Protocol.PLAY, PacketType.Sender.SERVER, TITLE, UNKNOWN_PACKET);
private static final PacketType tablistPacket = new PacketType(PacketType.Protocol.PLAY, PacketType.Sender.SERVER, TABLIST, UNKNOWN_PACKET);
private static PacketAPI instance;
private PacketAPI()
{
EnumProtocol.PLAY.b().put(CAMERA, PacketPlayOutCamera.class);
EnumProtocol.PLAY.b().put(WORLD_BORDER, PacketPlayOutWorldBorder.class);
EnumProtocol.PLAY.b().put(TITLE, PacketPlayOutTitle.class);
EnumProtocol.PLAY.b().put(TABLIST, PacketPlayOutTabListHeaderFooter.class);
Map<Class<?>, EnumProtocol> map = (Map<Class<?>, EnumProtocol>)
Accessors.getFieldAccessor(EnumProtocol.class, Map.class, true).get(EnumProtocol.PLAY);
map.put(PacketPlayOutCamera.class, EnumProtocol.PLAY);
map.put(PacketPlayOutWorldBorder.class, EnumProtocol.PLAY);
map.put(PacketPlayOutTitle.class, EnumProtocol.PLAY);
map.put(PacketPlayOutTabListHeaderFooter.class, EnumProtocol.PLAY);
}
public static PacketAPI getInstance()
{
if(instance == null) instance = new PacketAPI();
return instance;
}
public void switchCamera(Player p, LivingEntity entity) throws Exception
{
getProtocolManager().sendServerPacket(p, new PacketContainer(cameraPacket, new PacketPlayOutCamera(entity.getEntityId())));
}
public void resetCamera(Player p) throws Exception
{
getProtocolManager().sendServerPacket(p, new PacketContainer(cameraPacket, new PacketPlayOutCamera(p.getEntityId())));
}
public void setTitleWithAnimations(Player p, PacketPlayOutTitle.Action action, String json, int fadeIn, int stay, int fadeOut) throws Exception
{
getProtocolManager().sendServerPacket(p, new PacketContainer(titlePacket, new PacketPlayOutTitle(action, json, fadeIn, stay, fadeOut)));
}
public void clearTitle(Player p) throws Exception
{
getProtocolManager().sendServerPacket(p, new PacketContainer(titlePacket, new PacketPlayOutTitle(PacketPlayOutTitle.Action.CLEAR, null)));
}
public void resetTitle(Player p) throws Exception
{
getProtocolManager().sendServerPacket(p, new PacketContainer(titlePacket, new PacketPlayOutTitle(PacketPlayOutTitle.Action.RESET, null)));
}
public void setTitle(Player p, PacketPlayOutTitle.Action action, String json) throws Exception
{
getProtocolManager().sendServerPacket(p, new PacketContainer(titlePacket, new PacketPlayOutTitle(action, json)));
}
public void setTabListHeaderAndFooter(Player p, String header, String footer) throws Exception
{
getProtocolManager().sendServerPacket(p, new PacketContainer(tablistPacket, new PacketPlayOutTabListHeaderFooter(header, footer)));
}
public void sendWorldBorder(Player p, double x, double z, double oldRadius, double newRadius, long speed) throws Exception
{
getProtocolManager().sendServerPacket(p, new PacketContainer(borderPacket, new PacketPlayOutWorldBorder(x, z, oldRadius, newRadius, speed,-1,-1,-1)));
}
public void setWorldBorderCenter(Player p, double x, double z) throws Exception
{
getProtocolManager().sendServerPacket(p, new PacketContainer(borderPacket, new PacketPlayOutWorldBorder(x,z)));
}
public void setWorldBorderSize(Player p, double radius) throws Exception
{
getProtocolManager().sendServerPacket(p, new PacketContainer(borderPacket, new PacketPlayOutWorldBorder(radius)));
}
public void changeWorldBorderSize(Player p, double oldSize, double newSize) throws Exception
{
getProtocolManager().sendServerPacket(p, new PacketContainer(borderPacket, new PacketPlayOutWorldBorder(Double.valueOf(oldSize), Double.valueOf(newSize))));
}
public void setWorldBorderWarningTime(Player p, int warningTime) throws Exception
{
getProtocolManager().sendServerPacket(p, new PacketContainer(borderPacket, new PacketPlayOutWorldBorder(PacketPlayOutWorldBorder.Action.SET_WARNING_TIME, warningTime, -1)));
}
public void setWorldBorderWarningBlocks(Player p, int warningBlocks) throws Exception
{
getProtocolManager().sendServerPacket(p, new PacketContainer(borderPacket, new PacketPlayOutWorldBorder(PacketPlayOutWorldBorder.Action.SET_WARNING_BLOCKS,-1,warningBlocks)));
}
}
package me.sheigutn.Packet1_8;
import net.minecraft.server.v1_7_R4.Packet;
import net.minecraft.server.v1_7_R4.PacketDataSerializer;
import net.minecraft.server.v1_7_R4.PacketListener;
import java.io.IOException;
public class PacketPlayOutCamera extends Packet {
public int entityID;
public PacketPlayOutCamera(int entityID)
{
this.entityID = entityID;
}
@Override
public void a(PacketDataSerializer packetDataSerializer) throws IOException {
entityID = packetDataSerializer.a();
}
@Override
public void b(PacketDataSerializer packetDataSerializer) throws IOException {
packetDataSerializer.b(entityID);
}
@Override
public void handle(PacketListener packetListener) {
}
}
package me.sheigutn.Packet1_8;
import net.minecraft.server.v1_7_R4.Packet;
import net.minecraft.server.v1_7_R4.PacketDataSerializer;
import net.minecraft.server.v1_7_R4.PacketListener;
import java.io.IOException;
public class PacketPlayOutTabListHeaderFooter extends Packet{
public String header;
public String footer;
public PacketPlayOutTabListHeaderFooter(String header, String footer)
{
this.header = header;
this.footer = footer;
}
@Override
public void a(PacketDataSerializer packetDataSerializer) throws IOException {
header = packetDataSerializer.c(256);
footer = packetDataSerializer.c(256);
}
@Override
public void b(PacketDataSerializer packetDataSerializer) throws IOException {
packetDataSerializer.a(header);
packetDataSerializer.a(footer);
}
@Override
public void handle(PacketListener packetListener) {
}
}
package me.sheigutn.Packet1_8;
import net.minecraft.server.v1_7_R4.Packet;
import net.minecraft.server.v1_7_R4.PacketDataSerializer;
import net.minecraft.server.v1_7_R4.PacketListener;
import net.minecraft.server.v1_7_R4.PacketPlayOutListener;
import java.io.IOException;
public class PacketPlayOutTitle extends Packet {
public Action action;
public String text;
public int fadeIn;
public int stay;
public int fadeOut;
private PacketPlayOutTitle(int fadeIn, int stay, int fadeOut)
{
this.fadeIn = fadeIn;
this.stay = stay;
this.fadeOut = fadeOut;
}
public PacketPlayOutTitle(Action action, String title)
{
this(-1,-1,-1);
this.action = action;
this.text = title;
}
public PacketPlayOutTitle(Action action, String title, int fadeIn, int stay, int fadeOut)
{
this(fadeIn, stay, fadeOut);
this.action = action;
this.text = title;
}
@Override
public void a(PacketDataSerializer packetDataSerializer) throws IOException {
action = Action.values()[packetDataSerializer.a()];
if(action == Action.TITLE || action == Action.SUBTITLE)
{
text = packetDataSerializer.c(256);
}
else if(action == Action.TIMES)
{
fadeIn = packetDataSerializer.readInt();
stay = packetDataSerializer.readInt();
fadeOut = packetDataSerializer.readInt();
}
}
@Override
public void b(PacketDataSerializer packetDataSerializer) throws IOException {
packetDataSerializer.b(action.ordinal());
if(action == Action.TITLE || action == Action.SUBTITLE)
{
packetDataSerializer.a(text);
}
else if(action == Action.TIMES)
{
packetDataSerializer.writeInt(fadeIn);
packetDataSerializer.writeInt(stay);
packetDataSerializer.writeInt(fadeOut);
}
}
@Override
public void handle(PacketListener packetListener) {
}
enum Action{
TITLE, SUBTITLE, TIMES, CLEAR, RESET;
}
}
package me.sheigutn.Packet1_8;
import net.minecraft.server.v1_7_R4.Packet;
import net.minecraft.server.v1_7_R4.PacketDataSerializer;
import net.minecraft.server.v1_7_R4.PacketListener;
import java.io.IOException;
public class PacketPlayOutWorldBorder extends Packet {
public Action action;
public double x;
public double z;
public double oldRadius;
public double newRadius;
public long speed;
public int portalTeleportBoundary = 29999984;
public int warningTime = 15;
public int warningBlocks = 5;
public PacketPlayOutWorldBorder(double x, double z, double oldRadius, double newRadius, long speed, int portalTeleportBoundary, int warningTime, int warningBlocks)
{
this.x = x;
this.z = z;
this.oldRadius = oldRadius;
this.newRadius = newRadius;
this.speed = speed;
if(portalTeleportBoundary != -1) this.portalTeleportBoundary = portalTeleportBoundary;
if(warningTime != -1) this.warningTime = warningTime;
if(warningBlocks != -1) this.warningBlocks = warningBlocks;
}
public PacketPlayOutWorldBorder(double x, double z)
{
this.action = Action.SET_CENTER;
this.x = x;
this.z = z;
}
public PacketPlayOutWorldBorder(double newRadius)
{
this.action = Action.SET_SIZE;
this.newRadius = newRadius;
}
public PacketPlayOutWorldBorder(Double oldRadius, Double newRadius)
{
this.action = Action.LERP_SIZE;
this.oldRadius = oldRadius;
this.newRadius = newRadius;
}
public PacketPlayOutWorldBorder(Action action, int warningTime, int warningBlocks)
{
this.action = action;
if(warningTime != -1) this.warningTime = warningTime;
if(warningBlocks != -1) this.warningBlocks = warningBlocks;
}
@Override
public void a(PacketDataSerializer packetDataSerializer) throws IOException {
action = Action.values()[packetDataSerializer.a()];
if(action.ordinal() == Action.SET_SIZE.ordinal())
{
newRadius = packetDataSerializer.readDouble();
}
if(action.ordinal() == Action.LERP_SIZE.ordinal())
{
oldRadius = packetDataSerializer.readDouble();
newRadius = packetDataSerializer.readDouble();
speed = VarLong.readVarLong(packetDataSerializer);
}
if(action.ordinal() == Action.SET_CENTER.ordinal()) {
x = packetDataSerializer.readDouble();
z = packetDataSerializer.readDouble();
}
if(action.ordinal() == Action.INITIALIZE.ordinal())
{
x = packetDataSerializer.readDouble();
z = packetDataSerializer.readDouble();
oldRadius = packetDataSerializer.readDouble();
newRadius = packetDataSerializer.readDouble();
speed = VarLong.readVarLong(packetDataSerializer);
portalTeleportBoundary = packetDataSerializer.a();
warningTime = packetDataSerializer.a();
warningBlocks = packetDataSerializer.a();
}
if(action.ordinal() == Action.SET_WARNING_TIME.ordinal())
{
warningTime = packetDataSerializer.a();
}
if(action.ordinal() == Action.SET_WARNING_BLOCKS.ordinal())
{
warningBlocks = packetDataSerializer.a();
}
}
@Override
public void b(PacketDataSerializer packetDataSerializer) throws IOException {
packetDataSerializer.b(action.ordinal());
if(action.ordinal() == Action.SET_SIZE.ordinal())
{
packetDataSerializer.writeDouble(newRadius);
}
if(action.ordinal() == Action.LERP_SIZE.ordinal())
{
packetDataSerializer.writeDouble(oldRadius);
packetDataSerializer.writeDouble(newRadius);
VarLong.writeVarLong(speed, packetDataSerializer);
}
if(action.ordinal() == Action.SET_CENTER.ordinal()) {
packetDataSerializer.writeDouble(x);
packetDataSerializer.writeDouble(z);
}
if(action.ordinal() == Action.INITIALIZE.ordinal())
{
packetDataSerializer.writeDouble(x);
packetDataSerializer.writeDouble(z);
packetDataSerializer.writeDouble(oldRadius);
packetDataSerializer.writeDouble(newRadius);
VarLong.writeVarLong(speed, packetDataSerializer);
packetDataSerializer.b(portalTeleportBoundary);
packetDataSerializer.b(warningTime);
packetDataSerializer.b(warningBlocks);
}
if(action.ordinal() == Action.SET_WARNING_TIME.ordinal())
{
packetDataSerializer.b(warningTime);
}
if(action.ordinal() == Action.SET_WARNING_BLOCKS.ordinal())
{
packetDataSerializer.b(warningBlocks);
}
}
@Override
public void handle(PacketListener packetListener) {
}
enum Action{
SET_SIZE, LERP_SIZE, SET_CENTER, INITIALIZE, SET_WARNING_TIME, SET_WARNING_BLOCKS
}
}
package me.sheigutn.Packet1_8;
import net.minecraft.server.v1_7_R4.PacketDataSerializer;
public class VarLong {
public static void writeVarLong(long paramLong, PacketDataSerializer ser) {
while (true) {
if ((paramLong & 0xFFFFFF80) == 0L) {
ser.writeByte((int) paramLong);
return;
}
ser.writeByte((int) (paramLong & 0x7F) | 0x80);
paramLong >>>= 7;
}
}
public static long readVarLong(PacketDataSerializer ser) {
long l = 0L;
int i = 0;
while (true)
{
int j = ser.readByte();
l |= (j & 0x7F) << i++ * 7;
if (i > 10) {
throw new RuntimeException("VarLong too big");
}
if ((j & 0x80) != 128) {
break;
}
}
return l;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment