Skip to content

Commit

Permalink
Use Sponge register listener with lookup API if possible
Browse files Browse the repository at this point in the history
Leverage SpongePowered/SpongeAPI#2441 if
  available. Otherwise fallback to method scanning and warn user.
  • Loading branch information
A248 committed Aug 17, 2023
1 parent ed6215b commit e9a6143
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 23 deletions.
2 changes: 1 addition & 1 deletion bans-env/sponge/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
<repositories>
<repository>
<id>sponge</id>
<url>https://repo.spongepowered.org/repository/maven-releases/</url>
<url>https://repo.spongepowered.org/repository/maven-public/</url>
</repository>
</repositories>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package space.arim.libertybans.env.sponge;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongepowered.api.Game;
import org.spongepowered.plugin.PluginContainer;
Expand All @@ -31,8 +32,10 @@
import space.arim.libertybans.core.CommandsModule;
import space.arim.libertybans.core.PillarOneBindModule;
import space.arim.libertybans.core.PillarTwoBindModule;
import space.arim.libertybans.env.sponge.listener.RegisterListeners;
import space.arim.libertybans.env.sponge.listener.RegisterListenersByMethodScan;
import space.arim.libertybans.env.sponge.listener.RegisterListenersStandard;
import space.arim.libertybans.env.sponge.listener.RegisterListenersWithLookup;
import space.arim.omnibus.Omnibus;
import space.arim.omnibus.OmnibusProvider;

Expand All @@ -58,12 +61,26 @@ public SpongeLauncher(PluginContainer plugin, Game game, Path folder, Omnibus om

@Override
public BaseFoundation launch() {
boolean spongeApi9;
Class<? extends RegisterListeners> registerListenersBinding;
{
int dataVersion = game.platform().minecraftVersion().dataVersion().orElseThrow();
// Sponge servers beyond MC 1.16.5 (data version 2586) use API 9
spongeApi9 = dataVersion > 2586;
LoggerFactory.getLogger(getClass()).info("Using Sponge API 9+: {}", spongeApi9);
boolean spongeApi9 = dataVersion > 2586;
Logger logger = LoggerFactory.getLogger(getClass());
logger.info("Using Sponge API 9+: {}", spongeApi9);

if (!spongeApi9) {
registerListenersBinding = RegisterListenersStandard.class;
} else if (RegisterListenersWithLookup.detectIfUsable()) {
logger.info("Listener registration with lookup API detected and available");
registerListenersBinding = RegisterListenersWithLookup.class;
} else {
// Fallback to manual method scanning
logger.warn(
"Proper listener registration for Sponge API 9 is not available on your outdated version. " +
"Please update your Sponge implementation so that the latest APIs are usable.");
registerListenersBinding = RegisterListenersByMethodScan.class;
}
}
return new InjectorBuilder()
.bindInstance(PluginContainer.class, plugin)
Expand All @@ -75,9 +92,9 @@ public BaseFoundation launch() {
new PillarOneBindModule(),
new PillarTwoBindModule(),
new CommandsModule(),
new SpongeBindModule(),
spongeApi9 ? new RegisterListenersByMethodScan.Module() : new RegisterListenersStandard.Module()
new SpongeBindModule()
)
.bindIdentifier(RegisterListeners.class, registerListenersBinding)
.specification(SpecificationSupport.JAKARTA)
.multiBindings(true)
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,6 @@ public RegisterListenersByMethodScan(PluginContainer plugin, Game game) {
this.game = game;
}

public static class Module {

public RegisterListeners registerListeners(RegisterListenersByMethodScan registerListeners) {
return registerListeners;
}
}

@Override
public void register(PlatformListener listener) {
if (registrations.containsKey(listener)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,6 @@ public RegisterListenersStandard(PluginContainer plugin, Game game) {
this.game = game;
}

public static final class Module {

public RegisterListeners registerListeners(RegisterListenersStandard registerListenersStandard) {
return registerListenersStandard;
}
}

@Override
public void register(PlatformListener listener) {
game.eventManager().registerListeners(plugin, listener);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* LibertyBans
* Copyright © 2023 Anand Beh
*
* LibertyBans is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* LibertyBans 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with LibertyBans. If not, see <https://www.gnu.org/licenses/>
* and navigate to version 3 of the GNU Affero General Public License.
*/

package space.arim.libertybans.env.sponge.listener;

import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import org.spongepowered.api.Game;
import org.spongepowered.api.event.EventManager;
import org.spongepowered.plugin.PluginContainer;
import space.arim.libertybans.core.env.PlatformListener;

import java.lang.invoke.MethodHandles;

@Singleton
public final class RegisterListenersWithLookup implements RegisterListeners {

private final PluginContainer plugin;
private final Game game;

@Inject
public RegisterListenersWithLookup(PluginContainer plugin, Game game) {
this.plugin = plugin;
this.game = game;
}

public static boolean detectIfUsable() {
try {
EventManager.class.getMethod("registerListeners", PluginContainer.class, Object.class, MethodHandles.Lookup.class);
return true;
} catch (NoSuchMethodException ex) {
return false;
}
}

@Override
public void register(PlatformListener listener) {
game.eventManager().registerListeners(plugin, listener, MethodHandles.lookup());
}

@Override
public void unregister(PlatformListener listener) {
game.eventManager().unregisterListeners(listener);
}

}
2 changes: 1 addition & 1 deletion bans-env/spongeplugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<repositories>
<repository>
<id>sponge</id>
<url>https://repo.spongepowered.org/repository/maven-releases/</url>
<url>https://repo.spongepowered.org/repository/maven-public/</url>
</repository>
</repositories>
</project>
5 changes: 3 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@

<!-- Internal dependencies -->
<jakarta-inject.version>2.0.0</jakarta-inject.version>
<solidinjector.version>1.1.0-RC1</solidinjector.version>
<solidinjector.version>1.1.0-RC2</solidinjector.version>
<dazzleconf.version>1.3.0-M2</dazzleconf.version>
<slf4j.version>1.7.30</slf4j.version>
<hikari.version>5.0.1</hikari.version>
Expand All @@ -71,7 +71,7 @@
<!-- Platform dependencies -->
<bungeecord.version>1.16-R0.4</bungeecord.version>
<spigot.version>1.8.8-R0.1-20160221.082514-43</spigot.version>
<sponge.version>9.0.0</sponge.version>
<sponge.version>9.1.0-SNAPSHOT</sponge.version>
<velocity.version>3.1.0</velocity.version>

<!-- Internal hash -->
Expand Down Expand Up @@ -156,6 +156,7 @@
<excludes>
<exclude>space.arim.libertybans:*</exclude>
<exclude>org.spigotmc:spigot-api</exclude>
<exclude>org.spongepowered:spongeapi</exclude>
</excludes>
</requireReleaseDeps>
</rules>
Expand Down

0 comments on commit e9a6143

Please sign in to comment.