Skip to content

Commit b376b52

Browse files
Refactor dimension switch handling across all protocols (#884)
1 parent 36baf6e commit b376b52

26 files changed

+78
-75
lines changed

common/src/main/java/com/viaversion/viabackwards/api/rewriters/EntityRewriter.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ public PacketHandler worldTrackerHandlerByKey() {
127127
String world = wrapper.get(Types.STRING, 1);
128128
if (tracker.currentWorld() != null && !tracker.currentWorld().equals(world)) {
129129
tracker.clearEntities();
130-
tracker.trackClientEntity();
131130
}
132131
tracker.setCurrentWorld(world);
133132
};

common/src/main/java/com/viaversion/viabackwards/api/rewriters/EntityRewriterBase.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.viaversion.viabackwards.api.entities.storage.EntityReplacement;
2424
import com.viaversion.viabackwards.api.entities.storage.WrappedEntityData;
2525
import com.viaversion.viaversion.api.connection.UserConnection;
26+
import com.viaversion.viaversion.api.data.entity.EntityTracker;
2627
import com.viaversion.viaversion.api.data.entity.StoredEntityData;
2728
import com.viaversion.viaversion.api.data.entity.TrackedEntity;
2829
import com.viaversion.viaversion.api.minecraft.ClientWorld;
@@ -238,15 +239,27 @@ protected PacketHandler getTrackerHandler() {
238239
return getTrackerHandler(Types.VAR_INT, 1);
239240
}
240241

241-
protected PacketHandler getTrackerHandler(EntityType entityType, Type<? extends Number> intType) {
242-
return wrapper -> tracker(wrapper.user()).addEntity((int) wrapper.get(intType, 0), entityType);
242+
protected PacketHandler getTrackerHandler(EntityType entityType) {
243+
return wrapper -> tracker(wrapper.user()).addEntity((int) wrapper.get(Types.VAR_INT, 0), entityType);
244+
}
245+
246+
protected PacketHandler getPlayerTrackerHandler() {
247+
return wrapper -> {
248+
final int entityId = wrapper.get(Types.INT, 0);
249+
250+
final EntityTracker tracker = tracker(wrapper.user());
251+
tracker(wrapper.user()).setClientEntityId(entityId);
252+
tracker.addEntity(entityId, tracker.playerType());
253+
};
243254
}
244255

245256
protected PacketHandler getDimensionHandler(int index) {
246257
return wrapper -> {
247-
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
258+
ClientWorld clientWorld = wrapper.user().getClientWorld(this.protocol.getClass());
248259
int dimensionId = wrapper.get(Types.INT, index);
249-
clientWorld.setEnvironment(dimensionId);
260+
if (clientWorld.setEnvironment(dimensionId)) {
261+
tracker(wrapper.user()).clearEntities();
262+
}
250263
};
251264
}
252265
}

common/src/main/java/com/viaversion/viabackwards/api/rewriters/LegacyEntityRewriter.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,10 @@ protected void registerRespawn(C packetType) {
6666
public void register() {
6767
map(Types.INT);
6868
handler(wrapper -> {
69-
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
70-
clientWorld.setEnvironment(wrapper.get(Types.INT, 0));
69+
ClientWorld clientWorld = wrapper.user().getClientWorld(protocol.getClass());
70+
if (clientWorld.setEnvironment(wrapper.get(Types.INT, 0))) {
71+
tracker(wrapper.user()).clearEntities();
72+
}
7173
});
7274
}
7375
});
@@ -81,8 +83,8 @@ public void register() {
8183
map(Types.UNSIGNED_BYTE); // 1 - Gamemode
8284
map(Types.INT); // 2 - Dimension
8385
handler(wrapper -> {
84-
ClientWorld clientChunks = wrapper.user().get(ClientWorld.class);
85-
clientChunks.setEnvironment(wrapper.get(Types.INT, 1));
86+
ClientWorld clientWorld = wrapper.user().getClientWorld(protocol.getClass());
87+
clientWorld.setEnvironment(wrapper.get(Types.INT, 1));
8688

8789
final int entityId = wrapper.get(Types.INT, 0);
8890
addTrackedEntity(wrapper, entityId, playerType);

common/src/main/java/com/viaversion/viabackwards/protocol/v1_10to1_9_3/Protocol1_10To1_9_3.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,8 @@ public void register() {
9898

9999
@Override
100100
public void init(UserConnection user) {
101-
if (!user.has(ClientWorld.class)) {
102-
user.put(new ClientWorld());
103-
}
104-
105101
user.addEntityTracker(this.getClass(), new EntityTrackerBase(user, EntityTypes1_10.EntityType.PLAYER));
102+
user.addClientWorld(this.getClass(), new ClientWorld());
106103
}
107104

108105
@Override

common/src/main/java/com/viaversion/viabackwards/protocol/v1_10to1_9_3/rewriter/BlockItemPacketRewriter1_10.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ protected void registerPackets() {
5050
registerSetCreativeModeSlot(ServerboundPackets1_9_3.SET_CREATIVE_MODE_SLOT);
5151

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

5555
ChunkType1_9_3 type = ChunkType1_9_3.forEnvironment(clientWorld.getEnvironment());
5656
Chunk chunk = wrapper.passthrough(type);

common/src/main/java/com/viaversion/viabackwards/protocol/v1_11_1to1_11/Protocol1_11_1To1_11.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,8 @@ public Protocol1_11_1To1_11() {
3939

4040
@Override
4141
public void init(UserConnection user) {
42-
if (!user.has(ClientWorld.class)) {
43-
user.put(new ClientWorld());
44-
}
45-
4642
user.addEntityTracker(this.getClass(), new EntityTrackerBase(user, EntityTypes1_11.EntityType.PLAYER));
43+
user.addClientWorld(this.getClass(), new ClientWorld());
4744
}
4845

4946
@Override

common/src/main/java/com/viaversion/viabackwards/protocol/v1_11to1_10/Protocol1_11To1_10.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,8 @@ protected void registerPackets() {
6262

6363
@Override
6464
public void init(UserConnection user) {
65-
if (!user.has(ClientWorld.class)) {
66-
user.put(new ClientWorld());
67-
}
68-
6965
user.addEntityTracker(this.getClass(), new EntityTrackerBase(user, EntityTypes1_11.EntityType.PLAYER));
66+
user.addClientWorld(this.getClass(), new ClientWorld());
7067

7168
if (!user.has(WindowTracker.class)) {
7269
user.put(new WindowTracker());

common/src/main/java/com/viaversion/viabackwards/protocol/v1_11to1_10/rewriter/BlockItemPacketRewriter1_11.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public void register() {
147147
registerSetCreativeModeSlot(ServerboundPackets1_9_3.SET_CREATIVE_MODE_SLOT);
148148

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

152152
ChunkType1_9_3 type = ChunkType1_9_3.forEnvironment(clientWorld.getEnvironment()); // Use the 1.10 Chunk type since nothing changed.
153153
Chunk chunk = wrapper.passthrough(type);

common/src/main/java/com/viaversion/viabackwards/protocol/v1_12to1_11_1/Protocol1_12To1_11_1.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,8 @@ protected void registerPackets() {
7171

7272
@Override
7373
public void init(UserConnection user) {
74-
if (!user.has(ClientWorld.class)) {
75-
user.put(new ClientWorld());
76-
}
77-
7874
user.addEntityTracker(this.getClass(), new EntityTrackerBase(user, EntityTypes1_12.EntityType.PLAYER));
75+
user.addClientWorld(this.getClass(), new ClientWorld());
7976

8077
user.put(new ShoulderTracker(user));
8178
}

common/src/main/java/com/viaversion/viabackwards/protocol/v1_12to1_11_1/rewriter/BlockItemPacketRewriter1_12.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public void register() {
131131
registerSetCreativeModeSlot(ServerboundPackets1_9_3.SET_CREATIVE_MODE_SLOT);
132132

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

136136
ChunkType1_9_3 type = ChunkType1_9_3.forEnvironment(clientWorld.getEnvironment()); // Use the 1.9.4 Chunk type since nothing changed.
137137
Chunk chunk = wrapper.passthrough(type);

0 commit comments

Comments
 (0)