-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix reading client settings throws error on 1.20.2
- Loading branch information
Showing
3 changed files
with
64 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...n/java/me/rothes/protocolstringreplacer/packetlisteners/client/SettingsLocaleUpper20.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package me.rothes.protocolstringreplacer.packetlisteners.client; | ||
|
||
import com.comphenix.protocol.PacketType; | ||
import com.comphenix.protocol.events.PacketEvent; | ||
import me.rothes.protocolstringreplacer.api.user.PsrUser; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.Locale; | ||
|
||
public class SettingsLocaleUpper20 extends AbstractClientPacketListener { | ||
|
||
private Field language; | ||
|
||
public SettingsLocaleUpper20() { | ||
super(PacketType.Play.Client.SETTINGS); | ||
} | ||
|
||
@Override | ||
protected void process(@NotNull PacketEvent packetEvent) { | ||
PsrUser user = getEventUser(packetEvent); | ||
if (user == null) { | ||
return; | ||
} | ||
Object record = packetEvent.getPacket().getModifier().read(0); | ||
String read; | ||
try { | ||
read = (String) field().get(record); | ||
} catch (IllegalAccessException e) { | ||
throw new RuntimeException(e); | ||
} | ||
user.setClientLocale(read.toLowerCase(Locale.ROOT).replace('-', '_')); | ||
} | ||
|
||
private Field field() { | ||
if (language == null) { | ||
language = PacketType.Play.Client.SETTINGS.getPacketClass().getDeclaredFields()[0].getType().getDeclaredFields()[0]; | ||
language.setAccessible(true); | ||
} | ||
return language; | ||
} | ||
|
||
@Override | ||
protected boolean canWrite(@NotNull PacketEvent packetEvent) { | ||
// We just read it. | ||
return true; | ||
} | ||
|
||
} |