Java Code Examples for com.nukkitx.network.VarInts#writeUnsignedInt()

The following examples show how to use com.nukkitx.network.VarInts#writeUnsignedInt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 6 votes vote down vote up
public static void writeGameRule(ByteBuf buffer, GameRuleData gameRule) {
    Preconditions.checkNotNull(buffer, "buffer");
    Preconditions.checkNotNull(gameRule, "gameRule");

    Object value = gameRule.getValue();
    int type = GameRulesChangedSerializer_v313.RULE_TYPES.get(value.getClass());

    BedrockUtils.writeString(buffer, gameRule.getName());
    VarInts.writeUnsignedInt(buffer, type);

    switch (type) {
        case 1:
            buffer.writeBoolean((boolean) value);
            break;
        case 2:
            VarInts.writeUnsignedInt(buffer, (int) value);
            break;
        case 3:
            buffer.writeFloatLE((float) value);
            break;
    }
}
 
Example 2
Source File: AdventureSettingsSerializer_v313.java    From Protocol with Apache License 2.0 6 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, AdventureSettingsPacket packet) {
    int flags1 = 0;
    int flags2 = 0;
    for (AdventureSettingsPacket.Flag flag : packet.getFlags()) {
        if (FLAGS_TO_BIT_1.containsKey(flag)) {
            flags1 |= FLAGS_TO_BIT_1.get(flag);
        } else if (FLAGS_TO_BIT_2.containsKey(flag)) {
            flags2 |= FLAGS_TO_BIT_2.get(flag);
        }
    }
    VarInts.writeUnsignedInt(buffer, flags1);
    VarInts.writeUnsignedInt(buffer, packet.getCommandPermission().ordinal());
    VarInts.writeUnsignedInt(buffer, flags2);
    VarInts.writeUnsignedInt(buffer, packet.getPlayerPermission().ordinal());
    VarInts.writeUnsignedInt(buffer, 0); // Useless
    buffer.writeLongLE(packet.getUniqueEntityId());
}
 
Example 3
Source File: LoginSerializer_v332.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, LoginPacket packet) {
    buffer.writeInt(packet.getProtocolVersion());

    AsciiString chainData = packet.getChainData();
    AsciiString skinData = packet.getSkinData();

    VarInts.writeUnsignedInt(buffer, chainData.length() + skinData.length() + 8);

    BedrockUtils.writeLEAsciiString(buffer, chainData);
    BedrockUtils.writeLEAsciiString(buffer, skinData);
}
 
Example 4
Source File: PhotoTransferSerializer_v332.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, PhotoTransferPacket packet) {
    BedrockUtils.writeString(buffer, packet.getName());
    byte[] data = packet.getData();
    VarInts.writeUnsignedInt(buffer, data.length);
    buffer.writeBytes(data);
    BedrockUtils.writeString(buffer, packet.getBookId());
}
 
Example 5
Source File: InventoryContentSerializer_v291.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, InventoryContentPacket packet) {
    VarInts.writeUnsignedInt(buffer, packet.getContainerId());

    ItemData[] contents = packet.getContents();
    VarInts.writeUnsignedInt(buffer, contents.length);
    for (ItemData content : contents) {
        BedrockUtils.writeItemData(buffer, content);
    }
}
 
Example 6
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static void writeBlockPosition(ByteBuf buffer, Vector3i blockPosition) {
    Preconditions.checkNotNull(buffer, "buffer");
    Preconditions.checkNotNull(blockPosition, "blockPosition");
    VarInts.writeInt(buffer, blockPosition.getX());
    VarInts.writeUnsignedInt(buffer, blockPosition.getY());
    VarInts.writeInt(buffer, blockPosition.getZ());
}
 
Example 7
Source File: FullChunkDataSerializer_v291.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, LevelChunkPacket packet) {
    VarInts.writeInt(buffer, packet.getChunkX());
    VarInts.writeInt(buffer, packet.getChunkZ());
    byte[] data = packet.getData();
    VarInts.writeUnsignedInt(buffer, data.length);
    buffer.writeBytes(data);
}
 
Example 8
Source File: PhotoTransferSerializer_v361.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, PhotoTransferPacket packet) {
    BedrockUtils.writeString(buffer, packet.getName());
    byte[] data = packet.getData();
    VarInts.writeUnsignedInt(buffer, data.length);
    buffer.writeBytes(data);
    BedrockUtils.writeString(buffer, packet.getBookId());
}
 
Example 9
Source File: UpdateBlockSyncedSerializer_v388.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, UpdateBlockSyncedPacket packet) {
    BedrockUtils.writeBlockPosition(buffer, packet.getBlockPosition());
    VarInts.writeUnsignedInt(buffer, packet.getRuntimeId());
    int flagValue = 0;
    for (Flag flag : packet.getFlags()) {
        flagValue |= (1 << flag.ordinal());
    }
    VarInts.writeUnsignedInt(buffer, flagValue);
    VarInts.writeUnsignedInt(buffer, packet.getDataLayer());
    VarInts.writeUnsignedLong(buffer, packet.getRuntimeEntityId());
    VarInts.writeUnsignedLong(buffer, packet.getUnknownLong1());
}
 
Example 10
Source File: InventoryTransactionSerializer_v340.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, InventoryTransactionPacket packet) {
    Type transactionType = packet.getTransactionType();
    VarInts.writeUnsignedInt(buffer, transactionType.ordinal());

    BedrockUtils.writeArray(buffer, packet.getActions(), BedrockUtils::writeInventoryAction);

    switch (transactionType) {
        case ITEM_USE:
            VarInts.writeUnsignedInt(buffer, packet.getActionType());
            BedrockUtils.writeBlockPosition(buffer, packet.getBlockPosition());
            VarInts.writeInt(buffer, packet.getFace());
            VarInts.writeInt(buffer, packet.getHotbarSlot());
            BedrockUtils.writeItemData(buffer, packet.getItemInHand());
            BedrockUtils.writeVector3f(buffer, packet.getPlayerPosition());
            BedrockUtils.writeVector3f(buffer, packet.getClickPosition());
            VarInts.writeUnsignedInt(buffer, packet.getBlockRuntimeId());
            break;
        case ITEM_USE_ON_ENTITY:
            VarInts.writeUnsignedLong(buffer, packet.getRuntimeEntityId());
            VarInts.writeUnsignedInt(buffer, packet.getActionType());
            VarInts.writeInt(buffer, packet.getHotbarSlot());
            BedrockUtils.writeItemData(buffer, packet.getItemInHand());
            BedrockUtils.writeVector3f(buffer, packet.getPlayerPosition());
            BedrockUtils.writeVector3f(buffer, packet.getClickPosition());
            break;
        case ITEM_RELEASE:
            VarInts.writeUnsignedInt(buffer, packet.getActionType());
            VarInts.writeInt(buffer, packet.getHotbarSlot());
            BedrockUtils.writeItemData(buffer, packet.getItemInHand());
            BedrockUtils.writeVector3f(buffer, packet.getHeadPosition());
    }
}
 
Example 11
Source File: ClientCacheBlobStatusSerializer_v361.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, ClientCacheBlobStatusPacket packet) {
    LongList acks = packet.getAcks();
    LongList nacks = packet.getNaks();
    VarInts.writeUnsignedInt(buffer, acks.size());
    VarInts.writeUnsignedInt(buffer, nacks.size());

    acks.forEach((LongConsumer) buffer::writeLongLE);
    nacks.forEach((LongConsumer) buffer::writeLongLE);
}
 
Example 12
Source File: UpdateBlockSyncedSerializer_v361.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, UpdateBlockSyncedPacket packet) {
    BedrockUtils.writeBlockPosition(buffer, packet.getBlockPosition());
    VarInts.writeUnsignedInt(buffer, packet.getRuntimeId());
    int flagValue = 0;
    for (Flag flag : packet.getFlags()) {
        flagValue |= (1 << flag.ordinal());
    }
    VarInts.writeUnsignedInt(buffer, flagValue);
    VarInts.writeUnsignedInt(buffer, packet.getDataLayer());
    VarInts.writeUnsignedLong(buffer, packet.getRuntimeEntityId());
    VarInts.writeUnsignedLong(buffer, packet.getUnknownLong1());
}
 
Example 13
Source File: InventoryContentSerializer_v361.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, InventoryContentPacket packet) {
    VarInts.writeUnsignedInt(buffer, packet.getContainerId());

    ItemData[] contents = packet.getContents();
    VarInts.writeUnsignedInt(buffer, contents.length);
    for (ItemData content : contents) {
        BedrockUtils.writeItemData(buffer, content);
    }
}
 
Example 14
Source File: ClientboundMapItemDataSerializer_v354.java    From Protocol with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, ClientboundMapItemDataPacket packet) {
    VarInts.writeLong(buffer, packet.getUniqueMapId());

    int type = 0;
    int[] colors = packet.getColors();
    if (colors != null && colors.length > 0) {
        type |= 0x2;
    }
    List<MapDecoration> decorations = packet.getDecorations();
    List<MapTrackedObject> trackedObjects = packet.getTrackedObjects();
    if (!decorations.isEmpty() && !trackedObjects.isEmpty()) {
        type |= 0x4;
    }
    LongList trackedEntityIds = packet.getTrackedEntityIds();
    if (!trackedEntityIds.isEmpty()) {
        type |= 0x8;
    }

    VarInts.writeUnsignedInt(buffer, type);
    buffer.writeByte(packet.getDimensionId());
    buffer.writeBoolean(packet.isLocked());

    if ((type & 0x8) != 0) {
        VarInts.writeUnsignedInt(buffer, trackedEntityIds.size());
        for (long trackedEntityId : trackedEntityIds) {
            VarInts.writeLong(buffer, trackedEntityId);
        }
    }

    if ((type & 0xe) != 0) {
        buffer.writeByte(packet.getScale());
    }

    if ((type & 0x4) != 0) {
        VarInts.writeUnsignedInt(buffer, trackedObjects.size());
        for (MapTrackedObject object : trackedObjects) {
            switch (object.getType()) {
                case BLOCK:
                    buffer.writeIntLE(object.getType().ordinal());
                    BedrockUtils.writeBlockPosition(buffer, object.getPosition());
                    break;
                case ENTITY:
                    buffer.writeIntLE(object.getType().ordinal());
                    VarInts.writeLong(buffer, object.getEntityId());
                    break;
            }
        }

        VarInts.writeUnsignedInt(buffer, decorations.size());
        for (MapDecoration decoration : decorations) {
            buffer.writeByte(decoration.getImage());
            buffer.writeByte(decoration.getRotation());
            buffer.writeByte(decoration.getXOffset());
            buffer.writeByte(decoration.getYOffset());
            BedrockUtils.writeString(buffer, decoration.getLabel());
            VarInts.writeUnsignedInt(buffer, decoration.getColor());
        }
    }

    if ((type & 0x2) != 0) {
        VarInts.writeInt(buffer, packet.getWidth());
        VarInts.writeInt(buffer, packet.getHeight());
        VarInts.writeInt(buffer, packet.getXOffset());
        VarInts.writeInt(buffer, packet.getYOffset());

        VarInts.writeUnsignedInt(buffer, colors.length);
        for (int color : colors) {
            VarInts.writeUnsignedInt(buffer, color);
        }
    }
}
 
Example 15
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 4 votes vote down vote up
public static <T> void writeArray(ByteBuf buffer, T[] array, BiConsumer<ByteBuf, T> biConsumer) {
    VarInts.writeUnsignedInt(buffer, array.length);
    for (T val : array) {
        biConsumer.accept(buffer, val);
    }
}
 
Example 16
Source File: PlayerHotbarSerializer_v291.java    From Protocol with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, PlayerHotbarPacket packet) {
    VarInts.writeUnsignedInt(buffer, packet.getSelectedHotbarSlot());
    buffer.writeByte(packet.getContainerId());
    buffer.writeBoolean(packet.isSelectHotbarSlot());
}
 
Example 17
Source File: NetworkChunkPublisherUpdateSerializer_v361.java    From Protocol with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, NetworkChunkPublisherUpdatePacket packet) {
    BedrockUtils.writeVector3i(buffer, packet.getPosition());
    VarInts.writeUnsignedInt(buffer, packet.getRadius());
}
 
Example 18
Source File: StartGameSerializer_v313.java    From Protocol with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, StartGamePacket packet) {
    VarInts.writeLong(buffer, packet.getUniqueEntityId());
    VarInts.writeUnsignedLong(buffer, packet.getRuntimeEntityId());
    VarInts.writeInt(buffer, packet.getPlayerGamemode());
    BedrockUtils.writeVector3f(buffer, packet.getPlayerPosition());
    BedrockUtils.writeVector2f(buffer, packet.getRotation());
    // Level settings start
    VarInts.writeInt(buffer, packet.getSeed());
    VarInts.writeInt(buffer, packet.getDimensionId());
    VarInts.writeInt(buffer, packet.getGeneratorId());
    VarInts.writeInt(buffer, packet.getLevelGamemode());
    VarInts.writeInt(buffer, packet.getDifficulty());
    BedrockUtils.writeBlockPosition(buffer, packet.getDefaultSpawn());
    buffer.writeBoolean(packet.isAchievementsDisabled());
    VarInts.writeInt(buffer, packet.getTime());
    buffer.writeBoolean(packet.getEduEditionOffers() != 0);
    buffer.writeBoolean(packet.isEduFeaturesEnabled());
    buffer.writeFloatLE(packet.getRainLevel());
    buffer.writeFloatLE(packet.getLightningLevel());
    buffer.writeBoolean(packet.isMultiplayerGame());
    buffer.writeBoolean(packet.isBroadcastingToLan());
    buffer.writeBoolean(packet.getXblBroadcastMode() != GamePublishSetting.NO_MULTI_PLAY);
    buffer.writeBoolean(packet.isCommandsEnabled());
    buffer.writeBoolean(packet.isTexturePacksRequired());
    BedrockUtils.writeArray(buffer, packet.getGamerules(), BedrockUtils::writeGameRule);
    buffer.writeBoolean(packet.isBonusChestEnabled());
    buffer.writeBoolean(packet.isStartingWithMap());
    buffer.writeBoolean(packet.isTrustingPlayers());
    VarInts.writeInt(buffer, packet.getDefaultPlayerPermission().ordinal());
    VarInts.writeInt(buffer, packet.getXblBroadcastMode().ordinal());
    buffer.writeIntLE(packet.getServerChunkTickRange());
    buffer.writeBoolean(packet.getPlatformBroadcastMode() != GamePublishSetting.NO_MULTI_PLAY);
    VarInts.writeInt(buffer, packet.getPlatformBroadcastMode().ordinal());
    buffer.writeBoolean(packet.getXblBroadcastMode() != GamePublishSetting.NO_MULTI_PLAY);
    buffer.writeBoolean(packet.isBehaviorPackLocked());
    buffer.writeBoolean(packet.isResourcePackLocked());
    buffer.writeBoolean(packet.isFromLockedWorldTemplate());
    buffer.writeBoolean(packet.isUsingMsaGamertagsOnly());
    buffer.writeBoolean(packet.isFromWorldTemplate());
    buffer.writeBoolean(packet.isWorldTemplateOptionLocked());
    // Level settings end
    BedrockUtils.writeString(buffer, packet.getLevelId());
    BedrockUtils.writeString(buffer, packet.getWorldName());
    BedrockUtils.writeString(buffer, packet.getPremiumWorldTemplateId());
    buffer.writeBoolean(packet.isTrial());
    buffer.writeLongLE(packet.getCurrentTick());
    VarInts.writeInt(buffer, packet.getEnchantmentSeed());

    List<CompoundTag> palette = packet.getBlockPalette().getValue();
    VarInts.writeUnsignedInt(buffer, palette.size());
    for (CompoundTag entry : palette) {
        CompoundTag blockTag = entry.getCompound("block");
        BedrockUtils.writeString(buffer, blockTag.getString("name"));
        buffer.writeShortLE(entry.getShort("meta"));
    }

    BedrockUtils.writeString(buffer, packet.getMultiplayerCorrelationId());
}
 
Example 19
Source File: ServerSettingsResponseSerializer_v340.java    From Protocol with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, ServerSettingsResponsePacket packet) {
    VarInts.writeUnsignedInt(buffer, packet.getFormId());
    BedrockUtils.writeString(buffer, packet.getFormData());
}
 
Example 20
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 4 votes vote down vote up
public static void writeVarIntAsciiString(ByteBuf buffer, AsciiString string) {
    Preconditions.checkNotNull(buffer, "buffer");
    Preconditions.checkNotNull(string, "string");
    VarInts.writeUnsignedInt(buffer, string.length());
    buffer.writeBytes(string.toByteArray());
}