Skip to content

Commit

Permalink
Updated internals to Java Edition 1.20.2
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphiMC committed Nov 5, 2023
1 parent 0f056f2 commit 3dc6afa
Show file tree
Hide file tree
Showing 47 changed files with 1,153 additions and 675 deletions.
4 changes: 2 additions & 2 deletions Data Asset Sources.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ This file has been dumped from a BDS server using [CloudburstMC/ProxyPass](https
This file has been copied from the official bedrock client's assets folder:
`%MC-Root%\data\skin_packs\vanilla\*`

### data/java/mapping-1.20.json
[ViaVersion Mappings](https://github.com/ViaVersion/Mappings/blob/main/mappings/mapping-1.20.json)
### data/java/via_mappings.json
[ViaVersion Mappings](https://github.com/ViaVersion/Mappings/blob/main/mappings/mapping-1.20.2.json)

### data/java/registries.nbt
This file has been dumped from a vanilla server using a fabric mod.
Expand Down
14 changes: 14 additions & 0 deletions Update Process.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# ViaBedrock Update Process

This file lists the steps necessary to update ViaBedrock.

1. Update data assets (See `Data Asset Sources.md`) and BedrockMappingData
2. Update ProtocolConstants class
3. Update hardcoded blockstates: Search all files for `new BlockState("`
4. Replace `Types1_20_2` with the new type
5. Replace `EntityTypes1_19_4` and `Protocol1_19_4To1_19_3.class` with the new type
6. Replace `ClientboundPackets1_20_2` and `ServerboundPackets1_20_2` with the new packet enum
7. Replace `ClientboundConfigurationPackets1_20_2` and `ServerboundConfigurationPackets1_20_2` with the new packet enum
8. Update changed packet contents
9. Update rewriters
10. Done!
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ repositories {
}

dependencies {
compileOnly "com.viaversion:viaversion:4.9.0-23w42a-SNAPSHOT"
compileOnly "com.viaversion:viaversion:4.9.0-23w44a-SNAPSHOT"
compileOnly "org.yaml:snakeyaml:2.2"
compileOnly "com.google.guava:guava:32.1.3-jre"
compileOnly "io.netty:netty-handler:4.1.100.Final"
Expand All @@ -54,7 +54,7 @@ dependencies {
api("net.lenni0451.mcstructs-bedrock:forms:1.2.0") {
exclude group: "com.google.code.gson", module: "gson"
}
api("net.lenni0451.mcstructs:text:2.3.1") {
api("net.lenni0451.mcstructs:text:2.4.0-SNAPSHOT") {
exclude group: "com.google.code.gson", module: "gson"
}
api "com.vdurmont:semver4j:3.1.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@
package net.raphimc.viabedrock.api;

import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import net.raphimc.viabedrock.protocol.data.ProtocolConstants;

import java.util.ArrayList;
import java.util.List;

public class BedrockProtocolVersion {

public static final List<ProtocolVersion> PROTOCOLS = new ArrayList<>();
public static final String LATEST_BEDROCK_VERSION = "1.20.30";
public static final int PROTOCOL_ID_OVERLAP_PREVENTION_OFFSET = 1_000_000;

public static final ProtocolVersion bedrockLatest = registerBedrock(1_000_618, "Bedrock " + LATEST_BEDROCK_VERSION);
public static final ProtocolVersion bedrockLatest = registerBedrock(PROTOCOL_ID_OVERLAP_PREVENTION_OFFSET + ProtocolConstants.BEDROCK_PROTOCOL_VERSION, "Bedrock " + ProtocolConstants.BEDROCK_VERSION_NAME);

private static ProtocolVersion registerBedrock(final int version, final String name) {
final ProtocolVersion protocolVersion = ProtocolVersion.register(version, name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import com.viaversion.viaversion.api.minecraft.Position;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.protocols.protocol1_19_4to1_19_3.ClientboundPackets1_19_4;
import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.ClientboundPackets1_20_2;
import com.viaversion.viaversion.util.Pair;
import net.raphimc.viabedrock.ViaBedrock;
import net.raphimc.viabedrock.protocol.BedrockProtocol;
Expand Down Expand Up @@ -82,7 +82,7 @@ public void closeDownloadingTerrainScreen() throws Exception {
final SpawnPositionStorage spawnPositionStorage = this.user.get(SpawnPositionStorage.class);
final ChunkTracker chunkTracker = this.user.get(ChunkTracker.class);

final PacketWrapper spawnPosition = PacketWrapper.create(ClientboundPackets1_19_4.SPAWN_POSITION, this.user);
final PacketWrapper spawnPosition = PacketWrapper.create(ClientboundPackets1_20_2.SPAWN_POSITION, this.user);
spawnPosition.write(Type.POSITION1_14, spawnPositionStorage.getSpawnPosition(chunkTracker.getDimensionId())); // position
spawnPosition.write(Type.FLOAT, 0F); // angle
spawnPosition.send(BedrockProtocol.class);
Expand All @@ -93,7 +93,7 @@ public void sendPlayerPositionPacketToClient(final boolean keepRotation) throws
}

public void sendPlayerPositionPacketToClient(final boolean keepRotation, final boolean fakeTeleport) throws Exception {
final PacketWrapper playerPosition = PacketWrapper.create(ClientboundPackets1_19_4.PLAYER_POSITION, this.user);
final PacketWrapper playerPosition = PacketWrapper.create(ClientboundPackets1_20_2.PLAYER_POSITION, this.user);
this.writePlayerPositionPacketToClient(playerPosition, keepRotation, fakeTeleport);
playerPosition.send(BedrockProtocol.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_19_4;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.protocols.protocol1_19_4to1_19_3.ClientboundPackets1_19_4;
import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.ClientboundPackets1_20_2;
import net.raphimc.viabedrock.api.util.StringUtil;
import net.raphimc.viabedrock.api.util.TextUtil;
import net.raphimc.viabedrock.protocol.BedrockProtocol;
Expand All @@ -35,7 +35,7 @@ public PlayerEntity(final UserConnection user, final long uniqueId, final long r
}

public void createTeam() throws Exception {
final PacketWrapper teams = PacketWrapper.create(ClientboundPackets1_19_4.TEAMS, this.user);
final PacketWrapper teams = PacketWrapper.create(ClientboundPackets1_20_2.TEAMS, this.user);
teams.write(Type.STRING, "vb_" + this.javaId); // team name
teams.write(Type.BYTE, (byte) 0); // mode | 0 = ADD
teams.write(Type.COMPONENT, TextUtil.stringToGson("vb_" + this.javaId)); // display name
Expand All @@ -52,7 +52,7 @@ public void createTeam() throws Exception {
public void updateName(final String name) throws Exception {
this.setName(name);

final PacketWrapper teams = PacketWrapper.create(ClientboundPackets1_19_4.TEAMS, this.user);
final PacketWrapper teams = PacketWrapper.create(ClientboundPackets1_20_2.TEAMS, this.user);
teams.write(Type.STRING, "vb_" + this.javaId); // team name
teams.write(Type.BYTE, (byte) 2); // mode | 2 = UPDATE
teams.write(Type.COMPONENT, TextUtil.stringToGson("vb_" + this.javaId)); // display name
Expand All @@ -66,7 +66,7 @@ public void updateName(final String name) throws Exception {
}

public void deleteTeam() throws Exception {
final PacketWrapper teams = PacketWrapper.create(ClientboundPackets1_19_4.TEAMS, this.user);
final PacketWrapper teams = PacketWrapper.create(ClientboundPackets1_20_2.TEAMS, this.user);
teams.write(Type.STRING, "vb_" + this.javaId); // team name
teams.write(Type.BYTE, (byte) 1); // mode | 1 = REMOVE
teams.send(BedrockProtocol.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.protocols.protocol1_19_4to1_19_3.ClientboundPackets1_19_4;
import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.ClientboundPackets1_20_2;
import net.lenni0451.mcstructs.text.ATextComponent;
import net.raphimc.viabedrock.api.model.inventory.Container;
import net.raphimc.viabedrock.protocol.BedrockProtocol;
Expand Down Expand Up @@ -57,7 +57,7 @@ public void onClosed() throws Exception {
}

protected void close() throws Exception {
final PacketWrapper closeWindow = PacketWrapper.create(ClientboundPackets1_19_4.CLOSE_WINDOW, this.user);
final PacketWrapper closeWindow = PacketWrapper.create(ClientboundPackets1_20_2.CLOSE_WINDOW, this.user);
closeWindow.write(Type.UNSIGNED_BYTE, (short) this.windowId); // window id
closeWindow.send(BedrockProtocol.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.protocols.protocol1_19_4to1_19_3.ClientboundPackets1_19_4;
import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.ClientboundPackets1_20_2;
import net.raphimc.viabedrock.protocol.BedrockProtocol;

import java.util.HashMap;
Expand Down Expand Up @@ -82,7 +82,7 @@ public void removeEntry(final UserConnection user, final long scoreboardId) thro
}

public void updateEntry(final UserConnection user, final ScoreboardEntry entry, final int action) throws Exception {
final PacketWrapper updateScore = PacketWrapper.create(ClientboundPackets1_19_4.UPDATE_SCORE, user);
final PacketWrapper updateScore = PacketWrapper.create(ClientboundPackets1_20_2.UPDATE_SCORE, user);
updateScore.write(Type.STRING, entry.javaName()); // player name
updateScore.write(Type.VAR_INT, action); // action
updateScore.write(Type.STRING, this.name); // objective name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.protocols.protocol1_19_4to1_19_3.ClientboundPackets1_19_4;
import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.ClientboundPackets1_20_2;
import net.raphimc.viabedrock.protocol.BedrockProtocol;
import net.raphimc.viabedrock.protocol.model.SkinData;
import net.raphimc.viabedrock.protocol.types.primitive.ImageType;
Expand Down Expand Up @@ -51,7 +51,7 @@ public static void sendSkin(final UserConnection user, final UUID uuid, final Sk
final int chunkCount = (int) Math.ceil(skinData.length / (double) maxPayloadSize);

{
final PacketWrapper pluginMessage = PacketWrapper.create(ClientboundPackets1_19_4.PLUGIN_MESSAGE, user);
final PacketWrapper pluginMessage = PacketWrapper.create(ClientboundPackets1_20_2.PLUGIN_MESSAGE, user);
pluginMessage.write(Type.STRING, CHANNEL); // Channel
pluginMessage.write(Type.INT, MESSAGE_SKIN_INFORMATION);
pluginMessage.write(Type.INT, VERSION);
Expand All @@ -68,7 +68,7 @@ public static void sendSkin(final UserConnection user, final UUID uuid, final Sk
pluginMessage.send(BedrockProtocol.class);
}
for (int i = 0; i < chunkCount; i++) {
final PacketWrapper pluginMessage = PacketWrapper.create(ClientboundPackets1_19_4.PLUGIN_MESSAGE, user);
final PacketWrapper pluginMessage = PacketWrapper.create(ClientboundPackets1_20_2.PLUGIN_MESSAGE, user);
pluginMessage.write(Type.STRING, CHANNEL); // Channel
pluginMessage.write(Type.INT, MESSAGE_SKIN_DATA);
pluginMessage.write(Type.UUID, uuid);
Expand All @@ -83,7 +83,7 @@ public static void sendSkin(final UserConnection user, final UUID uuid, final Sk
if (skin.capeData() != null) {
final byte[] capeData = ImageType.getImageData(skin.capeData());

final PacketWrapper pluginMessage = PacketWrapper.create(ClientboundPackets1_19_4.PLUGIN_MESSAGE, user);
final PacketWrapper pluginMessage = PacketWrapper.create(ClientboundPackets1_20_2.PLUGIN_MESSAGE, user);
pluginMessage.write(Type.STRING, CHANNEL); // Channel
pluginMessage.write(Type.INT, MESSAGE_CAPE);
pluginMessage.write(Type.INT, VERSION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.protocols.base.ServerboundHandshakePackets;
import com.viaversion.viaversion.protocols.base.ServerboundLoginPackets;
import net.raphimc.viabedrock.api.BedrockProtocolVersion;
import net.raphimc.viabedrock.protocol.storage.HandshakeStorage;

public class BedrockBaseProtocol extends AbstractSimpleProtocol {
Expand All @@ -41,7 +42,7 @@ protected void registerPackets() {
public void register() {
handler(wrapper -> {
wrapper.cancel();
final int protocolVersion = wrapper.read(Type.VAR_INT) - 1_000_000; // protocol id
final int protocolVersion = wrapper.read(Type.VAR_INT) - BedrockProtocolVersion.PROTOCOL_ID_OVERLAP_PREVENTION_OFFSET; // protocol id
final String hostname = wrapper.read(Type.STRING); // hostname
final int port = wrapper.read(Type.UNSIGNED_SHORT); // port

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* This file is part of ViaBedrock - https://github.com/RaphiMC/ViaBedrock
* Copyright (C) 2023 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.api.protocol;

import com.viaversion.viaversion.api.protocol.AbstractProtocol;
import com.viaversion.viaversion.api.protocol.packet.*;

public abstract class StatelessProtocol<CU extends ClientboundPacketType, CM extends ClientboundPacketType, SM extends ServerboundPacketType, SU extends ServerboundPacketType> extends AbstractProtocol<CU, CM, SM, SU> {

public StatelessProtocol(final Class<CU> unmappedClientboundPacketType, final Class<CM> mappedClientboundPacketType, final Class<SM> mappedServerboundPacketType, final Class<SU> unmappedServerboundPacketType) {
super(unmappedClientboundPacketType, mappedClientboundPacketType, mappedServerboundPacketType, unmappedServerboundPacketType);
}

@Override
public void transform(Direction direction, State state, PacketWrapper packetWrapper) throws Exception {
if (state == State.STATUS) { // Needed for RakNet ping workaround
super.transform(direction, state, packetWrapper);
return;
}

super.transform(direction, direction == Direction.SERVERBOUND ? state : State.PLAY, packetWrapper);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* This file is part of ViaBedrock - https://github.com/RaphiMC/ViaBedrock
* Copyright (C) 2023 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.api.protocol;

import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType;
import com.viaversion.viaversion.api.protocol.packet.State;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandler;

public abstract class StatelessTransitionProtocol<CU extends ClientboundPacketType, CM extends ClientboundPacketType, SM extends ServerboundPacketType, SU extends ServerboundPacketType> extends StatelessProtocol<CU, CM, SM, SU> {

public StatelessTransitionProtocol(final Class<CU> unmappedClientboundPacketType, final Class<CM> mappedClientboundPacketType, final Class<SM> mappedServerboundPacketType, final Class<SU> unmappedServerboundPacketType) {
super(unmappedClientboundPacketType, mappedClientboundPacketType, mappedServerboundPacketType, unmappedServerboundPacketType);
}

public void registerServerboundTransition(final ServerboundPacketType unmappedPacketType, final SM mappedPacketType, final PacketHandler handler) {
this.registerServerbound(unmappedPacketType.state(), unmappedPacketType.getId(), mappedPacketType != null ? mappedPacketType.getId() : -1, wrapper -> {
wrapper.setPacketType(mappedPacketType);
if (handler != null) {
handler.handle(wrapper);
}
});
}

public void registerClientboundTransition(final CU unmappedPacketType, final Object... handlers) {
if (handlers.length % 2 != 0) throw new IllegalArgumentException("handlers.length % 2 != 0");

this.registerClientbound(unmappedPacketType.state(), unmappedPacketType.getId(), -1, wrapper -> {
final State currentState = wrapper.user().getProtocolInfo().getServerState();

for (int i = 0; i < handlers.length; i += 2) {
if (handlers[i] instanceof State) {
final State state = (State) handlers[i];
if (state != currentState) continue;
} else {
final ClientboundPacketType mappedPacketType = (ClientboundPacketType) handlers[i];
if (mappedPacketType.state() != currentState) continue;
wrapper.setPacketType(mappedPacketType);
}

final PacketHandler handler = (PacketHandler) handlers[i + 1];
if (handler != null) {
handler.handle(wrapper);
}
return;
}

throw new IllegalStateException("No handler found for packet " + unmappedPacketType + " in state " + currentState);
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.libs.gson.JsonElement;
import com.viaversion.viaversion.protocols.protocol1_19_4to1_19_3.ClientboundPackets1_19_4;
import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.ClientboundPackets1_20_2;
import net.raphimc.viabedrock.protocol.BedrockProtocol;

public class PacketFactory {

public static PacketWrapper systemChat(final UserConnection user, final JsonElement message) {
final PacketWrapper systemChat = PacketWrapper.create(ClientboundPackets1_19_4.SYSTEM_CHAT, user);
final PacketWrapper systemChat = PacketWrapper.create(ClientboundPackets1_20_2.SYSTEM_CHAT, user);
systemChat.write(Type.COMPONENT, message); // message
systemChat.write(Type.BOOLEAN, false); // overlay
return systemChat;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/raphimc/viabedrock/api/util/TextUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import net.lenni0451.mcstructs.core.TextFormatting;
import net.lenni0451.mcstructs.text.ATextComponent;
import net.lenni0451.mcstructs.text.serializer.LegacyStringDeserializer;
import net.lenni0451.mcstructs.text.serializer.TextComponentSerializer;
import net.lenni0451.mcstructs_bedrock.text.BedrockTextFormatting;
import net.raphimc.viabedrock.protocol.data.ProtocolConstants;

import java.util.HashSet;
import java.util.Optional;
Expand Down Expand Up @@ -53,7 +53,7 @@ public static String stringToJson(final String text) {
}

public static String componentToJson(final ATextComponent textComponent) {
return TextComponentSerializer.V1_19_4.serialize(textComponent);
return ProtocolConstants.JAVA_TEXT_COMPONENT_SERIALIZER.serialize(textComponent);
}

public static JsonElement stringToGson(final String text) {
Expand Down
Loading

0 comments on commit 3dc6afa

Please sign in to comment.