mirror of
https://github.com/xHyroM/lighteco.git
synced 2024-11-10 01:18:07 +01:00
feat: logger, lighteco & loader bootstrap, simulate slow db query in msp
MSP - memory storage provider
This commit is contained in:
parent
7f56f34b47
commit
04f76d21a6
15 changed files with 219 additions and 31 deletions
|
@ -1,7 +0,0 @@
|
|||
package dev.xhyrom.lighteco.api.exceptions;
|
||||
|
||||
public class NotImplementedException extends RuntimeException {
|
||||
public NotImplementedException() {
|
||||
super("This feature is not implemented yet!");
|
||||
}
|
||||
}
|
|
@ -1,11 +1,7 @@
|
|||
package dev.xhyrom.lighteco.api.model.currency;
|
||||
|
||||
import dev.xhyrom.lighteco.api.exceptions.NotImplementedException;
|
||||
import dev.xhyrom.lighteco.api.model.user.User;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public abstract class Currency<T> {
|
||||
@Getter
|
||||
private final Class<T> valueType;
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
package dev.xhyrom.lighteco.bukkit;
|
||||
|
||||
import dev.xhyrom.lighteco.bukkit.logger.BukkitLogger;
|
||||
import dev.xhyrom.lighteco.common.plugin.bootstrap.LightEcoBootstrap;
|
||||
import dev.xhyrom.lighteco.common.plugin.bootstrap.LoaderBootstrap;
|
||||
import dev.xhyrom.lighteco.common.plugin.logger.PluginLogger;
|
||||
import lombok.Getter;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class BukkitLightEcoBootstrap implements LightEcoBootstrap, LoaderBootstrap {
|
||||
@Getter
|
||||
private final JavaPlugin loader;
|
||||
private final BukkitLightEcoPlugin plugin = new BukkitLightEcoPlugin(this);
|
||||
|
||||
@Getter
|
||||
private final PluginLogger logger;
|
||||
|
||||
public BukkitLightEcoBootstrap(JavaPlugin loader) {
|
||||
this.loader = loader;
|
||||
|
||||
this.logger = new BukkitLogger(loader.getSLF4JLogger());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoad() {
|
||||
plugin.load();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
plugin.enable();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
|
||||
}
|
||||
|
||||
public Server getServer() {
|
||||
return loader.getServer();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UUID> getOnlinePlayers() {
|
||||
return getServer().getOnlinePlayers().stream()
|
||||
.map(Entity::getUniqueId)
|
||||
.toList();
|
||||
}
|
||||
}
|
|
@ -1,18 +1,27 @@
|
|||
package dev.xhyrom.lighteco.bukkit;
|
||||
|
||||
import dev.xhyrom.lighteco.bukkit.listeners.BukkitConnectionListener;
|
||||
import dev.xhyrom.lighteco.common.plugin.bootstrap.LoaderBootstrap;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public class BukkitLightEcoLoader extends JavaPlugin {
|
||||
private final BukkitLightEcoPlugin plugin = new BukkitLightEcoPlugin();
|
||||
private final LoaderBootstrap bootstrap;
|
||||
|
||||
public BukkitLightEcoLoader() {
|
||||
this.bootstrap = new BukkitLightEcoBootstrap(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoad() {
|
||||
plugin.enable();
|
||||
this.bootstrap.onLoad();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
getServer().getPluginManager().registerEvents(new BukkitConnectionListener(plugin), this);
|
||||
this.bootstrap.onEnable();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
this.bootstrap.onDisable();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,9 @@
|
|||
package dev.xhyrom.lighteco.bukkit;
|
||||
|
||||
import dev.xhyrom.lighteco.api.managers.ContextManager;
|
||||
import dev.xhyrom.lighteco.api.managers.CurrencyManager;
|
||||
import dev.xhyrom.lighteco.api.managers.UserManager;
|
||||
import dev.xhyrom.lighteco.api.platform.Platform;
|
||||
import dev.xhyrom.lighteco.common.api.impl.ApiCurrencyManager;
|
||||
import dev.xhyrom.lighteco.bukkit.listeners.BukkitConnectionListener;
|
||||
import dev.xhyrom.lighteco.bukkit.managers.BukkitContextManager;
|
||||
import dev.xhyrom.lighteco.common.managers.currency.StandardCurrencyManager;
|
||||
import dev.xhyrom.lighteco.common.plugin.AbstractLightEcoPlugin;
|
||||
import dev.xhyrom.lighteco.common.managers.user.StandardUserManager;
|
||||
|
@ -13,6 +12,9 @@ import org.bukkit.entity.Player;
|
|||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
public class BukkitLightEcoPlugin extends AbstractLightEcoPlugin {
|
||||
@Getter
|
||||
private final BukkitLightEcoBootstrap bootstrap;
|
||||
|
||||
@Getter
|
||||
private StandardUserManager userManager;
|
||||
@Getter
|
||||
|
@ -20,6 +22,15 @@ public class BukkitLightEcoPlugin extends AbstractLightEcoPlugin {
|
|||
@Getter
|
||||
private ContextManager<Player> contextManager;
|
||||
|
||||
public BukkitLightEcoPlugin(BukkitLightEcoBootstrap bootstrap) {
|
||||
this.bootstrap = bootstrap;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void registerListeners() {
|
||||
this.bootstrap.getServer().getPluginManager().registerEvents(new BukkitConnectionListener(this), this.bootstrap.getLoader());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setupManagers() {
|
||||
this.userManager = new StandardUserManager(this);
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package dev.xhyrom.lighteco.bukkit.listeners;
|
||||
|
||||
import dev.xhyrom.lighteco.api.model.user.User;
|
||||
import dev.xhyrom.lighteco.bukkit.BukkitLightEcoPlugin;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
|
@ -10,6 +10,8 @@ import org.bukkit.event.player.PlayerQuitEvent;
|
|||
|
||||
public class BukkitConnectionListener implements Listener {
|
||||
private final BukkitLightEcoPlugin plugin;
|
||||
private final MiniMessage miniMessage = MiniMessage.miniMessage();
|
||||
|
||||
public BukkitConnectionListener(BukkitLightEcoPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
@ -22,16 +24,18 @@ public class BukkitConnectionListener implements Listener {
|
|||
}
|
||||
|
||||
try {
|
||||
User user = this.plugin.getStorage().loadUser(event.getUniqueId()).join();
|
||||
this.plugin.getStorage().loadUser(event.getUniqueId()).join();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
this.plugin.getBootstrap().getLogger()
|
||||
.error("Failed to load user data for %s (%s)", e, event.getName(), event.getUniqueId());
|
||||
|
||||
event.disallow(AsyncPlayerPreLoginEvent.Result.KICK_OTHER, miniMessage.deserialize(
|
||||
"<bold>LightEco</bold> <red>Failed to load your data. Please try again later."
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
public void onPlayerQuit(PlayerQuitEvent event) {
|
||||
// house keeper stuff
|
||||
|
||||
// for now:
|
||||
this.plugin.getUserManager().unload(event.getPlayer().getUniqueId());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,62 @@
|
|||
package dev.xhyrom.lighteco.bukkit.logger;
|
||||
|
||||
import dev.xhyrom.lighteco.common.plugin.logger.PluginLogger;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
public class BukkitLogger implements PluginLogger {
|
||||
private final Logger logger;
|
||||
public BukkitLogger(Logger logger) {
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void info(String message) {
|
||||
this.logger.info(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void info(String message, Object... args) {
|
||||
this.logger.info(String.format(message, args));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warn(String message) {
|
||||
this.logger.warn(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warn(String message, Object... args) {
|
||||
this.logger.warn(String.format(message, args));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warn(String message, Throwable throwable) {
|
||||
this.logger.warn(message, throwable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warn(String message, Throwable throwable, Object... args) {
|
||||
this.logger.warn(String.format(message, args), throwable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void error(String message) {
|
||||
this.logger.error(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void error(String message, Object... args) {
|
||||
this.logger.error(String.format(message, args));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void error(String message, Throwable throwable) {
|
||||
this.logger.error(message, throwable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void error(String message, Throwable throwable, Object... args) {
|
||||
this.logger.error(String.format(message, args), throwable);
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package dev.xhyrom.lighteco.bukkit;
|
||||
package dev.xhyrom.lighteco.bukkit.managers;
|
||||
|
||||
import dev.xhyrom.lighteco.api.managers.ContextManager;
|
||||
import org.bukkit.entity.Player;
|
|
@ -24,8 +24,7 @@ public class StandardUserManager extends AbstractManager<UUID, User> implements
|
|||
@Override
|
||||
public CompletableFuture<Void> load() {
|
||||
Set<UUID> uniqueIds = new HashSet<>(keys());
|
||||
// TODO: Add all players lol
|
||||
//uniqueIds.addAll(this.plugin.getBootstrap().getOnlinePlayers());
|
||||
uniqueIds.addAll(this.plugin.getBootstrap().getOnlinePlayers());
|
||||
|
||||
return uniqueIds.stream()
|
||||
.map(id -> this.plugin.getStorage().loadUser(id))
|
||||
|
|
|
@ -11,16 +11,24 @@ public abstract class AbstractLightEcoPlugin implements LightEcoPlugin {
|
|||
private Storage storage;
|
||||
private LightEcoApi api;
|
||||
|
||||
public final void enable() {
|
||||
public final void load() {
|
||||
// setup storage
|
||||
StorageFactory factory = new StorageFactory(this);
|
||||
|
||||
this.storage = factory.get();
|
||||
|
||||
// setup managers
|
||||
this.setupManagers();
|
||||
|
||||
// register api
|
||||
this.api = new LightEcoApi(this);
|
||||
LightEcoProvider.set(this.api);
|
||||
}
|
||||
|
||||
public final void enable() {
|
||||
// register listeners
|
||||
this.registerListeners();
|
||||
}
|
||||
|
||||
protected abstract void registerListeners();
|
||||
protected abstract void setupManagers();
|
||||
}
|
||||
|
|
|
@ -4,12 +4,15 @@ import dev.xhyrom.lighteco.api.managers.ContextManager;
|
|||
import dev.xhyrom.lighteco.api.platform.Platform;
|
||||
import dev.xhyrom.lighteco.common.managers.currency.CurrencyManager;
|
||||
import dev.xhyrom.lighteco.common.managers.user.UserManager;
|
||||
import dev.xhyrom.lighteco.common.plugin.bootstrap.LightEcoBootstrap;
|
||||
import dev.xhyrom.lighteco.common.storage.Storage;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
public interface LightEcoPlugin {
|
||||
Platform.@NonNull Type getPlatformType();
|
||||
|
||||
@NonNull LightEcoBootstrap getBootstrap();
|
||||
|
||||
@NonNull UserManager getUserManager();
|
||||
@NonNull CurrencyManager getCurrencyManager();
|
||||
@NonNull ContextManager<?> getContextManager();
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
package dev.xhyrom.lighteco.common.plugin.bootstrap;
|
||||
|
||||
import dev.xhyrom.lighteco.common.plugin.logger.PluginLogger;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public interface LightEcoBootstrap {
|
||||
Object getLoader();
|
||||
PluginLogger getLogger();
|
||||
List<UUID> getOnlinePlayers();
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package dev.xhyrom.lighteco.common.plugin.bootstrap;
|
||||
|
||||
public interface LoaderBootstrap {
|
||||
void onLoad();
|
||||
void onEnable();
|
||||
void onDisable();
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package dev.xhyrom.lighteco.common.plugin.logger;
|
||||
|
||||
public interface PluginLogger {
|
||||
void info(String message);
|
||||
void info(String message, Object ...args);
|
||||
|
||||
void warn(String message);
|
||||
void warn(String message, Object ...args);
|
||||
|
||||
void warn(String message, Throwable throwable);
|
||||
void warn(String message, Throwable throwable, Object ...args);
|
||||
|
||||
void error(String message);
|
||||
void error(String message, Object ...args);
|
||||
|
||||
void error(String message, Throwable throwable);
|
||||
void error(String message, Throwable throwable, Object ...args);
|
||||
}
|
|
@ -23,12 +23,16 @@ public class MemoryStorageProvider implements StorageProvider {
|
|||
|
||||
@Override
|
||||
public @NonNull User loadUser(@NonNull UUID uniqueId) {
|
||||
return createUser(uniqueId, userDatabase.get(uniqueId));
|
||||
this.simulateSlowDatabaseQuery();
|
||||
|
||||
return this.createUser(uniqueId, userDatabase.get(uniqueId));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveUser(@NonNull User user) {
|
||||
userDatabase.put(user.getUniqueId(), user);
|
||||
this.simulateSlowDatabaseQuery();
|
||||
|
||||
this.userDatabase.put(user.getUniqueId(), user);
|
||||
}
|
||||
|
||||
private User createUser(UUID uniqueId, User data) {
|
||||
|
@ -36,4 +40,12 @@ public class MemoryStorageProvider implements StorageProvider {
|
|||
|
||||
return user.getProxy();
|
||||
}
|
||||
|
||||
private void simulateSlowDatabaseQuery() {
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue