Search in sources :

Example 6 with GVersion

use of org.anhcraft.spaciouslib.utils.GVersion in project SpaciousLib by anhcraft.

the class PlayerList method create.

/**
 * Creates a PlayerListHeaderFooter packet<br>
 * Supports raw JSON text.
 * @param header the header
 * @param footer the footer
 * @return PacketSender object
 */
public static PacketSender create(String header, String footer) {
    if (JSONUtils.isValid(header)) {
        header = Strings.color(header);
    } else {
        header = "{\"text\": \"" + Strings.color(header) + "\"}";
    }
    if (JSONUtils.isValid(footer)) {
        footer = Strings.color(footer);
    } else {
        footer = "{\"text\": \"" + Strings.color(footer) + "\"}";
    }
    GVersion v = GameVersion.getVersion();
    try {
        Class<?> chatSerializerClass = Class.forName("net.minecraft.server." + v.toString() + "." + (v.equals(GVersion.v1_8_R1) ? "" : "IChatBaseComponent$") + "ChatSerializer");
        Class<?> packetPlayOutPlayerListHeaderFooterClass = Class.forName("net.minecraft.server." + GameVersion.getVersion().toString() + ".PacketPlayOutPlayerListHeaderFooter");
        Method chatSerializer = chatSerializerClass.getDeclaredMethod("a", String.class);
        Object chatBaseComponentHeader = chatSerializer.invoke(null, header);
        Object chatBaseComponentFooter = chatSerializer.invoke(null, footer);
        Constructor<?> packetCons = packetPlayOutPlayerListHeaderFooterClass.getDeclaredConstructor();
        Object packet = packetCons.newInstance();
        Field headerField = packetPlayOutPlayerListHeaderFooterClass.getDeclaredField("a");
        headerField.setAccessible(true);
        headerField.set(packet, chatBaseComponentHeader);
        Field footerField = packetPlayOutPlayerListHeaderFooterClass.getDeclaredField("b");
        footerField.setAccessible(true);
        footerField.set(packet, chatBaseComponentFooter);
        return new PacketSender(packet);
    } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException | NoSuchFieldException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) GVersion(org.anhcraft.spaciouslib.utils.GVersion) Field(java.lang.reflect.Field)

Example 7 with GVersion

use of org.anhcraft.spaciouslib.utils.GVersion in project SpaciousLib by anhcraft.

the class Title method create.

/**
 * Creates a specific title packet<br>
 * Supports raw JSON text.
 * @param text the message
 * @param type the type of the title
 * @param fadeIn the fading in time
 * @param stay the staying time
 * @param fadeOut the fading out time
 * @return PacketSender object
 */
public static PacketSender create(String text, Type type, int fadeIn, int stay, int fadeOut) {
    if (JSONUtils.isValid(text)) {
        text = Strings.color(text);
    } else {
        text = "{\"text\": \"" + Strings.color(text) + "\"}";
    }
    GVersion v = GameVersion.getVersion();
    try {
        Class<?> chatSerializerClass = Class.forName("net.minecraft.server." + v.toString() + "." + (v.equals(GVersion.v1_8_R1) ? "" : "IChatBaseComponent$") + "ChatSerializer");
        Class<?> chatBaseComponentClass = Class.forName("net.minecraft.server." + v.toString() + ".IChatBaseComponent");
        Method chatSerializer = chatSerializerClass.getDeclaredMethod("a", String.class);
        Object chatBaseComponent = chatSerializer.invoke(null, text);
        Class<?> packetPlayOutTitleClass = Class.forName("net.minecraft.server." + GameVersion.getVersion().toString() + ".PacketPlayOutTitle");
        Class<?> enumTitleActionClass = Class.forName("net.minecraft.server." + v.toString() + "." + (v.equals(GVersion.v1_8_R1) ? "" : "PacketPlayOutTitle$") + "EnumTitleAction");
        Field enumTitleField = enumTitleActionClass.getDeclaredField(type.toString());
        enumTitleField.setAccessible(true);
        Object enumTitle = enumTitleField.get(null);
        Constructor<?> packetCons = packetPlayOutTitleClass.getDeclaredConstructor(enumTitleActionClass, chatBaseComponentClass, int.class, int.class, int.class);
        Object packet = packetCons.newInstance(enumTitle, chatBaseComponent, fadeIn, stay, fadeOut);
        return new PacketSender(packet);
    } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException | NoSuchFieldException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) GVersion(org.anhcraft.spaciouslib.utils.GVersion) Field(java.lang.reflect.Field)

Aggregations

InvocationTargetException (java.lang.reflect.InvocationTargetException)7 GVersion (org.anhcraft.spaciouslib.utils.GVersion)7 Method (java.lang.reflect.Method)6 Field (java.lang.reflect.Field)5 Channel (io.netty.channel.Channel)1 Constructor (java.lang.reflect.Constructor)1