Base API & more dependency work

This commit is contained in:
Ivan Pekov 2020-09-11 17:27:55 +03:00
parent f4e0322ec6
commit c8a033f12c
No known key found for this signature in database
GPG Key ID: BC975C392D9CA3A3
9 changed files with 268 additions and 2 deletions

View File

@ -0,0 +1,35 @@
package eu.authme.api;
/**
* Represents auth event listener.
*/
public interface AuthEventListener {
/**
* Called when a {@link User} has logged in.
*
* @param user the user who logged in.
*/
default void onUserLogin(User user) {}
/**
* Called when a {@link User} has changed password.
*
* @param user the user who changed password.
*/
default void onUserChangePassword(User user) {}
/**
* Called when a {@link User} has registered.
*
* @param user the user who has registered.
*/
default void onUserRegister(User user) {}
/**
* Called when a {@link User} has unregistered.
*
* @param user the user who has unregistered.
*/
default void onUserUnregister(User user) {}
}

View File

@ -1,3 +1,48 @@
package eu.authme.api;
public interface AuthMeAPI {}
import java.util.Optional;
import java.util.UUID;
// todo: find a way to get this api for platforms with no service manager (singleton????)
/** Represents the base AuthMe API */
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.
*
* <p><b>WARNING: This may or may not be a thread blocking method. Call
* asynchronously!!!!!!1!!11!!!!</b>
*
* @param name name of the user you want to get
* @return non-null user object
*/
User getUser(String name);
/**
* Returns the user with the specified name.
*
* <p><b>WARNING: This is a thread blocking method. Call asynchronously!!!!!!1!!11!!!!</b>
*
* @param name name of the user you want to get
* @return user if present in AuthMe's cache, empty optional otherwise
*/
Optional<User> getUserIfPresent(String name);
/**
* Returns the user with the specified unique id.
*
* <p><b>WARNING: This is a thread blocking method. Call asynchronously!!!!!!1!!11!!!!</b>
*
* @param uuid uuid of the user you want to get
* @return user if present in AuthMe's cache, empty optional otherwise
*/
Optional<User> getUserIfPresent(UUID uuid);
/**
* Registers the specified {@link AuthEventListener}
*
* @param eventListener event listener
*/
void registerEventListener(AuthEventListener eventListener);
}

View File

@ -0,0 +1,72 @@
package eu.authme.api;
import java.time.Instant;
import java.util.Optional;
import java.util.UUID;
/**
* Represents an (un)authenticated user.
*/
public interface User {
/**
* Returns the name of the user.
*
* @return name
*/
String getName();
/**
* Returns the unique id of the user.
*
* @return unique id
*/
UUID getUniqueId();
/**
* Returns whether or not this user has logged in.
*
* @return boolean value
*/
boolean hasLoggedIn();
/**
* Returns whether or not this user is registered.
*
* @return boolean value
*/
boolean isRegistered();
/**
* Force logins this user if he isn't logged in yet.
*/
void forceLogin();
/**
* Force registers this user with the specified password if he isn't registered yet.
*
* @param password the password you want to register the user with.
*/
void forceRegister(String password);
/**
* Returns the date on which this user has registered.
*
* @return registration date if present, empty optional otherwise
*/
Optional<Instant> getRegistrationDate();
/**
* Returns the date on which this user has last logged in.
*
* @return last login date if present, empty optional otherwise
*/
Optional<Instant> getLastLoginDate();
/**
* Returns the email of the user
*
* @return email if present, empty optional otherwise
*/
Optional<String> getEmail();
}

View File

@ -97,6 +97,14 @@
</repositories>
<dependencies>
<!-- AuthMe commons -->
<dependency>
<groupId>${groupId}</groupId>
<artifactId>authme-common</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<!-- SpigotAPI dependency -->
<dependency>
<groupId>org.spigotmc</groupId>

View File

@ -35,6 +35,14 @@
</repositories>
<dependencies>
<!-- AuthMe commons -->
<dependency>
<groupId>${groupId}</groupId>
<artifactId>authme-common</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<!-- BungeeCord -->
<dependency>
<groupId>net.md-5</groupId>

View File

@ -14,5 +14,44 @@
<name>AuthMe-Common</name>
<inceptionYear>2020</inceptionYear>
<properties>
<maven.deploy.skip>true</maven.deploy.skip>
</properties>
<dependencies>
<!-- AuthMe API -->
<dependency>
<groupId>${groupId}</groupId>
<artifactId>authme-api</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Generate a jar containing classes, resources and shaded libraries -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- Don't create the reduced pom file, as we don't deploy the shaded jar -->
<createDependencyReducedPom>false</createDependencyReducedPom>
<!-- Deploy the non shaded jar as main artifact -->
<shadedArtifactAttached>true</shadedArtifactAttached>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,46 @@
package eu.authme.common;
import eu.authme.api.AuthEventListener;
import eu.authme.api.AuthMeAPI;
import eu.authme.api.User;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Consumer;
public abstract class AbstractAuthMeAPI implements AuthMeAPI {
private Set<AuthEventListener> eventListeners;
public AbstractAuthMeAPI() {
this.eventListeners = new HashSet<>();
}
@Override
public void registerEventListener(AuthEventListener eventListener) {
eventListeners.add(eventListener);
}
public void callUserLogin(User user) {
callEvent(listener -> listener.onUserLogin(user));
}
public void callUserChangePassword(User user) {
callEvent(listener -> listener.onUserChangePassword(user));
}
public void callUserRegister(User user) {
callEvent(listener -> listener.onUserRegister(user));
}
public void callUserUnregister(User user) {
callEvent(listener -> listener.onUserUnregister(user));
}
private void callEvent(Consumer<AuthEventListener> listenerConsumer) {
if (!eventListeners.isEmpty()) {
for (AuthEventListener listener : eventListeners) {
listenerConsumer.accept(listener);
}
}
}
}

View File

@ -1,3 +1,8 @@
package eu.authme.common;
public interface AuthMePlugin {}
import eu.authme.api.AuthMeAPI;
public interface AuthMePlugin {
void registerInServiceManager(AuthMeAPI api);
}

View File

@ -35,6 +35,14 @@
</repositories>
<dependencies>
<!-- AuthMe commons -->
<dependency>
<groupId>${groupId}</groupId>
<artifactId>authme-common</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<!-- Velocity -->
<dependency>
<groupId>com.velocitypowered</groupId>