Skip to content

Commit cbc9263

Browse files
committed
Cache reflective data for tempOp
1 parent 45eddad commit cbc9263

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

src/main/java/com/laytonsmith/abstraction/bukkit/entities/BukkitMCPlayer.java

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
import org.bukkit.potion.PotionEffect;
6060
import org.bukkit.potion.PotionEffectType;
6161

62-
import java.lang.reflect.InvocationTargetException;
6362
import java.net.InetSocketAddress;
6463
import java.util.ArrayList;
6564
import java.util.List;
@@ -408,8 +407,14 @@ public MCEntity getSpectatorTarget() {
408407
return BukkitConvertor.BukkitGetCorrectEntity(p.getSpectatorTarget());
409408
}
410409

411-
@Override
412-
public void setTempOp(Boolean value) throws ClassNotFoundException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
410+
private static Class gameProfileClass = null;
411+
private static Class opListEntryClass = null;
412+
private static Map<String, Object> opMap = null;
413+
414+
private static void SetupTempOp() throws ClassNotFoundException {
415+
if(gameProfileClass != null) {
416+
return;
417+
}
413418
boolean isPaper = ((BukkitMCServer) Static.getServer()).isPaper();
414419
// Get some version specific mappings
415420
String nms = "net.minecraft.server";
@@ -446,18 +451,25 @@ public void setTempOp(Boolean value) throws ClassNotFoundException, NoSuchFieldE
446451

447452
Class nmsMinecraftServerClass = Class.forName(nms + ".MinecraftServer");
448453
/*n.m.s.MinecraftServer*/ Object nmsServer = ReflectionUtils.invokeMethod(nmsMinecraftServerClass, null, "getServer");
449-
/*n.m.s.PlayerList*/ Object nmsPlayerList = ReflectionUtils.invokeMethod(nmsServer, getPlayerList);
450-
/*n.m.s.OpList*/ Object opSet = ReflectionUtils.get(Class.forName(playersPackage + ".PlayerList"), nmsPlayerList, ops);
451-
//opSet.getClass().getSuperclass() == n.m.s.JsonList
452-
Map/*<String, n.m.s.OpListEntry>*/ d = (Map) ReflectionUtils.get(opSet.getClass().getSuperclass(), opSet, "d");
454+
/*n.m.s.players.PlayerList*/ Object nmsPlayerList = ReflectionUtils.invokeMethod(nmsServer, getPlayerList);
455+
/*n.m.s.players.OpList*/ Object opSet = ReflectionUtils.get(Class.forName(playersPackage + ".PlayerList"), nmsPlayerList, ops);
456+
//opSet.getClass().getSuperclass() == n.m.s.players.JsonList
457+
/*Map<String, n.m.s.players.OpListEntry>*/ opMap = (Map) ReflectionUtils.get(opSet.getClass().getSuperclass(), opSet, "d");
458+
/*n.m.s.players.OpListEntry*/ opListEntryClass = Class.forName(playersPackage + ".OpListEntry");
459+
/*com.mojang.authlib.GameProfile*/ gameProfileClass = Class.forName("com.mojang.authlib.GameProfile");
460+
}
461+
462+
@Override
463+
public void setTempOp(Boolean value) throws ClassNotFoundException {
464+
SetupTempOp();
453465
if(value) {
454-
/*n.m.s.OpListEntry*/ Class nmsOpListEntry = Class.forName(playersPackage + ".OpListEntry");
455-
/*com.mojang.authlib.GameProfile*/ Class nmsGameProfile = Class.forName("com.mojang.authlib.GameProfile");
456466
Object gameProfile = ReflectionUtils.invokeMethod(p, "getProfile");
457-
Object opListEntry = ReflectionUtils.newInstance(nmsOpListEntry, new Class[]{nmsGameProfile, int.class, boolean.class}, new Object[]{gameProfile, 4, false});
458-
d.put(p.getUniqueId().toString(), opListEntry);
467+
Object opListEntry = ReflectionUtils.newInstance(opListEntryClass,
468+
new Class[]{gameProfileClass, int.class, boolean.class},
469+
new Object[]{gameProfile, 4, false});
470+
opMap.put(p.getUniqueId().toString(), opListEntry);
459471
} else {
460-
d.remove(p.getUniqueId().toString());
472+
opMap.remove(p.getUniqueId().toString());
461473
}
462474
p.recalculatePermissions();
463475
}

0 commit comments

Comments
 (0)