Plan API 5.2-R0.3: ListenerService

Allows registering listeners for platforms that don't have static
accessors for registering listeners, such as Velocity
This commit is contained in:
Risto Lahtela 2021-04-11 10:16:37 +03:00
parent 5af889cba3
commit 9302067a49
4 changed files with 113 additions and 1 deletions

View File

@ -4,7 +4,7 @@ dependencies {
compileOnly "com.google.code.gson:gson:$gsonVersion" compileOnly "com.google.code.gson:gson:$gsonVersion"
} }
ext.apiVersion = '5.2-R0.2' ext.apiVersion = '5.2-R0.3'
publishing { publishing {
repositories { repositories {

View File

@ -0,0 +1,65 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.settings;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
/**
* Service for registering listeners as Plan.
* <p>
* This is an utility service for implementing listeners for platforms that don't have
* static access to the listener registration.
*/
public interface ListenerService {
/**
* Obtain instance of ListenerService.
*
* @return QueryService implementation.
* @throws NoClassDefFoundError If Plan is not installed and this class can not be found or if older Plan version is installed.
* @throws IllegalStateException If Plan is installed, but not enabled.
*/
static ListenerService getInstance() {
return Optional.ofNullable(ListenerService.Holder.service.get())
.orElseThrow(() -> new IllegalStateException("ListenerService has not been initialised yet."));
}
/**
* Attempts to register an event listener to the platform as Plan.
* <p>
* This is an utility method for implementing listeners for platforms that don't have
* static access to the listener registration.
* <p>
* The listener needs to fulfill the qualities required by the platform.
*
* @param listener listener object.
*/
void registerListenerForPlan(Object listener);
class Holder {
static final AtomicReference<ListenerService> service = new AtomicReference<>();
private Holder() {
/* Static variable holder */
}
static void set(ListenerService service) {
ListenerService.Holder.service.set(service);
}
}
}

View File

@ -31,6 +31,7 @@ import com.djrapitops.plan.identification.ServerInfo;
import com.djrapitops.plan.processing.Processing; import com.djrapitops.plan.processing.Processing;
import com.djrapitops.plan.query.QuerySvc; import com.djrapitops.plan.query.QuerySvc;
import com.djrapitops.plan.settings.ConfigSystem; import com.djrapitops.plan.settings.ConfigSystem;
import com.djrapitops.plan.settings.ListenerSvc;
import com.djrapitops.plan.settings.SettingsSvc; import com.djrapitops.plan.settings.SettingsSvc;
import com.djrapitops.plan.settings.locale.LocaleSystem; import com.djrapitops.plan.settings.locale.LocaleSystem;
import com.djrapitops.plan.storage.database.DBSystem; import com.djrapitops.plan.storage.database.DBSystem;
@ -75,6 +76,7 @@ public class PlanSystem implements SubSystem {
private final ResourceSvc resourceService; private final ResourceSvc resourceService;
private final ExtensionSvc extensionService; private final ExtensionSvc extensionService;
private final QuerySvc queryService; private final QuerySvc queryService;
private final ListenerSvc listenerService;
private final SettingsSvc settingsService; private final SettingsSvc settingsService;
private final PluginLogger logger; private final PluginLogger logger;
private final ErrorLogger errorLogger; private final ErrorLogger errorLogger;
@ -99,6 +101,7 @@ public class PlanSystem implements SubSystem {
ResourceSvc resourceService, ResourceSvc resourceService,
ExtensionSvc extensionService, ExtensionSvc extensionService,
QuerySvc queryService, QuerySvc queryService,
ListenerSvc listenerService,
SettingsSvc settingsService, SettingsSvc settingsService,
PluginLogger logger, PluginLogger logger,
ErrorLogger errorLogger, ErrorLogger errorLogger,
@ -122,6 +125,7 @@ public class PlanSystem implements SubSystem {
this.resourceService = resourceService; this.resourceService = resourceService;
this.extensionService = extensionService; this.extensionService = extensionService;
this.queryService = queryService; this.queryService = queryService;
this.listenerService = listenerService;
this.settingsService = settingsService; this.settingsService = settingsService;
this.logger = logger; this.logger = logger;
this.errorLogger = errorLogger; this.errorLogger = errorLogger;
@ -144,6 +148,7 @@ public class PlanSystem implements SubSystem {
extensionService.register(); extensionService.register();
resolverService.register(); resolverService.register();
resourceService.register(); resourceService.register();
listenerService.register();
settingsService.register(); settingsService.register();
queryService.register(); queryService.register();

View File

@ -0,0 +1,42 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.settings;
import net.playeranalytics.plugin.server.Listeners;
import javax.inject.Inject;
import javax.inject.Singleton;
@Singleton
public class ListenerSvc implements ListenerService {
private final Listeners listeners;
@Inject
public ListenerSvc(Listeners listeners) {
this.listeners = listeners;
}
@Override
public void registerListenerForPlan(Object listener) {
listeners.registerListener(listener);
}
public void register() {
Holder.set(this);
}
}