Search in sources :

Example 1 with WindowTypeTracker

use of com.github.dirtpowered.dirtmv.network.versions.Release47To5.inventory.WindowTypeTracker in project DirtMultiversion by DirtPowered.

the class InventoryPackets method registerTranslators.

@Override
public void registerTranslators() {
    // set slot
    addTranslator(0x2F, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            ProtocolStorage storage = session.getStorage();
            short windowId = data.read(Type.UNSIGNED_BYTE, 0);
            short slot = data.read(Type.SHORT, 1);
            int windowType = 0;
            if (storage.hasObject(WindowTypeTracker.class)) {
                WindowTypeTracker windowTracker = storage.get(WindowTypeTracker.class);
                windowType = windowTracker.getWindowType(windowId);
            }
            ItemStack originalItem = data.read(Type.V1_3R_ITEM, 2);
            if (originalItem == null)
                return new PacketData(0x2F, data.getObjects());
            ItemStack itemStack = itemRemapper.replaceItem(originalItem);
            if (windowType == 4) {
                if (slot >= 1) {
                    slot++;
                }
            }
            // quick-bar cache
            if (storage.hasObject(QuickBarTracker.class)) {
                QuickBarTracker quickBarTracker = storage.get(QuickBarTracker.class);
                quickBarTracker.setItem(slot, itemStack);
            }
            return PacketUtil.createPacket(0x2F, new TypeHolder[] { data.read(0), set(Type.SHORT, slot), set(Type.V1_8R_ITEM, itemStack) });
        }
    });
    // window items
    addTranslator(0x30, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            ProtocolStorage storage = session.getStorage();
            short windowId = data.read(Type.UNSIGNED_BYTE, 0);
            ItemStack[] itemArray = data.read(Type.V1_3R_ITEM_ARRAY, 1);
            int windowType = 0;
            if (storage.hasObject(WindowTypeTracker.class)) {
                WindowTypeTracker windowTracker = storage.get(WindowTypeTracker.class);
                windowType = windowTracker.getWindowType(windowId);
            }
            if (windowType == 4) {
                ItemStack[] copy = itemArray;
                itemArray = new ItemStack[copy.length + 1];
                itemArray[0] = copy[0];
                int slot = 0;
                while (slot < copy.length - 1) {
                    itemArray[slot + 2] = copy[slot + 1];
                    slot++;
                }
                // lapis
                itemArray[1] = new ItemStack(351, 3, 4, null);
            }
            for (int i = 0; i < itemArray.length; i++) {
                ItemStack originalItem = itemArray[i];
                ItemStack item = originalItem;
                if (originalItem != null) {
                    item = itemRemapper.replaceItem(originalItem);
                }
                itemArray[i] = item;
                // quick-bar cache
                if (storage.hasObject(QuickBarTracker.class)) {
                    QuickBarTracker quickBarTracker = storage.get(QuickBarTracker.class);
                    quickBarTracker.setItem(i, item);
                }
            }
            return PacketUtil.createPacket(0x30, new TypeHolder[] { data.read(0), set(Type.V1_8R_ITEM_ARRAY, itemArray) });
        }
    });
    // open window
    addTranslator(0x2D, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            ProtocolStorage storage = session.getStorage();
            short windowId = data.read(Type.UNSIGNED_BYTE, 0);
            short type = data.read(Type.UNSIGNED_BYTE, 1);
            String title = data.read(Type.V1_7_STRING, 2);
            short slots = data.read(Type.UNSIGNED_BYTE, 3);
            // non storage inventories have always 0 slots
            slots = InventoryUtils.isNonStorageInventory(type) ? 0 : slots;
            // cache window type (for enchanting tables)
            if (storage.hasObject(WindowTypeTracker.class)) {
                WindowTypeTracker windowTracker = storage.get(WindowTypeTracker.class);
                windowTracker.setWindowTypeFor(windowId, type);
            }
            return PacketUtil.createPacket(0x2D, new TypeHolder[] { data.read(0), set(Type.V1_7_STRING, InventoryUtils.getNamedTypeFromId(type)), set(Type.V1_7_STRING, InventoryUtils.addTranslateComponent(type == 6 ? "entity.Villager.name" : title)), set(Type.UNSIGNED_BYTE, slots) });
        }
    });
    // window click
    addTranslator(0x0E, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            ProtocolStorage storage = session.getStorage();
            byte windowId = data.read(Type.BYTE, 0);
            short slot = data.read(Type.SHORT, 1);
            int windowType = 0;
            if (storage.hasObject(WindowTypeTracker.class)) {
                WindowTypeTracker windowTracker = storage.get(WindowTypeTracker.class);
                windowType = windowTracker.getWindowType(windowId);
            }
            if (windowType == 4) {
                if (slot > 1) {
                    slot--;
                }
            }
            if (storage.hasObject(QuickBarTracker.class)) {
                QuickBarTracker cache = storage.get(QuickBarTracker.class);
                boolean shiftClick = data.read(Type.BYTE, 4) == 1;
                updateLocalInventory(cache, slot, shiftClick);
            }
            return PacketUtil.createPacket(0x0E, new TypeHolder[] { data.read(0), set(Type.SHORT, slot), data.read(2), data.read(3), data.read(4), set(Type.V1_3R_ITEM, data.read(Type.V1_8R_ITEM, 5)) });
        }
    });
    // creative item get
    addTranslator(0x10, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            ItemStack item = data.read(Type.V1_8R_ITEM, 1);
            boolean notNull = item != null;
            if (notNull && !creativeTab.exists(item.getItemId())) {
                // replace all unknown items to stone
                item.setItemId(1);
                item.setData(0);
            }
            return PacketUtil.createPacket(0x10, new TypeHolder[] { data.read(0), set(Type.V1_3R_ITEM, item) });
        }
    });
    // change held slot
    addTranslator(0x09, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            ProtocolStorage storage = session.getStorage();
            if (storage.hasObject(QuickBarTracker.class)) {
                QuickBarTracker quickBarTracker = storage.get(QuickBarTracker.class);
                quickBarTracker.setCurrentHotBarSlot(data.read(Type.SHORT, 0));
            }
            return data;
        }
    });
    // change held slot (server-side)
    addTranslator(0x09, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            ProtocolStorage storage = session.getStorage();
            if (storage.hasObject(QuickBarTracker.class)) {
                QuickBarTracker quickBarTracker = storage.get(QuickBarTracker.class);
                quickBarTracker.setCurrentHotBarSlot(data.read(Type.BYTE, 0));
            }
            return data;
        }
    });
    // window property
    addTranslator(0x31, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            int property = data.read(Type.SHORT, 1);
            switch(property) {
                case 0:
                    property = 2;
                    PacketData windowProperty = PacketUtil.createPacket(0x31, new TypeHolder[] { data.read(0), set(Type.SHORT, (short) 3), set(Type.SHORT, (short) 200) });
                    session.sendPacket(windowProperty, PacketDirection.TO_CLIENT, getFrom());
                    break;
                case 1:
                    property = 0;
                    break;
                case 2:
                    property = 1;
                    break;
            }
            return PacketUtil.createPacket(0x31, new TypeHolder[] { data.read(0), set(Type.SHORT, (short) property), data.read(2) });
        }
    });
}
Also used : WindowTypeTracker(com.github.dirtpowered.dirtmv.network.versions.Release47To5.inventory.WindowTypeTracker) TypeHolder(com.github.dirtpowered.dirtmv.data.protocol.TypeHolder) ServerSession(com.github.dirtpowered.dirtmv.network.server.ServerSession) ProtocolStorage(com.github.dirtpowered.dirtmv.data.user.ProtocolStorage) PacketTranslator(com.github.dirtpowered.dirtmv.data.translator.PacketTranslator) ItemStack(com.github.dirtpowered.dirtmv.data.protocol.objects.ItemStack) PacketData(com.github.dirtpowered.dirtmv.data.protocol.PacketData) QuickBarTracker(com.github.dirtpowered.dirtmv.network.versions.Release47To5.inventory.QuickBarTracker)

Example 2 with WindowTypeTracker

use of com.github.dirtpowered.dirtmv.network.versions.Release47To5.inventory.WindowTypeTracker in project DirtMultiversion by DirtPowered.

the class ProtocolRelease47To5 method onConnect.

@Override
public void onConnect(ServerSession session) {
    ProtocolStorage storage = session.getStorage();
    storage.set(OnGroundTracker.class, new OnGroundTracker());
    storage.set(WindowTypeTracker.class, new WindowTypeTracker());
    storage.set(QuickBarTracker.class, new QuickBarTracker());
    // additional block storage (for nether portal rotation fix)
    storage.set(PortalFrameCache.class, new PortalFrameCache());
    if (session.getMain().getConfiguration().enableViaVersion()) {
        // it's not needed for 1.8->1.7, but ViaVersion will need that to fix issues
        // with eating, entering portals, effects (potions)
        storage.set(PlayerMovementTracker.class, new PlayerMovementTracker());
    }
    // check if 1.6 entity tracker exists - if not, create one
    if (!storage.hasObject(EntityTracker.class)) {
        storage.set(V1_7EntityTracker.class, new V1_7EntityTracker());
    }
    // fixes block hardness inconsistencies
    if (session.getMain().getConfiguration().getServerVersion() == MinecraftVersion.B1_7_3) {
        storage.set(BlockMiningTimeFixer.class, new BlockMiningTimeFixer(session));
    }
}
Also used : WindowTypeTracker(com.github.dirtpowered.dirtmv.network.versions.Release47To5.inventory.WindowTypeTracker) ProtocolStorage(com.github.dirtpowered.dirtmv.data.user.ProtocolStorage) PortalFrameCache(com.github.dirtpowered.dirtmv.network.versions.Release47To5.chunk.PortalFrameCache) V1_7EntityTracker(com.github.dirtpowered.dirtmv.network.versions.Release47To5.entity.V1_7EntityTracker) EntityTracker(com.github.dirtpowered.dirtmv.network.versions.Release73To61.entity.EntityTracker) OnGroundTracker(com.github.dirtpowered.dirtmv.network.versions.Release47To5.entity.OnGroundTracker) BlockMiningTimeFixer(com.github.dirtpowered.dirtmv.network.versions.Release47To5.other.BlockMiningTimeFixer) V1_7EntityTracker(com.github.dirtpowered.dirtmv.network.versions.Release47To5.entity.V1_7EntityTracker) QuickBarTracker(com.github.dirtpowered.dirtmv.network.versions.Release47To5.inventory.QuickBarTracker) PlayerMovementTracker(com.github.dirtpowered.dirtmv.network.versions.Release47To5.entity.PlayerMovementTracker)

Aggregations

ProtocolStorage (com.github.dirtpowered.dirtmv.data.user.ProtocolStorage)2 QuickBarTracker (com.github.dirtpowered.dirtmv.network.versions.Release47To5.inventory.QuickBarTracker)2 WindowTypeTracker (com.github.dirtpowered.dirtmv.network.versions.Release47To5.inventory.WindowTypeTracker)2 PacketData (com.github.dirtpowered.dirtmv.data.protocol.PacketData)1 TypeHolder (com.github.dirtpowered.dirtmv.data.protocol.TypeHolder)1 ItemStack (com.github.dirtpowered.dirtmv.data.protocol.objects.ItemStack)1 PacketTranslator (com.github.dirtpowered.dirtmv.data.translator.PacketTranslator)1 ServerSession (com.github.dirtpowered.dirtmv.network.server.ServerSession)1 PortalFrameCache (com.github.dirtpowered.dirtmv.network.versions.Release47To5.chunk.PortalFrameCache)1 OnGroundTracker (com.github.dirtpowered.dirtmv.network.versions.Release47To5.entity.OnGroundTracker)1 PlayerMovementTracker (com.github.dirtpowered.dirtmv.network.versions.Release47To5.entity.PlayerMovementTracker)1 V1_7EntityTracker (com.github.dirtpowered.dirtmv.network.versions.Release47To5.entity.V1_7EntityTracker)1 BlockMiningTimeFixer (com.github.dirtpowered.dirtmv.network.versions.Release47To5.other.BlockMiningTimeFixer)1 EntityTracker (com.github.dirtpowered.dirtmv.network.versions.Release73To61.entity.EntityTracker)1