-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathStatelessTransitionProtocol.java
More file actions
67 lines (58 loc) · 3.36 KB
/
StatelessTransitionProtocol.java
File metadata and controls
67 lines (58 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
* This file is part of ViaLegacy - https://github.com/RaphiMC/ViaLegacy
* Copyright (C) 2020-2026 RK_01/RaphiMC 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 net.raphimc.vialegacy.api.protocol;
import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType;
import com.viaversion.viaversion.api.protocol.packet.State;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandler;
public abstract class StatelessTransitionProtocol<CU extends ClientboundPacketType, CM extends ClientboundPacketType, SM extends ServerboundPacketType, SU extends ServerboundPacketType> extends StatelessProtocol<CU, CM, SM, SU> {
public StatelessTransitionProtocol(final Class<CU> unmappedClientboundPacketType, final Class<CM> mappedClientboundPacketType, final Class<SM> mappedServerboundPacketType, final Class<SU> unmappedServerboundPacketType) {
super(unmappedClientboundPacketType, mappedClientboundPacketType, mappedServerboundPacketType, unmappedServerboundPacketType);
}
public void registerServerboundTransition(final ServerboundPacketType unmappedPacketType, final SM mappedPacketType, final PacketHandler handler) {
this.registerServerbound(unmappedPacketType.state(), unmappedPacketType.getId(), mappedPacketType != null ? mappedPacketType.getId() : -1, wrapper -> {
wrapper.setPacketType(mappedPacketType);
if (handler != null) {
handler.handle(wrapper);
}
});
}
public void registerClientboundTransition(final CU unmappedPacketType, final Object... handlers) {
if (handlers.length % 2 != 0) {
throw new IllegalArgumentException("handlers.length % 2 != 0");
}
this.registerClientbound(unmappedPacketType.state(), unmappedPacketType.getId(), -1, wrapper -> {
final State currentState = wrapper.user().getProtocolInfo().getServerState();
for (int i = 0; i < handlers.length; i += 2) {
if (handlers[i] instanceof State state) {
if (state != currentState) continue;
} else {
final ClientboundPacketType mappedPacketType = (ClientboundPacketType) handlers[i];
if (mappedPacketType.state() != currentState) continue;
wrapper.setPacketType(mappedPacketType);
}
final PacketHandler handler = (PacketHandler) handlers[i + 1];
if (handler != null) {
handler.handle(wrapper);
}
return;
}
throw new IllegalStateException("No handler found for packet " + unmappedPacketType + " in state " + currentState);
});
}
}