Search in sources :

Example 1 with PacketTranslator

use of com.github.dirtpowered.dirtmv.data.translator.PacketTranslator in project DirtMultiversion by DirtPowered.

the class ProtocolRelease29To28 method registerTranslators.

@Override
public void registerTranslators() {
    // login
    addTranslator(0x01, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x01, new TypeHolder[] { // protocol version
            set(Type.INT, 28), data.read(1), data.read(2), data.read(3), data.read(4), data.read(5), data.read(6), data.read(7) });
        }
    });
    // player abilities
    addTranslator(0xCA, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            // cancel packet
            return cancel();
        }
    });
    // window click
    addTranslator(0x66, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            // 1.2.5 introduced shift-clicking support in furnaces. We have to block that action
            int windowId = data.read(Type.BYTE, 0);
            boolean shiftClick = (data.read(Type.BYTE, 4) == 1);
            ItemStack item = data.read(Type.V1_0R_ITEM, 5);
            if (shiftClick && windowId != 0) /* generic inventory */
            {
                // sending other than original item will cancel any inventory interaction
                item.setItemId(34);
            }
            return PacketUtil.createPacket(0x66, new TypeHolder[] { data.read(0), data.read(1), data.read(2), data.read(3), data.read(4), set(Type.V1_0R_ITEM, item) });
        }
    });
}
Also used : TypeHolder(com.github.dirtpowered.dirtmv.data.protocol.TypeHolder) ServerSession(com.github.dirtpowered.dirtmv.network.server.ServerSession) 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)

Example 2 with PacketTranslator

use of com.github.dirtpowered.dirtmv.data.translator.PacketTranslator in project DirtMultiversion by DirtPowered.

the class ProtocolRelease39To29 method registerTranslators.

@Override
public void registerTranslators() {
    // handshake
    addTranslator(0x02, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            // since 1.3 handshake is one-way (client -> server)
            return cancel();
        }
    });
    // handshake
    addTranslator(0x02, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            String username = data.read(Type.STRING, 1);
            session.getUserData().setUsername(username);
            PacketData encryptRequest = EncryptionUtils.createEncryptionRequest(session);
            // server -> client
            session.sendPacket(encryptRequest, PacketDirection.TO_CLIENT, getFrom());
            return PacketUtil.createPacket(0x02, new TypeHolder[] { set(Type.STRING, username) });
        }
    });
    // client shared key
    addTranslator(0xFC, PacketDirection.TO_SERVER, new PacketTranslator() {

        @SneakyThrows
        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            PacketData request = session.getUserData().getProxyRequest();
            SecretKey sharedKey = EncryptionUtils.getSecret(data, request);
            // server -> client
            EncryptionUtils.sendEmptyEncryptionResponse(session, getFrom());
            // enable encryption
            EncryptionUtils.setEncryption(session.getChannel(), sharedKey);
            // cancel packet
            return cancel();
        }
    });
    // client command
    addTranslator(0xCD, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            byte command = data.read(Type.BYTE, 0);
            if (command == 0x00) {
                String username = session.getUserData().getUsername();
                return PacketUtil.createPacket(0x01, new TypeHolder[] { // protocol version
                set(Type.INT, 29), set(Type.STRING, username), set(Type.STRING, "default"), set(Type.INT, 0), set(Type.INT, 0), set(Type.BYTE, (byte) 0), set(Type.BYTE, (byte) 0), set(Type.BYTE, (byte) 0) });
            } else {
                return PacketUtil.createPacket(0x09, new TypeHolder[] { set(Type.INT, 0), set(Type.BYTE, (byte) 0), set(Type.BYTE, (byte) 0), set(Type.SHORT, (short) 0), set(Type.STRING, "default") });
            }
        }
    });
    // login
    addTranslator(0x01, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            session.getUserData().setEntityId(data.read(Type.INT, 0));
            return PacketUtil.createPacket(0x01, new TypeHolder[] { data.read(0), data.read(2), set(Type.BYTE, data.read(Type.INT, 3).byteValue()), set(Type.BYTE, data.read(Type.INT, 4).byteValue()), data.read(5), data.read(6), data.read(7) });
        }
    });
    // tab command complete
    addTranslator(0xCB, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return cancel();
        }
    });
    // client settings
    addTranslator(0xCC, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return cancel();
        }
    });
    // spawn position
    addTranslator(0x06, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            EntityTracker tracker = new EntityTracker();
            int x = data.read(Type.INT, 0);
            int y = data.read(Type.INT, 1);
            int z = data.read(Type.INT, 2);
            Location loc = new Location(x, y, z);
            tracker.addEntity(-999, new HumanEntity(-999, loc));
            session.getStorage().set(EntityTracker.class, tracker);
            return data;
        }
    });
    // window click
    addTranslator(0x66, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            ItemStack newItem = data.read(Type.V1_3R_ITEM, 5);
            return PacketUtil.createPacket(0x66, new TypeHolder[] { data.read(0), data.read(1), data.read(2), data.read(3), data.read(4), set(Type.V1_0R_ITEM, newItem) });
        }
    });
    // set slot
    addTranslator(0x67, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            ItemStack oldItem = data.read(Type.V1_0R_ITEM, 2);
            return PacketUtil.createPacket(0x67, new TypeHolder[] { data.read(0), data.read(1), set(Type.V1_3R_ITEM, oldItem) });
        }
    });
    // window items
    addTranslator(0x68, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            ItemStack[] items = data.read(Type.V1_0R_ITEM_ARRAY, 1);
            for (ItemStack item : items) {
                if (item != null && item.getCompoundTag() == null) {
                    // since 1.3 all items contains NBT data
                    item.setCompoundTag(CompoundBinaryTag.empty());
                }
            }
            return PacketUtil.createPacket(0x68, new TypeHolder[] { data.read(0), set(Type.V1_3R_ITEM_ARRAY, items) });
        }
    });
    // entity destroy
    addTranslator(0x1D, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            int entityId = data.read(Type.INT, 0);
            EntityTracker tracker = session.getStorage().get(EntityTracker.class);
            tracker.removeEntity(entityId);
            return PacketUtil.createPacket(0x1D, new TypeHolder[] { set(Type.BYTE_INT_ARRAY, new int[] { entityId }) });
        }
    });
    // mob spawn
    addTranslator(0x18, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            int entityId = data.read(Type.INT, 0);
            EntityType entityType = EntityType.fromEntityTypeId(data.read(Type.BYTE, 1));
            double x = data.read(Type.INT, 2) / 32.0D;
            double y = data.read(Type.INT, 3) / 32.0D;
            double z = data.read(Type.INT, 4) / 32.0D;
            Location location = new Location(x, y, z);
            Entity entity = new Entity(entityId, location, entityType);
            EntityTracker tracker = session.getStorage().get(EntityTracker.class);
            tracker.addEntity(entityId, entity);
            return PacketUtil.createPacket(0x18, new TypeHolder[] { data.read(0), data.read(1), data.read(2), data.read(3), data.read(4), data.read(5), data.read(6), data.read(7), set(Type.SHORT, (short) 0), set(Type.SHORT, (short) 0), set(Type.SHORT, (short) 0), data.read(8) });
        }
    });
    // entity status
    addTranslator(0x26, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            int entityId = data.read(Type.INT, 0);
            byte status = data.read(Type.BYTE, 1);
            if (status == 2) {
                // hurt
                WorldEntityEvent.onDamage(session, entityId);
            }
            if (status == 3) {
                // death
                WorldEntityEvent.onDeath(session, entityId);
            }
            return data;
        }
    });
    // entity teleport
    addTranslator(0x22, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            int entityId = data.read(Type.INT, 0);
            int x = data.read(Type.INT, 1);
            int y = data.read(Type.INT, 2);
            int z = data.read(Type.INT, 3);
            updateEntityLocation(session, entityId, x, y, z, false);
            return data;
        }
    });
    // entity relative move
    addTranslator(0x1F, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            int entityId = data.read(Type.INT, 0);
            int x = data.read(Type.BYTE, 1);
            int y = data.read(Type.BYTE, 2);
            int z = data.read(Type.BYTE, 3);
            updateEntityLocation(session, entityId, x, y, z, true);
            return data;
        }
    });
    // entity relative look move
    addTranslator(0x21, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            int entityId = data.read(Type.INT, 0);
            int x = data.read(Type.BYTE, 1);
            int y = data.read(Type.BYTE, 2);
            int z = data.read(Type.BYTE, 3);
            updateEntityLocation(session, entityId, x, y, z, true);
            return data;
        }
    });
    // vehicle spawn
    addTranslator(0x17, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            Motion motion = data.read(Type.MOTION, 5);
            int throwerId = motion.getThrowerId();
            byte type = data.read(Type.BYTE, 1);
            // sound emulation, entity mount fixes
            EntityTracker tracker = session.getStorage().get(EntityTracker.class);
            int entityId = data.read(Type.INT, 0);
            double x = data.read(Type.INT, 2) / 32.0D;
            double y = data.read(Type.INT, 3) / 32.0D;
            double z = data.read(Type.INT, 4) / 32.0D;
            float pitch;
            Location loc = new Location(x, y, z);
            switch(type) {
                case 1:
                    // cache boats
                    Entity boat = new Entity(entityId, loc, EntityType.BOAT);
                    tracker.addEntity(entityId, boat);
                    break;
                case 10:
                case 11:
                case 12:
                    // cache minecarts
                    Entity minecart = new Entity(entityId, loc, EntityType.MINECART);
                    tracker.addEntity(entityId, minecart);
                    break;
                case 50:
                    // cache primed tnt entity
                    Entity primedTNT = new Entity(entityId, loc, EntityType.PRIMED_TNT);
                    tracker.addEntity(entityId, primedTNT);
                    WorldEntityEvent.onCustomAction(session, entityId);
                    break;
                case 60:
                    // bow sound
                    pitch = 1.0F / (session.getMain().getSharedRandom().nextFloat() * 0.4F + 1.2F) + 0.5F;
                    WorldEntityEvent.playSoundAt(session, loc, WorldSound.RANDOM_BOW, 0.2F, pitch);
                    break;
                // snowball
                case 61:
                // egg
                case 62:
                // fishing rod
                case 90:
                // ender pearl
                case 65:
                // ender eye
                case 72:
                // throwable potion
                case 73:
                case // exp bottle
                75:
                    pitch = 0.4F / (session.getMain().getSharedRandom().nextFloat() * 0.4F + 0.8F);
                    WorldEntityEvent.playSoundAt(session, loc, WorldSound.RANDOM_BOW, 0.5F, pitch);
                    if (type == 90) {
                        Location hookLocation = new Location(x, y, z);
                        AbstractEntity nearest = getNearestEntity(tracker, hookLocation, 2.0D);
                        throwerId = nearest.getEntityId() != -1 ? nearest.getEntityId() : session.getUserData().getEntityId();
                    }
                    break;
            }
            switch(type) {
                case 70:
                    throwerId = 12;
                    break;
                case 71:
                    throwerId = 13;
                    type = 70;
                    break;
                case 74:
                    throwerId = 122;
                    type = 70;
                    break;
            }
            motion.setThrowerId(throwerId);
            return PacketUtil.createPacket(0x17, new TypeHolder[] { data.read(0), set(Type.BYTE, type), data.read(2), data.read(3), data.read(4), set(Type.MOTION, motion) });
        }
    });
    // block place
    addTranslator(0x0F, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            ItemStack newItem = data.read(Type.V1_3R_ITEM, 4);
            return PacketUtil.createPacket(0x0F, new TypeHolder[] { data.read(0), data.read(1), data.read(2), data.read(3), set(Type.V1_0R_ITEM, newItem) });
        }
    });
    // creative set slot
    addTranslator(0x6B, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            ItemStack newItem = data.read(Type.V1_3R_ITEM, 1);
            boolean notNull = newItem != null;
            if (notNull && !creativeTab.exists(newItem.getItemId())) {
                // replace all unknown items to stone
                newItem.setItemId(1);
                newItem.setData(0);
            }
            return PacketUtil.createPacket(0x6B, new TypeHolder[] { data.read(0), set(Type.V1_0R_ITEM, newItem) });
        }
    });
    // named entity spawn
    addTranslator(0x14, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            EntityTracker tracker = session.getStorage().get(EntityTracker.class);
            int entityId = data.read(Type.INT, 0);
            double x = data.read(Type.INT, 2) / 32.0D;
            double y = data.read(Type.INT, 3) / 32.0D;
            double z = data.read(Type.INT, 4) / 32.0D;
            Location loc = new Location(x, y, z);
            HumanEntity human = new HumanEntity(entityId, loc);
            tracker.addEntity(entityId, human);
            /* default 1.3.x metadata */
            List<WatchableObject> watchableObjects = Arrays.asList(new WatchableObject(MetadataType.BYTE, 0, 0), new WatchableObject(MetadataType.BYTE, 16, 0), new WatchableObject(MetadataType.SHORT, 1, 300), new WatchableObject(MetadataType.BYTE, 17, 0), new WatchableObject(MetadataType.INT, 8, 0));
            return PacketUtil.createPacket(0x14, new TypeHolder[] { // entityId
            data.read(0), // playerName
            data.read(1), // x
            data.read(2), // y
            data.read(3), // z
            data.read(4), // yaw
            data.read(5), // pitch
            data.read(6), // item
            data.read(7), // default metadata
            set(Type.V1_3B_METADATA, watchableObjects.toArray(new WatchableObject[0])) });
        }
    });
    // entity metadata
    addTranslator(0x28, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            int entityId = data.read(Type.INT, 0);
            WatchableObject[] watchableObjects = data.read(Type.V1_3B_METADATA, 1);
            EntityTracker tracker = session.getStorage().get(EntityTracker.class);
            for (WatchableObject watchableObject : watchableObjects) {
                int index = watchableObject.getIndex();
                MetadataType type = watchableObject.getType();
                Object value = watchableObject.getValue();
                if (type == MetadataType.BYTE && index == 0 && tracker.isEntityTracked(entityId)) {
                    if (((Byte) value).intValue() == 4) {
                        // entity mount
                        if (tracker.getEntity(entityId).getEntityType() == EntityType.HUMAN) {
                            HumanEntity humanEntity = (HumanEntity) tracker.getEntity(entityId);
                            AbstractEntity nearbyEntity = getNearestEntity(tracker, humanEntity.getLocation(), 1.5D);
                            EntityType eType = nearbyEntity.getEntityType();
                            if (nearbyEntity.getEntityId() != -1) {
                                if (eType == EntityType.MINECART || eType == EntityType.PIG || eType == EntityType.BOAT) {
                                    PacketData entityAttach = PacketUtil.createPacket(0x27, new TypeHolder[] { set(Type.INT, entityId), set(Type.INT, nearbyEntity.getEntityId()) });
                                    humanEntity.setRidingEntity(true);
                                    session.sendPacket(entityAttach, PacketDirection.TO_CLIENT, getFrom());
                                }
                            }
                        }
                    } else if (((Byte) value).intValue() == 0) {
                        // un-mount
                        if (tracker.isEntityTracked(entityId) && tracker.getEntity(entityId) instanceof HumanEntity) {
                            HumanEntity humanEntity = (HumanEntity) tracker.getEntity(entityId);
                            if (humanEntity.isRidingEntity()) {
                                PacketData entityAttach = PacketUtil.createPacket(0x27, new TypeHolder[] { set(Type.INT, entityId), set(Type.INT, -1) });
                                humanEntity.setRidingEntity(false);
                                session.sendPacket(entityAttach, PacketDirection.TO_CLIENT, getFrom());
                            }
                        }
                    }
                }
            }
            return data;
        }
    });
    // explosion
    addTranslator(0x3C, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            double x = data.read(Type.DOUBLE, 0);
            double y = data.read(Type.DOUBLE, 1);
            double z = data.read(Type.DOUBLE, 2);
            Location loc = new Location(x, y, z);
            WorldEntityEvent.playSoundAt(session, loc, WorldSound.RANDOM_EXPLODE);
            return PacketUtil.createPacket(0x3C, new TypeHolder[] { data.read(0), data.read(1), data.read(2), data.read(3), data.read(4), set(Type.FLOAT, 0.0F), set(Type.FLOAT, 0.0F), set(Type.FLOAT, 0.0F) });
        }
    });
    // item collect
    addTranslator(0x16, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            EntityTracker tracker = session.getStorage().get(EntityTracker.class);
            int entityId = data.read(Type.INT, 0);
            AbstractEntity itemPickup = tracker.getEntity(entityId);
            if (itemPickup != null) {
                Random shared = session.getMain().getSharedRandom();
                float pitch = ((shared.nextFloat() - shared.nextFloat()) * 0.7F + 1.0F) * 2.0F;
                WorldEntityEvent.playSoundAt(session, itemPickup.getLocation(), WorldSound.RANDOM_POP, 0.2F, pitch);
            }
            return data;
        }
    });
    // pickup spawn
    addTranslator(0x15, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            EntityTracker tracker = session.getStorage().get(EntityTracker.class);
            int entityId = data.read(Type.INT, 0);
            double x = data.read(Type.INT, 4) / 32.0D;
            double y = data.read(Type.INT, 5) / 32.0D;
            double z = data.read(Type.INT, 6) / 32.0D;
            Location b = new Location(x, y, z);
            Entity itemPickup = new Entity(entityId, b, EntityType.ITEM);
            tracker.addEntity(entityId, itemPickup);
            return data;
        }
    });
    // player abilities
    addTranslator(0xCA, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            byte mask = data.read(Type.BYTE, 0);
            return PacketUtil.createPacket(0xCA, new TypeHolder[] { set(Type.BOOLEAN, ((mask & 1) > 0)), set(Type.BOOLEAN, ((mask & 2) > 0)), set(Type.BOOLEAN, ((mask & 4) > 0)), set(Type.BOOLEAN, ((mask & 8) > 0)) });
        }
    });
    // player abilities
    addTranslator(0xCA, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            boolean invulnerable = data.read(Type.BOOLEAN, 0);
            boolean flying = data.read(Type.BOOLEAN, 1);
            boolean allowFlying = data.read(Type.BOOLEAN, 2);
            boolean instantBreak = data.read(Type.BOOLEAN, 3);
            byte mask = 0;
            if (invulnerable) {
                mask = (byte) (mask | 1);
            }
            if (flying) {
                mask = (byte) (mask | 2);
            }
            if (allowFlying) {
                mask = (byte) (mask | 4);
            }
            if (instantBreak) {
                mask = (byte) (mask | 8);
            }
            return PacketUtil.createPacket(0xCA, new TypeHolder[] { set(Type.BYTE, mask), set(Type.BYTE, (byte) (0.05f * 255)), set(Type.BYTE, (byte) (0.1f * 255)) });
        }
    });
    // player look move
    addTranslator(0x0D, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            double x = data.read(Type.DOUBLE, 0);
            double y = data.read(Type.DOUBLE, 1);
            double z = data.read(Type.DOUBLE, 3);
            EntityTracker tracker = session.getStorage().get(EntityTracker.class);
            AbstractEntity e = tracker.getEntity(-999);
            if (e != null) {
                e.setLocation(new Location(x, y, z));
            }
            return data;
        }
    });
    // update tile entity
    addTranslator(0x84, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            int x = data.read(Type.INT, 0);
            short y = data.read(Type.SHORT, 1);
            int z = data.read(Type.INT, 2);
            int entityTypeId = data.read(Type.INT, 4);
            CompoundBinaryTag.Builder builder = CompoundBinaryTag.builder();
            builder.putString("EntityId", EntityIdToTypeString.getEntityTypeString(entityTypeId));
            builder.putShort("Delay", (short) 20);
            builder.putInt("x", x);
            builder.putInt("y", y);
            builder.putInt("z", z);
            CompoundBinaryTag tileTag = builder.build();
            return PacketUtil.createPacket(0x84, new TypeHolder[] { data.read(0), data.read(1), data.read(2), data.read(3), set(Type.COMPOUND_TAG, tileTag) });
        }
    });
    // entity equipment
    addTranslator(0x05, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            short itemId = data.read(Type.SHORT, 2);
            short itemData = data.read(Type.SHORT, 3);
            ItemStack itemStack;
            if (itemId == -1) {
                itemStack = null;
            } else {
                itemStack = new ItemStack(itemId, 0, itemData, null);
            }
            return PacketUtil.createPacket(0x05, new TypeHolder[] { data.read(0), data.read(1), set(Type.V1_3R_ITEM, itemStack) });
        }
    });
}
Also used : AbstractEntity(com.github.dirtpowered.dirtmv.network.versions.Release39To29.entity.model.AbstractEntity) Entity(com.github.dirtpowered.dirtmv.network.versions.Release39To29.entity.Entity) HumanEntity(com.github.dirtpowered.dirtmv.network.versions.Release39To29.entity.HumanEntity) Motion(com.github.dirtpowered.dirtmv.data.protocol.objects.Motion) ServerSession(com.github.dirtpowered.dirtmv.network.server.ServerSession) PacketTranslator(com.github.dirtpowered.dirtmv.data.translator.PacketTranslator) EntityTracker(com.github.dirtpowered.dirtmv.network.versions.Release39To29.entity.EntityTracker) MetadataType(com.github.dirtpowered.dirtmv.data.protocol.objects.MetadataType) SneakyThrows(lombok.SneakyThrows) EntityIdToTypeString(com.github.dirtpowered.dirtmv.network.versions.Release39To29.entity.EntityIdToTypeString) TypeHolder(com.github.dirtpowered.dirtmv.data.protocol.TypeHolder) EntityType(com.github.dirtpowered.dirtmv.data.entity.EntityType) SecretKey(javax.crypto.SecretKey) Random(java.util.Random) AbstractEntity(com.github.dirtpowered.dirtmv.network.versions.Release39To29.entity.model.AbstractEntity) HumanEntity(com.github.dirtpowered.dirtmv.network.versions.Release39To29.entity.HumanEntity) List(java.util.List) WatchableObject(com.github.dirtpowered.dirtmv.data.protocol.objects.WatchableObject) ItemStack(com.github.dirtpowered.dirtmv.data.protocol.objects.ItemStack) WatchableObject(com.github.dirtpowered.dirtmv.data.protocol.objects.WatchableObject) CompoundBinaryTag(net.kyori.adventure.nbt.CompoundBinaryTag) PacketData(com.github.dirtpowered.dirtmv.data.protocol.PacketData) Location(com.github.dirtpowered.dirtmv.data.protocol.objects.Location)

Example 3 with PacketTranslator

use of com.github.dirtpowered.dirtmv.data.translator.PacketTranslator in project DirtMultiversion by DirtPowered.

the class WorldPackets method registerTranslators.

@Override
public void registerTranslators() {
    // block change
    addTranslator(0x35, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            BlockStorage blockStorage = session.getStorage().get(BlockStorage.class);
            if (blockStorage.getVersion() == getTo()) {
                int x = data.read(Type.INT, 0);
                byte y = data.read(Type.BYTE, 1);
                int z = data.read(Type.INT, 2);
                int chunkX = x >> 4;
                int chunkZ = z >> 4;
                byte blockId = data.read(Type.BYTE, 3);
                if (shouldCache(blockId)) {
                    blockStorage.setBlockAt(chunkX, chunkZ, x, y, z, blockId);
                }
            }
            return PacketUtil.createPacket(0x35, new TypeHolder[] { data.read(0), data.read(1), data.read(2), set(Type.SHORT, data.read(Type.BYTE, 3).shortValue()), data.read(4) });
        }
    });
    // multi block change
    addTranslator(0x34, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            BlockStorage blockStorage = session.getStorage().get(BlockStorage.class);
            if (blockStorage.getVersion() == getTo()) {
                int chunkX = data.read(Type.INT, 0);
                int chunkZ = data.read(Type.INT, 1);
                V1_2MultiBlockArray blocks = data.read(Type.V1_2MULTIBLOCK_ARRAY, 2);
                DataInput dis = new DataInputStream(new ByteArrayInputStream(blocks.getData()));
                for (int i = 0; i < blocks.getRecordCount(); i++) {
                    try {
                        short pos = dis.readShort();
                        int x = pos >> 12 & 15;
                        int y = pos & 255;
                        int z = pos >> 8 & 15;
                        int xPos = x + (chunkX << 4);
                        int zPos = z + (chunkZ << 4);
                        int blockId = dis.readShort() >> 4 & 4095;
                        if (shouldCache(blockId)) {
                            blockStorage.setBlockAt(chunkX, chunkZ, xPos, y, zPos, blockId);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return data;
        }
    });
    // chunk data
    addTranslator(0x33, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            BlockStorage blockStorage = session.getStorage().get(BlockStorage.class);
            V1_2Chunk chunk = data.read(Type.V1_2_CHUNK, 0);
            if (blockStorage.getVersion() == getTo()) {
                List<ExtendedBlockStorage> parts = new ArrayList<>();
                V1_2RChunkStorage storage;
                if (chunk.getStorage() != null) {
                    storage = chunk.getStorage();
                } else {
                    // create chunk storage
                    storage = new V1_2RChunkStorage(true, true, chunk.getChunkX(), chunk.getChunkZ());
                    storage.readChunk(chunk.isGroundUp(), chunk.getPrimaryBitmap(), chunk.getUncompressedData());
                    // cache for later use (r1.8 -> r1.7)
                    chunk.setStorage(storage);
                }
                ExtendedBlockStorage[] columns = storage.getColumnStorage();
                for (int i = 0; i < columns.length; ++i) {
                    ExtendedBlockStorage e = columns[i];
                    boolean f = e != null && !columns[i].isEmpty();
                    if (e != null && (chunk.getPrimaryBitmap() & 1 << i) != 0 && (!chunk.isGroundUp() || f)) {
                        parts.add(e);
                    }
                }
                for (int i = 0; i < parts.size(); ++i) {
                    byte[] blockArray = parts.get(i).getBlockLSBArray();
                    for (int j = 0; j < blockArray.length; ++j) {
                        int x = j & 15;
                        int y = (j >> 8) + i * 16 & 255;
                        int z = j >> 4 & 15;
                        int blockId = blockArray[j] & 255;
                        if (shouldCache(blockId)) {
                            blockStorage.setBlockAt(chunk.getChunkX(), chunk.getChunkZ(), x, y, z, blockId);
                        }
                    }
                }
            }
            return PacketUtil.createPacket(0x33, new TypeHolder[] { set(Type.V1_3_CHUNK, chunk) });
        }
    });
    // pre chunk
    addTranslator(0x32, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            ProtocolStorage storage = session.getStorage();
            byte mode = data.read(Type.BYTE, 2);
            if (mode == 1) {
                return cancel();
            }
            int chunkX = data.read(Type.INT, 0);
            int chunkZ = data.read(Type.INT, 1);
            OpenChestTracker chestTracker = storage.get(OpenChestTracker.class);
            for (BlockLocation entry : chestTracker.getChestStates().keySet()) {
                int x = entry.getX() >> 4;
                int z = entry.getZ() >> 4;
                if (chunkX == x && chunkZ == z) {
                    chestTracker.remove(entry);
                }
            }
            BlockStorage blockStorage = storage.get(BlockStorage.class);
            if (blockStorage.getVersion() == getTo()) {
                blockStorage.removeChunk(chunkX, chunkZ);
            }
            V1_2Chunk chunk = new V1_2Chunk(chunkX, chunkZ, true, (short) 0, (short) 0, 0, new byte[0], new byte[0], null);
            return PacketUtil.createPacket(0x33, new TypeHolder[] { set(Type.V1_3_CHUNK, chunk) });
        }
    });
    // play noteblock
    addTranslator(0x36, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            int x = data.read(Type.INT, 0);
            int y = data.read(Type.SHORT, 1);
            int z = data.read(Type.INT, 2);
            byte type = data.read(Type.BYTE, 3);
            byte pitch = data.read(Type.BYTE, 4);
            ProtocolStorage storage = session.getStorage();
            short blockId = (short) storage.get(BlockStorage.class).getBlockAt(x, y, z);
            WorldSound worldSound;
            OpenChestTracker chestTracker = storage.get(OpenChestTracker.class);
            switch(type) {
                case 0:
                    if (blockId == 33 || blockId == 29) {
                        worldSound = WorldSound.PISTON_OUT;
                    } else {
                        worldSound = WorldSound.NOTE_HARP;
                    }
                    break;
                case 1:
                    if (blockId == 54) {
                        BlockLocation loc = new BlockLocation(x, y, z);
                        if (pitch == 1) {
                            if (!chestTracker.getState(loc)) {
                                chestTracker.setOpen(loc, true);
                                worldSound = WorldSound.CHEST_OPEN;
                                pitch = 12;
                            } else {
                                worldSound = WorldSound.NO_SOUND;
                            }
                        } else {
                            if (chestTracker.getState(loc)) {
                                worldSound = WorldSound.CHEST_CLOSE;
                                chestTracker.setOpen(loc, false);
                            } else {
                                worldSound = WorldSound.NO_SOUND;
                            }
                        }
                    } else if (blockId == 33 || blockId == 29) {
                        worldSound = WorldSound.PISTON_IN;
                    } else {
                        worldSound = WorldSound.NOTE_CLICK;
                    }
                    break;
                case 2:
                    worldSound = WorldSound.NOTE_SNARE;
                    break;
                case 3:
                    worldSound = WorldSound.NOTE_HAT;
                    break;
                case 4:
                    worldSound = WorldSound.NOTE_BASS_ATTACK;
                    break;
                default:
                    worldSound = WorldSound.NOTE_HARP;
                    break;
            }
            float correctedPitch = (float) (0.5f * (Math.pow(2, pitch / 12.0f)));
            WorldEntityEvent.playSoundAt(session, new Location(x, y, z), worldSound, 3.0f, correctedPitch);
            return PacketUtil.createPacket(0x36, new TypeHolder[] { data.read(0), data.read(1), data.read(2), data.read(3), data.read(4), set(Type.SHORT, blockId) });
        }
    });
}
Also used : ServerSession(com.github.dirtpowered.dirtmv.network.server.ServerSession) ProtocolStorage(com.github.dirtpowered.dirtmv.data.user.ProtocolStorage) PacketTranslator(com.github.dirtpowered.dirtmv.data.translator.PacketTranslator) V1_2Chunk(com.github.dirtpowered.dirtmv.data.protocol.objects.V1_2Chunk) V1_2RChunkStorage(com.github.dirtpowered.dirtmv.data.chunk.storage.V1_2RChunkStorage) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) ExtendedBlockStorage(com.github.dirtpowered.dirtmv.data.chunk.storage.ExtendedBlockStorage) BlockLocation(com.github.dirtpowered.dirtmv.data.protocol.objects.BlockLocation) OpenChestTracker(com.github.dirtpowered.dirtmv.network.versions.Release39To29.sound.OpenChestTracker) WorldSound(com.github.dirtpowered.dirtmv.network.versions.Release39To29.sound.WorldSound) V1_2MultiBlockArray(com.github.dirtpowered.dirtmv.data.protocol.objects.V1_2MultiBlockArray) TypeHolder(com.github.dirtpowered.dirtmv.data.protocol.TypeHolder) DataInput(java.io.DataInput) ExtendedBlockStorage(com.github.dirtpowered.dirtmv.data.chunk.storage.ExtendedBlockStorage) BlockStorage(com.github.dirtpowered.dirtmv.network.versions.Beta17To14.storage.BlockStorage) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) List(java.util.List) PacketData(com.github.dirtpowered.dirtmv.data.protocol.PacketData) BlockLocation(com.github.dirtpowered.dirtmv.data.protocol.objects.BlockLocation) Location(com.github.dirtpowered.dirtmv.data.protocol.objects.Location)

Example 4 with PacketTranslator

use of com.github.dirtpowered.dirtmv.data.translator.PacketTranslator in project DirtMultiversion by DirtPowered.

the class ProtocolBeta11To10 method registerTranslators.

@Override
public void registerTranslators() {
    // handshake
    addTranslator(0x02, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x02, new TypeHolder[] { set(Type.UTF8_STRING, data.read(Type.STRING, 0)) });
        }
    });
    // handshake
    addTranslator(0x02, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x02, new TypeHolder[] { set(Type.STRING, data.read(Type.UTF8_STRING, 0)) });
        }
    });
    // login
    addTranslator(0x01, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x01, new TypeHolder[] { set(Type.INT, 10), set(Type.UTF8_STRING, data.read(Type.STRING, 1)), set(Type.UTF8_STRING, "-"), data.read(2), data.read(3) });
        }
    });
    // login
    addTranslator(0x01, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x01, new TypeHolder[] { data.read(0), set(Type.STRING, data.read(Type.UTF8_STRING, 1)), data.read(3), data.read(4) });
        }
    });
    // chat
    addTranslator(0x03, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x03, new TypeHolder[] { set(Type.STRING, data.read(Type.UTF8_STRING, 0)) });
        }
    });
    // chat
    addTranslator(0x03, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x03, new TypeHolder[] { set(Type.UTF8_STRING, data.read(Type.STRING, 0)) });
        }
    });
    // kick disconnect
    addTranslator(0xFF, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0xFF, new TypeHolder[] { set(Type.STRING, data.read(Type.UTF8_STRING, 0)) });
        }
    });
    // named entity spawn
    addTranslator(0x14, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x14, new TypeHolder[] { data.read(0), set(Type.STRING, data.read(Type.UTF8_STRING, 1)), data.read(2), data.read(3), data.read(4), data.read(5), data.read(6), data.read(7) });
        }
    });
    // spawn painting
    addTranslator(0x19, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x19, new TypeHolder[] { data.read(0), set(Type.STRING, data.read(Type.UTF8_STRING, 1)), data.read(2), data.read(3), data.read(4), data.read(5) });
        }
    });
    // update sign
    addTranslator(0x82, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x82, new TypeHolder[] { data.read(0), data.read(1), data.read(2), set(Type.STRING, data.read(Type.UTF8_STRING, 3)), set(Type.STRING, data.read(Type.UTF8_STRING, 4)), set(Type.STRING, data.read(Type.UTF8_STRING, 5)), set(Type.STRING, data.read(Type.UTF8_STRING, 6)) });
        }
    });
    // window click
    addTranslator(0x66, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            boolean shiftClick = (data.read(Type.BYTE, 4) == 1);
            // cancel shift-clicking
            ItemStack itemStack;
            if (!shiftClick) {
                itemStack = data.read(Type.V1_3B_ITEM, 5);
            } else {
                /*
                     * sending invalid/other item than held item is forcing server to send window update packet
                     * It should prevent against all inventory desyncs
                     */
                itemStack = new ItemStack(99, 0, 1, null);
            }
            return PacketUtil.createPacket(0x66, new TypeHolder[] { data.read(0), data.read(1), data.read(2), data.read(3), set(Type.V1_3B_ITEM, itemStack) });
        }
    });
}
Also used : TypeHolder(com.github.dirtpowered.dirtmv.data.protocol.TypeHolder) ServerSession(com.github.dirtpowered.dirtmv.network.server.ServerSession) 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)

Example 5 with PacketTranslator

use of com.github.dirtpowered.dirtmv.data.translator.PacketTranslator in project DirtMultiversion by DirtPowered.

the class ProtocolBeta14To13 method registerTranslators.

@Override
public void registerTranslators() {
    // login
    addTranslator(0x01, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x01, new TypeHolder[] { set(Type.INT, 13), data.read(1), data.read(2), data.read(3) });
        }
    });
    // login
    addTranslator(0x01, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x01, new TypeHolder[] { data.read(0), data.read(1), data.read(2), data.read(3) });
        }
    });
}
Also used : TypeHolder(com.github.dirtpowered.dirtmv.data.protocol.TypeHolder) ServerSession(com.github.dirtpowered.dirtmv.network.server.ServerSession) PacketTranslator(com.github.dirtpowered.dirtmv.data.translator.PacketTranslator) PacketData(com.github.dirtpowered.dirtmv.data.protocol.PacketData)

Aggregations

PacketData (com.github.dirtpowered.dirtmv.data.protocol.PacketData)26 PacketTranslator (com.github.dirtpowered.dirtmv.data.translator.PacketTranslator)26 ServerSession (com.github.dirtpowered.dirtmv.network.server.ServerSession)25 TypeHolder (com.github.dirtpowered.dirtmv.data.protocol.TypeHolder)23 ItemStack (com.github.dirtpowered.dirtmv.data.protocol.objects.ItemStack)14 ProtocolStorage (com.github.dirtpowered.dirtmv.data.user.ProtocolStorage)8 List (java.util.List)8 MinecraftVersion (com.github.dirtpowered.dirtmv.data.MinecraftVersion)5 Motion (com.github.dirtpowered.dirtmv.data.protocol.objects.Motion)5 IOException (java.io.IOException)5 SneakyThrows (lombok.SneakyThrows)5 EntityType (com.github.dirtpowered.dirtmv.data.entity.EntityType)4 V1_2MultiBlockArray (com.github.dirtpowered.dirtmv.data.protocol.objects.V1_2MultiBlockArray)4 WatchableObject (com.github.dirtpowered.dirtmv.data.protocol.objects.WatchableObject)4 BlockStorage (com.github.dirtpowered.dirtmv.network.versions.Beta17To14.storage.BlockStorage)4 ArrayList (java.util.ArrayList)4 DirtMultiVersion (com.github.dirtpowered.dirtmv.DirtMultiVersion)3 BlockLocation (com.github.dirtpowered.dirtmv.data.protocol.objects.BlockLocation)3 Location (com.github.dirtpowered.dirtmv.data.protocol.objects.Location)3 V1_2Chunk (com.github.dirtpowered.dirtmv.data.protocol.objects.V1_2Chunk)3