Skip to content

Commit 4f13bdc

Browse files
committed
Fix A Few Issues & Disable OffHand Key-Bind
1 parent 4f07af1 commit 4f13bdc

File tree

1 file changed

+78
-52
lines changed

1 file changed

+78
-52
lines changed

src/main/java/btw/lowercase/viasnapshot/protocol/v15w31ato1_8/rewriter/PacketRewriter15w31a.java

Lines changed: 78 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import com.viaversion.viaversion.api.type.types.version.Types1_8;
1111
import com.viaversion.viaversion.libs.gson.Gson;
1212
import com.viaversion.viaversion.libs.gson.GsonBuilder;
13-
import com.viaversion.viaversion.libs.gson.JsonObject;
13+
import com.viaversion.viaversion.protocols.v1_8to1_9.Protocol1_8To1_9;
1414
import com.viaversion.viaversion.protocols.v1_8to1_9.packet.ClientboundPackets1_8;
1515
import com.viaversion.viaversion.protocols.v1_8to1_9.packet.ServerboundPackets1_8;
1616

@@ -19,14 +19,37 @@
1919

2020
// https://wiki.vg/index.php?title=Pre-release_protocol&direction=prev&oldid=6740
2121
public class PacketRewriter15w31a {
22-
private static Gson gson = new GsonBuilder().setLenient().create();
22+
private static final Gson gson = new GsonBuilder().setLenient().create();
2323

2424
public static void register(final Protocol15w31a_To1_8 protocol) {
25-
// TODO: fix 1.8.x, weird json issue / race
26-
// TODO: ignore status 6 in packet 0x07 (swap offhand)
2725
// NOTE/TODO: Entity action no longer sends 6 for open inventory
2826
// TODO: client status contains open inventory now, needs to send entity action to 1.8 server
2927

28+
protocol.registerClientbound(ClientboundPackets1_8.CHAT, new PacketHandlers() {
29+
@Override
30+
protected void register() {
31+
map(Types.STRING, Protocol1_8To1_9.STRING_TO_JSON);
32+
}
33+
});
34+
35+
protocol.registerClientbound(ClientboundPackets1_8.SET_EQUIPPED_ITEM, new PacketHandlers() {
36+
@Override
37+
protected void register() {
38+
map(Types.VAR_INT); // Entity ID
39+
handler(wrapper -> {
40+
int slot = wrapper.read(Types.SHORT); // Slot
41+
if (slot > 0) {
42+
slot++;
43+
}
44+
// else {
45+
// slot = 1; // TODO: possible config option to make everyone offhand
46+
// }
47+
wrapper.write(Types.BYTE, (byte) slot);
48+
});
49+
map(Types.ITEM1_8); // Item
50+
}
51+
});
52+
3053
protocol.registerClientbound(ClientboundPackets1_8.ADD_PLAYER, new PacketHandlers() {
3154
@Override
3255
protected void register() {
@@ -39,10 +62,7 @@ protected void register() {
3962
map(Types.BYTE); // Pitch
4063
read(Types.SHORT); // Hand Stack ID? (1.8 server)
4164
map(Types1_8.ENTITY_DATA_LIST);
42-
// TODO: Fix entity data
43-
handler(wrapper -> {
44-
wrapper.set(Types1_8.ENTITY_DATA_LIST, 0, List.of());
45-
});
65+
// TODO: Fix errors in console about unknown/wrong entity data types
4666
}
4767
});
4868

@@ -95,24 +115,36 @@ protected void register() {
95115
}
96116
});
97117

98-
protocol.registerClientbound(ClientboundPackets1_8.SET_EQUIPPED_ITEM, new PacketHandlers() {
118+
protocol.registerClientbound(ClientboundPackets1_8.OPEN_SCREEN, new PacketHandlers() {
99119
@Override
100120
protected void register() {
101-
map(Types.VAR_INT); // Entity ID
102-
handler(wrapper -> {
103-
int slot = wrapper.read(Types.SHORT); // Slot
104-
if (slot > 0) {
105-
slot++;
106-
}
107-
// else {
108-
// slot = 1; // TODO: possible config option to make everyone offhand
109-
// }
110-
wrapper.write(Types.BYTE, (byte) slot);
111-
});
112-
map(Types.ITEM1_8); // Item
121+
map(Types.UNSIGNED_BYTE); // Window id
122+
map(Types.STRING); // Window type
123+
map(Types.STRING, Protocol1_8To1_9.STRING_TO_JSON); // Window title
124+
map(Types.UNSIGNED_BYTE); // Number of slots
125+
}
126+
});
127+
128+
protocol.registerClientbound(ClientboundPackets1_8.UPDATE_SIGN, new PacketHandlers() {
129+
@Override
130+
protected void register() {
131+
map(Types.BLOCK_POSITION1_8); // Block Position
132+
map(Types.STRING, Protocol1_8To1_9.STRING_TO_JSON); // Line 1
133+
map(Types.STRING, Protocol1_8To1_9.STRING_TO_JSON); // Line 2
134+
map(Types.STRING, Protocol1_8To1_9.STRING_TO_JSON); // Line 3
135+
map(Types.STRING, Protocol1_8To1_9.STRING_TO_JSON); // Line 4
136+
}
137+
});
138+
139+
// TODO: Issues in 1.21?
140+
protocol.registerClientbound(ClientboundPackets1_8.TAB_LIST, new PacketHandlers() {
141+
public void register() {
142+
map(Types.STRING, Protocol1_8To1_9.STRING_TO_JSON);
143+
map(Types.STRING, Protocol1_8To1_9.STRING_TO_JSON);
113144
}
114145
});
115146

147+
// TODO: Issues in 1.21?
116148
protocol.registerServerbound(ServerboundPackets15w31a.INTERACT, new PacketHandlers() {
117149
@Override
118150
public void register() {
@@ -132,6 +164,19 @@ public void register() {
132164
}
133165
});
134166

167+
protocol.registerServerbound(ServerboundPackets15w31a.PLAYER_ACTION, new PacketHandlers() {
168+
@Override
169+
protected void register() {
170+
map(Types.BYTE);
171+
handler(wrapper -> {
172+
final byte status = wrapper.get(Types.BYTE, 0);
173+
if (status == 6) {
174+
wrapper.cancel(); // Disable off-hand key-bind/action
175+
}
176+
});
177+
}
178+
});
179+
135180
protocol.registerServerbound(ServerboundPackets15w31a.CONTAINER_CLICK, new PacketHandlers() {
136181
@Override
137182
protected void register() {
@@ -142,7 +187,7 @@ protected void register() {
142187
map(Types.BYTE); // Mode
143188
map(Types.ITEM1_8); // Clicked item
144189
handler(wrapper -> {
145-
short slot = wrapper.get(Types.SHORT, 0);
190+
final short slot = wrapper.get(Types.SHORT, 0);
146191
if (slot == 45) {
147192
// TODO: set cursor back to item
148193
wrapper.cancel(); // Ignore offhand slot
@@ -153,7 +198,7 @@ protected void register() {
153198

154199
protocol.registerServerbound(ServerboundPackets15w31a.USE_ITEM, null, wrapper -> {
155200
wrapper.cancel();
156-
// NOTE: Possibly bannable/noticable by anti-cheats?
201+
// NOTE: Possibly bannable/noticeable by anti-cheats?
157202
final PacketWrapper useItemOn = PacketWrapper.create(ServerboundPackets1_8.USE_ITEM_ON, wrapper.user());
158203
useItemOn.write(Types.BLOCK_POSITION1_8, new BlockPosition(0, 0, 0)); // Block Position
159204
useItemOn.write(Types.UNSIGNED_BYTE, (short) -1); // Direction
@@ -184,41 +229,22 @@ public void register() {
184229
protocol.registerServerbound(ServerboundPackets15w31a.CLIENT_INFORMATION, new PacketHandlers() {
185230
@Override
186231
protected void register() {
187-
map(Types.STRING); // Locale
188-
map(Types.BYTE); // View Distance
189-
map(Types.BYTE); // Chat Mode
190-
map(Types.BOOLEAN); // Chat Colors
232+
map(Types.STRING); // Locale
233+
map(Types.BYTE); // View Distance
234+
map(Types.BYTE); // Chat Mode
235+
map(Types.BOOLEAN); // Chat Colors
191236
map(Types.UNSIGNED_BYTE); // Skin Parts
192-
read(Types.BYTE); // Hand (Ignore for 1.8)
237+
read(Types.BYTE); // Hand (Ignore for 1.8)
193238
}
194239
});
195240

196241
// Workarounds / Broken stuff
197242
{
198-
// Workaround/Fix for some servers (currently not ideal)
199-
protocol.registerClientbound(ClientboundPackets1_8.CHAT, wrapper -> {
200-
String data = wrapper.read(Types.STRING);
201-
byte unknown = wrapper.read(Types.BYTE);
202-
try {
203-
JsonObject object = gson.fromJson(data, JsonObject.class);
204-
wrapper.write(Types.COMPONENT, object);
205-
wrapper.write(Types.BYTE, unknown);
206-
} catch (Exception exception) {
207-
wrapper.cancel();
208-
}
209-
});
210-
211-
// ? causes weird kick
212-
protocol.registerClientbound(ClientboundPackets1_8.SET_TITLES, PacketWrapper::cancel);
213-
214-
// Causes byte kick (1.8.x servers)
215-
protocol.registerClientbound(ClientboundPackets1_8.TAB_LIST, PacketWrapper::cancel);
216-
217-
// TODO: Fix entity data
218-
protocol.registerClientbound(ClientboundPackets1_8.SET_ENTITY_DATA, PacketWrapper::cancel);
219-
220-
// (can cause json issues)
221-
protocol.registerClientbound(ClientboundPackets1_8.UPDATE_SIGN, PacketWrapper::cancel);
243+
// TODO: Figure out why I can't remap the values to a component, I even tried the code from 1.9->1.8 class
244+
protocol.cancelClientbound(ClientboundPackets1_8.SET_TITLES);
245+
246+
// TODO: Remap entity data
247+
protocol.cancelClientbound(ClientboundPackets1_8.SET_ENTITY_DATA);
222248
}
223249
}
224250
}

0 commit comments

Comments
 (0)