forked from CleanroomMC/Cleanroom
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from HaHaWTH/main
General cb performance improvement & dumpitem command
- Loading branch information
Showing
22 changed files
with
362 additions
and
50 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
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
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
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
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
69 changes: 69 additions & 0 deletions
69
src/main/java/catserver/server/executor/hiddenclass/BukkitEventExecutorFactory.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,69 @@ | ||
package catserver.server.executor.hiddenclass; | ||
|
||
import org.bukkit.event.Event; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.plugin.EventExecutor; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.lang.constant.ConstantDescs; | ||
import java.lang.invoke.MethodHandle; | ||
import java.lang.invoke.MethodHandles; | ||
import java.lang.invoke.MethodType; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Modifier; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public final class BukkitEventExecutorFactory { | ||
private static final byte[] TEMPLATE_CLASS_BYTES; | ||
|
||
static { | ||
try (final InputStream is = BukkitEventExecutorFactory.class.getResourceAsStream("MethodHandleEventExecutorTemplate.class")) { | ||
TEMPLATE_CLASS_BYTES = Objects.requireNonNull(is, "template class is missing").readAllBytes(); | ||
} catch (IOException e) { | ||
throw new AssertionError(e); | ||
} | ||
} | ||
|
||
private BukkitEventExecutorFactory() { | ||
} | ||
|
||
/** | ||
* @return an {@link EventExecutor} implemented by a hidden class calling a method handle | ||
* | ||
* @param method the method to be invoked by the created event executor | ||
* @param eventClass the class of the event to handle | ||
*/ | ||
public static EventExecutor create(final Method method, final Class<? extends Event> eventClass) { | ||
final List<?> classData = List.of(method, eventClass); | ||
try { | ||
final MethodHandles.Lookup newClass = MethodHandles.lookup().defineHiddenClassWithClassData(TEMPLATE_CLASS_BYTES, classData, true); | ||
return newClass.lookupClass().asSubclass(EventExecutor.class).getDeclaredConstructor().newInstance(); | ||
} catch (ReflectiveOperationException e) { | ||
throw new AssertionError(e); | ||
} | ||
} | ||
|
||
record ClassData(Method method, MethodHandle methodHandle, Class<? extends Event> eventClass) { | ||
|
||
} | ||
|
||
/** | ||
* Extracts the class data and creates an adjusted MethodHandle directly usable by the lookup class. | ||
* The logic is kept here to minimize memory usage per created class. | ||
*/ | ||
static ClassData classData(final MethodHandles.Lookup lookup) { | ||
try { | ||
final Method method = MethodHandles.classDataAt(lookup, ConstantDescs.DEFAULT_NAME, Method.class, 0); | ||
MethodHandle mh = lookup.unreflect(method); | ||
if (Modifier.isStatic(method.getModifiers())) { | ||
mh = MethodHandles.dropArguments(mh, 0, Listener.class); | ||
} | ||
mh = mh.asType(MethodType.methodType(void.class, Listener.class, Event.class)); | ||
final Class<?> eventClass = MethodHandles.classDataAt(lookup, ConstantDescs.DEFAULT_NAME, Class.class, 1); | ||
return new ClassData(method, mh, eventClass.asSubclass(Event.class)); | ||
} catch (ReflectiveOperationException e) { | ||
throw new AssertionError(e); | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/catserver/server/executor/hiddenclass/MethodHandleEventExecutorTemplate.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,53 @@ | ||
package catserver.server.executor.hiddenclass; | ||
|
||
import org.bukkit.event.Event; | ||
import org.bukkit.event.EventException; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.plugin.EventExecutor; | ||
import org.spigotmc.SneakyThrow; | ||
|
||
import java.lang.invoke.MethodHandle; | ||
import java.lang.invoke.MethodHandles; | ||
import java.lang.reflect.Method; | ||
|
||
/** | ||
* This class is designed to be used as hidden class template. | ||
* Initializing the class directly will fail due to missing {@code classData}. | ||
* Instead, {@link java.lang.invoke.MethodHandles.Lookup#defineHiddenClassWithClassData(byte[], Object, boolean, MethodHandles.Lookup.ClassOption...)} | ||
* must be used, with the {@code classData} object being a list consisting of two elements: | ||
* <ol> | ||
* <li>A {@link Method} representing the event handler method</li> | ||
* <li>A {@link Class} representing the event type</li> | ||
* </ol> | ||
* The method must take {@link Event} or a subtype of it as its single parameter. | ||
* If the method is non-static, it also needs to reside in a class implementing {@link Listener}. | ||
*/ | ||
@SuppressWarnings("unused") | ||
class MethodHandleEventExecutorTemplate implements EventExecutor { | ||
private static final Method METHOD; | ||
private static final MethodHandle HANDLE; | ||
private static final Class<? extends Event> EVENT_CLASS; | ||
|
||
static { | ||
final MethodHandles.Lookup lookup = MethodHandles.lookup(); | ||
final BukkitEventExecutorFactory.ClassData classData = BukkitEventExecutorFactory.classData(lookup); | ||
METHOD = classData.method(); | ||
HANDLE = classData.methodHandle(); | ||
EVENT_CLASS = classData.eventClass(); | ||
} | ||
|
||
@Override | ||
public void execute(final Listener listener, final Event event) throws EventException { | ||
if (!EVENT_CLASS.isInstance(event)) return; | ||
try { | ||
HANDLE.invokeExact(listener, event); | ||
} catch (Throwable t) { | ||
SneakyThrow.sneaky(t); | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "MethodHandleEventExecutorTemplate['" + METHOD + "']"; | ||
} | ||
} |
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
Oops, something went wrong.