mirror of
https://github.com/EssentialsX/Essentials.git
synced 2025-02-01 21:11:44 +01:00
Add support for permission contexts (#3320)
As discussed in #2960. Allows Essentials to register contexts with permission plugins that support them. A working implementation for LuckPerms is included in this PR. PermissionsEx 2 also has support for registering contexts, however it should probably be added at a later date, since as per zml2008 on Discord, the API is not yet stable. https://user-images.githubusercontent.com/17698576/82718550-f5507500-9c57-11ea-970d-9d5f8bce8037.png
This commit is contained in:
parent
9e2b906048
commit
baab65d776
@ -54,6 +54,12 @@
|
||||
<version>1.7</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.luckperms</groupId>
|
||||
<artifactId>api</artifactId>
|
||||
<version>5.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.papermc</groupId>
|
||||
<artifactId>paperlib</artifactId>
|
||||
|
@ -391,6 +391,9 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials {
|
||||
if (backup != null) {
|
||||
backup.stopTask();
|
||||
}
|
||||
|
||||
this.getPermissionsHandler().unregisterContexts();
|
||||
|
||||
Economy.setEss(null);
|
||||
Trade.closeLog();
|
||||
getUserMap().getUUIDMap().shutdown();
|
||||
|
@ -36,6 +36,7 @@ public class EssentialsPluginListener implements Listener, IConf {
|
||||
if (event.getPlugin().getName().equals("EssentialsChat")) {
|
||||
ess.getSettings().setEssentialsChatActive(false);
|
||||
}
|
||||
ess.getPermissionsHandler().checkPermissions();
|
||||
ess.getAlternativeCommandsHandler().removePlugin(event.getPlugin());
|
||||
// Check to see if the plugin thats being disabled is the one we are using
|
||||
if (ess.getPaymentMethod() != null && Methods.hasMethod() && Methods.checkDisabled(event.getPlugin())) {
|
||||
|
@ -3,6 +3,8 @@ package com.earth2me.essentials.perm;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
|
||||
public interface IPermissionsHandler {
|
||||
@ -23,5 +25,9 @@ public interface IPermissionsHandler {
|
||||
|
||||
String getSuffix(Player base);
|
||||
|
||||
void registerContext(String context, Function<Player, Iterable<String>> calculator, Supplier<Iterable<String>> suggestions);
|
||||
|
||||
void unregisterContexts();
|
||||
|
||||
boolean tryProvider();
|
||||
}
|
||||
|
@ -2,11 +2,14 @@ package com.earth2me.essentials.perm;
|
||||
|
||||
import com.earth2me.essentials.Essentials;
|
||||
import com.earth2me.essentials.perm.impl.*;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class PermissionsHandler implements IPermissionsHandler {
|
||||
@ -89,6 +92,16 @@ public class PermissionsHandler implements IPermissionsHandler {
|
||||
return suffix;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerContext(String context, Function<Player, Iterable<String>> calculator, Supplier<Iterable<String>> suggestions) {
|
||||
handler.registerContext(context, calculator, suggestions);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unregisterContexts() {
|
||||
handler.unregisterContexts();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean tryProvider() {
|
||||
return true;
|
||||
@ -97,6 +110,7 @@ public class PermissionsHandler implements IPermissionsHandler {
|
||||
public void checkPermissions() {
|
||||
// load and assign a handler
|
||||
List<Class<? extends SuperpermsHandler>> providerClazz = Arrays.asList(
|
||||
LuckPermsHandler.class,
|
||||
ModernVaultHandler.class,
|
||||
GenericVaultHandler.class,
|
||||
SuperpermsHandler.class
|
||||
@ -105,7 +119,14 @@ public class PermissionsHandler implements IPermissionsHandler {
|
||||
try {
|
||||
IPermissionsHandler provider = providerClass.newInstance();
|
||||
if (provider.tryProvider()) {
|
||||
if (provider.getClass().isInstance(this.handler)) {
|
||||
return;
|
||||
}
|
||||
if (this.handler != null) {
|
||||
unregisterContexts();
|
||||
}
|
||||
this.handler = provider;
|
||||
initContexts();
|
||||
break;
|
||||
}
|
||||
} catch (Throwable ignored) {
|
||||
@ -160,4 +181,10 @@ public class PermissionsHandler implements IPermissionsHandler {
|
||||
}
|
||||
}
|
||||
|
||||
private void initContexts() {
|
||||
registerContext("essentials:afk", player -> Collections.singleton(String.valueOf(ess.getUser(player).isAfk())), () -> ImmutableSet.of("true", "false"));
|
||||
registerContext("essentials:muted", player -> Collections.singleton(String.valueOf(ess.getUser(player).isMuted())), () -> ImmutableSet.of("true", "false"));
|
||||
registerContext("essentials:vanished", player -> Collections.singleton(String.valueOf(ess.getUser(player).isHidden())), () -> ImmutableSet.of("true", "false"));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,55 @@
|
||||
package com.earth2me.essentials.perm.impl;
|
||||
|
||||
import net.luckperms.api.LuckPerms;
|
||||
import net.luckperms.api.context.ContextCalculator;
|
||||
import net.luckperms.api.context.ContextConsumer;
|
||||
import net.luckperms.api.context.ContextSet;
|
||||
import net.luckperms.api.context.ImmutableContextSet;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class LuckPermsHandler extends ModernVaultHandler {
|
||||
private LuckPerms luckPerms;
|
||||
private Set<ContextCalculator<Player>> contextCalculators;
|
||||
|
||||
@Override
|
||||
public void registerContext(String context, Function<Player, Iterable<String>> calculator, Supplier<Iterable<String>> suggestions) {
|
||||
ContextCalculator<Player> contextCalculator = new ContextCalculator<Player>() {
|
||||
@Override
|
||||
public void calculate(Player target, ContextConsumer consumer) {
|
||||
calculator.apply(target).forEach(value -> consumer.accept(context, value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ContextSet estimatePotentialContexts() {
|
||||
ImmutableContextSet.Builder builder = ImmutableContextSet.builder();
|
||||
suggestions.get().forEach(value -> builder.add(context, value));
|
||||
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() {
|
||||
RegisteredServiceProvider<LuckPerms> provider = Bukkit.getServicesManager().getRegistration(LuckPerms.class);
|
||||
if (provider != null) {
|
||||
luckPerms = provider.getProvider();
|
||||
contextCalculators = new HashSet<>();
|
||||
}
|
||||
return luckPerms != null && super.tryProvider();
|
||||
}
|
||||
}
|
@ -7,6 +7,8 @@ import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
|
||||
public class SuperpermsHandler implements IPermissionsHandler {
|
||||
@ -72,6 +74,14 @@ public class SuperpermsHandler implements IPermissionsHandler {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerContext(String context, Function<Player, Iterable<String>> calculator, Supplier<Iterable<String>> suggestions) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unregisterContexts() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean tryProvider() {
|
||||
return getEnabledPermsPlugin() != null;
|
||||
|
@ -5,7 +5,7 @@ main: com.earth2me.essentials.Essentials
|
||||
version: ${full.version}
|
||||
website: http://tiny.cc/EssentialsCommands
|
||||
description: Provides an essential, core set of commands for Bukkit.
|
||||
softdepend: [Vault]
|
||||
softdepend: [Vault, LuckPerms]
|
||||
authors: [Zenexer, ementalo, Aelux, Brettflan, KimKandor, snowleo, ceulemans, Xeology, KHobbits, md_5, Iaccidentally, drtshock, vemacs, SupaHam, md678685]
|
||||
api-version: "1.13"
|
||||
commands:
|
||||
|
Loading…
Reference in New Issue
Block a user