1
0
Fork 0
mirror of https://github.com/xHyroM/lighteco.git synced 2024-11-22 06:51:05 +01:00

feat: logger, lighteco & loader bootstrap, simulate slow db query in msp

MSP - memory storage provider
This commit is contained in:
xHyroM 2023-08-03 09:45:18 +02:00
parent 7f56f34b47
commit 04f76d21a6
No known key found for this signature in database
GPG key ID: BE0423F386C436AA
15 changed files with 219 additions and 31 deletions

View file

@ -1,7 +0,0 @@
package dev.xhyrom.lighteco.api.exceptions;
public class NotImplementedException extends RuntimeException {
public NotImplementedException() {
super("This feature is not implemented yet!");
}
}

View file

@ -1,11 +1,7 @@
package dev.xhyrom.lighteco.api.model.currency; 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 lombok.Getter;
import java.util.List;
public abstract class Currency<T> { public abstract class Currency<T> {
@Getter @Getter
private final Class<T> valueType; private final Class<T> valueType;

View file

@ -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();
}
}

View file

@ -1,18 +1,27 @@
package dev.xhyrom.lighteco.bukkit; 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; import org.bukkit.plugin.java.JavaPlugin;
public class BukkitLightEcoLoader extends JavaPlugin { public class BukkitLightEcoLoader extends JavaPlugin {
private final BukkitLightEcoPlugin plugin = new BukkitLightEcoPlugin(); private final LoaderBootstrap bootstrap;
public BukkitLightEcoLoader() {
this.bootstrap = new BukkitLightEcoBootstrap(this);
}
@Override @Override
public void onLoad() { public void onLoad() {
plugin.enable(); this.bootstrap.onLoad();
} }
@Override @Override
public void onEnable() { public void onEnable() {
getServer().getPluginManager().registerEvents(new BukkitConnectionListener(plugin), this); this.bootstrap.onEnable();
}
@Override
public void onDisable() {
this.bootstrap.onDisable();
} }
} }

View file

@ -1,10 +1,9 @@
package dev.xhyrom.lighteco.bukkit; package dev.xhyrom.lighteco.bukkit;
import dev.xhyrom.lighteco.api.managers.ContextManager; 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.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.managers.currency.StandardCurrencyManager;
import dev.xhyrom.lighteco.common.plugin.AbstractLightEcoPlugin; import dev.xhyrom.lighteco.common.plugin.AbstractLightEcoPlugin;
import dev.xhyrom.lighteco.common.managers.user.StandardUserManager; import dev.xhyrom.lighteco.common.managers.user.StandardUserManager;
@ -13,6 +12,9 @@ import org.bukkit.entity.Player;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
public class BukkitLightEcoPlugin extends AbstractLightEcoPlugin { public class BukkitLightEcoPlugin extends AbstractLightEcoPlugin {
@Getter
private final BukkitLightEcoBootstrap bootstrap;
@Getter @Getter
private StandardUserManager userManager; private StandardUserManager userManager;
@Getter @Getter
@ -20,6 +22,15 @@ public class BukkitLightEcoPlugin extends AbstractLightEcoPlugin {
@Getter @Getter
private ContextManager<Player> contextManager; 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 @Override
public void setupManagers() { public void setupManagers() {
this.userManager = new StandardUserManager(this); this.userManager = new StandardUserManager(this);

View file

@ -1,7 +1,7 @@
package dev.xhyrom.lighteco.bukkit.listeners; package dev.xhyrom.lighteco.bukkit.listeners;
import dev.xhyrom.lighteco.api.model.user.User;
import dev.xhyrom.lighteco.bukkit.BukkitLightEcoPlugin; import dev.xhyrom.lighteco.bukkit.BukkitLightEcoPlugin;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@ -10,6 +10,8 @@ import org.bukkit.event.player.PlayerQuitEvent;
public class BukkitConnectionListener implements Listener { public class BukkitConnectionListener implements Listener {
private final BukkitLightEcoPlugin plugin; private final BukkitLightEcoPlugin plugin;
private final MiniMessage miniMessage = MiniMessage.miniMessage();
public BukkitConnectionListener(BukkitLightEcoPlugin plugin) { public BukkitConnectionListener(BukkitLightEcoPlugin plugin) {
this.plugin = plugin; this.plugin = plugin;
} }
@ -22,16 +24,18 @@ public class BukkitConnectionListener implements Listener {
} }
try { try {
User user = this.plugin.getStorage().loadUser(event.getUniqueId()).join(); this.plugin.getStorage().loadUser(event.getUniqueId()).join();
} catch (Exception e) { } 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) { public void onPlayerQuit(PlayerQuitEvent event) {
// house keeper stuff
// for now:
this.plugin.getUserManager().unload(event.getPlayer().getUniqueId()); this.plugin.getUserManager().unload(event.getPlayer().getUniqueId());
} }
} }

View file

@ -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);
}
}

View file

@ -1,4 +1,4 @@
package dev.xhyrom.lighteco.bukkit; package dev.xhyrom.lighteco.bukkit.managers;
import dev.xhyrom.lighteco.api.managers.ContextManager; import dev.xhyrom.lighteco.api.managers.ContextManager;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;

View file

@ -24,8 +24,7 @@ public class StandardUserManager extends AbstractManager<UUID, User> implements
@Override @Override
public CompletableFuture<Void> load() { public CompletableFuture<Void> load() {
Set<UUID> uniqueIds = new HashSet<>(keys()); 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() return uniqueIds.stream()
.map(id -> this.plugin.getStorage().loadUser(id)) .map(id -> this.plugin.getStorage().loadUser(id))

View file

@ -11,16 +11,24 @@ public abstract class AbstractLightEcoPlugin implements LightEcoPlugin {
private Storage storage; private Storage storage;
private LightEcoApi api; private LightEcoApi api;
public final void enable() { public final void load() {
// setup storage
StorageFactory factory = new StorageFactory(this); StorageFactory factory = new StorageFactory(this);
this.storage = factory.get(); this.storage = factory.get();
// setup managers
this.setupManagers(); this.setupManagers();
// register api
this.api = new LightEcoApi(this); this.api = new LightEcoApi(this);
LightEcoProvider.set(this.api); LightEcoProvider.set(this.api);
} }
public final void enable() {
// register listeners
this.registerListeners();
}
protected abstract void registerListeners();
protected abstract void setupManagers(); protected abstract void setupManagers();
} }

View file

@ -4,12 +4,15 @@ import dev.xhyrom.lighteco.api.managers.ContextManager;
import dev.xhyrom.lighteco.api.platform.Platform; import dev.xhyrom.lighteco.api.platform.Platform;
import dev.xhyrom.lighteco.common.managers.currency.CurrencyManager; import dev.xhyrom.lighteco.common.managers.currency.CurrencyManager;
import dev.xhyrom.lighteco.common.managers.user.UserManager; import dev.xhyrom.lighteco.common.managers.user.UserManager;
import dev.xhyrom.lighteco.common.plugin.bootstrap.LightEcoBootstrap;
import dev.xhyrom.lighteco.common.storage.Storage; import dev.xhyrom.lighteco.common.storage.Storage;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
public interface LightEcoPlugin { public interface LightEcoPlugin {
Platform.@NonNull Type getPlatformType(); Platform.@NonNull Type getPlatformType();
@NonNull LightEcoBootstrap getBootstrap();
@NonNull UserManager getUserManager(); @NonNull UserManager getUserManager();
@NonNull CurrencyManager getCurrencyManager(); @NonNull CurrencyManager getCurrencyManager();
@NonNull ContextManager<?> getContextManager(); @NonNull ContextManager<?> getContextManager();

View file

@ -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();
}

View file

@ -0,0 +1,7 @@
package dev.xhyrom.lighteco.common.plugin.bootstrap;
public interface LoaderBootstrap {
void onLoad();
void onEnable();
void onDisable();
}

View file

@ -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);
}

View file

@ -23,12 +23,16 @@ public class MemoryStorageProvider implements StorageProvider {
@Override @Override
public @NonNull User loadUser(@NonNull UUID uniqueId) { public @NonNull User loadUser(@NonNull UUID uniqueId) {
return createUser(uniqueId, userDatabase.get(uniqueId)); this.simulateSlowDatabaseQuery();
return this.createUser(uniqueId, userDatabase.get(uniqueId));
} }
@Override @Override
public void saveUser(@NonNull User user) { 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) { private User createUser(UUID uniqueId, User data) {
@ -36,4 +40,12 @@ public class MemoryStorageProvider implements StorageProvider {
return user.getProxy(); return user.getProxy();
} }
private void simulateSlowDatabaseQuery() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} }