Skip to content

Commit 74b0bfb

Browse files
committed
Fix spectator interact in 26.1->1.21.11
1 parent 757db4f commit 74b0bfb

File tree

4 files changed

+51
-3
lines changed

4 files changed

+51
-3
lines changed

common/src/main/java/com/viaversion/viabackwards/protocol/v26_1to1_21_11/Protocol26_1To1_21_11.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.viaversion.viabackwards.protocol.v26_1to1_21_11.rewriter.ComponentRewriter26_1;
2828
import com.viaversion.viabackwards.protocol.v26_1to1_21_11.rewriter.EntityPacketRewriter26_1;
2929
import com.viaversion.viabackwards.protocol.v26_1to1_21_11.storage.DayTimeStorage;
30+
import com.viaversion.viabackwards.protocol.v26_1to1_21_11.storage.GameModeStorage;
3031
import com.viaversion.viaversion.api.connection.UserConnection;
3132
import com.viaversion.viaversion.api.minecraft.RegistryType;
3233
import com.viaversion.viaversion.api.minecraft.data.version.StructuredDataKeys1_21_11;
@@ -168,6 +169,7 @@ public void init(final UserConnection connection) {
168169
addEntityTracker(connection, new EntityTrackerBase(connection, EntityTypes1_21_11.PLAYER));
169170
addItemHasher(connection, new ItemHasherBase(this, connection));
170171
connection.put(new DayTimeStorage());
172+
connection.put(new GameModeStorage());
171173
}
172174

173175
@Override

common/src/main/java/com/viaversion/viabackwards/protocol/v26_1to1_21_11/rewriter/EntityPacketRewriter26_1.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
import com.viaversion.viabackwards.api.rewriters.EntityRewriter;
2121
import com.viaversion.viabackwards.protocol.v26_1to1_21_11.Protocol26_1To1_21_11;
22+
import com.viaversion.viabackwards.protocol.v26_1to1_21_11.storage.GameModeStorage;
23+
import com.viaversion.viaversion.api.minecraft.GameMode;
2224
import com.viaversion.viaversion.api.minecraft.Vector3d;
2325
import com.viaversion.viaversion.api.minecraft.entities.EntityType;
2426
import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_21_11;
@@ -27,6 +29,7 @@
2729
import com.viaversion.viaversion.api.type.Types;
2830
import com.viaversion.viaversion.api.type.types.version.VersionedTypes;
2931
import com.viaversion.viaversion.protocols.v1_21_11to26_1.packet.ClientboundPacket26_1;
32+
import com.viaversion.viaversion.protocols.v1_21_11to26_1.packet.ClientboundPackets26_1;
3033
import com.viaversion.viaversion.protocols.v1_21_11to26_1.packet.ServerboundPackets26_1;
3134
import com.viaversion.viaversion.protocols.v1_21_5to1_21_6.packet.ServerboundPackets1_21_6;
3235

@@ -42,13 +45,23 @@ public EntityPacketRewriter26_1(final Protocol26_1To1_21_11 protocol) {
4245

4346
@Override
4447
public void registerPackets() {
48+
protocol.appendClientbound(ClientboundPackets26_1.RESPAWN, wrapper -> {
49+
final byte gamemode = wrapper.get(Types.BYTE, 0);
50+
wrapper.user().get(GameModeStorage.class).setGameMode(gamemode);
51+
});
52+
protocol.appendClientbound(ClientboundPackets26_1.LOGIN, wrapper -> {
53+
final byte gamemode = wrapper.get(Types.BYTE, 0);
54+
wrapper.user().get(GameModeStorage.class).setGameMode(gamemode);
55+
});
56+
4557
protocol.registerServerbound(ServerboundPackets1_21_6.INTERACT, wrapper -> {
4658
wrapper.passthrough(Types.VAR_INT); // Entity ID
4759
final int action = wrapper.read(Types.VAR_INT);
4860
switch (action) {
4961
case INTERACT_ACTION -> wrapper.cancel(); // Drop "normal" interacts, as interact_at is always sent by Vanilla clients, and always sent first, with this following after
5062
case ATTACK_ACTION -> {
51-
wrapper.setPacketType(ServerboundPackets26_1.ATTACK);
63+
final boolean spectator = wrapper.user().get(GameModeStorage.class).gameMode() == GameMode.SPECTATOR.id();
64+
wrapper.setPacketType(spectator ? ServerboundPackets26_1.SPECTATE_ENTITY : ServerboundPackets26_1.ATTACK);
5265
wrapper.read(Types.BOOLEAN); // Using secondary action
5366
}
5467
case INTERACT_AT_ACTION -> {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* This file is part of ViaBackwards - https://github.com/ViaVersion/ViaBackwards
3+
* Copyright (C) 2016-2026 ViaVersion and contributors
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package com.viaversion.viabackwards.protocol.v26_1to1_21_11.storage;
19+
20+
import com.viaversion.viaversion.api.connection.StorableObject;
21+
22+
public final class GameModeStorage implements StorableObject {
23+
24+
private int gameMode = -1;
25+
26+
public int gameMode() {
27+
return gameMode;
28+
}
29+
30+
public void setGameMode(final int gameMode) {
31+
this.gameMode = gameMode;
32+
}
33+
}

gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
projectVersion=5.9.0-SNAPSHOT
22

33
# Smile emoji (note that modrinth may not have added the version on release yet)
4-
mcVersions=26.1, 1.21.11, 1.21.10, 1.21.9, 1.21.8, 1.21.7, 1.21.6, 1.21.5, 1.21.4, 1.21.3, 1.21.2, 1.21.1, 1.21, 1.20.6, 1.20.5, 1.20.4, 1.20.3, 1.20.2, 1.20.1, 1.20, 1.19.4, 1.19.3, 1.19.2, 1.19.1, 1.19, 1.18.2, 1.18.1, 1.18, 1.17.1, 1.17, 1.16.5, 1.16.4, 1.16.3, 1.16.2, 1.16.1, 1.16, 1.15.2, 1.15.1, 1.15, 1.14.4, 1.14.3, 1.14.2, 1.14.1, 1.14, 1.13.2, 1.13.1, 1.13, 1.12.2, 1.12.1, 1.12, 1.11.2, 1.11.1, 1.11, 1.10.2, 1.10.1, 1.10
5-
mcVersionRange=1.10-26.1
4+
mcVersions=26.1.1, 26.1, 1.21.11, 1.21.10, 1.21.9, 1.21.8, 1.21.7, 1.21.6, 1.21.5, 1.21.4, 1.21.3, 1.21.2, 1.21.1, 1.21, 1.20.6, 1.20.5, 1.20.4, 1.20.3, 1.20.2, 1.20.1, 1.20, 1.19.4, 1.19.3, 1.19.2, 1.19.1, 1.19, 1.18.2, 1.18.1, 1.18, 1.17.1, 1.17, 1.16.5, 1.16.4, 1.16.3, 1.16.2, 1.16.1, 1.16, 1.15.2, 1.15.1, 1.15, 1.14.4, 1.14.3, 1.14.2, 1.14.1, 1.14, 1.13.2, 1.13.1, 1.13, 1.12.2, 1.12.1, 1.12, 1.11.2, 1.11.1, 1.11, 1.10.2, 1.10.1, 1.10
5+
mcVersionRange=1.10-26.1.1
66
velocityVersion=3.4-3.5
77

88
org.gradle.configureondemand=true

0 commit comments

Comments
 (0)