Skip to content

Instantly share code, notes, and snippets.

@Goblom
Created February 21, 2014 16:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Goblom/9137084 to your computer and use it in GitHub Desktop.
Save Goblom/9137084 to your computer and use it in GitHub Desktop.
A not completed version of a hologram type system in bukkit using NMS... A list of what is missing is located above the author tab
/*
* The MIT License
*
* Copyright 2014 Goblom.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.goblom.bukkitlibs.theories;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Random;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
/**
* Easily create Holograms (Floating Text) anywhere on any world and send it to any player
*
* Missing:
* ----> Setting the location of the hologram (Floating Text)
* ----> Missing Setting of Entity Id
* ----> Missing Setting of Hologram (Floating Text) Message
* @author Goblom
*/
public class Hologram {
private final String version = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3];
private final Location loc;
private final double dy;
private final String message;
private final Object world;
public Hologram(Location loc, double dy, String message) {
this.loc = loc;
this.dy = dy;
this.message = message;
this.world = getHandle(loc.getWorld());
}
public Hologram(String world, double x, double y, double z, double dy, String message) {
this.dy = dy;
this.loc = new Location(Bukkit.getWorld("world"), x, y, z);
this.message = message;
this.world = getHandle(loc.getWorld());
}
/*
* TODO:
* ----> Setting Location of the Horse
* ----> Setting Entity Id of Horse
*/
public Object getHorsePacket() throws Exception {
Class<?> PacketPlayOutSpawnEntityLiving = getNMSClass("PacketPlayOutSpawnEntityLiving");
Class<?> EntityLiving = getNMSClass("EntityLiving");
Class<?> EntityHorse = getNMSClass("EntityHorse");
Object horse = EntityHorse.getConstructor(getNMSClass("World")).newInstance(world);
return PacketPlayOutSpawnEntityLiving.getConstructor(new Class<?>[] { EntityLiving }).newInstance(horse);
}
/*
* TODO:
* ----> Setting Location of WitherSkull
* ----> Setting Entity Id of Horse
*/
public Object getSkullPacket() throws Exception {
Class<?> PacketPlayOutSpawnEntity = getNMSClass("PacketPlayOutSpawnEntity");
Class<?> Entity = getNMSClass("Entity");
Class<?> EntityWitherSkull = getNMSClass("EntityWitherSkull");
Object skull = EntityWitherSkull.getConstructor(getNMSClass("World")).newInstance(world);
return PacketPlayOutSpawnEntity.getConstructor(new Class<?>[] { Entity }).newInstance(skull);
}
/*
* TODO:
* ----> Missing the Hologram (Floating Text) Message
*/
public Object getAttachPacket(int id, Object obj1, Object obj2) throws Exception {
Class<?> PacketPlayOutAttachEntity = getNMSClass("PacketPlayOutAttachEntity");
Class<?> Entity = getNMSClass("Entity");
return PacketPlayOutAttachEntity.getConstructor(new Class<?>[] { int.class, Entity, Entity }).newInstance(id, obj1, obj2);
}
/*
* TODO:
* ----> Test getAttachPacket(int id, Object obj1, Object obj2), Packet values might need to be switched
*/
public void send(Player player) {
Object horse = null;
Object skull = null;
Object attach = null;
try {
horse = getHorsePacket();
skull = getSkullPacket();
attach = getAttachPacket(new Random().nextInt(), skull, horse);
} catch (Exception e) {
e.printStackTrace();
}
sendPacket(player, horse);
sendPacket(player, skull);
sendPacket(player, attach);
}
private Field getField(Class<?> cl, String fieldName) {
try {
Field f = cl.getDeclaredField(fieldName);
return f;
} catch (NoSuchFieldException ex) {
ex.printStackTrace();
} catch (SecurityException ex) {
ex.printStackTrace();
}
return null;
}
private Method getMethod(Class<?> cl, String method, Class<?>[] args) {
for (Method m : cl.getMethods()) {
if (m.getName().equals(method) && ClassListEqual(args, m.getParameterTypes())) {
return m;
}
}
return null;
}
private Class<?> getCraftClass(String name) {
Class<?> clazz = null;
try {
clazz = Class.forName("org.bukkit.craftbukkit." + version + "." + name);
} catch (ClassNotFoundException e) { e.printStackTrace(); }
return clazz;
}
private Class<?> getNMSClass(String name) {
Class<?> clazz = null;
try {
clazz = Class.forName("net.minecraft.server." + version + "." + name);
} catch (ClassNotFoundException e) { e.printStackTrace(); }
return clazz;
}
private boolean ClassListEqual(Class<?>[] l1, Class<?>[] l2) {
boolean equal = true;
if (l1.length != l2.length) {
return false;
}
for (int i = 0; i < l1.length; i++) {
if (l1[i] != l2[i]) {
equal = false;
break;
}
}
return equal;
}
private Method getMethod(Class<?> clazz, String method) {
for (Method m : clazz.getMethods()) {
if (m.getName().equals(method)) {
return m;
}
}
return null;
}
private Object getHandle(World world) {
Object nms_w = null;
Method handle = getMethod(world.getClass(), "getHandle");
try {
nms_w = handle.invoke(world);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return nms_w;
}
private Object getHandle(Entity ent) {
Object nms_e = null;
Method handle = getMethod(ent.getClass(), "getHandle");
try {
nms_e = handle.invoke(ent);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return nms_e;
}
private void sendPacket(Player p, Object packet) {
try {
Object nms_p = getHandle(p);
Field f_con = nms_p.getClass().getField("playerConnection");
Object conn = f_con.get(nms_p);
Method m_pack = getMethod(conn.getClass(), "sendPacket");
m_pack.invoke(conn, packet);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment