Skip to content

Commit 11fd673

Browse files
committed
Use MethodHandle API
1 parent 5efec64 commit 11fd673

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

bukkit/src/main/java/co/aikar/commands/ACFBukkitLocalesListener.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,17 @@
2828
import org.bukkit.event.Listener;
2929
import org.bukkit.event.player.PlayerLocaleChangeEvent;
3030

31+
import java.lang.invoke.MethodHandle;
32+
import java.lang.invoke.MethodHandles;
33+
import java.lang.invoke.MethodType;
3134
import java.lang.reflect.InvocationTargetException;
3235
import java.lang.reflect.Method;
3336
import java.util.Locale;
3437

3538
class ACFBukkitLocalesListener implements Listener {
3639

3740
private final BukkitCommandManager manager;
41+
private MethodHandle localeMethod1_8 = null;
3842

3943
ACFBukkitLocalesListener(BukkitCommandManager manager) {
4044
this.manager = manager;
@@ -56,13 +60,24 @@ void onLocaleChange(PlayerLocaleChangeEvent event) {
5660
}
5761
} catch (NoSuchMethodError ignored2) {
5862
try {
59-
Method getNewLocale = event.getClass().getMethod("getNewLocale");
60-
getNewLocale.setAccessible(true);
61-
String value = (String) getNewLocale.invoke(event);
63+
if (localeMethod1_8 != null) {
64+
String value = (String) localeMethod1_8.invoke(event);
65+
if (!value.equals(manager.issuersLocaleString.get(player.getUniqueId()))) {
66+
locale = ACFBukkitUtil.stringToLocale(value);
67+
}
68+
69+
return;
70+
}
71+
72+
MethodHandles.Lookup lookup = MethodHandles.lookup();
73+
MethodType type = MethodType.methodType(String.class);
74+
75+
localeMethod1_8 = lookup.findVirtual(PlayerLocaleChangeEvent.class, "getNewLocale", type);
76+
String value = (String) localeMethod1_8.invoke(event);
6277
if (!value.equals(manager.issuersLocaleString.get(player.getUniqueId()))) {
6378
locale = ACFBukkitUtil.stringToLocale(value);
6479
}
65-
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ignored3) {
80+
} catch (Throwable ignored3) {
6681
}
6782
}
6883
}

0 commit comments

Comments
 (0)