mirror of
https://github.com/xHyroM/lighteco.git
synced 2024-11-22 06:51:05 +01:00
feat: storage, abstract plugin
This commit is contained in:
parent
7680f04391
commit
cefa84404b
11 changed files with 156 additions and 13 deletions
|
@ -0,0 +1,14 @@
|
|||
package dev.xhyrom.lighteco.api.storage;
|
||||
|
||||
import dev.xhyrom.lighteco.api.model.user.User;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public interface StorageProvider {
|
||||
@NonNull String[] getIdentifiers();
|
||||
|
||||
@NonNull User loadUser(@NonNull UUID uniqueId) throws Exception;
|
||||
// todo: look at this
|
||||
void saveUser(@NonNull User user) /*throws Exception;*/;
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package dev.xhyrom.lighteco.bukkit;
|
||||
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public class BukkitLightEcoLoader extends JavaPlugin {
|
||||
private BukkitLightEcoPlugin plugin = new BukkitLightEcoPlugin();
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
plugin.enable();
|
||||
}
|
||||
}
|
|
@ -3,30 +3,24 @@ package dev.xhyrom.lighteco.bukkit;
|
|||
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.plugin.AbstractLightEcoPlugin;
|
||||
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 {
|
||||
public class BukkitLightEcoPlugin extends AbstractLightEcoPlugin {
|
||||
@Getter
|
||||
private UserManager userManager;
|
||||
@Getter
|
||||
private ContextManager<Player> contextManager;
|
||||
|
||||
@Override
|
||||
public void onLoad() {
|
||||
public void setupManagers() {
|
||||
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;
|
||||
|
|
|
@ -4,7 +4,7 @@ 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.plugin.LightEcoPlugin;
|
||||
import dev.xhyrom.lighteco.common.api.impl.ApiPlatform;
|
||||
import dev.xhyrom.lighteco.common.api.impl.ApiPlayerAdapter;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package dev.xhyrom.lighteco.common.api.impl;
|
||||
|
||||
import dev.xhyrom.lighteco.api.platform.Platform;
|
||||
import dev.xhyrom.lighteco.common.LightEcoPlugin;
|
||||
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
public class ApiPlatform implements Platform {
|
||||
|
|
|
@ -2,7 +2,7 @@ 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 dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
package dev.xhyrom.lighteco.common.plugin;
|
||||
|
||||
import dev.xhyrom.lighteco.common.storage.Storage;
|
||||
import dev.xhyrom.lighteco.common.storage.StorageFactory;
|
||||
import lombok.Getter;
|
||||
|
||||
public abstract class AbstractLightEcoPlugin implements LightEcoPlugin {
|
||||
@Getter
|
||||
private Storage storage;
|
||||
|
||||
public final void enable() {
|
||||
StorageFactory factory = new StorageFactory(this);
|
||||
|
||||
this.storage = factory.get();
|
||||
}
|
||||
|
||||
protected abstract void setupManagers();
|
||||
}
|
|
@ -1,8 +1,9 @@
|
|||
package dev.xhyrom.lighteco.common;
|
||||
package dev.xhyrom.lighteco.common.plugin;
|
||||
|
||||
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.storage.Storage;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
public interface LightEcoPlugin {
|
||||
|
@ -10,4 +11,6 @@ public interface LightEcoPlugin {
|
|||
|
||||
@NonNull UserManager getUserManager();
|
||||
@NonNull ContextManager<?> getContextManager();
|
||||
|
||||
@NonNull Storage getStorage();
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package dev.xhyrom.lighteco.common.storage;
|
||||
|
||||
import dev.xhyrom.lighteco.api.model.user.User;
|
||||
import dev.xhyrom.lighteco.api.storage.StorageProvider;
|
||||
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CompletionException;
|
||||
|
||||
public class Storage {
|
||||
private final LightEcoPlugin plugin;
|
||||
private final StorageProvider provider;
|
||||
|
||||
public Storage(LightEcoPlugin plugin, StorageProvider provider) {
|
||||
this.plugin = plugin;
|
||||
this.provider = provider;
|
||||
}
|
||||
|
||||
private <T> CompletableFuture<T> future(Callable<T> callable) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
try {
|
||||
return callable.call();
|
||||
} catch (Exception e) {
|
||||
if (e instanceof RuntimeException) {
|
||||
throw (RuntimeException) e;
|
||||
}
|
||||
|
||||
throw new CompletionException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private CompletableFuture<Void> future(Runnable runnable) {
|
||||
return CompletableFuture.runAsync(runnable);
|
||||
}
|
||||
|
||||
public CompletableFuture<User> loadUser(UUID uniqueId) {
|
||||
return future(() -> this.provider.loadUser(uniqueId));
|
||||
}
|
||||
|
||||
public CompletableFuture<Void> saveUser(User user) {
|
||||
return future(() -> this.provider.saveUser(user));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package dev.xhyrom.lighteco.common.storage;
|
||||
|
||||
import dev.xhyrom.lighteco.api.storage.StorageProvider;
|
||||
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
|
||||
import dev.xhyrom.lighteco.common.storage.provider.memory.MemoryStorageProvider;
|
||||
|
||||
public class StorageFactory {
|
||||
private final LightEcoPlugin plugin;
|
||||
|
||||
public StorageFactory(LightEcoPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
public Storage get() {
|
||||
// todo: use config
|
||||
String provider = "memory";//this.plugin.getConfig().getString("storage.provider");
|
||||
|
||||
return new Storage(this.plugin, createProvider(provider));
|
||||
}
|
||||
|
||||
private StorageProvider createProvider(String provider) {
|
||||
switch (provider.toLowerCase()) {
|
||||
case "memory":
|
||||
return new MemoryStorageProvider();
|
||||
default:
|
||||
throw new IllegalArgumentException("Unknown storage provider: " + provider);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package dev.xhyrom.lighteco.common.storage.provider.memory;
|
||||
|
||||
import dev.xhyrom.lighteco.api.model.user.User;
|
||||
import dev.xhyrom.lighteco.api.storage.StorageProvider;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.UUID;
|
||||
|
||||
public class MemoryStorageProvider implements StorageProvider {
|
||||
private HashMap<UUID, User> users = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public @NonNull String[] getIdentifiers() {
|
||||
return new String[] {"memory"};
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull User loadUser(@NonNull UUID uniqueId) throws Exception {
|
||||
return users.get(uniqueId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveUser(@NonNull User user) throws Exception {
|
||||
users.put(user.getUniqueId(), user);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue