Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to register main class as listener after unregistering it #1211

Draft
wants to merge 5 commits into
base: dev/3.0.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.velocitypowered.proxy.event.UntargetedEventHandler.EventTaskHandler;
import com.velocitypowered.proxy.event.UntargetedEventHandler.VoidHandler;
import com.velocitypowered.proxy.event.UntargetedEventHandler.WithContinuationHandler;
import com.velocitypowered.proxy.plugin.loader.VelocityPluginContainer;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
Expand Down Expand Up @@ -132,27 +133,16 @@ public <F> void registerHandlerAdapter(

/**
* Represents the registration of a single {@link EventHandler}.
*
* @param instance The instance of the {@link EventHandler} or the listener instance that was registered.
*/
static final class HandlerRegistration {

final PluginContainer plugin;
final short order;
final Class<?> eventType;
final EventHandler<Object> handler;

/**
* The instance of the {@link EventHandler} or the listener instance that was registered.
*/
final Object instance;

public HandlerRegistration(final PluginContainer plugin, final short order,
final Class<?> eventType, final Object instance, final EventHandler<Object> handler) {
this.plugin = plugin;
this.order = order;
this.eventType = eventType;
this.instance = instance;
this.handler = handler;
}
record HandlerRegistration(
PluginContainer plugin,
short order,
Class<?> eventType,
Object instance,
EventHandler<Object> handler
) {
}

enum AsyncType {
Expand All @@ -166,13 +156,7 @@ enum AsyncType {
NEVER
}

static final class HandlersCache {

final HandlerRegistration[] handlers;

HandlersCache(final HandlerRegistration[] handlers) {
this.handlers = handlers;
}
record HandlersCache(HandlerRegistration[] handlers) {
}

private @Nullable HandlersCache bakeHandlers(final Class<?> eventType) {
Expand Down Expand Up @@ -226,23 +210,13 @@ private UntargetedEventHandler buildUntargetedMethodHandler(final Method method)
return LambdaFactory.create(type.defineClassesWith(lookup), methodHandle);
}

static final class MethodHandlerInfo {

final Method method;
final @Nullable Class<?> eventType;
final short order;
final @Nullable String errors;
final @Nullable Class<?> continuationType;

private MethodHandlerInfo(final Method method, final @Nullable Class<?> eventType,
final short order, final @Nullable String errors,
final @Nullable Class<?> continuationType) {
this.method = method;
this.eventType = eventType;
this.order = order;
this.errors = errors;
this.continuationType = continuationType;
}
record MethodHandlerInfo(
Method method,
@Nullable Class<?> eventType,
short order,
@Nullable String errors,
@Nullable Class<?> continuationType
) {
}

private void collectMethods(final Class<?> targetClass,
Expand Down Expand Up @@ -339,9 +313,15 @@ private void register(final List<HandlerRegistration> registrations) {
@Override
public void register(final Object plugin, final Object listener) {
requireNonNull(listener, "listener");
final PluginContainer pluginContainer = pluginManager.ensurePluginContainer(plugin);
final VelocityPluginContainer pluginContainer
= (VelocityPluginContainer) pluginManager.ensurePluginContainer(plugin);
if (plugin == listener) {
throw new IllegalArgumentException("The plugin main instance is automatically registered.");
// Allow to register main class as listener after unregistering it
if (pluginContainer.unregisteredMainClassListener()) {
pluginContainer.unregisteredMainClassListener(false);
} else {
throw new IllegalArgumentException("The plugin main instance is automatically registered.");
}
}
registerInternally(pluginContainer, listener);
}
Expand Down Expand Up @@ -393,14 +373,24 @@ public void registerInternally(final PluginContainer pluginContainer, final Obje

@Override
public void unregisterListeners(final Object plugin) {
final PluginContainer pluginContainer = pluginManager.ensurePluginContainer(plugin);
final VelocityPluginContainer pluginContainer
= (VelocityPluginContainer) pluginManager.ensurePluginContainer(plugin);
// If the main class is unregistered, it is marked as such,
// to allow it to be re-registered
pluginContainer.unregisteredMainClassListener(true);
unregisterIf(registration -> registration.plugin == pluginContainer);
}

@Override
public void unregisterListener(final Object plugin, final Object handler) {
final PluginContainer pluginContainer = pluginManager.ensurePluginContainer(plugin);
requireNonNull(handler, "handler");
final VelocityPluginContainer pluginContainer
= (VelocityPluginContainer) pluginManager.ensurePluginContainer(plugin);
// If the main class is unregistered, it is marked as such,
// to allow it to be re-registered
if (plugin == handler) {
pluginContainer.unregisteredMainClassListener(true);
}
unregisterIf(registration ->
registration.plugin == pluginContainer && registration.instance == handler);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class VelocityPluginContainer implements PluginContainer {
private final PluginDescription description;
private Object instance;
private volatile ExecutorService service;
private boolean unregisteredMainClassListener = false;

public VelocityPluginContainer(PluginDescription description) {
this.description = description;
Expand Down Expand Up @@ -75,4 +76,12 @@ public ExecutorService getExecutorService() {
public boolean hasExecutorService() {
return this.service != null;
}

public void unregisteredMainClassListener(final boolean unregistered) {
this.unregisteredMainClassListener = unregistered;
}

public boolean unregisteredMainClassListener() {
return this.unregisteredMainClassListener;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -289,13 +289,7 @@ interface FancyContinuation {
void resumeWithError(Exception exception);
}

private static final class FancyContinuationImpl implements FancyContinuation {

private final Continuation continuation;

private FancyContinuationImpl(final Continuation continuation) {
this.continuation = continuation;
}
private record FancyContinuationImpl(Continuation continuation) implements FancyContinuation {

@Override
public void resume() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.velocitypowered.api.plugin.PluginContainer;
import com.velocitypowered.api.plugin.PluginDescription;
import com.velocitypowered.api.plugin.PluginManager;
import com.velocitypowered.proxy.plugin.loader.VelocityPluginContainer;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Optional;
Expand Down Expand Up @@ -52,14 +53,11 @@ public class FakePluginManager implements PluginManager {

@Override
public @NonNull Optional<PluginContainer> getPlugin(@NonNull String id) {
switch (id) {
case "a":
return Optional.of(PC_A);
case "b":
return Optional.of(PC_B);
default:
return Optional.empty();
}
return switch (id) {
case "a" -> Optional.of(PC_A);
case "b" -> Optional.of(PC_B);
default -> Optional.empty();
};
}

@Override
Expand All @@ -77,13 +75,14 @@ public void addToClasspath(@NonNull Object plugin, @NonNull Path path) {
throw new UnsupportedOperationException();
}

private static class FakePluginContainer implements PluginContainer {
private static class FakePluginContainer extends VelocityPluginContainer {

private final String id;
private final Object instance;
private final ExecutorService service;

private FakePluginContainer(String id, Object instance) {
super(null);
this.id = id;
this.instance = instance;
this.service = ForkJoinPool.commonPool();
Expand Down
Loading