Skip to content

Commit

Permalink
Refactor dimension switch handling across all protocols
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianMichael committed Sep 14, 2024
1 parent 0bac920 commit 084bded
Show file tree
Hide file tree
Showing 25 changed files with 79 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.viaversion.viabackwards.api.entities.storage.EntityReplacement;
import com.viaversion.viabackwards.api.entities.storage.WrappedEntityData;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.data.entity.EntityTracker;
import com.viaversion.viaversion.api.data.entity.StoredEntityData;
import com.viaversion.viaversion.api.data.entity.TrackedEntity;
import com.viaversion.viaversion.api.minecraft.ClientWorld;
Expand Down Expand Up @@ -238,15 +239,27 @@ protected PacketHandler getTrackerHandler() {
return getTrackerHandler(Types.VAR_INT, 1);
}

protected PacketHandler getTrackerHandler(EntityType entityType, Type<? extends Number> intType) {
return wrapper -> tracker(wrapper.user()).addEntity((int) wrapper.get(intType, 0), entityType);
protected PacketHandler getTrackerHandler(EntityType entityType) {
return wrapper -> tracker(wrapper.user()).addEntity((int) wrapper.get(Types.VAR_INT, 0), entityType);
}

protected PacketHandler getDimensionHandler(int index) {
protected PacketHandler getPlayerTrackerHandler() {
return wrapper -> {
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
int dimensionId = wrapper.get(Types.INT, index);
clientWorld.setEnvironment(dimensionId);
final int entityId = wrapper.get(Types.INT, 0);

final EntityTracker tracker = tracker(wrapper.user());
tracker(wrapper.user()).setClientEntityId(entityId);
tracker.addEntity(entityId, tracker.playerType());
};
}

protected PacketHandler getDimensionHandler() {
return wrapper -> {
ClientWorld clientWorld = wrapper.user().getClientWorld(this.protocol.getClass());
int dimensionId = wrapper.get(Types.INT, 1);
if (clientWorld.setEnvironment(dimensionId)) {
onDimensionChange(wrapper.user());
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ protected void registerRespawn(C packetType) {
public void register() {
map(Types.INT);
handler(wrapper -> {
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
clientWorld.setEnvironment(wrapper.get(Types.INT, 0));
ClientWorld clientWorld = wrapper.user().getClientWorld(protocol.getClass());
if (clientWorld.setEnvironment(wrapper.get(Types.INT, 0))) {
onDimensionChange(wrapper.user());
}
});
}
});
Expand All @@ -81,8 +83,8 @@ public void register() {
map(Types.UNSIGNED_BYTE); // 1 - Gamemode
map(Types.INT); // 2 - Dimension
handler(wrapper -> {
ClientWorld clientChunks = wrapper.user().get(ClientWorld.class);
clientChunks.setEnvironment(wrapper.get(Types.INT, 1));
ClientWorld clientWorld = wrapper.user().getClientWorld(protocol.getClass());
clientWorld.setEnvironment(wrapper.get(Types.INT, 1));

final int entityId = wrapper.get(Types.INT, 0);
addTrackedEntity(wrapper, entityId, playerType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,8 @@ public void register() {

@Override
public void init(UserConnection user) {
if (!user.has(ClientWorld.class)) {
user.put(new ClientWorld());
}

user.addEntityTracker(this.getClass(), new EntityTrackerBase(user, EntityTypes1_10.EntityType.PLAYER));
user.addClientWorld(this.getClass(), new ClientWorld());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected void registerPackets() {
registerSetCreativeModeSlot(ServerboundPackets1_9_3.SET_CREATIVE_MODE_SLOT);

protocol.registerClientbound(ClientboundPackets1_9_3.LEVEL_CHUNK, wrapper -> {
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
ClientWorld clientWorld = wrapper.user().getClientWorld(Protocol1_10To1_9_3.class);

ChunkType1_9_3 type = ChunkType1_9_3.forEnvironment(clientWorld.getEnvironment());
Chunk chunk = wrapper.passthrough(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,8 @@ public Protocol1_11_1To1_11() {

@Override
public void init(UserConnection user) {
if (!user.has(ClientWorld.class)) {
user.put(new ClientWorld());
}

user.addEntityTracker(this.getClass(), new EntityTrackerBase(user, EntityTypes1_11.EntityType.PLAYER));
user.addClientWorld(this.getClass(), new ClientWorld());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,8 @@ protected void registerPackets() {

@Override
public void init(UserConnection user) {
if (!user.has(ClientWorld.class)) {
user.put(new ClientWorld());
}

user.addEntityTracker(this.getClass(), new EntityTrackerBase(user, EntityTypes1_11.EntityType.PLAYER));
user.addClientWorld(this.getClass(), new ClientWorld());

if (!user.has(WindowTracker.class)) {
user.put(new WindowTracker());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public void register() {
registerSetCreativeModeSlot(ServerboundPackets1_9_3.SET_CREATIVE_MODE_SLOT);

protocol.registerClientbound(ClientboundPackets1_9_3.LEVEL_CHUNK, wrapper -> {
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
ClientWorld clientWorld = wrapper.user().getClientWorld(Protocol1_11To1_10.class);

ChunkType1_9_3 type = ChunkType1_9_3.forEnvironment(clientWorld.getEnvironment()); // Use the 1.10 Chunk type since nothing changed.
Chunk chunk = wrapper.passthrough(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,8 @@ protected void registerPackets() {

@Override
public void init(UserConnection user) {
if (!user.has(ClientWorld.class)) {
user.put(new ClientWorld());
}

user.addEntityTracker(this.getClass(), new EntityTrackerBase(user, EntityTypes1_12.EntityType.PLAYER));
user.addClientWorld(this.getClass(), new ClientWorld());

user.put(new ShoulderTracker(user));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public void register() {
registerSetCreativeModeSlot(ServerboundPackets1_9_3.SET_CREATIVE_MODE_SLOT);

protocol.registerClientbound(ClientboundPackets1_12.LEVEL_CHUNK, wrapper -> {
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
ClientWorld clientWorld = wrapper.user().getClientWorld(Protocol1_12To1_11_1.class);

ChunkType1_9_3 type = ChunkType1_9_3.forEnvironment(clientWorld.getEnvironment()); // Use the 1.9.4 Chunk type since nothing changed.
Chunk chunk = wrapper.passthrough(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,8 @@ public void register() {
map(Types.UNSIGNED_BYTE); // 1 - Gamemode
map(Types.INT); // 2 - Dimension

handler(getTrackerHandler(EntityTypes1_12.EntityType.PLAYER, Types.INT));

handler(getDimensionHandler(1));
handler(getDimensionHandler());
handler(getPlayerTrackerHandler());

handler(wrapper -> {
ShoulderTracker tracker = wrapper.user().get(ShoulderTracker.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,7 @@ public void register() {
@Override
public void init(UserConnection user) {
user.addEntityTracker(getClass(), new EntityTrackerBase(user, EntityTypes1_13.EntityType.PLAYER));

if (!user.has(ClientWorld.class)) {
user.put(new ClientWorld());
}
user.addClientWorld(getClass(), new ClientWorld());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static void register(Protocol1_13_1To1_13 protocol) {
BlockRewriter<ClientboundPackets1_13> blockRewriter = BlockRewriter.legacy(protocol);

protocol.registerClientbound(ClientboundPackets1_13.LEVEL_CHUNK, wrapper -> {
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
ClientWorld clientWorld = wrapper.user().getClientWorld(Protocol1_13_1To1_13.class);
Chunk chunk = wrapper.passthrough(ChunkType1_13.forEnvironment(clientWorld.getEnvironment()));

blockRewriter.handleChunk(chunk);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,8 @@ protected void registerPackets() {

@Override
public void init(UserConnection user) {
if (!user.has(ClientWorld.class)) {
user.put(new ClientWorld());
}

user.addEntityTracker(this.getClass(), new EntityTrackerBase(user, EntityTypes1_13.EntityType.PLAYER));
user.addClientWorld(this.getClass(), new ClientWorld());

user.put(new BackwardsBlockStorage());
user.put(new TabCompleteStorage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public void register() {
});

protocol.registerClientbound(ClientboundPackets1_13.LEVEL_CHUNK, wrapper -> {
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
ClientWorld clientWorld = wrapper.user().getClientWorld(Protocol1_13To1_12_2.class);

ChunkType1_9_3 type_old = ChunkType1_9_3.forEnvironment(clientWorld.getEnvironment());
ChunkType1_13 type = ChunkType1_13.forEnvironment(clientWorld.getEnvironment());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.viaversion.viabackwards.protocol.v1_13to1_12_2.storage.BackwardsBlockStorage;
import com.viaversion.viabackwards.protocol.v1_13to1_12_2.storage.NoteBlockStorage;
import com.viaversion.viabackwards.protocol.v1_13to1_12_2.storage.PlayerPositionStorage1_13;
import com.viaversion.viaversion.api.minecraft.ClientWorld;
import com.viaversion.viaversion.api.minecraft.Particle;
import com.viaversion.viaversion.api.minecraft.entities.EntityType;
import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_13;
Expand Down Expand Up @@ -172,7 +173,7 @@ public void register() {
map(Types.VAR_INT);
map(Types.UUID);

handler(getTrackerHandler(EntityTypes1_13.EntityType.PAINTING, Types.VAR_INT));
handler(getTrackerHandler(EntityTypes1_13.EntityType.PAINTING));
handler(wrapper -> {
int motive = wrapper.read(Types.VAR_INT);
String title = PaintingNames1_13.getStringId(motive);
Expand All @@ -188,10 +189,15 @@ public void register() {
public void register() {
map(Types.INT); // 0 - Dimension ID

handler(getDimensionHandler(0));
handler(wrapper -> {
wrapper.user().get(BackwardsBlockStorage.class).clear();
wrapper.user().get(NoteBlockStorage.class).clear();
ClientWorld clientWorld = wrapper.user().getClientWorld(Protocol1_13To1_12_2.class);
int dimensionId = wrapper.get(Types.INT, 0);

if (clientWorld.setEnvironment(dimensionId)) {
onDimensionChange(wrapper.user());
wrapper.user().get(BackwardsBlockStorage.class).clear();
wrapper.user().get(NoteBlockStorage.class).clear();
}
});
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,8 @@ private static boolean isSet(int mask, int i) {

@Override
public void init(UserConnection user) {
if (!user.has(ClientWorld.class)) {
user.put(new ClientWorld());
}

user.addEntityTracker(this.getClass(), new EntityTrackerBase(user, EntityTypes1_14.PLAYER));
user.addClientWorld(this.getClass(), new ClientWorld());

if (!user.has(ChunkLightStorage.class)) {
user.put(new ChunkLightStorage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ public void register() {
});

protocol.registerClientbound(ClientboundPackets1_14.LEVEL_CHUNK, wrapper -> {
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
ClientWorld clientWorld = wrapper.user().getClientWorld(Protocol1_14To1_13_2.class);
Chunk chunk = wrapper.read(ChunkType1_14.TYPE);
wrapper.write(ChunkType1_13.forEnvironment(clientWorld.getEnvironment()), chunk);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,8 @@ public void register() {
map(Types.UNSIGNED_BYTE); // 1 - Gamemode
map(Types.INT); // 2 - Dimension

handler(getTrackerHandler(EntityTypes1_14.PLAYER, Types.INT));
handler(getDimensionHandler(1));
handler(getDimensionHandler());
handler(getPlayerTrackerHandler());
handler(wrapper -> {
short difficulty = wrapper.user().get(DifficultyStorage.class).getDifficulty();
wrapper.write(Types.UNSIGNED_BYTE, difficulty);
Expand All @@ -329,14 +329,16 @@ public void register() {
map(Types.INT); // 0 - Dimension ID

handler(wrapper -> {
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
ClientWorld clientWorld = wrapper.user().getClientWorld(Protocol1_14To1_13_2.class);
int dimensionId = wrapper.get(Types.INT, 0);
clientWorld.setEnvironment(dimensionId);

if (clientWorld.setEnvironment(dimensionId)) {
onDimensionChange(wrapper.user());
wrapper.user().get(ChunkLightStorage.class).clear();
}

short difficulty = wrapper.user().get(DifficultyStorage.class).getDifficulty();
wrapper.write(Types.UNSIGNED_BYTE, difficulty);

wrapper.user().get(ChunkLightStorage.class).clear();
});
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ private int toEffectCoordinate(float coordinate) {

@Override
public void init(UserConnection user) {
user.put(new ImmediateRespawnStorage());
user.addEntityTracker(getClass(), new EntityTrackerBase(user, EntityTypes1_15.PLAYER));
user.put(new ImmediateRespawnStorage());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public void register() {
public void register() {
map(Types.INT);
read(Types.LONG); // Seed
handler(getDimensionHandler());
}
});

Expand All @@ -113,7 +114,7 @@ public void register() {
map(Types.VAR_INT); // 5 - View Distance
map(Types.BOOLEAN); // 6 - Reduce Debug Info

handler(getTrackerHandler(EntityTypes1_15.PLAYER, Types.INT));
handler(getPlayerTrackerHandler());

handler(wrapper -> {
boolean immediateRespawn = !wrapper.read(Types.BOOLEAN); // Inverted
Expand All @@ -138,7 +139,7 @@ public void register() {
map(Types.BYTE); // 6 - Pitch
handler(wrapper -> wrapper.write(Types1_14.ENTITY_DATA_LIST, new ArrayList<>())); // Entity data is no longer sent in 1.15, so we have to send an empty one

handler(getTrackerHandler(EntityTypes1_15.PLAYER, Types.VAR_INT));
handler(getTrackerHandler(EntityTypes1_15.PLAYER));
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,15 @@ public void register() {
wrapper.write(Types.UNSIGNED_BYTE, (short) Math.min(maxPlayers, 255));
});
// ...
handler(getTrackerHandler(EntityTypes1_16_2.PLAYER, Types.INT));
handler(getPlayerTrackerHandler());
}
});

protocol.registerClientbound(ClientboundPackets1_16_2.RESPAWN, wrapper -> {
CompoundTag dimensionData = wrapper.read(Types.NAMED_COMPOUND_TAG);
wrapper.write(Types.STRING, getDimensionFromData(dimensionData));

onDimensionChange(wrapper.user());
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,12 @@ public void register() {

@Override
public void init(UserConnection user) {
if (!user.has(ClientWorld.class)) {
user.put(new ClientWorld());
}
user.addEntityTracker(this.getClass(), new EntityTrackerBase(user, EntityTypes1_16.PLAYER));
user.addClientWorld(this.getClass(), new ClientWorld());

user.put(new PlayerSneakStorage());
user.put(new WorldNameTracker());
user.put(new PlayerAttributesStorage());
user.addEntityTracker(this.getClass(), new EntityTrackerBase(user, EntityTypes1_16.PLAYER));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void register() {
wrapper.read(Types.BYTE); // Previous gamemode

// Grab client world
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
ClientWorld clientWorld = wrapper.user().getClientWorld(Protocol1_16To1_15_2.class);
int dimension = wrapper.get(Types.INT, 0);

// Send a dummy respawn with a different dimension if the world name was different and the same dimension was used
Expand All @@ -127,7 +127,9 @@ public void register() {
packet.send(Protocol1_16To1_15_2.class);
}

clientWorld.setEnvironment(dimension);
if (clientWorld.setEnvironment(dimension)) {
onDimensionChange(wrapper.user());
}

wrapper.write(Types.STRING, "default"); // Level type
wrapper.read(Types.BOOLEAN); // Debug
Expand Down Expand Up @@ -168,9 +170,8 @@ public void register() {
map(Types.LONG); // Seed
map(Types.UNSIGNED_BYTE); // Max players
handler(wrapper -> {
ClientWorld clientChunks = wrapper.user().get(ClientWorld.class);
clientChunks.setEnvironment(wrapper.get(Types.INT, 1));
tracker(wrapper.user()).addEntity(wrapper.get(Types.INT, 0), EntityTypes1_16.PLAYER);
ClientWorld clientWorld = wrapper.user().getClientWorld(Protocol1_16To1_15_2.class);
clientWorld.setEnvironment(wrapper.get(Types.INT, 1));

wrapper.write(Types.STRING, "default"); // Level type

Expand Down
Loading

0 comments on commit 084bded

Please sign in to comment.