From c23a0e26567ff230c380116229a7c001d9888c1c Mon Sep 17 00:00:00 2001 From: Ivan Pekov Date: Sat, 12 Sep 2020 11:05:53 +0300 Subject: [PATCH] Started with velocity cuz its easier I don't understand configme tho --- .../main/java/eu/authme/api/AuthMeAPI.java | 10 +- .../java/eu/authme/common/AuthMePlugin.java | 8 - .../common/{ => api}/AbstractAuthMeAPI.java | 2 +- velocity/pom.xml | 14 ++ .../eu/authme/velocity/AuthMeVelocity.java | 22 ++- .../eu/authme/velocity/VelocityAuthMeAPI.java | 62 ++++++++ .../java/eu/authme/velocity/VelocityUser.java | 137 ++++++++++++++++++ .../listeners/PluginMessageListener.java | 5 + .../eu/authme/velocity/util/Constants.java | 8 + 9 files changed, 249 insertions(+), 19 deletions(-) delete mode 100644 common/src/main/java/eu/authme/common/AuthMePlugin.java rename common/src/main/java/eu/authme/common/{ => api}/AbstractAuthMeAPI.java (93%) create mode 100644 velocity/src/main/java/eu/authme/velocity/VelocityAuthMeAPI.java create mode 100644 velocity/src/main/java/eu/authme/velocity/VelocityUser.java create mode 100644 velocity/src/main/java/eu/authme/velocity/listeners/PluginMessageListener.java create mode 100644 velocity/src/main/java/eu/authme/velocity/util/Constants.java diff --git a/api/src/main/java/eu/authme/api/AuthMeAPI.java b/api/src/main/java/eu/authme/api/AuthMeAPI.java index ee9e049bb..354da9548 100644 --- a/api/src/main/java/eu/authme/api/AuthMeAPI.java +++ b/api/src/main/java/eu/authme/api/AuthMeAPI.java @@ -2,6 +2,7 @@ package eu.authme.api; import java.util.Optional; import java.util.UUID; +import java.util.concurrent.CompletableFuture; // todo: find a way to get this api for platforms with no service manager (singleton????) /** Represents the base AuthMe API */ @@ -11,19 +12,14 @@ public interface AuthMeAPI { * Returns the user with the specified name. If the user has not been found in AuthMe's cache, it * will create a one. * - *

WARNING: This may or may not be a thread blocking method. Call - * asynchronously!!!!!!1!!11!!!! - * * @param name name of the user you want to get * @return non-null user object */ - User getUser(String name); + CompletableFuture getUser(String name); /** * Returns the user with the specified name. * - *

WARNING: This is a thread blocking method. Call asynchronously!!!!!!1!!11!!!! - * * @param name name of the user you want to get * @return user if present in AuthMe's cache, empty optional otherwise */ @@ -32,8 +28,6 @@ public interface AuthMeAPI { /** * Returns the user with the specified unique id. * - *

WARNING: This is a thread blocking method. Call asynchronously!!!!!!1!!11!!!! - * * @param uuid uuid of the user you want to get * @return user if present in AuthMe's cache, empty optional otherwise */ diff --git a/common/src/main/java/eu/authme/common/AuthMePlugin.java b/common/src/main/java/eu/authme/common/AuthMePlugin.java deleted file mode 100644 index 3f1d8a499..000000000 --- a/common/src/main/java/eu/authme/common/AuthMePlugin.java +++ /dev/null @@ -1,8 +0,0 @@ -package eu.authme.common; - -import eu.authme.api.AuthMeAPI; - -public interface AuthMePlugin { - - void registerInServiceManager(AuthMeAPI api); -} diff --git a/common/src/main/java/eu/authme/common/AbstractAuthMeAPI.java b/common/src/main/java/eu/authme/common/api/AbstractAuthMeAPI.java similarity index 93% rename from common/src/main/java/eu/authme/common/AbstractAuthMeAPI.java rename to common/src/main/java/eu/authme/common/api/AbstractAuthMeAPI.java index ddab3fcae..e8f2b7923 100644 --- a/common/src/main/java/eu/authme/common/AbstractAuthMeAPI.java +++ b/common/src/main/java/eu/authme/common/api/AbstractAuthMeAPI.java @@ -1,4 +1,4 @@ -package eu.authme.common; +package eu.authme.common.api; import eu.authme.api.AuthEventListener; import eu.authme.api.AuthMeAPI; diff --git a/velocity/pom.xml b/velocity/pom.xml index b2522aa0a..8796965d9 100644 --- a/velocity/pom.xml +++ b/velocity/pom.xml @@ -50,6 +50,20 @@ ${velocity.version} provided + + + + ch.jalu + configme + ${configme.version} + provided + + + snakeyaml + org.yaml + + + diff --git a/velocity/src/main/java/eu/authme/velocity/AuthMeVelocity.java b/velocity/src/main/java/eu/authme/velocity/AuthMeVelocity.java index 40f3880eb..9de2cad2f 100644 --- a/velocity/src/main/java/eu/authme/velocity/AuthMeVelocity.java +++ b/velocity/src/main/java/eu/authme/velocity/AuthMeVelocity.java @@ -6,11 +6,15 @@ import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; import com.velocitypowered.api.event.proxy.ProxyShutdownEvent; import com.velocitypowered.api.plugin.annotation.DataDirectory; import com.velocitypowered.api.proxy.ProxyServer; +import com.velocitypowered.api.proxy.messages.ChannelIdentifier; +import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier; import java.nio.file.Path; import org.slf4j.Logger; public final class AuthMeVelocity { + public static ChannelIdentifier MESSAGING = MinecraftChannelIdentifier.create("authme", "main"); + private final Logger logger; private final ProxyServer proxy; private final Path dataDirectory; @@ -20,11 +24,25 @@ public final class AuthMeVelocity { this.logger = logger; this.proxy = proxy; this.dataDirectory = dataDirectory; + logger.info("Initialized"); } @Subscribe - public void onInitialize(ProxyInitializeEvent event) {} + public void onInitialize(ProxyInitializeEvent event) { + proxy.getChannelRegistrar().register(MESSAGING); + logger.info("Enabled"); + } @Subscribe - public void onShutdown(ProxyShutdownEvent event) {} + public void onShutdown(ProxyShutdownEvent event) { + logger.info("Disabled"); + } + + public ProxyServer getProxy() { + return proxy; + } + + public Path getDataDirectory() { + return dataDirectory; + } } diff --git a/velocity/src/main/java/eu/authme/velocity/VelocityAuthMeAPI.java b/velocity/src/main/java/eu/authme/velocity/VelocityAuthMeAPI.java new file mode 100644 index 000000000..5e8d3071e --- /dev/null +++ b/velocity/src/main/java/eu/authme/velocity/VelocityAuthMeAPI.java @@ -0,0 +1,62 @@ +package eu.authme.velocity; + +import com.google.common.io.ByteArrayDataOutput; +import com.google.common.io.ByteStreams; +import eu.authme.api.User; +import eu.authme.common.api.AbstractAuthMeAPI; +import java.util.Map; +import java.util.Optional; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ConcurrentHashMap; + +public class VelocityAuthMeAPI extends AbstractAuthMeAPI { + + private final AuthMeVelocity plugin; + private Map userCache = new ConcurrentHashMap<>(); + private Map> pendingFutures = new ConcurrentHashMap<>(); + + public VelocityAuthMeAPI(AuthMeVelocity plugin) { + this.plugin = plugin; + } + + @Override + public CompletableFuture getUser(String name) { + for (VelocityUser user : userCache.values()) { + if (user.getName().equalsIgnoreCase(name)) { + return CompletableFuture.completedFuture(user); + } + } + CompletableFuture future = new CompletableFuture<>(); + ByteArrayDataOutput out = ByteStreams.newDataOutput(); + out.writeUTF("GetUser"); + out.writeUTF(name); + // todo + try { + return future; + } finally { + pendingFutures.put(name, future); + } + } + + @Override + public Optional getUserIfPresent(String name) { + for (VelocityUser user : userCache.values()) { + if (user.getName().equalsIgnoreCase(name)) { + return Optional.of(user); + } + } + return Optional.empty(); + } + + @Override + public Optional getUserIfPresent(UUID uuid) { + return Optional.ofNullable(userCache.get(uuid)); + } + + public void putToUserCache(VelocityUser user) { + userCache.put(user.getUniqueId(), user); + } + + public void modify(VelocityUser user) {} +} diff --git a/velocity/src/main/java/eu/authme/velocity/VelocityUser.java b/velocity/src/main/java/eu/authme/velocity/VelocityUser.java new file mode 100644 index 000000000..f10112d42 --- /dev/null +++ b/velocity/src/main/java/eu/authme/velocity/VelocityUser.java @@ -0,0 +1,137 @@ +package eu.authme.velocity; + +import com.google.common.io.ByteArrayDataOutput; +import com.google.common.io.ByteStreams; +import com.velocitypowered.api.proxy.Player; +import com.velocitypowered.api.proxy.ProxyServer; +import eu.authme.api.User; +import java.time.Instant; +import java.util.Optional; +import java.util.UUID; + +public class VelocityUser implements User { + + private final VelocityAuthMeAPI api; + private final ProxyServer proxy; + private final String name; + private String email; + private final UUID uuid; + private boolean loggedIn, registered; + private Instant registrationDate; + private Instant lastLoginDate; + + public VelocityUser( + VelocityAuthMeAPI api, + ProxyServer proxy, + String name, + String email, + UUID uuid, + boolean loggedIn, + boolean registered, + Instant registrationDate, + Instant lastLoginDate) { + this.name = name; + this.email = email; + this.uuid = uuid; + this.loggedIn = loggedIn; + this.registered = registered; + this.registrationDate = registrationDate; + this.lastLoginDate = lastLoginDate; + this.api = api; + this.proxy = proxy; + } + + @Override + public String getName() { + return name; + } + + @Override + public UUID getUniqueId() { + return uuid; + } + + @Override + public boolean hasLoggedIn() { + return loggedIn; + } + + public void setLoggedIn(boolean loggedIn) { + this.loggedIn = loggedIn; + if (loggedIn) { + api.callUserLogin(this); + } + } + + @Override + public boolean isRegistered() { + return registered; + } + + public void setRegistered(boolean registered) { + this.registered = registered; + if (registered) { + api.callUserRegister(this); + } else { + api.callUserUnregister(this); + } + } + + @Override + public void forceLogin() { + Optional playerOpt = proxy.getPlayer(name); + if (playerOpt.isPresent()) { + Player player = playerOpt.get(); + if (!loggedIn && player.getCurrentServer().isPresent()) { + ByteArrayDataOutput out = ByteStreams.newDataOutput(); + out.writeUTF("ForceLogin"); + out.writeUTF(player.getUniqueId().toString()); + // todo: make sure is in auth server + player.getCurrentServer().get().sendPluginMessage(AuthMeVelocity.MESSAGING, out.toByteArray()); + } + } + } + + @Override + public void forceRegister(String password) { + Optional playerOpt = proxy.getPlayer(name); + if (playerOpt.isPresent()) { + Player player = playerOpt.get(); + if (!registered && player.getCurrentServer().isPresent()) { + ByteArrayDataOutput out = ByteStreams.newDataOutput(); + out.writeUTF("ForceRegister"); + out.writeUTF(player.getUniqueId().toString()); + out.writeUTF(password); + // todo: make sure is in auth server + player.getCurrentServer().get().sendPluginMessage(AuthMeVelocity.MESSAGING, out.toByteArray()); + } + } + } + + @Override + public Optional getRegistrationDate() { + return Optional.ofNullable(registrationDate); + } + + public void setRegistrationDate(Instant date) { + this.registrationDate = date; + } + + @Override + public Optional getLastLoginDate() { + return Optional.ofNullable(lastLoginDate); + } + + public void setLastLoginDate(Instant date) { + this.lastLoginDate = date; + } + + @Override + public Optional getEmail() { + return Optional.ofNullable(email); + } + + public void setEmail(String email) { + this.email = email; + } +} diff --git a/velocity/src/main/java/eu/authme/velocity/listeners/PluginMessageListener.java b/velocity/src/main/java/eu/authme/velocity/listeners/PluginMessageListener.java new file mode 100644 index 000000000..6e461ed71 --- /dev/null +++ b/velocity/src/main/java/eu/authme/velocity/listeners/PluginMessageListener.java @@ -0,0 +1,5 @@ +package eu.authme.velocity.listeners; + +public class PluginMessageListener { + +} diff --git a/velocity/src/main/java/eu/authme/velocity/util/Constants.java b/velocity/src/main/java/eu/authme/velocity/util/Constants.java new file mode 100644 index 000000000..19868ea95 --- /dev/null +++ b/velocity/src/main/java/eu/authme/velocity/util/Constants.java @@ -0,0 +1,8 @@ +package eu.authme.velocity.util; + +import com.google.gson.Gson; + +public final class Constants { + + public static final Gson GSON = new Gson(); +}