Skip to content

Commit

Permalink
Fix reading client settings throws error on 1.20.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Rothes committed Nov 26, 2023
1 parent 3c3ee03 commit c39ad58
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
10 changes: 8 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,16 @@
<version>2.12.0</version>
<scope>compile</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.comphenix.protocol</groupId>-->
<!-- <artifactId>ProtocolLib</artifactId>-->
<!-- <version>5.1.0</version>-->
<!-- <scope>provided</scope>-->
<!-- </dependency>-->
<dependency>
<groupId>com.comphenix.protocol</groupId>
<groupId>com.github.dmulloy2</groupId>
<artifactId>ProtocolLib</artifactId>
<version>5.0.0</version>
<version>master-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import me.rothes.protocolstringreplacer.PsrLocalization;
import me.rothes.protocolstringreplacer.packetlisteners.client.CloseWindow;
import me.rothes.protocolstringreplacer.packetlisteners.client.SettingsLocale;
import me.rothes.protocolstringreplacer.packetlisteners.client.SettingsLocaleUpper20;
import me.rothes.protocolstringreplacer.packetlisteners.client.itemstack.SetCreativeSlot;
import me.rothes.protocolstringreplacer.packetlisteners.client.itemstack.WindowClick;
import me.rothes.protocolstringreplacer.packetlisteners.server.actionbar.ChatActionBar;
Expand Down Expand Up @@ -143,7 +144,12 @@ public void addListeners() {
listeners.add(new WindowClick());
listeners.add(new SetCreativeSlot());
listeners.add(new CloseWindow());
listeners.add(new SettingsLocale());
if ((ProtocolStringReplacer.getInstance().getServerMajorVersion() == 20 && ProtocolStringReplacer.getInstance().getServerMinorVersion() >= 2)
|| ProtocolStringReplacer.getInstance().getServerMajorVersion() > 20) {
listeners.add(new SettingsLocaleUpper20());
} else {
listeners.add(new SettingsLocale());
}

for (AbstractPacketListener listener : listeners) {
try {
Expand Down
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;
}

}

0 comments on commit c39ad58

Please sign in to comment.