Search in sources :

Example 1 with PacketfromPlayerEvent

use of org.dragonet.proxy.events.defaults.packets.PacketfromPlayerEvent in project DragonProxy by DragonetMC.

the class PEPacketProcessor method handlePacket.

// this method should be in UpstreamSession
public void handlePacket(PEPacket packet) {
    if (packet == null)
        return;
    if (!client.getProxy().getConfig().disable_packet_events) {
        PacketfromPlayerEvent packetEvent = new PacketfromPlayerEvent(client, packet);
        client.getProxy().getEventManager().callEvent(packetEvent);
        if (packetEvent.isCancelled()) {
            return;
        }
    }
    // Wait for player logginig in
    if ("online_login_wait".equals(this.client.getDataCache().get(CacheKey.AUTHENTICATION_STATE))) {
        if (packet.pid() == ProtocolInfo.MOVE_PLAYER_PACKET) {
            InputComponent username = new InputComponent(this.client.getProxy().getLang().get(Lang.FORM_LOGIN_USERNAME)).setPlaceholder("steve@example.com");
            InputComponent password = new InputComponent(this.client.getProxy().getLang().get(Lang.FORM_LOGIN_PASSWORD)).setPlaceholder("123456");
            if (this.client.getProxy().getConfig().auto_login) {
                username.setDefaultValue(this.client.getProxy().getConfig().online_username);
                password.setDefaultValue(this.client.getProxy().getConfig().online_password);
            }
            // client.getDataCache().put(CacheKey.AUTHENTICATION_STATE, "online_login");
            ModalFormRequestPacket packetForm = new ModalFormRequestPacket();
            CustomFormComponent form = new CustomFormComponent(this.client.getProxy().getLang().get(Lang.FORM_LOGIN_TITLE));
            form.addComponent(new LabelComponent(this.client.getProxy().getLang().get(Lang.FORM_LOGIN_DESC)));
            form.addComponent(new LabelComponent(this.client.getProxy().getLang().get(Lang.FORM_LOGIN_PROMPT)));
            form.addComponent(username);
            form.addComponent(password);
            packetForm.formId = 1;
            packetForm.formData = form.serializeToJson().toString();
            this.client.sendPacket(packetForm, true);
            return;
        }
        if (packet.pid() == ProtocolInfo.MODAL_FORM_RESPONSE_PACKET) {
            try {
                this.client.sendChat(this.client.getProxy().getLang().get(Lang.MESSAGE_LOGIN_PROGRESS));
                ModalFormResponsePacket formResponse = (ModalFormResponsePacket) packet;
                JsonArray array = JsonUtil.parseArray(formResponse.formData);
                this.client.getDataCache().remove(CacheKey.AUTHENTICATION_STATE);
                this.client.authenticate(array.get(2).getAsString(), array.get(3).getAsString(), authProxy);
            } catch (Exception ex) {
                this.client.sendChat(this.client.getProxy().getLang().get(Lang.MESSAGE_ONLINE_LOGIN_FAILD));
            }
            return;
        }
    }
    switch(packet.pid()) {
        case ProtocolInfo.BATCH_PACKET:
            DragonProxy.getInstance().getLogger().debug("Received batch packet from client !");
            break;
        case ProtocolInfo.LOGIN_PACKET:
            this.client.onLogin((LoginPacket) packet);
            break;
        case ProtocolInfo.RESOURCE_PACK_CLIENT_RESPONSE_PACKET:
            if (!this.client.isLoggedIn())
                this.client.postLogin();
            break;
        default:
            if (this.client.getDownstream() == null || !this.client.getDownstream().isConnected())
                break;
            if (enableForward.get() && FORWARDED_PACKETS.contains(packet.getClass())) {
                BinaryStream bis = new BinaryStream();
                bis.putString("PacketForward");
                bis.putByteArray(packet.getBuffer());
                ClientPluginMessagePacket msg = new ClientPluginMessagePacket("DragonProxy", bis.getBuffer());
                client.getDownstream().send(msg);
            } else // IMPORTANT Do not send packet until client is connected !
            if (client.isSpawned()) {
                Packet[] translated = PacketTranslatorRegister.translateToPC(this.client, packet);
                if (translated == null || translated.length == 0)
                    break;
                client.getDownstream().send(translated);
            }
            break;
    }
}
Also used : JsonArray(com.google.gson.JsonArray) InputComponent(org.dragonet.common.gui.InputComponent) BinaryStream(org.dragonet.common.utilities.BinaryStream) PacketfromPlayerEvent(org.dragonet.proxy.events.defaults.packets.PacketfromPlayerEvent) CustomFormComponent(org.dragonet.common.gui.CustomFormComponent) ClientPluginMessagePacket(com.github.steveice10.mc.protocol.packet.ingame.client.ClientPluginMessagePacket) JsonParseException(com.google.gson.JsonParseException) LabelComponent(org.dragonet.common.gui.LabelComponent)

Aggregations

ClientPluginMessagePacket (com.github.steveice10.mc.protocol.packet.ingame.client.ClientPluginMessagePacket)1 JsonArray (com.google.gson.JsonArray)1 JsonParseException (com.google.gson.JsonParseException)1 CustomFormComponent (org.dragonet.common.gui.CustomFormComponent)1 InputComponent (org.dragonet.common.gui.InputComponent)1 LabelComponent (org.dragonet.common.gui.LabelComponent)1 BinaryStream (org.dragonet.common.utilities.BinaryStream)1 PacketfromPlayerEvent (org.dragonet.proxy.events.defaults.packets.PacketfromPlayerEvent)1