Search in sources :

Example 1 with NMSPacket

use of io.github.retrooper.packetevents.packetwrappers.NMSPacket in project packetevents by retrooper.

the class PacketProcessorInternal method postWrite.

/**
 * Make PacketEvents process an outgoing PLAY packet after minecraft has already sent the packet.
 * This doesn't necessarily mean the client already received the packet,
 * but the server has sent it for sure by this time.
 * As minecraft has already processed the packet, we cannot cancel the action, nor the event.
 *
 * @param player  Packet receiver.
 * @param channel Netty channel of the packet receiver.
 * @param packet  NMS Packet.
 */
public void postWrite(Player player, Object channel, Object packet) {
    if (getPacketState(player, packet) == PacketState.PLAY) {
        PostPacketPlaySendEvent event = new PostPacketPlaySendEvent(player, channel, new NMSPacket(packet));
        PacketEvents.get().getEventManager().callEvent(event);
        interceptPostPlaySend(event);
    }
}
Also used : NMSPacket(io.github.retrooper.packetevents.packetwrappers.NMSPacket)

Example 2 with NMSPacket

use of io.github.retrooper.packetevents.packetwrappers.NMSPacket in project packetevents by retrooper.

the class PacketProcessorInternal method read.

/**
 * Force PacketEvents to process an incoming packet.
 * This method could be used to spoof an incoming packet to the PacketEvents API.
 *
 * @param player  Packet sender.
 * @param channel Packet sender's netty channel.
 * @param packet  NMS Packet.
 * @return NMS Packet, null if the event was cancelled.
 */
public PacketData read(Player player, Object channel, Object packet) {
    PacketData data = new PacketData();
    data.packet = packet;
    PacketState state = getPacketState(player, packet);
    if (state == null) {
        return data;
    }
    switch(state) {
        case STATUS:
            PacketStatusReceiveEvent statusEvent = new PacketStatusReceiveEvent(channel, new NMSPacket(packet));
            PacketEvents.get().getEventManager().callEvent(statusEvent);
            // Apply modifications to the packet
            packet = statusEvent.getNMSPacket().getRawNMSPacket();
            // Process internally
            interceptStatusReceive(statusEvent);
            if (statusEvent.isCancelled()) {
                // Set the packet to null if the event was cancelled
                packet = null;
            }
            break;
        case HANDSHAKING:
            PacketHandshakeReceiveEvent handshakeEvent = new PacketHandshakeReceiveEvent(channel, new NMSPacket(packet));
            PacketEvents.get().getEventManager().callEvent(handshakeEvent);
            // Apply modifications to the packet
            packet = handshakeEvent.getNMSPacket().getRawNMSPacket();
            // Process internally
            interceptHandshakeReceive(handshakeEvent);
            if (handshakeEvent.isCancelled()) {
                // Set the packet to null if the event was cancelled
                packet = null;
            }
            break;
        case LOGIN:
            // Cache the channel
            PacketLoginReceiveEvent loginEvent = new PacketLoginReceiveEvent(channel, new NMSPacket(packet));
            if (loginEvent.getPacketId() == PacketType.Login.Client.START) {
                WrappedPacketLoginInStart startWrapper = new WrappedPacketLoginInStart(loginEvent.getNMSPacket());
                WrappedGameProfile gameProfile = startWrapper.getGameProfile();
                PacketEvents.get().getPlayerUtils().channels.put(gameProfile.getName(), channel);
            }
            PacketEvents.get().getEventManager().callEvent(loginEvent);
            packet = loginEvent.getNMSPacket().getRawNMSPacket();
            interceptLoginReceive(loginEvent);
            if (loginEvent.isCancelled()) {
                packet = null;
            }
            break;
        case PLAY:
            PacketPlayReceiveEvent event = new PacketPlayReceiveEvent(player, channel, new NMSPacket(packet));
            PacketEvents.get().getEventManager().callEvent(event);
            packet = event.getNMSPacket().getRawNMSPacket();
            interceptPlayReceive(event);
            if (event.isCancelled()) {
                packet = null;
            }
            break;
    }
    data.packet = packet;
    return data;
}
Also used : PacketState(io.github.retrooper.packetevents.packettype.PacketState) WrappedPacketLoginInStart(io.github.retrooper.packetevents.packetwrappers.login.in.start.WrappedPacketLoginInStart) NMSPacket(io.github.retrooper.packetevents.packetwrappers.NMSPacket) WrappedGameProfile(io.github.retrooper.packetevents.utils.gameprofile.WrappedGameProfile)

Example 3 with NMSPacket

use of io.github.retrooper.packetevents.packetwrappers.NMSPacket in project packetevents by retrooper.

the class PacketProcessorInternal method write.

/**
 * Force PacketEvents to process an outgoing packet.
 * This method could be used to spoof an outgoing packet to the PacketEvents API.
 *
 * @param player  Packet receiver.
 * @param channel Packet receiver's netty channel.
 * @param packet  NMS Packet.
 * @return NMS Packet, null if the event was cancelled.
 */
public PacketData write(Player player, Object channel, Object packet) {
    PacketData data = new PacketData();
    data.packet = packet;
    PacketState state = getPacketState(player, packet);
    if (state == null) {
        return data;
    }
    switch(state) {
        case STATUS:
            PacketStatusSendEvent statusEvent = new PacketStatusSendEvent(channel, new NMSPacket(packet));
            PacketEvents.get().getEventManager().callEvent(statusEvent);
            if (statusEvent.isPostTaskAvailable()) {
                data.postAction = statusEvent.getPostTask();
            }
            packet = statusEvent.getNMSPacket().getRawNMSPacket();
            interceptStatusSend(statusEvent);
            if (statusEvent.isCancelled()) {
                packet = null;
            }
            break;
        case LOGIN:
            PacketLoginSendEvent loginEvent = new PacketLoginSendEvent(channel, new NMSPacket(packet));
            PacketEvents.get().getEventManager().callEvent(loginEvent);
            if (loginEvent.isPostTaskAvailable()) {
                data.postAction = loginEvent.getPostTask();
            }
            packet = loginEvent.getNMSPacket().getRawNMSPacket();
            interceptLoginSend(loginEvent);
            if (loginEvent.isCancelled()) {
                packet = null;
            }
            break;
        case PLAY:
            PacketPlaySendEvent playEvent = new PacketPlaySendEvent(player, channel, new NMSPacket(packet));
            PacketEvents.get().getEventManager().callEvent(playEvent);
            if (playEvent.isPostTaskAvailable()) {
                data.postAction = playEvent.getPostTask();
            }
            packet = playEvent.getNMSPacket().getRawNMSPacket();
            interceptPlaySend(playEvent);
            if (playEvent.isCancelled()) {
                packet = null;
            }
            break;
    }
    data.packet = packet;
    return data;
}
Also used : PacketState(io.github.retrooper.packetevents.packettype.PacketState) NMSPacket(io.github.retrooper.packetevents.packetwrappers.NMSPacket)

Example 4 with NMSPacket

use of io.github.retrooper.packetevents.packetwrappers.NMSPacket in project packetevents by retrooper.

the class AttributeSnapshotWrapper method create.

public static AttributeSnapshotWrapper create(String key, double value, Collection<AttributeModifierWrapper> modifiers) {
    Object nmsAttributeSnapshot = null;
    if (attributeSnapshotClass == null) {
        attributeSnapshotClass = NMSUtils.getNMSClassWithoutException("AttributeSnapshot");
        if (attributeSnapshotClass == null) {
            attributeSnapshotClass = SubclassUtil.getSubClass(PacketTypeClasses.Play.Server.UPDATE_ATTRIBUTES, "AttributeSnapshot");
        }
    }
    if (attributeSnapshotConstructor == null) {
        try {
            attributeSnapshotConstructor = attributeSnapshotClass.getConstructor(PacketTypeClasses.Play.Server.UPDATE_ATTRIBUTES, String.class, double.class, Collection.class);
            constructorMode = 0;
        } catch (NoSuchMethodException e) {
            try {
                attributeSnapshotConstructor = attributeSnapshotClass.getConstructor(String.class, double.class, Collection.class);
                constructorMode = 1;
            } catch (NoSuchMethodException e2) {
                constructorMode = 2;
                if (attributeBaseClass == null) {
                    attributeBaseClass = NMSUtils.getNMSClassWithoutException("AttributeBase");
                }
                if (attributeSnapshotConstructor == null) {
                    try {
                        attributeSnapshotConstructor = attributeSnapshotClass.getConstructor(attributeBaseClass, double.class, Collection.class);
                    } catch (NoSuchMethodException e3) {
                        e3.printStackTrace();
                    }
                }
                Class<?> iRegistryClass = NMSUtils.getNMSClassWithoutException("IRegistry");
                if (iRegistryClass != null) {
                    try {
                        iRegistryAttributeBaseField = iRegistryClass.getField("ATTRIBUTE");
                        getiRegistryByMinecraftKeyMethod = iRegistryClass.getDeclaredMethod("get", NMSUtils.minecraftKeyClass);
                    } catch (NoSuchFieldException | NoSuchMethodException ignored) {
                    }
                }
            }
        }
    }
    List<Object> nmsModifiers = new ArrayList<>(modifiers.size());
    for (AttributeModifierWrapper modifier : modifiers) {
        nmsModifiers.add(modifier.getNMSPacket().getRawNMSPacket());
    }
    try {
        switch(constructorMode) {
            case 0:
                nmsAttributeSnapshot = attributeSnapshotConstructor.newInstance(null, key, value, nmsModifiers);
                break;
            case 1:
                nmsAttributeSnapshot = attributeSnapshotConstructor.newInstance(key, value, nmsModifiers);
                break;
            case 2:
                Object minecraftKey = NMSUtils.generateMinecraftKeyNew(key);
                Object attributeObj = iRegistryAttributeBaseField.get(null);
                Object nmsAttributeBase = getiRegistryByMinecraftKeyMethod.invoke(attributeObj, minecraftKey);
                nmsAttributeSnapshot = attributeSnapshotConstructor.newInstance(nmsAttributeBase, value, nmsModifiers);
                break;
            default:
                return null;
        }
    } catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
        e.printStackTrace();
    }
    return new AttributeSnapshotWrapper(new NMSPacket(nmsAttributeSnapshot));
}
Also used : ArrayList(java.util.ArrayList) InvocationTargetException(java.lang.reflect.InvocationTargetException) Collection(java.util.Collection) NMSPacket(io.github.retrooper.packetevents.packetwrappers.NMSPacket)

Example 5 with NMSPacket

use of io.github.retrooper.packetevents.packetwrappers.NMSPacket in project packetevents by retrooper.

the class AttributeSnapshotWrapper method getKey.

public String getKey() {
    if (stringKeyPresent) {
        return readString(0);
    } else {
        Object attributeBase = readObject(0, attributeBaseClass);
        WrappedPacket attributeBaseWrapper = new WrappedPacket(new NMSPacket(attributeBase), attributeBaseClass);
        return attributeBaseWrapper.readString(0);
    }
}
Also used : WrappedPacket(io.github.retrooper.packetevents.packetwrappers.WrappedPacket) NMSPacket(io.github.retrooper.packetevents.packetwrappers.NMSPacket)

Aggregations

NMSPacket (io.github.retrooper.packetevents.packetwrappers.NMSPacket)64 WrappedPacket (io.github.retrooper.packetevents.packetwrappers.WrappedPacket)43 InvocationTargetException (java.lang.reflect.InvocationTargetException)9 ArrayList (java.util.ArrayList)7 List (java.util.List)3 PacketState (io.github.retrooper.packetevents.packettype.PacketState)2 WrappedGameProfile (io.github.retrooper.packetevents.utils.gameprofile.WrappedGameProfile)2 ListWrapper (io.github.retrooper.packetevents.utils.list.ListWrapper)2 Vector3d (io.github.retrooper.packetevents.utils.vector.Vector3d)2 Vector3i (io.github.retrooper.packetevents.utils.vector.Vector3i)2 EpollSocketChannel (io.netty.channel.epoll.EpollSocketChannel)2 Field (java.lang.reflect.Field)2 HashMap (java.util.HashMap)2 Player (org.bukkit.entity.Player)2 GrimPlayer (ac.grim.grimac.player.GrimPlayer)1 BaseChunk (ac.grim.grimac.utils.chunkdata.BaseChunk)1 Column (ac.grim.grimac.utils.chunks.Column)1 GameProfile (com.mojang.authlib.GameProfile)1 PlayerChannelHandlerLegacy (io.github.retrooper.packetevents.injector.legacy.PlayerChannelHandlerLegacy)1 PlayerChannelHandlerModern (io.github.retrooper.packetevents.injector.modern.PlayerChannelHandlerModern)1