updated to luckperms 5.1 and velocity 1.1, fixed luckperms not being optional

This commit is contained in:
ActualPlayer 2020-06-30 14:35:01 +02:00
parent 5bb793f7f3
commit e991610ba2
3 changed files with 48 additions and 29 deletions

View File

@ -5,7 +5,7 @@ plugins {
} }
group = "com.actualplayer" group = "com.actualplayer"
version = "1.1" version = "1.2"
description = "A way for the user to login to the server he last connected to." description = "A way for the user to login to the server he last connected to."
repositories { repositories {
@ -14,6 +14,15 @@ repositories {
name = "velocity" name = "velocity"
setUrl("https://repo.velocitypowered.com/snapshots/") setUrl("https://repo.velocitypowered.com/snapshots/")
} }
maven {
setUrl("https://repo.spongepowered.org/maven")
}
maven {
setUrl("https://repo.velocitypowered.com/snapshots/")
}
maven {
setUrl("https://libraries.minecraft.net/")
}
} }
defaultTasks("clean", "build", "shadowJar") defaultTasks("clean", "build", "shadowJar")
@ -22,10 +31,10 @@ dependencies {
compileOnly("org.projectlombok:lombok:1.18.8") compileOnly("org.projectlombok:lombok:1.18.8")
annotationProcessor("org.projectlombok:lombok:1.18.8") annotationProcessor("org.projectlombok:lombok:1.18.8")
implementation("me.lucko.luckperms:luckperms-api:4.4") implementation("net.luckperms:api:5.1")
compileOnly("com.velocitypowered:velocity-api:1.0.3-SNAPSHOT") compileOnly("com.velocitypowered:velocity-api:1.1.0-SNAPSHOT")
annotationProcessor("com.velocitypowered:velocity-api:1.0.3-SNAPSHOT") annotationProcessor("com.velocitypowered:velocity-api:1.1.0-SNAPSHOT")
implementation("org.yaml:snakeyaml:1.18") implementation("org.yaml:snakeyaml:1.18")

View File

@ -2,18 +2,19 @@ package com.actualplayer.rememberme;
import com.actualplayer.rememberme.handlers.*; import com.actualplayer.rememberme.handlers.*;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.connection.LoginEvent; import com.velocitypowered.api.event.connection.LoginEvent;
import com.velocitypowered.api.event.player.ServerConnectedEvent; import com.velocitypowered.api.event.player.ServerConnectedEvent;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
import com.velocitypowered.api.plugin.Dependency; import com.velocitypowered.api.plugin.Dependency;
import com.velocitypowered.api.plugin.Plugin; import com.velocitypowered.api.plugin.Plugin;
import com.velocitypowered.api.plugin.PluginContainer;
import com.velocitypowered.api.plugin.annotation.DataDirectory; import com.velocitypowered.api.plugin.annotation.DataDirectory;
import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import lombok.Getter; import lombok.Getter;
import me.lucko.luckperms.LuckPerms; import net.luckperms.api.LuckPerms;
import me.lucko.luckperms.api.LuckPermsApi; import net.luckperms.api.LuckPermsProvider;
import org.slf4j.Logger; import org.slf4j.Logger;
import java.nio.file.Path; import java.nio.file.Path;
@ -34,24 +35,30 @@ public class RememberMe {
private IRememberMeHandler handler; private IRememberMeHandler handler;
@Inject private boolean hasLuckPerms;
@Inject()
public RememberMe(ProxyServer server, Logger logger) { public RememberMe(ProxyServer server, Logger logger) {
this.server = server; this.server = server;
this.logger = logger; this.logger = logger;
} }
@Inject(optional = true)
public void initLuckPerms(@Named("luckperms")PluginContainer luckPermsContainer) {
this.hasLuckPerms = luckPermsContainer != null;
}
/** /**
* If LuckPerms is present, use the User meta tags to save last server * If LuckPerms is present, use the User meta tags to save last server
* @param event Velocity init event * @param event Velocity init event
*/ */
@Subscribe @Subscribe
public void onProxyInitialization(ProxyInitializeEvent event) { public void onProxyInitialization(ProxyInitializeEvent event) {
try { if (hasLuckPerms) {
LuckPermsApi api = LuckPerms.getApi(); LuckPerms api = LuckPermsProvider.get();
handler = new LuckPermsHandler(api); handler = new LuckPermsHandler(api);
getLogger().info("LuckPerms is installed, using LuckPerms meta-data to store last server info"); getLogger().info("LuckPerms is installed, using LuckPerms meta-data to store last server info");
} catch (IllegalStateException ex) { } else {
getLogger().error(ex.toString());
handler = new FileHandler(this); handler = new FileHandler(this);
getLogger().info("Using file-based storage"); getLogger().info("Using file-based storage");
} }

View File

@ -1,14 +1,17 @@
package com.actualplayer.rememberme.handlers; package com.actualplayer.rememberme.handlers;
import com.actualplayer.rememberme.RememberMe; import com.actualplayer.rememberme.RememberMe;
import me.lucko.luckperms.api.Contexts; import net.luckperms.api.LuckPerms;
import me.lucko.luckperms.api.LuckPermsApi; import net.luckperms.api.cacheddata.CachedMetaData;
import me.lucko.luckperms.api.Node; import net.luckperms.api.context.ContextManager;
import me.lucko.luckperms.api.User; import net.luckperms.api.context.ImmutableContextSet;
import me.lucko.luckperms.api.caching.MetaData; import net.luckperms.api.model.user.User;
import me.lucko.luckperms.api.context.ContextManager; import net.luckperms.api.model.user.UserManager;
import me.lucko.luckperms.api.context.ImmutableContextSet; import net.luckperms.api.node.Node;
import me.lucko.luckperms.api.manager.UserManager; import net.luckperms.api.node.NodeType;
import net.luckperms.api.query.QueryMode;
import net.luckperms.api.query.QueryOptions;
import org.checkerframework.checker.nullness.Opt;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; import java.util.UUID;
@ -16,9 +19,9 @@ import java.util.concurrent.CompletableFuture;
public class LuckPermsHandler implements IRememberMeHandler { public class LuckPermsHandler implements IRememberMeHandler {
private final LuckPermsApi api; private final LuckPerms api;
public LuckPermsHandler(LuckPermsApi api) { public LuckPermsHandler(LuckPerms api) {
this.api = api; this.api = api;
} }
@ -29,10 +32,10 @@ public class LuckPermsHandler implements IRememberMeHandler {
return userFuture.thenApply(user -> { return userFuture.thenApply(user -> {
if (user != null) { if (user != null) {
ContextManager cm = api.getContextManager(); ContextManager cm = api.getContextManager();
Contexts contexts = cm.lookupApplicableContexts(user).orElse(cm.getStaticContexts()); ImmutableContextSet context = cm.getContext(user).orElse(cm.getStaticContext());
MetaData metaData = user.getCachedData().getMetaData(contexts); CachedMetaData metaData = user.getCachedData().getMetaData(QueryOptions.builder(QueryMode.CONTEXTUAL).context(context).build());
return metaData.getMeta().getOrDefault("last-server", null); return metaData.getMetaValue("last-server");
} }
return null; return null;
@ -41,14 +44,14 @@ public class LuckPermsHandler implements IRememberMeHandler {
@Override @Override
public void setLastServerName(UUID uuid, String serverName) { public void setLastServerName(UUID uuid, String serverName) {
User user = api.getUser(uuid); User user = api.getUserManager().getUser(uuid);
if (user != null) { if (user != null) {
// Remove last server // Remove last server
user.clearMatching(n -> n.isMeta() && n.getPermission().contains("meta.last-server")); user.getNodes().removeIf(n -> n.getType() == NodeType.META && n.getKey().contains("last-server"));
// Add current server as last server // Add current server as last server
Node node = api.getNodeFactory().newBuilder("meta.last-server." + serverName).build(); Node node = api.getNodeBuilderRegistry().forMeta().key("last-server").value(serverName).build();
user.setPermission(node); user.getNodes().add(node);
api.getUserManager().saveUser(user); api.getUserManager().saveUser(user);
} }
} }