Skip to content

Commit

Permalink
Several platform-related improvements
Browse files Browse the repository at this point in the history
* Use Sponge register listener with lookup API if possible.
  Leverage SpongePowered/SpongeAPI#2441 if
  available. Otherwise fallback to method scanning and warn user.
* Implement AlwaysAvailableMuteCache#clearCachedMuteIf for faster
  unmute resolution
* Add fast-path for user lookups when running on Folia
  • Loading branch information
A248 committed Aug 17, 2023
1 parent ed6215b commit 19a6494
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public CentralisedFuture<Component> executeAndCheckConnection(UUID uuid, String
@Override
public ReactionStage<Optional<Punishment>> getCachedMute(UUID uuid, NetworkAddress address) {
Objects.requireNonNull(uuid, "uuid");
Objects.requireNonNull(address, "address");
return muteCache.get().getCachedMute(uuid, address);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,29 @@ private CentralisedFuture<Optional<MuteAndMessage>> cacheRequest(MuteCacheKey ca

@Override
void clearCachedMuteIf(Predicate<Punishment> removeIfMatches) {
// Do nothing - the mute will automatically expire in due time
ConcurrentHashMap<MuteCacheKey, Entry> map = cache.map;

for (Map.Entry<MuteCacheKey, Entry> mapEntry : map.entrySet()) {
MuteCacheKey key = mapEntry.getKey();
Entry entry = mapEntry.getValue();

// Replace the current value with NULL if it matches the predicate
// However, perform the operation atomically with respect to entry updates
MuteAndMessage currentValue;
while ((currentValue = entry.currentValue) != null && removeIfMatches.test(currentValue.mute())) {
Entry newEntry = new Entry(null, entry.lastUpdated, entry.nextValue);
// Compare-and-swap the old entry with the new one
if (map.replace(key, entry, newEntry)) {
// Success
break;
}
entry = map.get(key);
if (entry == null) {
// Player logged off
break;
}
}
}
}

@Override
Expand Down Expand Up @@ -284,6 +306,6 @@ private void stopPurgeTask() {
}

private record Entry(@Nullable MuteAndMessage currentValue, long lastUpdated,
@Nullable CentralisedFuture<MuteAndMessage> nextValue) {
}
@Nullable CentralisedFuture<MuteAndMessage> nextValue) { }

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.bukkit.entity.Player;
import space.arim.libertybans.core.env.SimpleEnvUserResolver;
import space.arim.libertybans.core.env.UUIDAndAddress;
import space.arim.morepaperlib.MorePaperLib;
import space.arim.omnibus.util.concurrent.CentralisedFuture;
import space.arim.omnibus.util.concurrent.FactoryOfTheFuture;

Expand All @@ -36,15 +37,20 @@ public final class SpigotUserResolver extends SimpleEnvUserResolver {

private final FactoryOfTheFuture futuresFactory;
private final Server server;
private final MorePaperLib morePaperLib;

@Inject
public SpigotUserResolver(FactoryOfTheFuture futuresFactory, Server server) {
public SpigotUserResolver(FactoryOfTheFuture futuresFactory, Server server, MorePaperLib morePaperLib) {
this.futuresFactory = futuresFactory;
this.server = server;
this.morePaperLib = morePaperLib;
}

@Override
protected <U> CentralisedFuture<U> performLookup(Supplier<U> rootImplementation) {
if (morePaperLib.scheduling().isUsingFolia()) {
return futuresFactory.completedFuture(rootImplementation.get());
}
return futuresFactory.supplySync(rootImplementation);
}

Expand Down
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>
7 changes: 4 additions & 3 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 @@ -64,14 +64,14 @@
<jooq.version>3.18.3</jooq.version>
<adventure.version>4.7.0</adventure.version>
<examination.version>1.3.0</examination.version>
<arimapi.version>0.26.4</arimapi.version>
<arimapi.version>0.26.5</arimapi.version>
<managedwaits.version>0.1.3</managedwaits.version>
<morepaperlib.version>0.4.3</morepaperlib.version>

<!-- 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 19a6494

Please sign in to comment.