Reduce user lookups for LuckPerms contexts (#4869)

This commit is contained in:
lucko 2022-03-27 14:07:36 +01:00 committed by GitHub
parent c4f10d9c1d
commit 4bd1b3c09f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 87 additions and 39 deletions

View File

@ -1,5 +1,7 @@
package com.earth2me.essentials.perm; package com.earth2me.essentials.perm;
import com.earth2me.essentials.Essentials;
import com.earth2me.essentials.User;
import com.earth2me.essentials.utils.TriState; import com.earth2me.essentials.utils.TriState;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -27,11 +29,11 @@ public interface IPermissionsHandler {
String getSuffix(Player base); String getSuffix(Player base);
void registerContext(String context, Function<Player, Iterable<String>> calculator, Supplier<Iterable<String>> suggestions); void registerContext(String context, Function<User, Iterable<String>> calculator, Supplier<Iterable<String>> suggestions);
void unregisterContexts(); void unregisterContexts();
String getBackendName(); String getBackendName();
boolean tryProvider(); boolean tryProvider(Essentials ess);
} }

View File

@ -1,6 +1,7 @@
package com.earth2me.essentials.perm; package com.earth2me.essentials.perm;
import com.earth2me.essentials.Essentials; import com.earth2me.essentials.Essentials;
import com.earth2me.essentials.User;
import com.earth2me.essentials.perm.impl.AbstractVaultHandler; import com.earth2me.essentials.perm.impl.AbstractVaultHandler;
import com.earth2me.essentials.perm.impl.ConfigPermissionsHandler; import com.earth2me.essentials.perm.impl.ConfigPermissionsHandler;
import com.earth2me.essentials.perm.impl.GenericVaultHandler; import com.earth2me.essentials.perm.impl.GenericVaultHandler;
@ -105,7 +106,7 @@ public class PermissionsHandler implements IPermissionsHandler {
} }
@Override @Override
public void registerContext(final String context, final Function<Player, Iterable<String>> calculator, final Supplier<Iterable<String>> suggestions) { public void registerContext(final String context, final Function<User, Iterable<String>> calculator, final Supplier<Iterable<String>> suggestions) {
handler.registerContext(context, calculator, suggestions); handler.registerContext(context, calculator, suggestions);
} }
@ -120,7 +121,7 @@ public class PermissionsHandler implements IPermissionsHandler {
} }
@Override @Override
public boolean tryProvider() { public boolean tryProvider(Essentials ess) {
return true; return true;
} }
@ -135,7 +136,7 @@ public class PermissionsHandler implements IPermissionsHandler {
for (final Class<? extends IPermissionsHandler> providerClass : providerClazz) { for (final Class<? extends IPermissionsHandler> providerClass : providerClazz) {
try { try {
final IPermissionsHandler provider = providerClass.newInstance(); final IPermissionsHandler provider = providerClass.newInstance();
if (provider.tryProvider()) { if (provider.tryProvider(ess)) {
if (provider.getClass().isInstance(this.handler)) { if (provider.getClass().isInstance(this.handler)) {
return; return;
} }
@ -170,7 +171,7 @@ public class PermissionsHandler implements IPermissionsHandler {
if (enabledPermsPlugin == null) enabledPermsPlugin = "generic"; if (enabledPermsPlugin == null) enabledPermsPlugin = "generic";
ess.getLogger().info("Using Vault based permissions (" + enabledPermsPlugin + ")"); ess.getLogger().info("Using Vault based permissions (" + enabledPermsPlugin + ")");
} else if (handler.getClass() == SuperpermsHandler.class) { } else if (handler.getClass() == SuperpermsHandler.class) {
if (handler.tryProvider()) { if (handler.tryProvider(ess)) {
ess.getLogger().warning("Detected supported permissions plugin " + ess.getLogger().warning("Detected supported permissions plugin " +
((SuperpermsHandler) handler).getEnabledPermsPlugin() + " without Vault installed."); ((SuperpermsHandler) handler).getEnabledPermsPlugin() + " without Vault installed.");
ess.getLogger().warning("Features such as chat prefixes/suffixes and group-related functionality will not " + ess.getLogger().warning("Features such as chat prefixes/suffixes and group-related functionality will not " +
@ -199,11 +200,11 @@ public class PermissionsHandler implements IPermissionsHandler {
} }
private void initContexts() { private void initContexts() {
registerContext("essentials:afk", player -> Collections.singleton(String.valueOf(ess.getUser(player).isAfk())), () -> ImmutableSet.of("true", "false")); registerContext("essentials:afk", user -> Collections.singleton(String.valueOf(user.isAfk())), () -> ImmutableSet.of("true", "false"));
registerContext("essentials:muted", player -> Collections.singleton(String.valueOf(ess.getUser(player).isMuted())), () -> ImmutableSet.of("true", "false")); registerContext("essentials:muted", user -> Collections.singleton(String.valueOf(user.isMuted())), () -> ImmutableSet.of("true", "false"));
registerContext("essentials:vanished", player -> Collections.singleton(String.valueOf(ess.getUser(player).isHidden())), () -> ImmutableSet.of("true", "false")); registerContext("essentials:vanished", user -> Collections.singleton(String.valueOf(user.isHidden())), () -> ImmutableSet.of("true", "false"));
registerContext("essentials:jailed", player -> Collections.singleton(String.valueOf(ess.getUser(player).isJailed())), () -> ImmutableSet.of("true", "false")); registerContext("essentials:jailed", user -> Collections.singleton(String.valueOf(user.isJailed())), () -> ImmutableSet.of("true", "false"));
registerContext("essentials:jail", player -> Optional.ofNullable(ess.getUser(player).getJail()).map(Arrays::asList).orElse(Collections.emptyList()), () -> { registerContext("essentials:jail", user -> Optional.ofNullable(user.getJail()).map(Arrays::asList).orElse(Collections.emptyList()), () -> {
try { try {
return ess.getJails().getList(); return ess.getJails().getList();
} catch (final Exception e) { } catch (final Exception e) {

View File

@ -1,5 +1,6 @@
package com.earth2me.essentials.perm.impl; package com.earth2me.essentials.perm.impl;
import com.earth2me.essentials.Essentials;
import com.earth2me.essentials.utils.TriState; import com.earth2me.essentials.utils.TriState;
import net.ess3.api.IEssentials; import net.ess3.api.IEssentials;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -35,7 +36,7 @@ public class ConfigPermissionsHandler extends SuperpermsHandler {
} }
@Override @Override
public boolean tryProvider() { public boolean tryProvider(Essentials ess) {
return true; return true;
} }
} }

View File

@ -1,8 +1,10 @@
package com.earth2me.essentials.perm.impl; package com.earth2me.essentials.perm.impl;
import com.earth2me.essentials.Essentials;
public class GenericVaultHandler extends AbstractVaultHandler { public class GenericVaultHandler extends AbstractVaultHandler {
@Override @Override
public boolean tryProvider() { public boolean tryProvider(Essentials ess) {
return super.canLoad(); return super.canLoad();
} }
} }

View File

@ -1,5 +1,8 @@
package com.earth2me.essentials.perm.impl; package com.earth2me.essentials.perm.impl;
import com.earth2me.essentials.Essentials;
import com.earth2me.essentials.User;
import net.luckperms.api.LuckPerms; import net.luckperms.api.LuckPerms;
import net.luckperms.api.context.ContextCalculator; import net.luckperms.api.context.ContextCalculator;
import net.luckperms.api.context.ContextConsumer; import net.luckperms.api.context.ContextConsumer;
@ -16,40 +19,75 @@ import java.util.function.Supplier;
public class LuckPermsHandler extends ModernVaultHandler { public class LuckPermsHandler extends ModernVaultHandler {
private LuckPerms luckPerms; private LuckPerms luckPerms;
private Set<ContextCalculator<Player>> contextCalculators; private Essentials ess;
private CombinedCalculator calculator;
@Override @Override
public void registerContext(final String context, final Function<Player, Iterable<String>> calculator, final Supplier<Iterable<String>> suggestions) { public void registerContext(final String context, final Function<User, Iterable<String>> calculator, final Supplier<Iterable<String>> suggestions) {
final ContextCalculator<Player> contextCalculator = new ContextCalculator<Player>() { if (this.calculator == null) {
this.calculator = new CombinedCalculator();
this.luckPerms.getContextManager().registerCalculator(this.calculator);
}
this.calculator.calculators.add(new Calculator(context, calculator, suggestions));
}
@Override
public void unregisterContexts() {
if (this.calculator != null) {
this.luckPerms.getContextManager().unregisterCalculator(this.calculator);
this.calculator = null;
}
}
@Override
public boolean tryProvider(Essentials ess) {
final RegisteredServiceProvider<LuckPerms> provider = Bukkit.getServicesManager().getRegistration(LuckPerms.class);
if (provider != null) {
this.luckPerms = provider.getProvider();
this.ess = ess;
}
return luckPerms != null && super.tryProvider(ess);
}
private static final class Calculator {
private final String id;
private final Function<User, Iterable<String>> function;
private final Supplier<Iterable<String>> suggestions;
private Calculator(String id, Function<User, Iterable<String>> function, Supplier<Iterable<String>> suggestions) {
this.id = id;
this.function = function;
this.suggestions = suggestions;
}
}
// By combining all calculators into one, we only need to make one call to ess.getUser().
private class CombinedCalculator implements ContextCalculator<Player> {
private final Set<Calculator> calculators = new HashSet<>();
@Override @Override
public void calculate(final Player target, final ContextConsumer consumer) { public void calculate(final Player target, final ContextConsumer consumer) {
calculator.apply(target).forEach(value -> consumer.accept(context, value)); // If the player doesn't exist in the UserMap, just skip
// Ess will cause performance problems for permissions checks if it attempts to
// perform i/o to load the user data otherwise.
if (!ess.getUserMap().userExists(target.getUniqueId())) {
return;
}
final User user = ess.getUser(target);
for (Calculator calculator : this.calculators) {
calculator.function.apply(user).forEach(value -> consumer.accept(calculator.id, value));
}
} }
@Override @Override
public ContextSet estimatePotentialContexts() { public ContextSet estimatePotentialContexts() {
final ImmutableContextSet.Builder builder = ImmutableContextSet.builder(); final ImmutableContextSet.Builder builder = ImmutableContextSet.builder();
suggestions.get().forEach(value -> builder.add(context, value)); for (Calculator calculator : this.calculators) {
calculator.suggestions.get().forEach(value -> builder.add(calculator.id, value));
}
return builder.build(); return builder.build();
} }
};
luckPerms.getContextManager().registerCalculator(contextCalculator);
contextCalculators.add(contextCalculator);
}
@Override
public void unregisterContexts() {
contextCalculators.forEach(contextCalculator -> luckPerms.getContextManager().unregisterCalculator(contextCalculator));
contextCalculators.clear();
}
@Override
public boolean tryProvider() {
final RegisteredServiceProvider<LuckPerms> provider = Bukkit.getServicesManager().getRegistration(LuckPerms.class);
if (provider != null) {
luckPerms = provider.getProvider();
contextCalculators = new HashSet<>();
}
return luckPerms != null && super.tryProvider();
} }
} }

View File

@ -1,5 +1,7 @@
package com.earth2me.essentials.perm.impl; package com.earth2me.essentials.perm.impl;
import com.earth2me.essentials.Essentials;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.Arrays; import java.util.Arrays;
@ -21,7 +23,7 @@ public class ModernVaultHandler extends AbstractVaultHandler {
} }
@Override @Override
public boolean tryProvider() { public boolean tryProvider(Essentials ess) {
return super.canLoad() && supportedPlugins.contains(getEnabledPermsPlugin()); return super.canLoad() && supportedPlugins.contains(getEnabledPermsPlugin());
} }
} }

View File

@ -1,5 +1,7 @@
package com.earth2me.essentials.perm.impl; package com.earth2me.essentials.perm.impl;
import com.earth2me.essentials.Essentials;
import com.earth2me.essentials.User;
import com.earth2me.essentials.perm.IPermissionsHandler; import com.earth2me.essentials.perm.IPermissionsHandler;
import com.earth2me.essentials.utils.TriState; import com.earth2me.essentials.utils.TriState;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -124,7 +126,7 @@ public class SuperpermsHandler implements IPermissionsHandler {
} }
@Override @Override
public void registerContext(final String context, final Function<Player, Iterable<String>> calculator, final Supplier<Iterable<String>> suggestions) { public void registerContext(final String context, final Function<User, Iterable<String>> calculator, final Supplier<Iterable<String>> suggestions) {
} }
@Override @Override
@ -137,7 +139,7 @@ public class SuperpermsHandler implements IPermissionsHandler {
} }
@Override @Override
public boolean tryProvider() { public boolean tryProvider(Essentials ess) {
return getEnabledPermsPlugin() != null; return getEnabledPermsPlugin() != null;
} }