Search in sources :

Example 1 with Motion

use of com.github.dirtpowered.dirtmv.data.protocol.objects.Motion in project DirtMultiversion by DirtPowered.

the class VehicleMotionDataType method write.

@Override
public void write(TypeHolder typeHolder, PacketOutput packetOutput) {
    Motion motion = (Motion) typeHolder.getObject();
    packetOutput.writeInt(motion.getThrowerId());
    if (motion.getThrowerId() > 0) {
        packetOutput.writeShort(motion.getMotionX());
        packetOutput.writeShort(motion.getMotionY());
        packetOutput.writeShort(motion.getMotionZ());
    }
}
Also used : Motion(com.github.dirtpowered.dirtmv.data.protocol.objects.Motion)

Example 2 with Motion

use of com.github.dirtpowered.dirtmv.data.protocol.objects.Motion 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 Motion

use of com.github.dirtpowered.dirtmv.data.protocol.objects.Motion in project DirtMultiversion by DirtPowered.

the class ProtocolBeta13To11 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, 11), 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) });
        }
    });
    // respawn
    addTranslator(0x09, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x09, new TypeHolder[0]);
        }
    });
    // respawn
    addTranslator(0x09, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x09, new TypeHolder[] { set(Type.BYTE, (byte) 0) });
        }
    });
    // vehicle spawn
    addTranslator(0x17, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x17, new TypeHolder[] { data.read(0), data.read(1), data.read(2), data.read(3), data.read(4), // to indicate that there's no more data to read
            set(Type.MOTION, new Motion(0, (short) 0, (short) 0, (short) 0)) });
        }
    });
}
Also used : TypeHolder(com.github.dirtpowered.dirtmv.data.protocol.TypeHolder) 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) PacketData(com.github.dirtpowered.dirtmv.data.protocol.PacketData)

Example 4 with Motion

use of com.github.dirtpowered.dirtmv.data.protocol.objects.Motion in project DirtMultiversion by DirtPowered.

the class ProtocolRelease51To39 method registerTranslators.

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

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            ProtocolStorage storage = session.getStorage();
            if (!storage.hasObject(DimensionTracker.class)) {
                storage.set(DimensionTracker.class, new DimensionTracker());
            }
            DimensionTracker dimensionTracker = session.getStorage().get(DimensionTracker.class);
            dimensionTracker.setDimension(data.read(Type.BYTE, 3));
            return data;
        }
    });
    // respawn
    addTranslator(0x09, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            DimensionTracker dimensionTracker = session.getStorage().get(DimensionTracker.class);
            dimensionTracker.setDimension(data.read(Type.INT, 0));
            return data;
        }
    });
    // server ping request
    addTranslator(0xFE, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            // empty
            return PacketUtil.createPacket(0xFE, new TypeHolder[0]);
        }
    });
    // kick disconnect
    addTranslator(0xFF, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            String reason = data.read(Type.STRING, 0);
            // TODO: fix issues with detecting protocol state on post-netty versions
            if (reason.split("\u00a7").length != 3) {
                return data;
            }
            Configuration configuration = session.getMain().getConfiguration();
            // old to new format
            return PacketUtil.createPacket(0xFF, new TypeHolder[] { set(Type.STRING, transformMotd(reason, configuration)) });
        }
    });
    // handshake
    addTranslator(0x02, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x02, new TypeHolder[] { set(Type.BYTE, (byte) 39), data.read(1), data.read(2), data.read(3) });
        }
    });
    // client settings
    addTranslator(0xCC, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0xCC, new TypeHolder[] { data.read(0), data.read(1), data.read(2), data.read(3) });
        }
    });
    // map data
    addTranslator(0x83, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            byte[] mapData = data.read(Type.BYTE_BYTE_ARRAY, 2);
            if (mapData[0] == 1) {
                for (int i = 0; i < (mapData.length - 1) / 3; ++i) {
                    final byte icon = (byte) (mapData[i * 3 + 1] % 16);
                    final byte centerX = mapData[i * 3 + 2];
                    final byte centerZ = mapData[i * 3 + 3];
                    final byte iconRotation = (byte) (mapData[i * 3 + 1] / 16);
                    mapData[i * 3 + 1] = (byte) (icon << 4 | iconRotation & 15);
                    mapData[i * 3 + 2] = centerX;
                    mapData[i * 3 + 3] = centerZ;
                }
            }
            return PacketUtil.createPacket(0x83, new TypeHolder[] { data.read(0), data.read(1), set(Type.UNSIGNED_SHORT_BYTE_ARRAY, mapData) });
        }
    });
    // update time
    addTranslator(0x04, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x04, new TypeHolder[] { data.read(0), data.read(0) });
        }
    });
    // vehicle spawn
    addTranslator(0x17, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x17, new TypeHolder[] { data.read(0), data.read(1), data.read(2), data.read(3), data.read(4), // yaw
            set(Type.BYTE, (byte) 0), // pitch
            set(Type.BYTE, (byte) 0), data.read(5) });
        }
    });
    // item spawn
    addTranslator(0x15, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            PacketData vehicleSpawn = PacketUtil.createPacket(0x17, new TypeHolder[] { data.read(0), set(Type.BYTE, (byte) 2), data.read(4), data.read(5), data.read(6), data.read(7), data.read(8), set(Type.MOTION, new Motion(0, (short) 0, (short) 0, (short) 0)) });
            short itemId = data.read(Type.SHORT, 1);
            byte amount = data.read(Type.BYTE, 2);
            short itemData = data.read(Type.SHORT, 3);
            ItemStack itemStack = new ItemStack(itemId, amount, itemData, CompoundBinaryTag.empty());
            List<WatchableObject> metadata = Collections.singletonList(new WatchableObject(MetadataType.ITEM, 10, itemStack));
            PacketData itemMetadata = PacketUtil.createPacket(0x28, new TypeHolder[] { data.read(0), set(Type.V1_4R_METADATA, metadata.toArray(new WatchableObject[0])) });
            session.sendPacket(vehicleSpawn, PacketDirection.TO_CLIENT, getFrom());
            session.sendPacket(itemMetadata, PacketDirection.TO_CLIENT, getFrom());
            return cancel();
        }
    });
    // mob spawn
    addTranslator(0x18, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            session.sendPacket(data, PacketDirection.TO_CLIENT, getFrom());
            byte type = data.read(Type.BYTE, 1);
            int itemId = 0;
            if (type == 51)
                // bow
                itemId = 261;
            if (type == 57)
                // golden sword
                itemId = 283;
            if (itemId > 0) {
                ItemStack itemStack = new ItemStack(itemId, 1, 0, null);
                PacketData entityEquipment = PacketUtil.createPacket(0x05, new TypeHolder[] { data.read(0), set(Type.SHORT, (short) 0), set(Type.V1_3R_ITEM, itemStack) });
                session.sendPacket(entityEquipment, PacketDirection.TO_CLIENT, getFrom());
            }
            return cancel();
        }
    });
    // custom payload
    addTranslator(0xFA, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @SneakyThrows
        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            String channel = data.read(Type.STRING, 0);
            byte[] payload = data.read(Type.SHORT_BYTE_ARRAY, 1);
            if (channel.equals("MC|TrList")) {
                // TODO: Custom Payload reader
                NettyInputWrapper buf = new NettyInputWrapper(Unpooled.wrappedBuffer(payload));
                NettyOutputWrapper fixedPayload = new NettyOutputWrapper(Unpooled.buffer());
                fixedPayload.writeInt(buf.readInt());
                short size = buf.readUnsignedByte();
                fixedPayload.writeByte(size);
                for (int i = 0; i < size; i++) {
                    V1_3_1RProtocol.ITEM.write(set(Type.V1_3R_ITEM, V1_3_1RProtocol.ITEM.read(buf)), fixedPayload);
                    V1_3_1RProtocol.ITEM.write(set(Type.V1_3R_ITEM, V1_3_1RProtocol.ITEM.read(buf)), fixedPayload);
                    boolean b = buf.readBoolean();
                    fixedPayload.writeBoolean(b);
                    if (b) {
                        V1_3_1RProtocol.ITEM.write(set(Type.V1_3R_ITEM, V1_3_1RProtocol.ITEM.read(buf)), fixedPayload);
                    }
                    fixedPayload.writeBoolean(false);
                }
                return PacketUtil.createPacket(0xFA, new TypeHolder[] { data.read(0), set(Type.SHORT_BYTE_ARRAY, fixedPayload.array()) });
            }
            return data;
        }
    });
    // level sound
    addTranslator(0x3E, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            String soundName = data.read(Type.STRING, 0);
            String newSoundName = soundRemapper.getNewSoundName(soundName);
            if (newSoundName.isEmpty())
                return cancel();
            return PacketUtil.createPacket(0x3E, new TypeHolder[] { set(Type.STRING, newSoundName), data.read(1), data.read(2), data.read(3), data.read(4), data.read(5) });
        }
    });
    // door change
    addTranslator(0x3D, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x3D, new TypeHolder[] { data.read(0), data.read(1), data.read(2), data.read(3), data.read(4), set(Type.BOOLEAN, false) });
        }
    });
    // chunk bulk
    addTranslator(0x38, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            V1_3_4ChunkBulk oldChunk = data.read(Type.V1_3CHUNK_BULK, 0);
            DimensionTracker tracker = session.getStorage().get(DimensionTracker.class);
            oldChunk.setSkylight(tracker.getDimension() == 0);
            return PacketUtil.createPacket(0x38, new TypeHolder[] { set(Type.V1_4CHUNK_BULK, oldChunk) });
        }
    });
    // block place
    addTranslator(0x0F, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            ItemStack item = data.read(Type.V1_3R_ITEM, 4);
            if (item == null)
                return data;
            int itemId = item.getItemId();
            if (itemId >= 298 && itemId <= 317) {
                return PacketUtil.createPacket(0x66, new TypeHolder[] { set(Type.BYTE, (byte) 0), set(Type.SHORT, (short) 0), set(Type.BYTE, (byte) 0), set(Type.SHORT, (short) 0), set(Type.BYTE, (byte) 0), // fake item
                set(Type.V1_3R_ITEM, new ItemStack(256, 0, 0, null)) });
            }
            return data;
        }
    });
    // creative item get
    addTranslator(0x6B, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            ItemStack item = data.read(Type.V1_3R_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(0x6B, new TypeHolder[] { data.read(0), set(Type.V1_3R_ITEM, item) });
        }
    });
    // spawn painting
    addTranslator(0x19, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            int direction = data.read(Type.INT, 5);
            int correctedDirection = 0;
            switch(direction) {
                case 0:
                    correctedDirection = 2;
                    break;
                case 1:
                    correctedDirection = 1;
                    break;
                case 2:
                    break;
                case 3:
                    correctedDirection = 3;
                    break;
            }
            return PacketUtil.createPacket(0x19, new TypeHolder[] { data.read(0), data.read(1), data.read(2), data.read(3), data.read(4), set(Type.INT, correctedDirection) });
        }
    });
    // player look move
    addTranslator(0x0D, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            ProtocolStorage storage = session.getStorage();
            if (!storage.hasObject(BlockStorage.class)) {
                return data;
            }
            double x = data.read(Type.DOUBLE, 0);
            double y = data.read(Type.DOUBLE, 1);
            double originalStance = data.read(Type.DOUBLE, 2);
            double z = data.read(Type.DOUBLE, 3);
            MovementTranslator.updateBoundingBox(session, new Location(x, y, z));
            Location loc = MovementTranslator.correctPosition(session, x, y, z);
            return PacketUtil.createPacket(0x0D, new TypeHolder[] { set(Type.DOUBLE, loc.getX()), set(Type.DOUBLE, loc.getY()), // stance
            set(Type.DOUBLE, loc.getY() + (originalStance - y)), set(Type.DOUBLE, loc.getZ()), data.read(4), data.read(5), data.read(6) });
        }
    });
    // player position
    addTranslator(0x0B, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            ProtocolStorage storage = session.getStorage();
            if (!storage.hasObject(BlockStorage.class)) {
                return data;
            }
            double x = data.read(Type.DOUBLE, 0);
            double y = data.read(Type.DOUBLE, 1);
            double z = data.read(Type.DOUBLE, 3);
            MovementTranslator.updateBoundingBox(session, new Location(x, y, z));
            Location loc = MovementTranslator.correctPosition(session, x, y, z);
            return PacketUtil.createPacket(0x0B, new TypeHolder[] { set(Type.DOUBLE, loc.getX()), set(Type.DOUBLE, loc.getY()), set(Type.DOUBLE, loc.getY() + 1.6200000047683716D), set(Type.DOUBLE, loc.getZ()), data.read(4) });
        }
    });
}
Also used : Motion(com.github.dirtpowered.dirtmv.data.protocol.objects.Motion) ServerSession(com.github.dirtpowered.dirtmv.network.server.ServerSession) NettyOutputWrapper(com.github.dirtpowered.dirtmv.data.protocol.io.NettyOutputWrapper) ProtocolStorage(com.github.dirtpowered.dirtmv.data.user.ProtocolStorage) Configuration(com.github.dirtpowered.dirtmv.api.Configuration) PacketTranslator(com.github.dirtpowered.dirtmv.data.translator.PacketTranslator) DimensionTracker(com.github.dirtpowered.dirtmv.network.versions.Release28To23.chunk.DimensionTracker) SneakyThrows(lombok.SneakyThrows) TypeHolder(com.github.dirtpowered.dirtmv.data.protocol.TypeHolder) V1_3_4ChunkBulk(com.github.dirtpowered.dirtmv.data.protocol.objects.V1_3_4ChunkBulk) List(java.util.List) ItemStack(com.github.dirtpowered.dirtmv.data.protocol.objects.ItemStack) NettyInputWrapper(com.github.dirtpowered.dirtmv.data.protocol.io.NettyInputWrapper) WatchableObject(com.github.dirtpowered.dirtmv.data.protocol.objects.WatchableObject) PacketData(com.github.dirtpowered.dirtmv.data.protocol.PacketData) Location(com.github.dirtpowered.dirtmv.data.protocol.objects.Location)

Example 5 with Motion

use of com.github.dirtpowered.dirtmv.data.protocol.objects.Motion in project DirtMultiversion by DirtPowered.

the class EntityPackets method registerTranslators.

@Override
public void registerTranslators() {
    // spawn mob
    addTranslator(0x0F, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            ProtocolStorage storage = session.getStorage();
            EntityType entityType = EntityType.fromEntityTypeId(data.read(Type.BYTE, 1));
            if (storage.hasObject(V1_7EntityTracker.class)) {
                V1_7EntityTracker tracker = storage.get(V1_7EntityTracker.class);
                int entityId = data.read(Type.VAR_INT, 0);
                tracker.addEntity(entityId, entityType);
            }
            WatchableObject[] oldMeta = data.read(Type.V1_7R_METADATA, 11);
            WatchableObject[] newMeta = metadataTransformer.transformMetadata(entityType, oldMeta);
            return PacketUtil.createPacket(0x0F, 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), data.read(8), data.read(9), data.read(10), set(Type.V1_8R_METADATA, newMeta) });
        }
    });
    // entity metadata
    addTranslator(0x1C, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            ProtocolStorage storage = session.getStorage();
            SpawnableObject entityObject = null;
            int entityId = data.read(Type.INT, 0);
            if (storage.hasObject(EntityTracker.class)) {
                EntityTracker tracker = storage.get(EntityTracker.class);
                entityObject = tracker.getEntityById(entityId);
            } else if (storage.hasObject(V1_7EntityTracker.class)) {
                V1_7EntityTracker tracker = storage.get(V1_7EntityTracker.class);
                entityObject = tracker.getEntityById(entityId);
            }
            if (entityObject == null)
                return cancel();
            WatchableObject[] oldMeta = data.read(Type.V1_7R_METADATA, 1);
            WatchableObject[] watchableObjects = metadataTransformer.transformMetadata(entityObject, oldMeta);
            for (int i = 0; i < watchableObjects.length; i++) {
                WatchableObject watchableObject = watchableObjects[i];
                if (watchableObject.getType() == MetadataType.ITEM) {
                    ItemStack obj = (ItemStack) watchableObject.getValue();
                    if (obj != null) {
                        obj = InventoryPackets.itemRemapper.replaceItem(obj);
                    }
                    watchableObjects[i] = new WatchableObject(MetadataType.ITEM, watchableObject.getIndex(), obj);
                }
            }
            return PacketUtil.createPacket(0x1C, new TypeHolder[] { set(Type.VAR_INT, data.read(Type.INT, 0)), set(Type.V1_8R_METADATA, watchableObjects) });
        }
    });
    // entity velocity
    addTranslator(0x12, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x12, new TypeHolder[] { set(Type.VAR_INT, data.read(Type.INT, 0)), data.read(1), data.read(2), data.read(3) });
        }
    });
    // entity destroy
    addTranslator(0x13, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            ProtocolStorage storage = session.getStorage();
            int[] entities = data.read(Type.BYTE_INT_ARRAY, 0);
            if (storage.hasObject(V1_7EntityTracker.class)) {
                for (int entityId : entities) {
                    V1_7EntityTracker tracker = storage.get(V1_7EntityTracker.class);
                    tracker.removeEntity(entityId);
                }
            }
            return PacketUtil.createPacket(0x13, new TypeHolder[] { set(Type.VAR_INT_ARRAY, entities) });
        }
    });
    // spawn player
    addTranslator(0x0C, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @SneakyThrows
        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            ProtocolStorage storage = session.getStorage();
            UUID uniqueId = UUID.fromString(data.read(Type.V1_7_STRING, 1));
            String username = data.read(Type.V1_7_STRING, 2);
            GameProfile profile = GameProfileFetcher.getProfile(username).get();
            if (storage.hasObject(V1_7EntityTracker.class)) {
                V1_7EntityTracker tracker = storage.get(V1_7EntityTracker.class);
                int entityId = data.read(Type.VAR_INT, 0);
                tracker.addEntity(entityId, EntityType.HUMAN);
            }
            WatchableObject[] oldMeta = data.read(Type.V1_7R_METADATA, 10);
            WatchableObject[] newMeta = metadataTransformer.transformMetadata(EntityType.HUMAN, oldMeta);
            // skin layers
            WatchableObject[] layers = new WatchableObject[newMeta.length + 1];
            System.arraycopy(newMeta, 0, layers, 0, newMeta.length);
            layers[newMeta.length] = new WatchableObject(MetadataType.BYTE, 10, (byte) 127);
            newMeta = layers;
            PacketData playerSpawn = PacketUtil.createPacket(0x0C, new TypeHolder[] { data.read(0), set(Type.UUID, profile.getId()), data.read(4), data.read(5), data.read(6), data.read(7), data.read(8), data.read(9), set(Type.V1_8R_METADATA, newMeta) });
            // create fake profile
            // GameProfile profile = new GameProfile(uniqueId, username);
            TabListEntry tabAddListEntry = new TabListEntry(TabListAction.ADD_PLAYER, new PlayerListEntry[] { new PlayerListEntry(profile, new Property[0], 0, 0, null) });
            PacketData tabEntry = PacketUtil.createPacket(0x38, new TypeHolder[] { set(Type.TAB_LIST_ENTRY, tabAddListEntry) });
            // seems that client overwrites old tab packet after sending a new one
            session.sendPacket(tabEntry, PacketDirection.TO_CLIENT, getFrom());
            // send player spawn (right after tablist packet)
            session.sendPacket(playerSpawn, PacketDirection.TO_CLIENT, getFrom());
            new Timer().schedule(new TimerTask() {

                @Override
                public void run() {
                    TabListEntry tabRemoveListEntry = new TabListEntry(TabListAction.REMOVE_PLAYER, new PlayerListEntry[] { new PlayerListEntry(profile, new Property[0], 0, 0, null) });
                    PacketData removeTab = PacketUtil.createPacket(0x38, new TypeHolder[] { set(Type.TAB_LIST_ENTRY, tabRemoveListEntry) });
                    session.sendPacket(removeTab, PacketDirection.TO_CLIENT, getFrom());
                }
            }, 40L);
            return cancel();
        }
    });
    // entity equipment
    addTranslator(0x04, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            ItemStack originalItem = data.read(Type.V1_3R_ITEM, 2);
            if (originalItem != null) {
                originalItem = InventoryPackets.itemRemapper.replaceItem(originalItem);
            }
            return PacketUtil.createPacket(0x04, new TypeHolder[] { set(Type.VAR_INT, data.read(Type.INT, 0)), data.read(1), set(Type.V1_8R_ITEM, originalItem) });
        }
    });
    // update tile entity
    addTranslator(0x35, ProtocolState.PLAY, 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);
            return PacketUtil.createPacket(0x35, new TypeHolder[] { set(Type.LONG, ProtocolRelease47To5.toBlockPosition(x, y, z)), data.read(3), set(Type.COMPRESSED_COMPOUND_TAG, data.read(Type.COMPOUND_TAG, 4)) });
        }
    });
    // spawn painting
    addTranslator(0x10, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            int x = data.read(Type.INT, 2);
            int y = data.read(Type.INT, 3);
            int z = data.read(Type.INT, 4);
            int direction = data.read(Type.INT, 5);
            switch(direction) {
                case 0:
                    z += 1;
                    break;
                case 1:
                    x -= 1;
                    break;
                case 2:
                    z -= 1;
                    break;
                case 3:
                    x += 1;
                    break;
            }
            return PacketUtil.createPacket(0x10, new TypeHolder[] { data.read(0), data.read(1), set(Type.LONG, ProtocolRelease47To5.toBlockPosition(x, y, z)), set(Type.BYTE, data.read(Type.INT, 5).byteValue()) });
        }
    });
    // entity effect
    addTranslator(0x1D, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x1D, new TypeHolder[] { set(Type.VAR_INT, data.read(Type.INT, 0)), data.read(1), data.read(2), set(Type.VAR_INT, data.read(Type.SHORT, 3).intValue()), set(Type.BOOLEAN, false) });
        }
    });
    // remove entity effect
    addTranslator(0x1E, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x1E, new TypeHolder[] { set(Type.VAR_INT, data.read(Type.INT, 0)), data.read(1) });
        }
    });
    // spawn object
    addTranslator(0x0E, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            ProtocolStorage storage = session.getStorage();
            int x = data.read(Type.INT, 2);
            int y = data.read(Type.INT, 3);
            int z = data.read(Type.INT, 4);
            byte type = data.read(Type.BYTE, 1);
            byte yaw = data.read(Type.BYTE, 6);
            Motion motionData = data.read(Type.MOTION, 7);
            ObjectType objectType = ObjectType.fromObjectTypeId(type);
            switch(objectType) {
                case ITEM:
                    y -= 4;
                    break;
                case TNT_PRIMED:
                    y -= 16;
                    break;
                case FALLING_OBJECT:
                    int itemId = motionData.getThrowerId();
                    int itemData = motionData.getThrowerId() >> 16;
                    short mX = motionData.getMotionX();
                    short mY = motionData.getMotionY();
                    short mZ = motionData.getMotionZ();
                    y -= 16;
                    motionData = new Motion(itemId | itemData << 12, mX, mY, mZ);
                    break;
                case ITEM_FRAME:
                    int rotation = motionData.getThrowerId();
                    if (rotation == 0) {
                        z += 32;
                        yaw = 0;
                    } else if (rotation == 1) {
                        x -= 32;
                        yaw = 64;
                    } else if (rotation == 2) {
                        z -= 32;
                        yaw = -128;
                    } else if (rotation == 3) {
                        x += 32;
                        yaw = -64;
                    }
                    break;
            }
            if (storage.hasObject(V1_7EntityTracker.class)) {
                V1_7EntityTracker tracker = storage.get(V1_7EntityTracker.class);
                int entityId = data.read(Type.VAR_INT, 0);
                tracker.addEntity(entityId, objectType);
            }
            return PacketUtil.createPacket(0x0E, new TypeHolder[] { data.read(0), set(Type.BYTE, type), set(Type.INT, x), set(Type.INT, y), set(Type.INT, z), data.read(5), set(Type.BYTE, yaw), set(Type.MOTION, motionData) });
        }
    });
}
Also used : Motion(com.github.dirtpowered.dirtmv.data.protocol.objects.Motion) 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_7EntityTracker(com.github.dirtpowered.dirtmv.network.versions.Release47To5.entity.V1_7EntityTracker) EntityTracker(com.github.dirtpowered.dirtmv.network.versions.Release73To61.entity.EntityTracker) TabListEntry(com.github.dirtpowered.dirtmv.data.protocol.objects.tablist.TabListEntry) SneakyThrows(lombok.SneakyThrows) PlayerListEntry(com.github.dirtpowered.dirtmv.data.protocol.objects.tablist.PlayerListEntry) EntityType(com.github.dirtpowered.dirtmv.data.entity.EntityType) TypeHolder(com.github.dirtpowered.dirtmv.data.protocol.TypeHolder) ObjectType(com.github.dirtpowered.dirtmv.data.entity.ObjectType) Timer(java.util.Timer) TimerTask(java.util.TimerTask) GameProfile(com.mojang.authlib.GameProfile) V1_7EntityTracker(com.github.dirtpowered.dirtmv.network.versions.Release47To5.entity.V1_7EntityTracker) ItemStack(com.github.dirtpowered.dirtmv.data.protocol.objects.ItemStack) UUID(java.util.UUID) WatchableObject(com.github.dirtpowered.dirtmv.data.protocol.objects.WatchableObject) Property(com.mojang.authlib.properties.Property) SpawnableObject(com.github.dirtpowered.dirtmv.data.entity.SpawnableObject) PacketData(com.github.dirtpowered.dirtmv.data.protocol.PacketData)

Aggregations

Motion (com.github.dirtpowered.dirtmv.data.protocol.objects.Motion)6 PacketData (com.github.dirtpowered.dirtmv.data.protocol.PacketData)5 TypeHolder (com.github.dirtpowered.dirtmv.data.protocol.TypeHolder)5 PacketTranslator (com.github.dirtpowered.dirtmv.data.translator.PacketTranslator)5 ServerSession (com.github.dirtpowered.dirtmv.network.server.ServerSession)5 ItemStack (com.github.dirtpowered.dirtmv.data.protocol.objects.ItemStack)4 WatchableObject (com.github.dirtpowered.dirtmv.data.protocol.objects.WatchableObject)3 SneakyThrows (lombok.SneakyThrows)3 EntityType (com.github.dirtpowered.dirtmv.data.entity.EntityType)2 Location (com.github.dirtpowered.dirtmv.data.protocol.objects.Location)2 ProtocolStorage (com.github.dirtpowered.dirtmv.data.user.ProtocolStorage)2 List (java.util.List)2 Configuration (com.github.dirtpowered.dirtmv.api.Configuration)1 ObjectType (com.github.dirtpowered.dirtmv.data.entity.ObjectType)1 SpawnableObject (com.github.dirtpowered.dirtmv.data.entity.SpawnableObject)1 NettyInputWrapper (com.github.dirtpowered.dirtmv.data.protocol.io.NettyInputWrapper)1 NettyOutputWrapper (com.github.dirtpowered.dirtmv.data.protocol.io.NettyOutputWrapper)1 MetadataType (com.github.dirtpowered.dirtmv.data.protocol.objects.MetadataType)1 V1_3_4ChunkBulk (com.github.dirtpowered.dirtmv.data.protocol.objects.V1_3_4ChunkBulk)1 PlayerListEntry (com.github.dirtpowered.dirtmv.data.protocol.objects.tablist.PlayerListEntry)1