Skip to content

Commit 2912332

Browse files
committed
Use MethodHandles
For better performance
1 parent 2d578d6 commit 2912332

4 files changed

Lines changed: 68 additions & 32 deletions

File tree

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Project
22
group=com.gamerforea
3-
version=1.7.10-1.13.1
3+
version=1.7.10-1.13.2

src/main/java/com/gamerforea/eventhelper/EventHelper.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,19 @@ public void onServerStart(FMLServerStartingEvent event)
4545
}
4646

4747
@EventHandler
48-
public final void serverStarted(FMLServerStartedEvent event)
48+
public void serverStarted(FMLServerStartedEvent event)
4949
{
5050
Configuration cfg = ConfigUtils.getConfig("EventHelper");
5151
String c = CATEGORY_GENERAL;
5252
String[] plugins = cfg.getStringList("plugins", c, new String[] { "WorldGuard", "GriefPreventionPlus" }, "Plugins for sending events");
5353
boolean pluginHooking = cfg.getBoolean("pluginHooking", c, true, "Hooking plugins (allow checking regions)");
54-
craftPackage = Bukkit.getServer().getClass().getPackage().getName(); // TheAndrey: Automatic package detection
5554
explosions = cfg.getBoolean("explosions", c, explosions, "Explosions enabled");
5655
debug = cfg.getBoolean("debug", c, debug, "Debugging enabled");
5756
cfg.save();
5857

58+
craftPackage = Bukkit.getServer().getClass().getPackage().getName(); // TheAndrey: Automatic package detection
59+
LOGGER.debug("CraftBukkit package is: {}", craftPackage);
60+
5961
PluginManager plManager = Bukkit.getPluginManager();
6062
for (String plName : plugins)
6163
{
Lines changed: 63 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,77 @@
11
package com.gamerforea.eventhelper.util;
22

3+
import com.gamerforea.eventhelper.EventHelper;
34
import net.minecraft.entity.Entity;
45
import net.minecraft.entity.player.EntityPlayer;
56
import net.minecraft.item.ItemStack;
67
import net.minecraft.world.World;
78
import net.minecraftforge.common.util.ForgeDirection;
8-
import org.bukkit.Bukkit;
99
import org.bukkit.block.BlockFace;
1010
import org.bukkit.entity.Player;
1111

12-
import java.lang.reflect.Method;
12+
import java.lang.invoke.MethodHandle;
13+
import java.lang.invoke.MethodHandles;
14+
import java.lang.invoke.MethodType;
15+
import javax.annotation.Nullable;
1316

1417
public final class ConvertUtils
1518
{
16-
private static final Method getBukkitEntity;
17-
private static final Method asCraftMirror;
19+
private static final MethodHandle getWorld;
20+
private static final MethodHandle getBukkitEntity;
21+
private static final MethodHandle asCraftMirror;
1822

19-
public static org.bukkit.entity.Entity toBukkitEntity(Entity entity) throws Exception
23+
@Nullable
24+
public static org.bukkit.entity.Entity toBukkitEntity(@Nullable Entity entity)
2025
{
21-
return (org.bukkit.entity.Entity) getBukkitEntity.invoke(entity);
26+
if (entity == null)
27+
return null; // TheAndrey: null -> null
28+
29+
try
30+
{
31+
return (org.bukkit.entity.Entity) getBukkitEntity.bindTo(entity).invoke();
32+
}
33+
catch (Throwable e)
34+
{
35+
throw new RuntimeException("Unable to invoke getBukkitEntity() on " + entity, e);
36+
}
2237
}
2338

24-
public static Player toBukkitEntity(EntityPlayer player) throws Exception
39+
@Nullable
40+
public static Player toBukkitEntity(@Nullable EntityPlayer player)
2541
{
26-
return (Player) getBukkitEntity.invoke(player);
42+
return (Player) toBukkitEntity((Entity) player);
2743
}
2844

29-
public static org.bukkit.World toBukkitWorld(World world)
45+
@Nullable
46+
public static org.bukkit.World toBukkitWorld(@Nullable World world)
3047
{
31-
return Bukkit.getWorld(world.getWorldInfo().getWorldName());
48+
if (world == null)
49+
return null; // TheAndrey: null -> null
50+
51+
try
52+
{
53+
return (org.bukkit.World) getWorld.bindTo(world).invoke();
54+
}
55+
catch (Throwable e)
56+
{
57+
throw new RuntimeException("Unable to invoke getWorld() on " + world, e);
58+
}
3259
}
3360

34-
public static org.bukkit.inventory.ItemStack toBukkitItemStackMirror(ItemStack stack) throws Exception
61+
@Nullable
62+
public static org.bukkit.inventory.ItemStack toBukkitItemStackMirror(@Nullable ItemStack stack)
3563
{
36-
return (org.bukkit.inventory.ItemStack) asCraftMirror.invoke(null, stack);
64+
if (stack == null)
65+
return null; // TheAndrey: null -> null
66+
67+
try
68+
{
69+
return (org.bukkit.inventory.ItemStack) asCraftMirror.invoke(stack);
70+
}
71+
catch (Throwable e)
72+
{
73+
throw new RuntimeException("Unable to invoke asCraftMirror() with " + stack, e);
74+
}
3775
}
3876

3977
public static BlockFace toBukkitFace(ForgeDirection direction)
@@ -52,8 +90,6 @@ public static BlockFace toBukkitFace(ForgeDirection direction)
5290
return BlockFace.WEST;
5391
case EAST:
5492
return BlockFace.EAST;
55-
case UNKNOWN:
56-
return BlockFace.SELF;
5793
default:
5894
return BlockFace.SELF;
5995
}
@@ -63,15 +99,24 @@ public static BlockFace toBukkitFace(ForgeDirection direction)
6399
{
64100
try
65101
{
66-
getBukkitEntity = Entity.class.getDeclaredMethod("getBukkitEntity");
67-
getBukkitEntity.setAccessible(true);
102+
// TheAndrey: Use MethodHandles
103+
MethodHandles.Lookup lookup = MethodHandles.publicLookup();
104+
Class<?> craftWorld = getCraftClass("CraftWorld");
105+
Class<?> craftEntity = getCraftClass("entity.CraftEntity");
106+
Class<?> craftStack = getCraftClass("inventory.CraftItemStack");
68107

69-
asCraftMirror = CraftUtils.getCraftClass("inventory.CraftItemStack").getDeclaredMethod("asCraftMirror", ItemStack.class);
70-
asCraftMirror.setAccessible(true);
108+
getWorld = lookup.findVirtual(World.class, "getWorld", MethodType.methodType(craftWorld));
109+
getBukkitEntity = lookup.findVirtual(Entity.class, "getBukkitEntity", MethodType.methodType(craftEntity));
110+
asCraftMirror = lookup.findStatic(craftStack, "asCraftMirror", MethodType.methodType(craftStack, ItemStack.class));
71111
}
72112
catch (Throwable throwable)
73113
{
74114
throw new RuntimeException("Failed hooking CraftBukkit methods!", throwable);
75115
}
76116
}
117+
118+
public static Class<?> getCraftClass(String name) throws ClassNotFoundException
119+
{
120+
return Class.forName(EventHelper.craftPackage + '.' + name);
121+
}
77122
}

src/main/java/com/gamerforea/eventhelper/util/CraftUtils.java

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)