added luckperms support3

This commit is contained in:
ActualPlayer 2019-08-12 23:57:18 +02:00
parent 7a633c9ce5
commit c10c8ace2a
7 changed files with 164 additions and 88 deletions

View File

@ -26,6 +26,8 @@ dependencies {
compileOnly("org.projectlombok:lombok:1.18.8")
annotationProcessor("org.projectlombok:lombok:1.18.8")
implementation("me.lucko.luckperms:luckperms-api:4.4")
compileOnly("com.velocitypowered:velocity-api:1.0.3-SNAPSHOT")
annotationProcessor("com.velocitypowered:velocity-api:1.0.3-SNAPSHOT")
@ -47,3 +49,9 @@ blossom {
configure<JavaPluginConvention> {
sourceCompatibility = JavaVersion.VERSION_1_8
}
configurations {
runtime {
exclude(group = "me.lucko.luckperms")
}
}

View File

@ -1,19 +1,24 @@
package com.actualplayer.rememberme;
import com.actualplayer.rememberme.handlers.LoginHandler;
import com.actualplayer.rememberme.handlers.ServerChangeHandler;
import com.actualplayer.rememberme.handlers.*;
import com.google.inject.Inject;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.connection.LoginEvent;
import com.velocitypowered.api.event.player.ServerConnectedEvent;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
import com.velocitypowered.api.plugin.Dependency;
import com.velocitypowered.api.plugin.Plugin;
import com.velocitypowered.api.plugin.annotation.DataDirectory;
import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import lombok.Getter;
import me.lucko.luckperms.LuckPerms;
import me.lucko.luckperms.api.LuckPermsApi;
import org.slf4j.Logger;
import java.nio.file.Path;
@Plugin(id = "@ID@", name = "@NAME@", version = "@VERSION@", description = "@Description@", authors = {"ActualPlayer"})
@Plugin(id = "@ID@", name = "@NAME@", version = "@VERSION@", description = "@Description@", authors = {"ActualPlayer"}, dependencies = { @Dependency(id = "luckperms", optional = true) })
public class RememberMe {
@Getter
@ -27,15 +32,45 @@ public class RememberMe {
@Getter
private Path dataFolderPath;
private IRememberMeHandler handler;
@Inject
public RememberMe(ProxyServer server, Logger logger) {
this.server = server;
this.logger = logger;
}
/**
* If LuckPerms is present, use the User meta tags to save last server
* @param event Velocity init event
*/
@Subscribe
public void onProxyInitialization(ProxyInitializeEvent event) {
server.getEventManager().register(this, new LoginHandler(this));
server.getEventManager().register(this, new ServerChangeHandler(this));
try {
LuckPermsApi api = LuckPerms.getApi();
handler = new LuckPermsHandler(api);
getLogger().info("LuckPerms is installed, using LuckPerms meta-data to store last server info");
} catch (IllegalStateException ex) {
getLogger().error(ex.toString());
handler = new FileHandler(this);
getLogger().info("Using file-based storage");
}
}
@Subscribe
public void onLoginEvent(LoginEvent loginEvent) {
// Ignore plugin when user has notransfer permission
if (!loginEvent.getPlayer().hasPermission("rememberme.notransfer")) {
handler.getLastServerName(loginEvent.getPlayer().getUniqueId()).thenAcceptAsync(lastServerName -> {
if (lastServerName != null) {
getServer().getServer(lastServerName).ifPresent(lastServer -> loginEvent.getPlayer().createConnectionRequest(lastServer).connectWithIndication());
}
});
}
}
@Subscribe
public void onServerChange(ServerConnectedEvent serverConnectedEvent) {
handler.setLastServerName(serverConnectedEvent.getPlayer().getUniqueId(), serverConnectedEvent.getServer().getServerInfo().getName());
}
}

View File

@ -0,0 +1,54 @@
package com.actualplayer.rememberme.handlers;
import com.actualplayer.rememberme.RememberMe;
import com.actualplayer.rememberme.models.UserServer;
import com.actualplayer.rememberme.util.FileUtils;
import com.actualplayer.rememberme.util.YamlUtils;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
public class FileHandler implements IRememberMeHandler {
private RememberMe rememberMe;
public FileHandler(RememberMe rememberMe) {
this.rememberMe = rememberMe;
}
public CompletableFuture<String> getLastServerName(UUID uuid) {
try {
File userFile = FileUtils.getOrCreate(rememberMe.getDataFolderPath().resolve("data"), uuid.toString() + ".yml");
UserServer userServer = YamlUtils.readFile(userFile, UserServer.class);
if(userServer == null) return null;
Optional<RegisteredServer> serverOpt = rememberMe.getServer().getServer(userServer.getServer());
CompletableFuture<String> future = new CompletableFuture<>();
future.complete(serverOpt.map(registeredServer -> registeredServer.getServerInfo().getName()).orElse(null));
return future;
} catch (IOException ex) {
return null;
}
}
public void setLastServerName(UUID uuid, String serverName) {
File userFile = FileUtils.getOrCreate(rememberMe.getDataFolderPath().resolve("data"), uuid.toString() + ".yml");
Map<String, String> userServer = new HashMap<>();
userServer.put("server", serverName);
try {
YamlUtils.writeFile(userFile, userServer);
} catch (IOException ex) {
rememberMe.getLogger().error("Failed to write server name to user file.");
}
}
}

View File

@ -0,0 +1,11 @@
package com.actualplayer.rememberme.handlers;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
public interface IRememberMeHandler {
CompletableFuture<String> getLastServerName(UUID uuid);
void setLastServerName(UUID uuid, String serverName);
}

View File

@ -1,50 +0,0 @@
package com.actualplayer.rememberme.handlers;
import com.actualplayer.rememberme.RememberMe;
import com.actualplayer.rememberme.models.UserServer;
import com.actualplayer.rememberme.util.FileUtils;
import com.actualplayer.rememberme.util.YamlUtils;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.connection.LoginEvent;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import java.io.*;
import java.util.UUID;
public class LoginHandler {
private RememberMe rememberMe;
public LoginHandler(RememberMe rememberMe) {
this.rememberMe = rememberMe;
}
@Subscribe
public void onLoginEvent(LoginEvent loginEvent) {
// Ignore plugin when user has notransfer permission
if (!loginEvent.getPlayer().hasPermission("rememberme.notransfer")) {
RegisteredServer lastServer = getLastServer(loginEvent.getPlayer().getUniqueId());
if (lastServer != null) {
loginEvent.getPlayer().createConnectionRequest(lastServer).connectWithIndication();
}
}
}
/**
* Gets last server for user
* @param uniqueId user's UUID
* @return server user last moved to or NULL if user hasn't joined yet
*/
private RegisteredServer getLastServer(UUID uniqueId) {
try {
File userFile = FileUtils.getOrCreate(rememberMe.getDataFolderPath().resolve("data"), uniqueId.toString() + ".yml");
UserServer userServer = YamlUtils.readFile(userFile, UserServer.class);
if(userServer == null) return null;
return rememberMe.getServer().getServer(userServer.getServer()).orElse(null);
} catch (IOException ex) {
return null;
}
}
}

View File

@ -0,0 +1,51 @@
package com.actualplayer.rememberme.handlers;
import com.actualplayer.rememberme.RememberMe;
import me.lucko.luckperms.api.Contexts;
import me.lucko.luckperms.api.LuckPermsApi;
import me.lucko.luckperms.api.Node;
import me.lucko.luckperms.api.User;
import me.lucko.luckperms.api.caching.MetaData;
import me.lucko.luckperms.api.manager.UserManager;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
public class LuckPermsHandler implements IRememberMeHandler {
private final LuckPermsApi api;
public LuckPermsHandler(LuckPermsApi api) {
this.api = api;
}
@Override
public CompletableFuture<String> getLastServerName(UUID uuid) {
UserManager userManager = api.getUserManager();
CompletableFuture<User> userFuture = userManager.loadUser(uuid);
return userFuture.thenApply(user -> {
if (user != null) {
Optional<Contexts> contextsOpt = api.getContextManager().lookupApplicableContexts(user);
if (contextsOpt.isPresent()) {
MetaData metaData = user.getCachedData().getMetaData(contextsOpt.get());
return metaData.getMeta().getOrDefault("last-server", null);
}
}
return null;
});
}
@Override
public void setLastServerName(UUID uuid, String serverName) {
User user = api.getUser(uuid);
if (user != null) {
Node node = api.getNodeFactory().newBuilder("meta.last-server." + serverName).build();
user.setPermission(node);
api.getUserManager().saveUser(user);
}
}
}

View File

@ -1,33 +0,0 @@
package com.actualplayer.rememberme.handlers;
import com.actualplayer.rememberme.RememberMe;
import com.actualplayer.rememberme.models.UserServer;
import com.actualplayer.rememberme.util.FileUtils;
import com.actualplayer.rememberme.util.YamlUtils;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.player.ServerConnectedEvent;
import org.yaml.snakeyaml.Yaml;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class ServerChangeHandler {
private RememberMe rememberMe;
public ServerChangeHandler(RememberMe rememberMe) {
this.rememberMe = rememberMe;
}
@Subscribe
public void onServerConnectedEvent(ServerConnectedEvent serverConnectedEvent) throws IOException {
File userFile = FileUtils.getOrCreate(rememberMe.getDataFolderPath().resolve("data"), serverConnectedEvent.getPlayer().getUniqueId().toString() + ".yml");
Map<String, String> userServer = new HashMap<>();
userServer.put("server", serverConnectedEvent.getServer().getServerInfo().getName());
YamlUtils.writeFile(userFile, userServer);
}
}