Search in sources :

Example 1 with GVersion

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

the class PacketListener method getChannel.

public static Channel getChannel(Player player) {
    GVersion v = GameVersion.getVersion();
    try {
        Class<?> craftPlayerClass = Class.forName("org.bukkit.craftbukkit." + v.toString() + ".entity.CraftPlayer");
        Class<?> nmsEntityPlayerClass = Class.forName("net.minecraft.server." + v.toString() + ".EntityPlayer");
        Class<?> playerConnClass = Class.forName("net.minecraft.server." + v.toString() + ".PlayerConnection");
        Class<?> networkManagerClass = Class.forName("net.minecraft.server." + v.toString() + ".NetworkManager");
        Object craftPlayer = craftPlayerClass.cast(player);
        Method handle = craftPlayerClass.getDeclaredMethod("getHandle");
        Object nmsEntity = handle.invoke(craftPlayer);
        Object nmsEntityPlayer = nmsEntityPlayerClass.cast(nmsEntity);
        Field playerConnField = nmsEntityPlayerClass.getDeclaredField("playerConnection");
        playerConnField.setAccessible(true);
        Object playerConn = playerConnField.get(nmsEntityPlayer);
        Field networkManagerField = playerConnClass.getDeclaredField("networkManager");
        networkManagerField.setAccessible(true);
        Object networkManager = networkManagerField.get(playerConn);
        Field channelField = networkManagerClass.getDeclaredField("channel");
        channelField.setAccessible(true);
        return (Channel) channelField.get(networkManager);
    } catch (InvocationTargetException | ClassNotFoundException | NoSuchFieldException | NoSuchMethodException | IllegalAccessException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : GVersion(org.anhcraft.spaciouslib.utils.GVersion) Field(java.lang.reflect.Field) Channel(io.netty.channel.Channel) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with GVersion

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

the class ActionBar method create.

/**
 * Creates an action bar message packet<br>
 * Supports raw JSON text.
 * @param text the message
 * @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, 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);
        // 1.11 - 1.12
        if (v.equals(GVersion.v1_11_R1) || v.equals(GVersion.v1_12_R1)) {
            Class<?> packetPlayOutTitleClass = Class.forName("net.minecraft.server." + GameVersion.getVersion().toString() + ".PacketPlayOutTitle");
            Class<?> enumTitleActionClass = Class.forName("net.minecraft.server." + v.toString() + ".PacketPlayOutTitle$EnumTitleAction");
            Field enumActionBarField = enumTitleActionClass.getDeclaredField("ACTIONBAR");
            enumActionBarField.setAccessible(true);
            Object enumActionBar = enumActionBarField.get(null);
            Constructor<?> packetCons = packetPlayOutTitleClass.getDeclaredConstructor(enumTitleActionClass, chatBaseComponentClass, int.class, int.class, int.class);
            Object packet = packetCons.newInstance(enumActionBar, chatBaseComponent, fadeIn, stay, fadeOut);
            return new PacketSender(packet);
        } else // 1.8 R1 - 1.10
        {
            Class<?> packetPlayOutChatClass = Class.forName("net.minecraft.server." + GameVersion.getVersion().toString() + ".PacketPlayOutChat");
            Constructor<?> packetCons = packetPlayOutChatClass.getDeclaredConstructor(chatBaseComponentClass, byte.class);
            Object packet = packetCons.newInstance(chatBaseComponent, (byte) 2);
            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 3 with GVersion

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

the class Animation method create.

/**
 * Creates an animation packet
 * @param entity the entity
 * @param type the type of the aniamtion
 * @return PacketSender object
 */
public static PacketSender create(Entity entity, Type type) {
    GVersion v = GameVersion.getVersion();
    try {
        Class<?> craftEntityClass = Class.forName("org.bukkit.craftbukkit." + v.toString() + ".entity.CraftEntity");
        Class<?> nmsEntityClass = Class.forName("net.minecraft.server." + v.toString() + ".Entity");
        Class<?> packetPlayOutAnimationClass = Class.forName("net.minecraft.server." + v.toString() + ".PacketPlayOutAnimation");
        Object craftEntity = craftEntityClass.cast(entity);
        Method handle = craftEntityClass.getDeclaredMethod("getHandle");
        Object nmsEntity = handle.invoke(craftEntity);
        Constructor<?> packetCons = packetPlayOutAnimationClass.getDeclaredConstructor(nmsEntityClass, int.class);
        Object packet = packetCons.newInstance(nmsEntity, type.getID());
        return new PacketSender(packet);
    } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : GVersion(org.anhcraft.spaciouslib.utils.GVersion) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 4 with GVersion

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

the class PacketSender method sendPlayer.

/**
 * Sends that packet to the given player
 * @param player the player
 * @return this object
 */
public PacketSender sendPlayer(Player player) {
    GVersion v = GameVersion.getVersion();
    try {
        Class<?> craftPlayerClass = Class.forName("org.bukkit.craftbukkit." + v.toString() + ".entity.CraftPlayer");
        Class<?> nmsEntityPlayerClass = Class.forName("net.minecraft.server." + v.toString() + ".EntityPlayer");
        Class<?> packetClass = Class.forName("net.minecraft.server." + v.toString() + ".Packet");
        Class<?> playerConnClass = Class.forName("net.minecraft.server." + v.toString() + ".PlayerConnection");
        Object craftPlayer = craftPlayerClass.cast(player);
        Method handle = craftPlayerClass.getDeclaredMethod("getHandle");
        Object nmsEntity = handle.invoke(craftPlayer);
        Object nmsEntityPlayer = nmsEntityPlayerClass.cast(nmsEntity);
        Field playerConnField = nmsEntityPlayerClass.getDeclaredField("playerConnection");
        playerConnField.setAccessible(true);
        Object playerConn = playerConnField.get(nmsEntityPlayer);
        Method sendPacket = playerConnClass.getDeclaredMethod("sendPacket", packetClass);
        sendPacket.invoke(playerConn, packet);
    } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | NoSuchFieldException e) {
        e.printStackTrace();
    }
    return this;
}
Also used : GVersion(org.anhcraft.spaciouslib.utils.GVersion) Field(java.lang.reflect.Field) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 5 with GVersion

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

the class BlockBreakAnimation method create.

/**
 * Creates a block-breaking animation packet
 * @param id the unique id for the packet
 * @param block the block
 * @param stage the stage value
 * @return PacketSender object
 */
public static PacketSender create(int id, Block block, int stage) {
    GVersion v = GameVersion.getVersion();
    try {
        Class<?> blockPositionClass = Class.forName("net.minecraft.server." + v.toString() + ".BlockPosition");
        Class<?> packetPlayOutBlockBreakAnimationClass = Class.forName("net.minecraft.server." + v.toString() + ".PacketPlayOutBlockBreakAnimation");
        Constructor blockPositionCons = blockPositionClass.getConstructor(int.class, int.class, int.class);
        Object blockPosition = blockPositionCons.newInstance(block.getLocation().getBlockX(), block.getLocation().getBlockY(), block.getLocation().getBlockZ());
        Constructor<?> packetCons = packetPlayOutBlockBreakAnimationClass.getDeclaredConstructor(int.class, blockPositionClass, int.class);
        Object packet = packetCons.newInstance(id, blockPosition, stage);
        return new PacketSender(packet);
    } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : GVersion(org.anhcraft.spaciouslib.utils.GVersion) Constructor(java.lang.reflect.Constructor) InvocationTargetException(java.lang.reflect.InvocationTargetException)

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