Java Code Examples for com.nukkitx.nbt.NbtUtils#createNetworkWriter()

The following examples show how to use com.nukkitx.nbt.NbtUtils#createNetworkWriter() . 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: UpdateTradeSerializer_v313.java    From Protocol with Apache License 2.0 6 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, UpdateTradePacket packet) {
    buffer.writeByte(packet.getWindowId());
    buffer.writeByte(packet.getWindowType());
    VarInts.writeInt(buffer, packet.getUnknownInt());
    VarInts.writeInt(buffer, packet.isScreen2() ? 40 : 0);
    VarInts.writeInt(buffer, packet.getTradeTier());
    buffer.writeBoolean(packet.isWilling());
    VarInts.writeLong(buffer, packet.getTraderUniqueEntityId());
    VarInts.writeLong(buffer, packet.getPlayerUniqueEntityId());
    BedrockUtils.writeString(buffer, packet.getDisplayName());
    try (NBTOutputStream writer = NbtUtils.createNetworkWriter(new ByteBufOutputStream(buffer))) {
        writer.write(packet.getOffers());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 2
Source File: UpdateTradeSerializer_v361.java    From Protocol with Apache License 2.0 6 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, UpdateTradePacket packet) {
    buffer.writeByte(packet.getWindowId());
    buffer.writeByte(packet.getWindowType());
    VarInts.writeInt(buffer, packet.getUnknownInt());
    VarInts.writeInt(buffer, packet.getTradeTier());
    VarInts.writeLong(buffer, packet.getTraderUniqueEntityId());
    VarInts.writeLong(buffer, packet.getPlayerUniqueEntityId());
    BedrockUtils.writeString(buffer, packet.getDisplayName());
    buffer.writeBoolean(packet.isScreen2());
    buffer.writeBoolean(packet.isWilling());
    try (NBTOutputStream writer = NbtUtils.createNetworkWriter(new ByteBufOutputStream(buffer))) {
        writer.write(packet.getOffers());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 3
Source File: BlockEntityDataSerializer_v313.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, BlockEntityDataPacket packet) {
    BedrockUtils.writeBlockPosition(buffer, packet.getBlockPosition());
    try (NBTOutputStream writer = NbtUtils.createNetworkWriter(new ByteBufOutputStream(buffer))) {
        writer.write(packet.getData());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 4
Source File: UpdateEquipSerializer_v388.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, UpdateEquipPacket packet) {
    buffer.writeByte(packet.getWindowId());
    buffer.writeByte(packet.getWindowType());
    VarInts.writeInt(buffer, packet.getUnknown0());
    VarInts.writeLong(buffer, packet.getUniqueEntityId());
    try (NBTOutputStream writer = NbtUtils.createNetworkWriter(new ByteBufOutputStream(buffer))) {
        writer.write(packet.getTag());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 5
Source File: BiomeDefinitionListSerializer_v388.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, BiomeDefinitionListPacket packet) {
    try (NBTOutputStream writer = NbtUtils.createNetworkWriter(new ByteBufOutputStream(buffer))) {
        writer.write(packet.getTag());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 6
Source File: BiomeDefinitionListSerializer_v313.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, BiomeDefinitionListPacket packet) {
    try (NBTOutputStream writer = NbtUtils.createNetworkWriter(new ByteBufOutputStream(buffer))) {
        writer.write(packet.getTag());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 7
Source File: UpdateBlockPropertiesSerializer_v388.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, UpdateBlockPropertiesPacket packet) {
    try (NBTOutputStream writer = NbtUtils.createNetworkWriter(new ByteBufOutputStream(buffer))) {
        writer.write(packet.getProperties());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 8
Source File: StructureTemplateDataExportResponseSerializer_v388.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, StructureTemplateDataExportResponsePacket packet) {
    BedrockUtils.writeString(buffer, packet.getName());
    boolean save = packet.isSave();
    buffer.writeBoolean(save);

    if (save) {
        try (NBTOutputStream writer = NbtUtils.createNetworkWriter(new ByteBufOutputStream(buffer))) {
            writer.write(packet.getTag());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
 
Example 9
Source File: LevelEventGenericSerializer_v388.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, LevelEventGenericPacket packet) {
    VarInts.writeInt(buffer, packet.getEventId());
    try (NBTOutputStream writer = NbtUtils.createNetworkWriter(new ByteBufOutputStream(buffer))) {
        writer.write(packet.getTag());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 10
Source File: BiomeDefinitionListSerializer_v340.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, BiomeDefinitionListPacket packet) {
    try (NBTOutputStream writer = NbtUtils.createNetworkWriter(new ByteBufOutputStream(buffer))) {
        writer.write(packet.getTag());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 11
Source File: BlockEntityDataSerializer_v354.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, BlockEntityDataPacket packet) {
    BedrockUtils.writeBlockPosition(buffer, packet.getBlockPosition());
    try (NBTOutputStream writer = NbtUtils.createNetworkWriter(new ByteBufOutputStream(buffer))) {
        writer.write(packet.getData());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 12
Source File: UpdateEquipSerializer_v354.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, UpdateEquipPacket packet) {
    buffer.writeByte(packet.getWindowId());
    buffer.writeByte(packet.getWindowType());
    VarInts.writeInt(buffer, packet.getUnknown0());
    VarInts.writeLong(buffer, packet.getUniqueEntityId());
    try (NBTOutputStream writer = NbtUtils.createNetworkWriter(new ByteBufOutputStream(buffer))) {
        writer.write(packet.getTag());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 13
Source File: UpdateEquipSerializer_v313.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, UpdateEquipPacket packet) {
    buffer.writeByte(packet.getWindowId());
    buffer.writeByte(packet.getWindowType());
    VarInts.writeInt(buffer, packet.getUnknown0());
    VarInts.writeLong(buffer, packet.getUniqueEntityId());
    try (NBTOutputStream writer = NbtUtils.createNetworkWriter(new ByteBufOutputStream(buffer))) {
        writer.write(packet.getTag());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 14
Source File: LevelEventGenericSerializer_v361.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, LevelEventGenericPacket packet) {
    VarInts.writeInt(buffer, packet.getEventId());
    try (NBTOutputStream writer = NbtUtils.createNetworkWriter(new ByteBufOutputStream(buffer))) {
        writer.write(packet.getTag());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 15
Source File: ProxyPass.java    From ProxyPass with GNU Affero General Public License v3.0 5 votes vote down vote up
public void saveNBT(String dataName, Tag<?> dataTag) {
    Path path = dataDir.resolve(dataName + ".dat");
    try (OutputStream outputStream = Files.newOutputStream(path, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
         NBTOutputStream nbtOutputStream = NbtUtils.createNetworkWriter(outputStream)){
        nbtOutputStream.write(dataTag);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 16
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 4 votes vote down vote up
public static void writeItemData(ByteBuf buffer, ItemData item) {
    Preconditions.checkNotNull(buffer, "buffer");
    Preconditions.checkNotNull(item, "item");

    // Write id
    int id = item.getId();
    if (id == 0) {
        // We don't need to write anything extra.
        buffer.writeByte(0);
        return;
    }
    VarInts.writeInt(buffer, id);

    // Write damage and count
    short damage = item.getDamage();
    if (damage == -1) damage = Short.MAX_VALUE;
    VarInts.writeInt(buffer, (damage << 8) | (item.getCount() & 0xff));

    if (item.getTag() != null) {
        buffer.writeShortLE(-1);
        VarInts.writeUnsignedInt(buffer, 1); // Hardcoded in current version

        try (NBTOutputStream stream = NbtUtils.createNetworkWriter(new ByteBufOutputStream(buffer))) {
            stream.write(item.getTag());
        } catch (IOException e) {
            // This shouldn't happen (as this is backed by a Netty ByteBuf), but okay...
            throw new IllegalStateException("Unable to save NBT data", e);
        }
    } else {
        buffer.writeShortLE(0);
    }

    String[] canPlace = item.getCanPlace();
    VarInts.writeInt(buffer, canPlace.length);
    for (String aCanPlace : canPlace) {
        BedrockUtils.writeString(buffer, aCanPlace);
    }

    String[] canBreak = item.getCanBreak();
    VarInts.writeInt(buffer, canBreak.length);
    for (String aCanBreak : canBreak) {
        BedrockUtils.writeString(buffer, aCanBreak);
    }

    if (id == 513) { // TODO: 20/03/2019 We shouldn't be hardcoding this but it's what Microjang have made us do
        VarInts.writeLong(buffer, item.getBlockingTicks());
    }
}
 
Example 17
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 4 votes vote down vote up
public static void writeItemData(ByteBuf buffer, ItemData item) {
    Preconditions.checkNotNull(buffer, "buffer");
    Preconditions.checkNotNull(item, "item");

    // Write id
    int id = item.getId();
    if (id == 0) {
        // We don't need to write anything extra.
        buffer.writeByte(0);
        return;
    }
    VarInts.writeInt(buffer, id);

    // Write damage and count
    short damage = item.getDamage();
    if (damage == -1) damage = Short.MAX_VALUE;
    VarInts.writeInt(buffer, (damage << 8) | (item.getCount() & 0xff));

    if (item.getTag() != null) {
        buffer.writeShort(-1);
        VarInts.writeUnsignedInt(buffer, 1); // Hardcoded in current version

        try (NBTOutputStream stream = NbtUtils.createNetworkWriter(new ByteBufOutputStream(buffer))) {
            stream.write(item.getTag());
        } catch (IOException e) {
            // This shouldn't happen (as this is backed by a Netty ByteBuf), but okay...
            throw new IllegalStateException("Unable to save NBT data", e);
        }
    } else {
        buffer.writeShortLE(0);
    }

    String[] canPlace = item.getCanPlace();
    VarInts.writeInt(buffer, canPlace.length);
    for (String aCanPlace : canPlace) {
        BedrockUtils.writeString(buffer, aCanPlace);
    }

    String[] canBreak = item.getCanBreak();
    VarInts.writeInt(buffer, canBreak.length);
    for (String aCanBreak : canBreak) {
        BedrockUtils.writeString(buffer, aCanBreak);
    }

    if (id == 513) { // TODO: 20/03/2019 We shouldn't be hardcoding this but it's what Microjang have made us do
        VarInts.writeLong(buffer, item.getBlockingTicks());
    }
}
 
Example 18
Source File: StartGameSerializer_v388.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());
    VarInts.writeInt(buffer, packet.getEduEditionOffers());
    buffer.writeBoolean(packet.isEduFeaturesEnabled());
    buffer.writeFloatLE(packet.getRainLevel());
    buffer.writeFloatLE(packet.getLightningLevel());
    buffer.writeBoolean(packet.isPlatformLockedContentConfirmed());
    buffer.writeBoolean(packet.isMultiplayerGame());
    buffer.writeBoolean(packet.isBroadcastingToLan());
    VarInts.writeInt(buffer, packet.getXblBroadcastMode().ordinal());
    VarInts.writeInt(buffer, packet.getPlatformBroadcastMode().ordinal());
    buffer.writeBoolean(packet.isCommandsEnabled());
    buffer.writeBoolean(packet.isTexturePacksRequired());
    BedrockUtils.writeArray(buffer, packet.getGamerules(), BedrockUtils::writeGameRule);
    buffer.writeBoolean(packet.isBonusChestEnabled());
    buffer.writeBoolean(packet.isStartingWithMap());
    VarInts.writeInt(buffer, packet.getDefaultPlayerPermission().ordinal());
    buffer.writeIntLE(packet.getServerChunkTickRange());
    buffer.writeBoolean(packet.isBehaviorPackLocked());
    buffer.writeBoolean(packet.isResourcePackLocked());
    buffer.writeBoolean(packet.isFromLockedWorldTemplate());
    buffer.writeBoolean(packet.isUsingMsaGamertagsOnly());
    buffer.writeBoolean(packet.isFromWorldTemplate());
    buffer.writeBoolean(packet.isWorldTemplateOptionLocked());
    buffer.writeBoolean(packet.isOnlySpawningV1Villagers());
    BedrockUtils.writeString(buffer, packet.getVanillaVersion());

    // Level settings end
    BedrockUtils.writeString(buffer, packet.getLevelId());
    BedrockUtils.writeString(buffer, packet.getWorldName());
    BedrockUtils.writeString(buffer, packet.getPremiumWorldTemplateId());
    buffer.writeBoolean(packet.isTrial());
    buffer.writeBoolean(packet.isMovementServerAuthoritative());
    buffer.writeLongLE(packet.getCurrentTick());
    VarInts.writeInt(buffer, packet.getEnchantmentSeed());

    // cache palette for fast writing
    try (NBTOutputStream stream = NbtUtils.createNetworkWriter(new ByteBufOutputStream(buffer))) {
        stream.write(packet.getBlockPalette());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    BedrockUtils.writeArray(buffer, packet.getItemEntries(), (buf, entry) -> {
        BedrockUtils.writeString(buf, entry.getIdentifier());
        buf.writeShortLE(entry.getId());
    });

    BedrockUtils.writeString(buffer, packet.getMultiplayerCorrelationId());

}
 
Example 19
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 4 votes vote down vote up
public static void writeItemData(ByteBuf buffer, ItemData item) {
    Preconditions.checkNotNull(buffer, "buffer");
    Preconditions.checkNotNull(item, "item");

    // Write id
    int id = item.getId();
    if (id == 0) {
        // We don't need to write anything extra.
        buffer.writeByte(0);
        return;
    }
    VarInts.writeInt(buffer, id);

    // Write damage and count
    short damage = item.getDamage();
    if (damage == -1) damage = Short.MAX_VALUE;
    VarInts.writeInt(buffer, (damage << 8) | (item.getCount() & 0xff));

    if (item.getTag() != null) {
        buffer.writeShort(-1);
        VarInts.writeUnsignedInt(buffer, 1); // Hardcoded in current version

        try (NBTOutputStream stream = NbtUtils.createNetworkWriter(new ByteBufOutputStream(buffer))) {
            stream.write(item.getTag());
        } catch (IOException e) {
            // This shouldn't happen (as this is backed by a Netty ByteBuf), but okay...
            throw new IllegalStateException("Unable to save NBT data", e);
        }
    } else {
        buffer.writeShortLE(0);
    }

    String[] canPlace = item.getCanPlace();
    VarInts.writeInt(buffer, canPlace.length);
    for (String aCanPlace : canPlace) {
        BedrockUtils.writeString(buffer, aCanPlace);
    }

    String[] canBreak = item.getCanBreak();
    VarInts.writeInt(buffer, canBreak.length);
    for (String aCanBreak : canBreak) {
        BedrockUtils.writeString(buffer, aCanBreak);
    }

    if (id == 513) { // TODO: 20/03/2019 We shouldn't be hardcoding this but it's what Microjang have made us do
        VarInts.writeLong(buffer, item.getBlockingTicks());
    }
}
 
Example 20
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 4 votes vote down vote up
public static void writeItemData(ByteBuf buffer, ItemData item) {
    Preconditions.checkNotNull(buffer, "buffer");
    Preconditions.checkNotNull(item, "item");

    // Write id
    int id = item.getId();
    if (id == 0) {
        // We don't need to write anything extra.
        buffer.writeByte(0);
        return;
    }
    VarInts.writeInt(buffer, id);

    // Write damage and count
    short damage = item.getDamage();
    if (damage == -1) damage = Short.MAX_VALUE;
    VarInts.writeInt(buffer, (damage << 8) | (item.getCount() & 0xff));

    if (item.getTag() != null) {
        buffer.writeShortLE(-1);
        VarInts.writeUnsignedInt(buffer, 1); // Hardcoded in current version

        try (NBTOutputStream stream = NbtUtils.createNetworkWriter(new ByteBufOutputStream(buffer))) {
            stream.write(item.getTag());
        } catch (IOException e) {
            // This shouldn't happen (as this is backed by a Netty ByteBuf), but okay...
            throw new IllegalStateException("Unable to save NBT data", e);
        }
    } else {
        buffer.writeShortLE(0);
    }

    String[] canPlace = item.getCanPlace();
    VarInts.writeInt(buffer, canPlace.length);
    for (String aCanPlace : canPlace) {
        BedrockUtils.writeString(buffer, aCanPlace);
    }

    String[] canBreak = item.getCanBreak();
    VarInts.writeInt(buffer, canBreak.length);
    for (String aCanBreak : canBreak) {
        BedrockUtils.writeString(buffer, aCanBreak);
    }
}