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

feat: command manager [WIP]

must finish this
This commit is contained in:
Jozef Steinhübl 2023-08-26 11:57:08 +02:00
parent 7a58f7fbea
commit b9c7b9eb88
15 changed files with 125 additions and 5 deletions

View file

@ -1,5 +1,6 @@
package dev.xhyrom.lighteco.api;
import dev.xhyrom.lighteco.api.manager.CommandManager;
import dev.xhyrom.lighteco.api.manager.CurrencyManager;
import dev.xhyrom.lighteco.api.manager.UserManager;
import dev.xhyrom.lighteco.api.platform.Platform;
@ -18,6 +19,7 @@ public interface LightEco {
@NonNull UserManager getUserManager();
@NonNull CurrencyManager getCurrencyManager();
@NonNull CommandManager getCommandManager();
<T> @NonNull PlayerAdapter<T> getPlayerAdapter(@NonNull Class<T> playerClass);
}

View file

@ -0,0 +1,8 @@
package dev.xhyrom.lighteco.api.manager;
import dev.xhyrom.lighteco.api.model.currency.Currency;
import org.checkerframework.checker.nullness.qual.NonNull;
public interface CommandManager {
void registerCurrencyCommand(@NonNull Currency currency);
}

View file

@ -9,6 +9,8 @@ repositories {
dependencies {
implementation(project(":lighteco-common"))
implementation("dev.jorel:commandapi-bukkit-shade:9.1.0")
compileOnly("io.papermc.paper:paper-api:1.20.1-R0.1-SNAPSHOT")
compileOnly("org.projectlombok:lombok:1.18.28")
@ -16,4 +18,8 @@ dependencies {
compileOnly("org.checkerframework:checker-qual:3.8.0")
compileOnly("org.jetbrains:annotations:20.1.0")
}
tasks.shadowJar {
relocate("dev.jorel.commandapi", "dev.xhyrom.lighteco.libraries.commandapi")
}

View file

@ -1,5 +1,7 @@
package dev.xhyrom.lighteco.bukkit;
import dev.jorel.commandapi.CommandAPI;
import dev.jorel.commandapi.CommandAPIBukkitConfig;
import dev.xhyrom.lighteco.common.plugin.bootstrap.LoaderBootstrap;
import org.bukkit.plugin.java.JavaPlugin;
@ -12,16 +14,19 @@ public class BukkitLightEcoLoader extends JavaPlugin {
@Override
public void onLoad() {
CommandAPI.onLoad(new CommandAPIBukkitConfig(this).verboseOutput(true));
this.bootstrap.onLoad();
}
@Override
public void onEnable() {
CommandAPI.onEnable();
this.bootstrap.onEnable();
}
@Override
public void onDisable() {
CommandAPI.onDisable();
this.bootstrap.onDisable();
}
}

View file

@ -4,6 +4,7 @@ import dev.xhyrom.lighteco.api.LightEco;
import dev.xhyrom.lighteco.api.manager.ContextManager;
import dev.xhyrom.lighteco.api.platform.Platform;
import dev.xhyrom.lighteco.bukkit.listeners.BukkitConnectionListener;
import dev.xhyrom.lighteco.bukkit.manager.BukkitCommandManager;
import dev.xhyrom.lighteco.bukkit.manager.BukkitContextManager;
import dev.xhyrom.lighteco.common.manager.currency.StandardCurrencyManager;
import dev.xhyrom.lighteco.common.plugin.AbstractLightEcoPlugin;
@ -22,6 +23,8 @@ public class BukkitLightEcoPlugin extends AbstractLightEcoPlugin {
@Getter
private StandardCurrencyManager currencyManager;
@Getter
private BukkitCommandManager commandManager;
@Getter
private ContextManager<Player> contextManager;
public BukkitLightEcoPlugin(BukkitLightEcoBootstrap bootstrap) {
@ -37,6 +40,7 @@ public class BukkitLightEcoPlugin extends AbstractLightEcoPlugin {
public void setupManagers() {
this.userManager = new StandardUserManager(this);
this.currencyManager = new StandardCurrencyManager(this);
this.commandManager = new BukkitCommandManager(this);
this.contextManager = new BukkitContextManager();
}

View file

@ -0,0 +1,26 @@
package dev.xhyrom.lighteco.bukkit.manager;
import dev.jorel.commandapi.CommandAPICommand;
import dev.xhyrom.lighteco.common.manager.command.AbstractCommandManager;
import dev.xhyrom.lighteco.common.model.currency.Currency;
import dev.xhyrom.lighteco.common.model.user.User;
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
import org.checkerframework.checker.nullness.qual.NonNull;
public class BukkitCommandManager extends AbstractCommandManager {
public BukkitCommandManager(LightEcoPlugin plugin) {
super(plugin);
}
@Override
public void registerCurrencyCommand(@NonNull Currency currency) {
new CommandAPICommand(currency.getIdentifier())
.withPermission("lighteco.command.balance." + currency.getIdentifier())
.executesPlayer((sender, args) -> {
User user = this.plugin.getUserManager().getIfLoaded(sender.getUniqueId());
this.onBalance(user, currency);
})
.register();
}
}

View file

@ -33,6 +33,8 @@ public class TestPlugin extends JavaPlugin implements Listener {
currencyManager.getRegisteredCurrencies().forEach(currency -> {
getLogger().info("Currency: " + currency.getIdentifier() + " (" + currency.getType() + ", " + currency.getDecimalPlaces() + ", " + currency.isPayable() + ")");
});
provider.getCommandManager().registerCurrencyCommand(currencyManager.getCurrency("test"));
}
@EventHandler

View file

@ -1,15 +1,13 @@
package dev.xhyrom.lighteco.common.api;
import dev.xhyrom.lighteco.api.LightEco;
import dev.xhyrom.lighteco.api.manager.CommandManager;
import dev.xhyrom.lighteco.api.manager.CurrencyManager;
import dev.xhyrom.lighteco.api.manager.UserManager;
import dev.xhyrom.lighteco.api.platform.Platform;
import dev.xhyrom.lighteco.api.platform.PlayerAdapter;
import dev.xhyrom.lighteco.common.api.impl.ApiCurrencyManager;
import dev.xhyrom.lighteco.common.api.impl.ApiUserManager;
import dev.xhyrom.lighteco.common.api.impl.*;
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;
public class LightEcoApi implements LightEco {
@ -18,6 +16,7 @@ public class LightEcoApi implements LightEco {
private final Platform platform;
private final UserManager userManager;
private final CurrencyManager currencyManager;
private final CommandManager commandManager;
private final PlayerAdapter<?> playerAdapter;
public LightEcoApi(LightEcoPlugin plugin) {
@ -26,6 +25,7 @@ public class LightEcoApi implements LightEco {
this.platform = new ApiPlatform(plugin);
this.userManager = new ApiUserManager(plugin, plugin.getUserManager());
this.currencyManager = new ApiCurrencyManager(plugin, plugin.getCurrencyManager());
this.commandManager = new ApiCommandManager(plugin, plugin.getCommandManager());
this.playerAdapter = new ApiPlayerAdapter<>(userManager, plugin.getContextManager());
}
@ -44,6 +44,11 @@ public class LightEcoApi implements LightEco {
return this.currencyManager;
}
@Override
public @NonNull CommandManager getCommandManager() {
return this.commandManager;
}
@Override
public @NonNull <T> PlayerAdapter<T> getPlayerAdapter(@NonNull Class<T> playerClass) {
Class<?> expected = this.plugin.getContextManager().getPlayerClass();

View file

@ -0,0 +1,19 @@
package dev.xhyrom.lighteco.common.api.impl;
import dev.xhyrom.lighteco.api.manager.CommandManager;
import dev.xhyrom.lighteco.api.model.currency.Currency;
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
import org.checkerframework.checker.nullness.qual.NonNull;
public class ApiCommandManager extends ApiAbstractManager<dev.xhyrom.lighteco.common.manager.command.CommandManager> implements CommandManager {
public ApiCommandManager(LightEcoPlugin plugin, dev.xhyrom.lighteco.common.manager.command.CommandManager handler) {
super(plugin, handler);
}
@Override
public void registerCurrencyCommand(@NonNull Currency currency) {
dev.xhyrom.lighteco.common.model.currency.Currency internal = this.plugin.getCurrencyManager().getIfLoaded(currency.getIdentifier());
this.handler.registerCurrencyCommand(internal);
}
}

View file

@ -5,6 +5,7 @@ import eu.okaeri.configs.annotation.Comment;
import eu.okaeri.configs.annotation.Header;
@Header("LightEco configuration file.")
@Header("")
public class Config extends OkaeriConfig {
@Comment("This property must be unique for each server.")
@Comment("If you have multiple servers, you must set this property to a different value for each server.")

View file

@ -0,0 +1,23 @@
package dev.xhyrom.lighteco.common.manager.command;
import dev.xhyrom.lighteco.common.model.currency.Currency;
import dev.xhyrom.lighteco.common.model.user.User;
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
import java.math.BigDecimal;
public abstract class AbstractCommandManager implements CommandManager {
protected final LightEcoPlugin plugin;
public AbstractCommandManager(LightEcoPlugin plugin) {
this.plugin = plugin;
}
@Override
public void onBalance(User sender, Currency currency) {
BigDecimal balance = sender.getBalance(currency);
System.out.println("Your balance: " + balance.toPlainString() + " " + currency.getIdentifier());
//sender.sendMessage("&aYour balance: &e" + balance.toPlainString() + " &a" + currency.getIdentifier() + "&r");
}
}

View file

@ -0,0 +1,11 @@
package dev.xhyrom.lighteco.common.manager.command;
import dev.xhyrom.lighteco.common.model.currency.Currency;
import dev.xhyrom.lighteco.common.model.user.User;
import org.checkerframework.checker.nullness.qual.NonNull;
public interface CommandManager {
void registerCurrencyCommand(@NonNull Currency currency);
void onBalance(User sender, Currency currency);
}

View file

@ -9,8 +9,8 @@ import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.UUID;
@Getter
public class User {
@Getter
private final LightEcoPlugin plugin;
@Getter
private final ApiUser proxy = new ApiUser(this);

View file

@ -16,6 +16,8 @@ public abstract class AbstractLightEcoPlugin implements LightEcoPlugin {
@Getter
private Storage storage;
private LightEcoApi api;
@Getter
private Config config;
public final void load() {
Config config = ConfigManager.create(Config.class, (it) -> {
@ -28,6 +30,8 @@ public abstract class AbstractLightEcoPlugin implements LightEcoPlugin {
it.saveDefaults();
it.load(true);
});
this.config = config;
}
public final void enable() {

View file

@ -2,6 +2,8 @@ package dev.xhyrom.lighteco.common.plugin;
import dev.xhyrom.lighteco.api.manager.ContextManager;
import dev.xhyrom.lighteco.api.platform.Platform;
import dev.xhyrom.lighteco.common.config.Config;
import dev.xhyrom.lighteco.common.manager.command.CommandManager;
import dev.xhyrom.lighteco.common.manager.currency.CurrencyManager;
import dev.xhyrom.lighteco.common.manager.user.UserManager;
import dev.xhyrom.lighteco.common.plugin.bootstrap.LightEcoBootstrap;
@ -13,8 +15,10 @@ public interface LightEcoPlugin {
@NonNull LightEcoBootstrap getBootstrap();
@NonNull Config getConfig();
@NonNull UserManager getUserManager();
@NonNull CurrencyManager getCurrencyManager();
@NonNull CommandManager getCommandManager();
@NonNull ContextManager<?> getContextManager();
@NonNull Storage getStorage();