Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ repositories {
}
}

configurations {
testImplementation.extendsFrom compileOnly
}

dependencies {
compileOnly "com.viaversion:viaversion-common:5.12.0"
compileOnly "org.yaml:snakeyaml:2.7"
Expand Down Expand Up @@ -98,11 +102,20 @@ dependencies {
transitive = false
}

testImplementation "org.junit.jupiter:junit-jupiter:6.1.3"
testImplementation "org.mockito:mockito-core:5.24.0"
testImplementation sourceSets.tool.output
testRuntimeOnly "org.junit.platform:junit-platform-launcher:6.1.3"

toolImplementation "org.jsoup:jsoup:1.23.2"
toolImplementation "org.ow2.asm:asm-tree:9.11.1"
toolImplementation "net.lenni0451.commons:asm:1.9.2"
}

test {
useJUnitPlatform()
}

def registerToolTask(String name, String toolMainClass, String toolDescription) {
tasks.register(name, JavaExec) {
group = "viabedrock tools"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,9 @@ public JsonBlockStateUpgradeSchema(final JsonObject jsonObject) {

if (tag.get("states") instanceof CompoundTag states) {
for (Pair<String, ?> property : toAdd) {
states.put(property.key(), NbtUtil.createTag(property.value()));
if (!states.contains(property.key())) {
states.put(property.key(), NbtUtil.createTag(property.value()));
}
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final L
}

final int rawAlgorithm = in.readUnsignedByte();
final PacketCompressionAlgorithm algorithm = PacketCompressionAlgorithm.getByValue(rawAlgorithm);
// NetworkSettings uses a uint16 sentinel; a compressed frame has only a uint8 header.
final PacketCompressionAlgorithm algorithm = rawAlgorithm == 0xFF ? PacketCompressionAlgorithm.None : PacketCompressionAlgorithm.getByValue(rawAlgorithm);
if (algorithm == null) { // Bedrock client drops the packet if the algorithm is unknown
ViaBedrock.getPlatform().getLogger().log(Level.WARNING, "Dropping packet with unknown PacketCompressionAlgorithm: " + rawAlgorithm);
in.skipBytes(in.readableBytes());
Expand All @@ -95,7 +96,7 @@ protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final L

final CompressionAlgorithm compressionAlgorithm = this.getCompressionAlgorithm(algorithm);
if (compressionAlgorithm instanceof NoopCompression) {
out.add(in.retain());
out.add(in.readRetainedSlice(in.readableBytes()));
} else {
final ByteBuf uncompressedData = ctx.alloc().buffer();
compressionAlgorithm.decompress(in, uncompressedData); // Bedrock client would drop packets with invalid data, but this would be too insane to do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,11 @@ public enum ClientboundBedrockPackets implements ClientboundPacketType {
PLAYER_LOCATION(MinecraftPacketIds.PlayerLocation.getValue()),
//CAMERA_SPLINE(MinecraftPacketIds.CameraSplinePacket.getValue()),
SYNC_WORLD_CLOCKS(MinecraftPacketIds.SyncWorldClocks.getValue()),
VOXEL_SHAPES(MinecraftPacketIds.VoxelShapes.getValue()),
VOXEL_SHAPES(MinecraftPacketIds.VoxelShapesPacket.getValue()),
SET_PLAYER_FURNACE_OPTIONS(MinecraftPacketIds.SetPlayerFurnaceOptions.getValue()),
RECORD_STARTED(MinecraftPacketIds.RecordStarted.getValue());
//STONECUTTER_SET_RECIPE(MinecraftPacketIds.ClientboundStonecutterSetRecipePacket.getValue());
RECORD_STARTED(MinecraftPacketIds.RecordStarted.getValue()),
SET_PASSENGER_OF_BLOCK(MinecraftPacketIds.SetPassengerOfBlockPacket.getValue()),
PLAY_AUDIO_CONTENT(MinecraftPacketIds.ClientboundPlayAudioContent.getValue());
// UPDATE_SOUND_DATA(MinecraftPacketIds.ClientboundUpdateSoundDataPacket.getValue());
// CONTROL_SCHEME_SET(MinecraftPacketIds.ClientboundControlSchemeSetPacket.getValue()),
// PRIMITIVE_SHAPES(MinecraftPacketIds.PrimitiveShapesPacket.getValue()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.viaversion.nbt.tag.CompoundTag;
import com.viaversion.nbt.tag.ListTag;
import com.viaversion.nbt.tag.StringTag;
import com.viaversion.viaversion.libs.fastutil.ints.IntIntPair;
import com.viaversion.viaversion.util.Key;
import net.raphimc.viabedrock.ViaBedrock;
import net.raphimc.viabedrock.api.resourcepack.definition.BiomeDefinitions;
Expand All @@ -29,6 +28,7 @@
import net.raphimc.viabedrock.protocol.BedrockProtocol;
import net.raphimc.viabedrock.protocol.data.enums.Dimension;
import net.raphimc.viabedrock.protocol.data.generated.java.RegistryKeys;
import net.raphimc.viabedrock.protocol.model.DimensionDefinition;
import net.raphimc.viabedrock.protocol.storage.GameSessionStorage;
import net.raphimc.viabedrock.protocol.storage.ResourcePackStorage;

Expand Down Expand Up @@ -62,13 +62,24 @@ private static void modifyDimensionRegistry(final GameSessionStorage gameSession
dimensionRegistry.getCompoundTag(Dimension.OVERWORLD.getKey()).putInt("height", 256);
dimensionRegistry.getCompoundTag(Dimension.OVERWORLD.getKey()).putInt("logical_height", 256);
}
for (Map.Entry<String, IntIntPair> entry : gameSession.getBedrockDimensionDefinitions().entrySet()) {
final CompoundTag dimensionTag = new CompoundTag();
final int height = entry.getValue().rightInt() - entry.getValue().leftInt();
dimensionTag.putInt("min_y", entry.getValue().leftInt());
dimensionTag.putInt("height", height);
dimensionTag.putInt("logical_height", height);
dimensionRegistry.getCompoundTag(entry.getKey()).putAll(dimensionTag);
for (Map.Entry<String, DimensionDefinition> entry : gameSession.getBedrockDimensionDefinitions().entrySet()) {
applyDimensionDefinition(dimensionRegistry.getCompoundTag(entry.getKey()), entry.getValue());
}
}

static void applyDimensionDefinition(final CompoundTag dimensionTag, final DimensionDefinition definition) {
dimensionTag.putInt("min_y", definition.minimumY());
dimensionTag.putInt("height", definition.height());
dimensionTag.putInt("logical_height", definition.height());
CompoundTag attributes = dimensionTag.getCompoundTag("attributes");
if (attributes == null) {
attributes = new CompoundTag();
dimensionTag.put("attributes", attributes);
}
attributes.putFloat("minecraft:visual/cloud_height", definition.cloudHeight());
if (!definition.renderClouds()) {
// Java's day timeline multiplies cloud color, preserving zero alpha.
attributes.putString("minecraft:visual/cloud_color", "#00ffffff");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ public enum MinecraftPacketIds {
PlayerVideoCapturePacket(324),
PlayerUpdateEntityOverridesPacket(325),
PlayerLocation(326),
VoxelShapes(337),
SyncWorldClocks(344),
SendPartyDestinationCookie(349),
PartyDestinationCookieResponse(350),
Expand All @@ -227,6 +226,29 @@ public enum MinecraftPacketIds {
ServerboundMatchmakingCancel(356),
ClientboundPlayAudioContent(359),
ServerboundRegisterAudioContent(360),
ClientboundControlSchemeSetPacket(327),
PrimitiveShapesPacket(328),
ServerboundPackSettingChangePacket(329),
ClientboundDataStorePacket(330),
GraphicsOverrideParameterPacket(331),
ServerboundDataStorePacket(332),
ClientboundDataDrivenUIShowScreenPacket(333),
ClientboundDataDrivenUICloseScreenPacket(334),
ClientboundDataDrivenUIReloadPacket(335),
ClientboundTextureShiftPacket(336),
VoxelShapesPacket(337),
CameraSplinePacket(338),
ResourcePacksReadyForValidationPacket(340),
LocatorBarPacket(341),
PartyChangedPacket(342),
ServerboundDataDrivenScreenClosedPacket(343),
ClientboundAttributeLayerSyncPacket(345),
ServerStoreInfoPacket(346),
ServerPresenceInfoPacket(347),
ClientboundUpdateSoundDataPacket(348),
ServerboundStonecutterSetRecipePacket(354),
ClientboundStonecutterSetRecipePacket(355),
SetPassengerOfBlockPacket(357),
;

private static final Int2ObjectMap<MinecraftPacketIds> BY_VALUE = new Int2ObjectOpenHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public enum PacketCompressionAlgorithm {

ZLib(0),
Snappy(1),
None(255),
None(65535),
;

private static final Int2ObjectMap<PacketCompressionAlgorithm> BY_VALUE = new Int2ObjectOpenHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static ResourcePackResponse getByName(final String name, final ResourcePa
}

public int getValue() {
return this.value - 1; // TODO
return this.value;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* This file is part of ViaBedrock - https://github.com/RaphiMC/ViaBedrock
* Copyright (C) 2023-2026 RK_01/RaphiMC and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.raphimc.viabedrock.protocol.model;

public record DimensionDefinition(int minimumY, int height, int cloudHeight, boolean renderClouds) {

public DimensionDefinition {
if (height <= 0) {
throw new IllegalArgumentException("Dimension height range must be positive");
}
Math.addExact(minimumY, height);
}

public int maximumY() {
return this.minimumY + this.height;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static void register(final BedrockProtocol protocol) {
final long entityUniqueId = wrapper.read(BedrockTypes.VAR_LONG); // actorUniqueId
final String name = wrapper.read(BedrockTypes.STRING); // name
final String xuid = wrapper.read(BedrockTypes.STRING); // xuid
wrapper.read(BedrockTypes.STRING); // skin playfab id
final String playFabId = wrapper.read(BedrockTypes.STRING); // PlayFab id
final String platformOnlineId = wrapper.read(BedrockTypes.STRING); // platform online id
final int deviceOs = wrapper.read(BedrockTypes.INT_LE); // device os
final SkinData skin = wrapper.read(BedrockTypes.SKIN); // skin
Expand All @@ -90,6 +90,7 @@ public static void register(final BedrockProtocol protocol) {

profiles.add(new GameProfile(name, uuid, new GameProfile.Property[]{
new GameProfile.Property("xuid", xuid),
new GameProfile.Property("playfab_id", playFabId),
new GameProfile.Property("platform_online_id", platformOnlineId),
new GameProfile.Property("device_os", String.valueOf(deviceOs)),
new GameProfile.Property("is_teacher", String.valueOf(isTeacher)),
Expand All @@ -98,7 +99,6 @@ public static void register(final BedrockProtocol protocol) {
}));

Via.getManager().getProviders().get(SkinProvider.class).setSkin(wrapper.user(), uuid, skin);
// TODO: Update skin playFabId

final Pair<Long, String> entry = playerListStorage.addPlayer(uuid, entityUniqueId, name);
if (entry != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import com.viaversion.viaversion.api.type.Types;
import com.viaversion.viaversion.libs.fastutil.ints.IntIntImmutablePair;
import com.viaversion.viaversion.protocols.base.ClientboundLoginPackets;
import com.viaversion.viaversion.protocols.base.v1_7.ClientboundBaseProtocol1_7;
import com.viaversion.viaversion.protocols.v26_2to26_3.packet.ClientboundConfigurationPackets26_3;
Expand All @@ -55,6 +54,7 @@
import net.raphimc.viabedrock.protocol.data.enums.java.Relative;
import net.raphimc.viabedrock.protocol.data.enums.java.generated.PlayerInfoUpdateAction;
import net.raphimc.viabedrock.protocol.data.generated.java.Attributes;
import net.raphimc.viabedrock.protocol.model.DimensionDefinition;
import net.raphimc.viabedrock.protocol.model.*;
import net.raphimc.viabedrock.protocol.rewriter.BlockStateRewriter;
import net.raphimc.viabedrock.protocol.rewriter.ItemRewriter;
Expand Down Expand Up @@ -434,16 +434,9 @@ protected void register() {
final int count = wrapper.read(BedrockTypes.UNSIGNED_VAR_INT); // entry count
for (int i = 0; i < count; i++) {
final String dimensionIdentifier = wrapper.read(BedrockTypes.STRING); // dimension identifier
final int minimumHeight = wrapper.read(BedrockTypes.VAR_INT); // minimum height
final int maximumHeight = minimumHeight + wrapper.read(BedrockTypes.VAR_INT); // maximum height
wrapper.read(BedrockTypes.VAR_INT); // generator type
wrapper.read(BedrockTypes.VAR_INT); // dimension type
wrapper.read(BedrockTypes.UUID); // pack id
wrapper.read(BedrockTypes.STRING); // default biome
wrapper.read(BedrockTypes.VAR_INT); // cloud height
wrapper.read(Types.BOOLEAN); // render clouds
if (dimensionIdentifier.equals(Dimension.OVERWORLD.getKey())) { // Bedrock client currently only supports overworld
gameSession.putBedrockDimensionDefinition(dimensionIdentifier, new IntIntImmutablePair(minimumHeight, maximumHeight));
final DimensionDefinition definition = readDimensionDefinition(wrapper);
if (dimensionIdentifier.equals(Dimension.OVERWORLD.getKey())) { // Only Overworld overrides currently map to Java
gameSession.putBedrockDimensionDefinition(dimensionIdentifier, definition);
}
}
}, State.PLAY, (PacketHandler) PacketWrapper::cancel // Bedrock client ignores dimension data after start game
Expand Down Expand Up @@ -636,6 +629,18 @@ private static void handleJavaClientGameJoin(final UserConnection user) {
setTime.send(BedrockProtocol.class, false);
}

static DimensionDefinition readDimensionDefinition(final PacketWrapper wrapper) {
final int minimumY = wrapper.read(BedrockTypes.VAR_INT);
final int heightRange = wrapper.read(BedrockTypes.VAR_INT);
wrapper.read(BedrockTypes.VAR_INT); // generator type
wrapper.read(BedrockTypes.VAR_INT); // dimension type
wrapper.read(BedrockTypes.UUID); // pack id
wrapper.read(BedrockTypes.STRING); // default biome
final int cloudHeight = wrapper.read(BedrockTypes.VAR_INT);
final boolean renderClouds = wrapper.read(Types.BOOLEAN);
return new DimensionDefinition(minimumY, heightRange, cloudHeight, renderClouds);
}

private JoinPackets() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ public static void register(final BedrockProtocol protocol) {
final PlayerAbilities abilities = wrapper.read(BedrockTypes.PLAYER_ABILITIES); // abilities
wrapper.read(BedrockTypes.ENTITY_LINK_ARRAY); // entity links
wrapper.read(BedrockTypes.OPTIONAL_PASSENGER_OF_BLOCKS); // passenger of blocks
wrapper.read(BedrockTypes.STRING); // device id
wrapper.read(BedrockTypes.INT_LE); // build platform
final String deviceId = wrapper.read(BedrockTypes.STRING); // device id
final int buildPlatform = wrapper.read(BedrockTypes.INT_LE); // build platform

final PlayerEntity entity = entityTracker.addEntity(new PlayerEntity(wrapper.user(), entityRuntimeId, entityTracker.getNextJavaEntityId(), uuid, abilities));
entity.setPosition(position);
Expand All @@ -92,8 +92,8 @@ public static void register(final BedrockProtocol protocol) {
playerInfoUpdate.write(Types.STRING, StringUtil.encodeUuid(uuid)); // username
playerInfoUpdate.write(Types.PROFILE_PROPERTY_ARRAY, new GameProfile.Property[]{
new GameProfile.Property("platform_online_id", platformOnlineId),
new GameProfile.Property("device_id", wrapper.read(BedrockTypes.STRING)), // device id
new GameProfile.Property("device_os", wrapper.read(BedrockTypes.INT_LE).toString()) // device os
new GameProfile.Property("device_id", deviceId),
new GameProfile.Property("device_os", Integer.toString(buildPlatform))
}); // properties
playerInfoUpdate.write(Types.VAR_INT, GameTypeRewriter.getEffectiveGameMode(gameType, gameSession.getLevelGameType()).ordinal()); // game mode
playerInfoUpdate.send(BedrockProtocol.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* This file is part of ViaBedrock - https://github.com/RaphiMC/ViaBedrock
* Copyright (C) 2023-2026 RK_01/RaphiMC and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.raphimc.viabedrock.protocol.packet;

import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import net.raphimc.viabedrock.protocol.data.enums.bedrock.generated.ResourcePackResponse;
import net.raphimc.viabedrock.protocol.types.BedrockTypes;

public final class ResourcePackClientResponse {

private ResourcePackClientResponse() {
}

public static void write(final PacketWrapper wrapper, final ResourcePackResponse status) {
if (status == ResourcePackResponse.Downloading) {
throw new IllegalArgumentException("Downloading requires a pack list");
}
writeStatus(wrapper, status);
}

public static void writeDownloading(final PacketWrapper wrapper, final String[] packIds) {
writeStatus(wrapper, ResourcePackResponse.Downloading);
wrapper.write(BedrockTypes.STRING_ARRAY, packIds);
}

private static void writeStatus(final PacketWrapper wrapper, final ResourcePackResponse status) {
final String name = switch (status) {
case Cancel -> "cancel";
case Downloading -> "downloading";
case DownloadingFinished -> "downloadingfinished";
case ResourcePackStackFinished -> "resourcepackstackfinished";
};
wrapper.write(BedrockTypes.UNSIGNED_VAR_INT, status.getValue() - 1); // Cereal variant indexes start at zero, enum values at one.
wrapper.write(BedrockTypes.STRING, name);
}

}
Loading