1
0
Fork 0
mirror of https://github.com/xHyroM/lighteco.git synced 2024-11-10 01:18:07 +01:00

feat: add some basic managers, models, api, bukkit & common

This commit is contained in:
xHyroM 2023-07-30 21:25:07 +02:00
parent d2e7160452
commit 7680f04391
No known key found for this signature in database
GPG key ID: BE0423F386C436AA
23 changed files with 438 additions and 9 deletions

42
api/.gitignore vendored Normal file
View file

@ -0,0 +1,42 @@
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

11
api/build.gradle.kts Normal file
View file

@ -0,0 +1,11 @@
plugins {
id("lighteco.shadow-logic")
}
dependencies {
compileOnly("org.projectlombok:lombok:1.18.28")
annotationProcessor("org.projectlombok:lombok:1.18.28")
compileOnly("org.checkerframework:checker-qual:3.8.0")
compileOnly("org.jetbrains:annotations:20.1.0")
}

View file

@ -0,0 +1,20 @@
package dev.xhyrom.lighteco.api;
import dev.xhyrom.lighteco.api.managers.UserManager;
import dev.xhyrom.lighteco.api.platform.Platform;
import dev.xhyrom.lighteco.api.platform.PlayerAdapter;
import org.checkerframework.checker.nullness.qual.NonNull;
public interface LightEco {
/**
* Gets the {@link Platform}, which represents the current platform the
* plugin is running on.
*
* @return the platform
*/
@NonNull Platform getPlatform();
@NonNull UserManager getUserManager();
<T> @NonNull PlayerAdapter<T> getPlayerAdapter(@NonNull Class<T> playerClass);
}

View file

@ -0,0 +1,35 @@
package dev.xhyrom.lighteco.api;
import lombok.experimental.UtilityClass;
import org.checkerframework.checker.nullness.qual.NonNull;
import static org.jetbrains.annotations.ApiStatus.Internal;
@UtilityClass
public final class LightEcoProvider {
private static LightEco instance;
public static @NonNull LightEco get() {
LightEco instance = LightEcoProvider.instance;
if (instance == null) {
throw new NotLoadedException();
};
return instance;
}
@Internal
public static void set(LightEco instance) {
if (LightEcoProvider.instance != null) {
throw new IllegalStateException("LightEco is already loaded!");
};
LightEcoProvider.instance = instance;
}
private static final class NotLoadedException extends IllegalStateException {
private NotLoadedException() {
super("LightEco is not loaded yet!");
}
}
}

View file

@ -0,0 +1,10 @@
package dev.xhyrom.lighteco.api.managers;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.UUID;
public interface ContextManager<T> {
@NonNull UUID getPlayerUniqueId(@NonNull T context);
@NonNull Class<?> getPlayerClass();
}

View file

@ -0,0 +1,18 @@
package dev.xhyrom.lighteco.api.managers;
import dev.xhyrom.lighteco.api.model.user.User;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
public interface UserManager {
@NonNull CompletableFuture<User> loadUser(@NonNull UUID uniqueId);
@NonNull CompletableFuture<Void> saveUser(@NonNull User user);
@Nullable User getUser(@NonNull UUID uniqueId);
boolean isLoaded(@NonNull UUID uniqueId);
}

View file

@ -0,0 +1,21 @@
package dev.xhyrom.lighteco.api.model.user;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.UUID;
public interface User {
/**
* Get the unique id of this user.
*
* @return the unique id
*/
@NonNull UUID getUniqueId();
/**
* Get the username of this user.
*
* @return the username
*/
@NonNull String getUsername();
}

View file

@ -0,0 +1,23 @@
package dev.xhyrom.lighteco.api.platform;
import org.checkerframework.checker.nullness.qual.NonNull;
public interface Platform {
@NonNull Type getType();
enum Type {
BUKKIT("Bukkit"),
VELOCITY("Velocity"),
BUNGEECORD("BungeeCord");
private final String displayName;
Type(String displayName) {
this.displayName = displayName;
}
public String getName() {
return displayName;
}
}
}

View file

@ -0,0 +1,10 @@
package dev.xhyrom.lighteco.api.platform;
import dev.xhyrom.lighteco.api.model.user.User;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.concurrent.CompletableFuture;
public interface PlayerAdapter<T> {
@NonNull CompletableFuture<User> getUser(@NonNull T player);
}

View file

@ -4,7 +4,7 @@ plugins {
} }
java { java {
toolchain.languageVersion.set(JavaLanguageVersion.of(17)) // toolchain.languageVersion.set(JavaLanguageVersion.of(17))
withSourcesJar() withSourcesJar()
} }

View file

@ -7,5 +7,13 @@ repositories {
} }
dependencies { dependencies {
implementation(project(":lighteco-common"))
compileOnly("io.papermc.paper:paper-api:1.20.1-R0.1-SNAPSHOT") compileOnly("io.papermc.paper:paper-api:1.20.1-R0.1-SNAPSHOT")
compileOnly("org.projectlombok:lombok:1.18.28")
annotationProcessor("org.projectlombok:lombok:1.18.28")
compileOnly("org.checkerframework:checker-qual:3.8.0")
compileOnly("org.jetbrains:annotations:20.1.0")
} }

View file

@ -0,0 +1,19 @@
package dev.xhyrom.lighteco.bukkit;
import dev.xhyrom.lighteco.api.managers.ContextManager;
import org.bukkit.entity.Player;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.UUID;
public class BukkitContextManager implements ContextManager<Player> {
@Override
public @NonNull UUID getPlayerUniqueId(@NonNull Player player) {
return player.getUniqueId();
}
@Override
public @NonNull Class<?> getPlayerClass() {
return Player.class;
}
}

View file

@ -1,4 +1,34 @@
package dev.xhyrom.lighteco.bukkit; package dev.xhyrom.lighteco.bukkit;
public class BukkitLightEcoPlugin extends JavaPlugin { import dev.xhyrom.lighteco.api.managers.ContextManager;
import dev.xhyrom.lighteco.api.managers.UserManager;
import dev.xhyrom.lighteco.api.platform.Platform;
import dev.xhyrom.lighteco.common.LightEcoPlugin;
import dev.xhyrom.lighteco.common.managers.StandardUserManager;
import lombok.Getter;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.checkerframework.checker.nullness.qual.NonNull;
public class BukkitLightEcoPlugin extends JavaPlugin implements LightEcoPlugin {
@Getter
private UserManager userManager;
@Getter
private ContextManager<Player> contextManager;
@Override
public void onLoad() {
this.userManager = new StandardUserManager(this);
this.contextManager = new BukkitContextManager();
}
@Override
public void onEnable() {
getLogger().info("BukkitLightEco is enabled!");
}
@Override
public Platform.@NonNull Type getPlatformType() {
return Platform.Type.BUKKIT;
}
} }

View file

@ -0,0 +1,9 @@
name: ${name}
description: ${description}
version: ${version}
main: dev.xhyrom.lighteco.bukkit.BukkitLightEcoPlugin
author: ${author}
api-version: 1.20
softdepend:
- Vault

View file

@ -1,3 +1,13 @@
plugins { plugins {
id("lighteco.shadow-logic") id("lighteco.shadow-logic")
}
dependencies {
api(project(":lighteco-api"))
compileOnly("org.projectlombok:lombok:1.18.28")
annotationProcessor("org.projectlombok:lombok:1.18.28")
compileOnly("org.checkerframework:checker-qual:3.8.0")
compileOnly("org.jetbrains:annotations:20.1.0")
} }

View file

@ -1,7 +0,0 @@
package dev.xhyrom;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}

View file

@ -0,0 +1,13 @@
package dev.xhyrom.lighteco.common;
import dev.xhyrom.lighteco.api.managers.ContextManager;
import dev.xhyrom.lighteco.api.managers.UserManager;
import dev.xhyrom.lighteco.api.platform.Platform;
import org.checkerframework.checker.nullness.qual.NonNull;
public interface LightEcoPlugin {
Platform.@NonNull Type getPlatformType();
@NonNull UserManager getUserManager();
@NonNull ContextManager<?> getContextManager();
}

View file

@ -0,0 +1,45 @@
package dev.xhyrom.lighteco.common.api;
import dev.xhyrom.lighteco.api.LightEco;
import dev.xhyrom.lighteco.api.managers.UserManager;
import dev.xhyrom.lighteco.api.platform.Platform;
import dev.xhyrom.lighteco.api.platform.PlayerAdapter;
import dev.xhyrom.lighteco.common.LightEcoPlugin;
import dev.xhyrom.lighteco.common.api.impl.ApiPlatform;
import dev.xhyrom.lighteco.common.api.impl.ApiPlayerAdapter;
import org.checkerframework.checker.nullness.qual.NonNull;
public class LightEcoApi implements LightEco {
private final LightEcoPlugin plugin;
private final Platform platform;
private final PlayerAdapter<?> playerAdapter;
public LightEcoApi(LightEcoPlugin plugin) {
this.plugin = plugin;
this.platform = new ApiPlatform(plugin);
this.playerAdapter = new ApiPlayerAdapter<>(plugin.getUserManager(), plugin.getContextManager());
}
@Override
public @NonNull Platform getPlatform() {
return this.platform;
}
@Override
public @NonNull UserManager getUserManager() {
return this.plugin.getUserManager();
}
@Override
public @NonNull <T> PlayerAdapter<T> getPlayerAdapter(@NonNull Class<T> playerClass) {
Class<?> expected = this.plugin.getContextManager().getPlayerClass();
if (!expected.equals(playerClass)) {
throw new IllegalArgumentException("Expected player class " + expected.getName() + ", got " + playerClass.getName());
}
return (PlayerAdapter<T>) this.playerAdapter;
}
}

View file

@ -0,0 +1,18 @@
package dev.xhyrom.lighteco.common.api.impl;
import dev.xhyrom.lighteco.api.platform.Platform;
import dev.xhyrom.lighteco.common.LightEcoPlugin;
import org.checkerframework.checker.nullness.qual.NonNull;
public class ApiPlatform implements Platform {
private final LightEcoPlugin plugin;
public ApiPlatform(LightEcoPlugin plugin) {
this.plugin = plugin;
}
@Override
public @NonNull Type getType() {
return this.plugin.getPlatformType();
}
}

View file

@ -0,0 +1,32 @@
package dev.xhyrom.lighteco.common.api.impl;
import dev.xhyrom.lighteco.api.managers.ContextManager;
import dev.xhyrom.lighteco.api.model.user.User;
import dev.xhyrom.lighteco.api.managers.UserManager;
import dev.xhyrom.lighteco.api.platform.PlayerAdapter;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
public class ApiPlayerAdapter<T> implements PlayerAdapter<T> {
private final UserManager userManager;
private final ContextManager<T> contextManager;
public ApiPlayerAdapter(UserManager userManager, ContextManager<T> contextManager) {
this.userManager = userManager;
this.contextManager = contextManager;
}
@Override
public @NonNull CompletableFuture<User> getUser(@NonNull T player) {
UUID uniqueId = contextManager.getPlayerUniqueId(player);
boolean loaded = userManager.isLoaded(uniqueId);
if (loaded) {
return CompletableFuture.completedFuture(userManager.getUser(uniqueId));
}
return userManager.loadUser(uniqueId);
}
}

View file

@ -0,0 +1,41 @@
package dev.xhyrom.lighteco.common.managers;
import dev.xhyrom.lighteco.api.model.user.User;
import dev.xhyrom.lighteco.api.managers.UserManager;
import dev.xhyrom.lighteco.common.LightEcoPlugin;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
public class StandardUserManager implements UserManager {
private final LightEcoPlugin plugin;
public StandardUserManager(LightEcoPlugin plugin) {
this.plugin = plugin;
}
public User apply(UUID uniqueId) {
return new dev.xhyrom.lighteco.common.model.user.User(uniqueId);
}
@Override
public @NonNull CompletableFuture<User> loadUser(@NonNull UUID uniqueId) {
return null;
}
@Override
public @NonNull CompletableFuture<Void> saveUser(User user) {
return null;
}
@Override
public @Nullable User getUser(@NonNull UUID uniqueId) {
return null;
}
@Override
public boolean isLoaded(@NonNull UUID uniqueId) {
return false;
}
}

View file

@ -0,0 +1,20 @@
package dev.xhyrom.lighteco.common.model.user;
import lombok.Getter;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.UUID;
public class User implements dev.xhyrom.lighteco.api.model.user.User {
@Getter
private final UUID uniqueId;
public User(UUID uniqueId) {
this.uniqueId = uniqueId;
}
@Override
public @NonNull String getUsername() {
return null;
}
}

View file

@ -1,6 +1,7 @@
rootProject.name = "lighteco-parent" rootProject.name = "lighteco-parent"
sequenceOf( sequenceOf(
"api",
"common", "common",
"bukkit", "bukkit",
"velocity" "velocity"