Search in sources :

Example 1 with CachedWindow

use of org.dragonet.proxy.network.cache.CachedWindow in project DragonProxy by DragonetMC.

the class PCWindowItemsTranslator method translate.

public PEPacket[] translate(UpstreamSession session, ServerWindowItemsPacket packet) {
    if (!session.getWindowCache().hasWindow(packet.getWindowId())) {
        // Cache this
        session.getWindowCache().newCachedPacket(packet.getWindowId(), packet);
        return null;
    }
    CachedWindow win = session.getWindowCache().get(packet.getWindowId());
    if (packet.getWindowId() == 0) {
        if (packet.getItems().length < 40) {
            // Almost impossible to happen either.
            return null;
        }
        // Update items in window cache
        win.slots = packet.getItems();
        return InventoryTranslatorRegister.sendPlayerInventory(session);
    }
    InventoryTranslatorRegister.updateContent(session, packet);
    return null;
}
Also used : CachedWindow(org.dragonet.proxy.network.cache.CachedWindow)

Example 2 with CachedWindow

use of org.dragonet.proxy.network.cache.CachedWindow in project DragonProxy by DragonetMC.

the class InventoryTranslatorRegister method updateSlot.

public static void updateSlot(UpstreamSession session, ServerSetSlotPacket packet) {
    if (packet.getWindowId() == 0)
        // We don't process player inventory updates here.
        return;
    if (!session.getDataCache().containsKey(CacheKey.CURRENT_WINDOW_ID) || !session.getWindowCache().hasWindow(packet.getWindowId())) {
        session.getDownstream().send(new ClientCloseWindowPacket(packet.getWindowId()));
        return;
    }
    int openedId = (int) session.getDataCache().get(CacheKey.CURRENT_WINDOW_ID);
    if (packet.getWindowId() != openedId) {
        // Hmm
        closeOpened(session, true);
        session.getDownstream().send(new ClientCloseWindowPacket(packet.getWindowId()));
        return;
    }
    CachedWindow win = session.getWindowCache().get(openedId);
    if (win.size <= packet.getSlot()) {
        session.getDownstream().send(new ClientCloseWindowPacket(packet.getWindowId()));
        return;
    }
    IInventoryTranslator translator = TRANSLATORS.get(win.pcType);
    if (translator == null) {
        session.getDownstream().send(new ClientCloseWindowPacket(packet.getWindowId()));
        return;
    }
    // Update here
    win.slots[packet.getSlot()] = packet.getItem();
    translator.updateSlot(session, win, packet.getSlot());
}
Also used : CachedWindow(org.dragonet.proxy.network.cache.CachedWindow) IInventoryTranslator(org.dragonet.proxy.network.translator.IInventoryTranslator) ClientCloseWindowPacket(com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientCloseWindowPacket)

Example 3 with CachedWindow

use of org.dragonet.proxy.network.cache.CachedWindow in project DragonProxy by DragonetMC.

the class InventoryTranslatorRegister method open.

public static void open(UpstreamSession session, ServerOpenWindowPacket win) {
    closeOpened(session, true);
    if (TRANSLATORS.containsKey(win.getType())) {
        CachedWindow cached = new CachedWindow(win.getWindowId(), win.getType(), win.getSlots() + 36);
        session.getWindowCache().cacheWindow(cached);
        final IInventoryTranslator translator = TRANSLATORS.get(win.getType());
        session.getDataCache().put(CacheKey.CURRENT_WINDOW_ID, win.getWindowId());
        // -36 for the player inv size
        session.getDataCache().put(CacheKey.CURRENT_WINDOW_SIZE, win.getSlots());
        translator.prepare(session, cached);
        DragonProxy.getInstance().getGeneralThreadPool().schedule(() -> {
            // with this -> double chest, without -> content....
            translator.open(session, cached);
            cached.isOpen = true;
            com.github.steveice10.packetlib.packet.Packet[] items = session.getWindowCache().getCachedPackets(win.getWindowId());
            for (com.github.steveice10.packetlib.packet.Packet item : items) if (item != null)
                if (ServerWindowItemsPacket.class.isAssignableFrom(item.getClass()))
                    updateContent(session, (ServerWindowItemsPacket) item);
                else
                    updateSlot(session, (ServerSetSlotPacket) item);
        }, 200, TimeUnit.MILLISECONDS);
    } else
        // Not supported
        session.getDownstream().send(new ClientCloseWindowPacket(win.getWindowId()));
}
Also used : InventoryContentPacket(org.dragonet.protocol.packets.InventoryContentPacket) ServerOpenWindowPacket(com.github.steveice10.mc.protocol.packet.ingame.server.window.ServerOpenWindowPacket) ClientCloseWindowPacket(com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientCloseWindowPacket) PEPacket(org.dragonet.protocol.PEPacket) ServerWindowItemsPacket(com.github.steveice10.mc.protocol.packet.ingame.server.window.ServerWindowItemsPacket) ServerSetSlotPacket(com.github.steveice10.mc.protocol.packet.ingame.server.window.ServerSetSlotPacket) ContainerClosePacket(org.dragonet.protocol.packets.ContainerClosePacket) CachedWindow(org.dragonet.proxy.network.cache.CachedWindow) ServerSetSlotPacket(com.github.steveice10.mc.protocol.packet.ingame.server.window.ServerSetSlotPacket) ClientCloseWindowPacket(com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientCloseWindowPacket) IInventoryTranslator(org.dragonet.proxy.network.translator.IInventoryTranslator) ServerWindowItemsPacket(com.github.steveice10.mc.protocol.packet.ingame.server.window.ServerWindowItemsPacket)

Example 4 with CachedWindow

use of org.dragonet.proxy.network.cache.CachedWindow in project DragonProxy by DragonetMC.

the class InventoryTranslatorRegister method sendPlayerInventory.

public static PEPacket[] sendPlayerInventory(UpstreamSession session) {
    CachedWindow win = session.getWindowCache().getPlayerInventory();
    // Translate and send
    InventoryContentPacket ret = new InventoryContentPacket();
    ret.windowId = ContainerId.INVENTORY.getId();
    ret.items = new Slot[40];
    // hotbar
    for (int i = 36; i < 45; i++) ret.items[i - 36] = ItemBlockTranslator.translateSlotToPE(win.slots[i]);
    // inventory
    for (int i = 9; i < 36; i++) // TODO: Add NBT support
    ret.items[i] = ItemBlockTranslator.translateSlotToPE(win.slots[i]);
    // armors
    for (int i = 5; i < 9; i++) ret.items[i + 31] = ItemBlockTranslator.translateSlotToPE(win.slots[i]);
    // TODO: Add armor support
    return new PEPacket[] { ret };
}
Also used : CachedWindow(org.dragonet.proxy.network.cache.CachedWindow) PEPacket(org.dragonet.protocol.PEPacket) InventoryContentPacket(org.dragonet.protocol.packets.InventoryContentPacket)

Example 5 with CachedWindow

use of org.dragonet.proxy.network.cache.CachedWindow in project DragonProxy by DragonetMC.

the class InventoryTranslatorRegister method updateContent.

public static void updateContent(UpstreamSession session, ServerWindowItemsPacket packet) {
    if (packet.getWindowId() == 0)
        // We don't process player inventory updates here.
        return;
    if (!session.getDataCache().containsKey(CacheKey.CURRENT_WINDOW_ID) || !session.getWindowCache().hasWindow(packet.getWindowId())) {
        session.getDownstream().send(new ClientCloseWindowPacket(packet.getWindowId()));
        return;
    }
    int openedId = (int) session.getDataCache().get(CacheKey.CURRENT_WINDOW_ID);
    if (packet.getWindowId() != openedId) {
        // Hmm
        closeOpened(session, true);
        return;
    }
    CachedWindow win = session.getWindowCache().get(openedId);
    IInventoryTranslator translator = TRANSLATORS.get(win.pcType);
    if (translator == null) {
        session.getDownstream().send(new ClientCloseWindowPacket(packet.getWindowId()));
        return;
    }
    win.slots = packet.getItems();
    translator.updateContent(session, win);
}
Also used : CachedWindow(org.dragonet.proxy.network.cache.CachedWindow) IInventoryTranslator(org.dragonet.proxy.network.translator.IInventoryTranslator) ClientCloseWindowPacket(com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientCloseWindowPacket)

Aggregations

CachedWindow (org.dragonet.proxy.network.cache.CachedWindow)5 ClientCloseWindowPacket (com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientCloseWindowPacket)3 IInventoryTranslator (org.dragonet.proxy.network.translator.IInventoryTranslator)3 PEPacket (org.dragonet.protocol.PEPacket)2 InventoryContentPacket (org.dragonet.protocol.packets.InventoryContentPacket)2 ServerOpenWindowPacket (com.github.steveice10.mc.protocol.packet.ingame.server.window.ServerOpenWindowPacket)1 ServerSetSlotPacket (com.github.steveice10.mc.protocol.packet.ingame.server.window.ServerSetSlotPacket)1 ServerWindowItemsPacket (com.github.steveice10.mc.protocol.packet.ingame.server.window.ServerWindowItemsPacket)1 ContainerClosePacket (org.dragonet.protocol.packets.ContainerClosePacket)1