22
33import fr .mrmicky .viachatfixer .ViaChatFixerPlatform ;
44import fr .mrmicky .viachatfixer .handlers .ChatHandler ;
5- import us .myles .ViaVersion .api .PacketWrapper ;
65import us .myles .ViaVersion .api .Via ;
76import us .myles .ViaVersion .api .data .UserConnection ;
87import us .myles .ViaVersion .api .protocol .Protocol ;
98import us .myles .ViaVersion .api .protocol .ProtocolRegistry ;
109import us .myles .ViaVersion .api .protocol .ProtocolVersion ;
11- import us .myles .ViaVersion .api .remapper .PacketHandler ;
1210import us .myles .ViaVersion .api .remapper .PacketRemapper ;
1311import us .myles .ViaVersion .api .type .Type ;
1412import us .myles .ViaVersion .packets .State ;
1513import us .myles .ViaVersion .protocols .protocol1_11to1_10 .Protocol1_11To1_10 ;
14+ import us .myles .ViaVersion .protocols .protocol1_9_3to1_9_1_2 .ServerboundPackets1_9_3 ;
1615
17- import java .lang .reflect .Field ;
1816import java .util .HashSet ;
19- import java .util .Map ;
2017import java .util .Set ;
2118import java .util .UUID ;
2219
2320public class ViaChatHandler implements ChatHandler {
2421
2522 private final Set <UUID > unknownPlayers = new HashSet <>();
2623
27- private ViaChatFixerPlatform platform ;
24+ private final ViaChatFixerPlatform platform ;
2825
2926 public ViaChatHandler (ViaChatFixerPlatform platform ) {
3027 this .platform = platform ;
3128 }
3229
3330 @ Override
34- public void init () throws Exception {
31+ public void init () {
3532 if (ProtocolRegistry .SERVER_PROTOCOL >= ProtocolVersion .v1_11 .getId ()) {
3633 platform .getLogger ().warning ("This plugin is not required on 1.11+ servers, you can just remove it :)" );
34+ return ;
3735 }
3836
39- Field registryMapField = ProtocolRegistry .class .getDeclaredField ("registryMap" );
40- registryMapField .setAccessible (true );
41-
4237 //noinspection unchecked
43- Map <Integer , Map <Integer , Protocol >> registryMap = (Map <Integer , Map <Integer , Protocol >>) registryMapField .get (null );
44-
45- Protocol protocol = null ;
46- for (Map <Integer , Protocol > protocolMap : registryMap .values ()) {
47- for (Protocol prot : protocolMap .values ()) {
48- if (prot instanceof Protocol1_11To1_10 ) {
49- protocol = prot ;
50- break ;
51- }
52- }
53- }
38+ Protocol <?, ?, ?, ServerboundPackets1_9_3 > protocol = ProtocolRegistry .getProtocol (Protocol1_11To1_10 .class );
5439
5540 if (protocol == null ) {
56- throw new RuntimeException ("Protocol 1_11To1_10 not found" );
41+ throw new IllegalStateException ("Protocol 1_11To1_10 not found" );
5742 }
5843
5944 protocol .registerIncoming (State .PLAY , 0x02 , 0x02 , new PacketRemapper () {
6045 @ Override
6146 public void registerMap () {
6247 map (Type .STRING ); // 0 - Message
63- handler (new PacketHandler () {
64- @ Override
65- public void handle (PacketWrapper wrapper ) throws Exception {
66- String msg = wrapper .get (Type .STRING , 0 );
48+ handler (wrapper -> {
49+ // 100 character limit on older servers
50+ String message = wrapper .get (Type .STRING , 0 );
6751
68- if (msg .length () > 100 ) {
69- wrapper .set (Type .STRING , 0 , msg .substring (0 , 100 ));
52+ if (message .length () <= 100 ) {
53+ return ;
54+ }
7055
71- UserConnection connection = wrapper .user ();
72- ChatTracker chatTracker = connection .get (ChatTracker .class );
56+ wrapper .set (Type .STRING , 0 , message .substring (0 , 100 ));
7357
74- if (chatTracker == null ) {
75- chatTracker = new ChatTracker (connection );
76- connection .put (chatTracker );
77- }
58+ UserConnection connection = wrapper .user ();
59+ ChatTracker chatTracker = connection .get (ChatTracker .class );
7860
79- // don't allow messages longer than 256 characters
80- if ( msg . length () > 256 ) {
81- msg = msg . substring ( 0 , 256 );
82- }
61+ if ( chatTracker == null ) {
62+ chatTracker = new ChatTracker ( connection );
63+ connection . put ( chatTracker );
64+ }
8365
84- chatTracker .updateLastMessage (msg );
85- }
66+ // don't allow messages longer than 256 characters
67+ if (message .length () > 256 ) {
68+ message = message .substring (0 , 256 );
8669 }
70+
71+ chatTracker .updateLastMessage (message );
8772 });
8873 }
8974 }, true );
@@ -102,17 +87,19 @@ public String handle(UUID uuid) {
10287
10388 ChatTracker chatTracker = connection .get (ChatTracker .class );
10489
105- if (chatTracker != null && chatTracker .getLastMessage () != null ) {
106- if (!chatTracker .isValid (100 )) {
107- chatTracker .reset ();
108- return null ;
109- }
90+ if (chatTracker == null || chatTracker .getLastMessage () == null ) {
91+ return null ;
92+ }
11093
111- String message = chatTracker .getLastMessage ();
94+ if (! chatTracker .isValid ( 100 )) {
11295 chatTracker .reset ();
113- return message ;
96+ return null ;
11497 }
11598
116- return null ;
99+ String message = chatTracker .getLastMessage ();
100+
101+ chatTracker .reset ();
102+
103+ return message ;
117104 }
118105}
0 commit comments