Skip to content

Commit

Permalink
Start working on 23w31a
Browse files Browse the repository at this point in the history
  • Loading branch information
kennytv committed Aug 5, 2023
1 parent 2904ad5 commit e12d491
Show file tree
Hide file tree
Showing 12 changed files with 712 additions and 7 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {

allprojects {
group = "com.viaversion"
version = "4.7.1-SNAPSHOT"
version = "4.8.0-23w31a-SNAPSHOT"
description = "Allow older clients to join newer server versions."
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import com.viaversion.viabackwards.protocol.protocol1_19_3to1_19_4.Protocol1_19_3To1_19_4;
import com.viaversion.viabackwards.protocol.protocol1_19_4to1_20.Protocol1_19_4To1_20;
import com.viaversion.viabackwards.protocol.protocol1_19to1_19_1.Protocol1_19To1_19_1;
import com.viaversion.viabackwards.protocol.protocol1_20_4to1_20_2.Protocol1_20To1_20_2;
import com.viaversion.viabackwards.protocol.protocol1_9_4to1_10.Protocol1_9_4To1_10;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.protocol.ProtocolManager;
Expand All @@ -61,7 +62,7 @@

public interface ViaBackwardsPlatform {

String MINIMUM_VV_VERSION = "4.7.0";
String MINIMUM_VV_VERSION = "4.8.0";
String IMPL_VERSION = "$IMPL_VERSION";

/**
Expand Down Expand Up @@ -126,6 +127,7 @@ default void init(File dataFolder) {
protocolManager.registerProtocol(new Protocol1_19_3To1_19_4(), ProtocolVersion.v1_19_3, ProtocolVersion.v1_19_4);

protocolManager.registerProtocol(new Protocol1_19_4To1_20(), ProtocolVersion.v1_19_4, ProtocolVersion.v1_20);
protocolManager.registerProtocol(new Protocol1_20To1_20_2(), ProtocolVersion.v1_20, ProtocolVersion.v1_20_2);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public ItemRewriter(T protocol) {
super(protocol, true);
}

public ItemRewriter(T protocol, Type<Item> itemType, Type<Item[]> itemArrayType) {
super(protocol, itemType, itemArrayType, true);
}

@Override
public @Nullable Item handleItemToClient(@Nullable Item item) {
if (item == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag;
Expand All @@ -35,7 +36,11 @@ public abstract class ItemRewriterBase<C extends ClientboundPacketType, S extend
protected final boolean jsonNameFormat;

protected ItemRewriterBase(T protocol, boolean jsonNameFormat) {
super(protocol);
this(protocol, Type.FLAT_VAR_INT_ITEM, Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT, jsonNameFormat);
}

public ItemRewriterBase(T protocol, Type<Item> itemType, Type<Item[]> itemArrayType, boolean jsonNameFormat) {
super(protocol, itemType, itemArrayType);
this.jsonNameFormat = jsonNameFormat;
nbtTagName = "VB|" + protocol.getClass().getSimpleName();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public void register() {
map(Type.UNSIGNED_BYTE);
map(Type.FLAT_ITEM_ARRAY, Type.ITEM_ARRAY);

handler(itemArrayHandler(Type.ITEM_ARRAY));
handler(itemArrayToClientHandler(Type.ITEM_ARRAY));
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public void register() {
wrapper.cancel();
}
});
handler(getSpawnParticleHandler(Type.FLAT_VAR_INT_ITEM));
handler(getSpawnParticleHandler());
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public void register() {
wrapper.cancel();
}
});
handler(getSpawnParticleHandler(Type.FLAT_VAR_INT_ITEM));
handler(getSpawnParticleHandler());
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
* This file is part of ViaBackwards - https://github.com/ViaVersion/ViaBackwards
* Copyright (C) 2023 ViaVersion 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 com.viaversion.viabackwards.protocol.protocol1_20_4to1_20_2;

import com.viaversion.viabackwards.api.BackwardsProtocol;
import com.viaversion.viabackwards.protocol.protocol1_20_4to1_20_2.rewriter.BlockItemPacketRewriter1_20_2;
import com.viaversion.viabackwards.protocol.protocol1_20_4to1_20_2.rewriter.EntityPacketRewriter1_20_2;
import com.viaversion.viabackwards.protocol.protocol1_20_4to1_20_2.storage.ConfigurationPacketStorage;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.entities.Entity1_19_4Types;
import com.viaversion.viaversion.api.protocol.packet.Direction;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.packet.State;
import com.viaversion.viaversion.api.rewriter.EntityRewriter;
import com.viaversion.viaversion.api.rewriter.ItemRewriter;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.data.entity.EntityTrackerBase;
import com.viaversion.viaversion.exception.CancelException;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag;
import com.viaversion.viaversion.protocols.base.ClientboundLoginPackets;
import com.viaversion.viaversion.protocols.base.ServerboundLoginPackets;
import com.viaversion.viaversion.protocols.protocol1_19_4to1_19_3.ClientboundPackets1_19_4;
import com.viaversion.viaversion.protocols.protocol1_19_4to1_19_3.ServerboundPackets1_19_4;
import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.ClientboundConfigurationPackets1_20_2;
import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.ClientboundPackets1_20_2;
import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.ServerboundConfigurationPackets1_20_2;
import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.ServerboundPackets1_20_2;
import java.util.UUID;

public final class Protocol1_20To1_20_2 extends BackwardsProtocol<ClientboundPackets1_20_2, ClientboundPackets1_19_4, ServerboundPackets1_20_2, ServerboundPackets1_19_4> {

private final EntityPacketRewriter1_20_2 entityPacketRewriter = new EntityPacketRewriter1_20_2(this);
private final BlockItemPacketRewriter1_20_2 itemPacketRewriter = new BlockItemPacketRewriter1_20_2(this);

public Protocol1_20To1_20_2() {
super(ClientboundPackets1_20_2.class, ClientboundPackets1_19_4.class, ServerboundPackets1_20_2.class, ServerboundPackets1_19_4.class);
}

@Override
protected void registerPackets() {
super.registerPackets();
Via.getManager().debugHandler().setEnabled(true);

registerClientbound(ClientboundPackets1_20_2.SCOREBOARD_OBJECTIVE, wrapper -> {
final int slot = wrapper.read(Type.VAR_INT);
wrapper.write(Type.BYTE, (byte) slot);
});

registerClientbound(State.LOGIN, ClientboundLoginPackets.GAME_PROFILE.getId(), ClientboundLoginPackets.GAME_PROFILE.getId(), wrapper -> {
final ServerboundLoginPackets ackPacket = ServerboundLoginPackets.LOGIN_ACKNOWLEDGED;
wrapper.create(ackPacket).sendToServer(Protocol1_20To1_20_2.class);

// We can't set the internal state to configuration here as protocols down the line will expect the state to be play
wrapper.user().put(new ConfigurationPacketStorage());
System.out.println(wrapper.user().getProtocolInfo().getState());
});

registerClientbound(State.CONFIGURATION, ClientboundConfigurationPackets1_20_2.FINISH_CONFIGURATION.getId(), ClientboundConfigurationPackets1_20_2.FINISH_CONFIGURATION.getId(), wrapper -> {
wrapper.cancel();
wrapper.create(ServerboundConfigurationPackets1_20_2.FINISH_CONFIGURATION).sendToServer(Protocol1_20To1_20_2.class);
wrapper.user().getProtocolInfo().setState(State.PLAY);
});

registerServerbound(State.LOGIN, ServerboundLoginPackets.HELLO.getId(), ServerboundLoginPackets.HELLO.getId(), wrapper -> {
wrapper.passthrough(Type.STRING); // Name

// TODO Bad
final UUID uuid = wrapper.read(Type.OPTIONAL_UUID);
wrapper.write(Type.UUID, uuid != null ? uuid : new UUID(0, 0));
});

cancelClientbound(ClientboundPackets1_20_2.START_CONFIGURATION); // TODO

// Some can be directly remapped to play packets, others need to be queued
registerClientbound(State.CONFIGURATION, ClientboundConfigurationPackets1_20_2.DISCONNECT.getId(), ClientboundPackets1_19_4.DISCONNECT.getId());
registerClientbound(State.CONFIGURATION, ClientboundConfigurationPackets1_20_2.KEEP_ALIVE.getId(), ClientboundPackets1_19_4.KEEP_ALIVE.getId());
registerClientbound(State.CONFIGURATION, ClientboundConfigurationPackets1_20_2.RESOURCE_PACK.getId(), ClientboundPackets1_19_4.RESOURCE_PACK.getId());
registerClientbound(State.CONFIGURATION, ClientboundConfigurationPackets1_20_2.REGISTRY_DATA.getId(), -1, wrapper -> {
wrapper.cancel();

final CompoundTag registry = wrapper.read(Type.NAMELESS_NBT);
entityPacketRewriter.trackBiomeSize(wrapper.user(), registry);
entityPacketRewriter.cacheDimensionData(wrapper.user(), registry);
wrapper.user().get(ConfigurationPacketStorage.class).setRegistry(registry);
});
registerClientbound(State.CONFIGURATION, ClientboundConfigurationPackets1_20_2.UPDATE_ENABLED_FEATURES.getId(), -1, wrapper -> {
wrapper.cancel();

final String[] enabledFeatures = wrapper.read(Type.STRING_ARRAY);
wrapper.user().get(ConfigurationPacketStorage.class).setEnabledFeatures(enabledFeatures);
});
registerClientbound(State.CONFIGURATION, ClientboundConfigurationPackets1_20_2.UPDATE_TAGS.getId(), -1, wrapper -> {
wrapper.cancel();
wrapper.user().get(ConfigurationPacketStorage.class).addRawPacket(wrapper, ClientboundPackets1_19_4.TAGS);
});
registerClientbound(State.CONFIGURATION, ClientboundConfigurationPackets1_20_2.CUSTOM_PAYLOAD.getId(), -1, wrapper -> {
wrapper.cancel();
wrapper.user().get(ConfigurationPacketStorage.class).addRawPacket(wrapper, ClientboundPackets1_19_4.PLUGIN_MESSAGE);
});
}

@Override
public void transform(final Direction direction, final State state, final PacketWrapper wrapper) throws Exception {
final ConfigurationPacketStorage configurationPacketStorage = wrapper.user().get(ConfigurationPacketStorage.class);
if (configurationPacketStorage == null) {
super.transform(direction, state, wrapper);
return;
}
if (direction == Direction.CLIENTBOUND) {
super.transform(direction, State.CONFIGURATION, wrapper);
return;
}

// Map some of the packets to their configuration counterparts
final int id = wrapper.getId();
if (id == ServerboundPackets1_19_4.PLUGIN_MESSAGE.getId()) {
wrapper.setPacketType(ServerboundConfigurationPackets1_20_2.CUSTOM_PAYLOAD);
} else if (id == ServerboundPackets1_19_4.KEEP_ALIVE.getId()) {
wrapper.setPacketType(ServerboundConfigurationPackets1_20_2.KEEP_ALIVE);
} else if (id == ServerboundPackets1_19_4.PONG.getId()) {
wrapper.setPacketType(ServerboundConfigurationPackets1_20_2.PONG);
} else if (id == ServerboundPackets1_19_4.RESOURCE_PACK_STATUS.getId()) {
wrapper.setPacketType(ServerboundConfigurationPackets1_20_2.RESOURCE_PACK);
} else {
// Can't do
// TODO Queue
System.out.println("Cancelling: " + state + " - " + wrapper.getPacketType() + " " + wrapper.getId());
throw CancelException.generate();
}
}

@Override
public void init(final UserConnection connection) {
addEntityTracker(connection, new EntityTrackerBase(connection, Entity1_19_4Types.PLAYER));
}

@Override
public EntityRewriter<Protocol1_20To1_20_2> getEntityRewriter() {
return entityPacketRewriter;
}

@Override
public ItemRewriter<Protocol1_20To1_20_2> getItemRewriter() {
return itemPacketRewriter;
}
}
Loading

0 comments on commit e12d491

Please sign in to comment.