Skip to content

Instantly share code, notes, and snippets.

@effective-light
Last active June 20, 2017 07:12
Show Gist options
  • Select an option

  • Save effective-light/38a37fe0b5355ecdd176 to your computer and use it in GitHub Desktop.

Select an option

Save effective-light/38a37fe0b5355ecdd176 to your computer and use it in GitHub Desktop.
Tab
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
package packagename;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.chat.ComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Tab
{
private static Constructor<?> headerFooterConstructor;
private static Method sendPacket;
private static Method getHandle;
private static Method serialize;
private static Field playerConnection;
private static Field headerField;
private static Field footerField;
private Object packet;
static
{
String version = Bukkit.getServer().getClass().getPackage().getName().split( "\\." )[ 3 ];
String nms = "net.minecraft.server." + version + ".";
try
{
Class<?> entityPlayerClass = Class.forName( nms + "EntityPlayer" );
Class<?> listHeaderFooterClass = Class.forName( nms + "PacketPlayOutPlayerListHeaderFooter" );
getHandle = Class.forName( "org.bukkit.craftbukkit." + version + ".entity.CraftPlayer" )
.getMethod( "getHandle" );
playerConnection = entityPlayerClass.getField( "playerConnection" );
serialize = Class.forName( nms + "IChatBaseComponent$ChatSerializer" ).getMethod( "a", String.class );
sendPacket = Class.forName( nms + "PlayerConnection" ).getMethod( "sendPacket",
Class.forName( "net.minecraft.server." + version + ".Packet" ) );
headerField = listHeaderFooterClass.getDeclaredField( "a" );
headerField.setAccessible( true );
footerField = listHeaderFooterClass.getDeclaredField( "b" );
footerField.setAccessible( true );
headerFooterConstructor = listHeaderFooterClass.getConstructor();
} catch ( ClassNotFoundException | NoSuchMethodException
| NoSuchFieldException e )
{
e.printStackTrace();
}
}
public Tab(BaseComponent[] header, BaseComponent[] footer)
{
try
{
packet = headerFooterConstructor.newInstance();
setHeader( header );
setFooter( footer );
} catch ( InstantiationException | IllegalAccessException | InvocationTargetException e )
{
e.printStackTrace();
}
}
public void setHeader(BaseComponent[] header)
{
try
{
headerField.set( packet, serialize.invoke( null, ComponentSerializer.toString( header ) ) );
} catch ( IllegalAccessException | InvocationTargetException e )
{
e.printStackTrace();
}
}
public void setFooter(BaseComponent[] footer)
{
try
{
footerField.set( packet, serialize.invoke( null, ComponentSerializer.toString( footer ) ) );
} catch ( IllegalAccessException | InvocationTargetException e )
{
e.printStackTrace();
}
}
public void setHeaderFooter(Player player)
{
try
{
Object handle = getHandle.invoke( player );
Object connection = playerConnection.get( handle );
sendPacket.invoke( connection, packet );
} catch ( IllegalAccessException
| InvocationTargetException e )
{
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment