1
0
Fork 0
mirror of https://github.com/xHyroM/lighteco.git synced 2024-11-21 14:31:09 +01:00

feat: configuration file

This commit is contained in:
Jozef Steinhübl 2023-08-26 10:44:38 +02:00
parent 2ab1bdeb85
commit 7a58f7fbea
7 changed files with 58 additions and 12 deletions

View file

@ -24,6 +24,7 @@ subprojects {
repositories {
mavenCentral()
maven("https://storehouse.okaeri.eu/repository/maven-public/")
}
}

View file

@ -9,6 +9,8 @@ import org.bukkit.Server;
import org.bukkit.entity.Entity;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.nio.file.Path;
import java.util.List;
import java.util.UUID;
@ -27,7 +29,9 @@ public class BukkitLightEcoBootstrap implements LightEcoBootstrap, LoaderBootstr
}
@Override
public void onLoad() {}
public void onLoad() {
plugin.load();
}
@Override
public void onEnable() {
@ -43,6 +47,11 @@ public class BukkitLightEcoBootstrap implements LightEcoBootstrap, LoaderBootstr
return loader.getServer();
}
@Override
public File getDataFolder() {
return loader.getDataFolder();
}
@Override
public List<UUID> getOnlinePlayers() {
return getServer().getOnlinePlayers().stream()

View file

@ -12,6 +12,8 @@ import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.plugin.java.JavaPlugin;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
public class TestPlugin extends JavaPlugin implements Listener {
@Override
@ -34,7 +36,7 @@ public class TestPlugin extends JavaPlugin implements Listener {
}
@EventHandler
public void onWalk(AsyncPlayerChatEvent event) {
public void onWalk(AsyncPlayerChatEvent event) throws ParseException {
Player player = event.getPlayer();
LightEco provider = LightEcoProvider.get();
@ -49,19 +51,22 @@ public class TestPlugin extends JavaPlugin implements Listener {
Currency currency = currencyManager.getCurrency(args[0]);
switch (command) {
case "balance": {
player.sendMessage(user.getBalance(currency).toString());
break;
}
case "add": {
case "balance" -> player.sendMessage(user.getBalance(currency).toString());
case "add" -> {
if (currency.getDecimalPlaces() > 0) {
user.setBalance(currency, BigDecimal.valueOf(Integer.parseInt(args[1])));
DecimalFormat decimalFormat = new DecimalFormat("#0.00");
user.setBalance(currency, BigDecimal.valueOf(
Double.parseDouble(
decimalFormat.format(
Double.parseDouble(args[1])
)
)
));
} else {
user.setBalance(currency, BigDecimal.valueOf(Double.parseDouble(args[1])));
user.setBalance(currency, BigDecimal.valueOf(Integer.parseInt(args[1])));
}
provider.getUserManager().saveUser(user).thenAccept(aVoid -> player.sendMessage("Saved!"));
break;
}
}
}

View file

@ -5,7 +5,8 @@ plugins {
dependencies {
api(project(":lighteco-api"))
implementation("org.spongepowered:configurate-yaml:4.0.0")
implementation("eu.okaeri:okaeri-configs-yaml-snakeyaml:5.0.0-beta.5")
implementation("eu.okaeri:okaeri-configs-validator-okaeri:5.0.0-beta.5")
compileOnly("org.projectlombok:lombok:1.18.28")
annotationProcessor("org.projectlombok:lombok:1.18.28")

View file

@ -1,4 +1,13 @@
package dev.xhyrom.lighteco.common.config;
public class Config {
import eu.okaeri.configs.OkaeriConfig;
import eu.okaeri.configs.annotation.Comment;
import eu.okaeri.configs.annotation.Header;
@Header("LightEco configuration file.")
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.")
@Comment("Used for local currencies.")
public String server = "none";
}

View file

@ -3,15 +3,33 @@ package dev.xhyrom.lighteco.common.plugin;
import dev.xhyrom.lighteco.api.LightEco;
import dev.xhyrom.lighteco.api.LightEcoProvider;
import dev.xhyrom.lighteco.common.api.LightEcoApi;
import dev.xhyrom.lighteco.common.config.Config;
import dev.xhyrom.lighteco.common.storage.Storage;
import dev.xhyrom.lighteco.common.storage.StorageFactory;
import eu.okaeri.configs.ConfigManager;
import eu.okaeri.configs.yaml.snakeyaml.YamlSnakeYamlConfigurer;
import lombok.Getter;
import java.io.File;
public abstract class AbstractLightEcoPlugin implements LightEcoPlugin {
@Getter
private Storage storage;
private LightEcoApi api;
public final void load() {
Config config = ConfigManager.create(Config.class, (it) -> {
File path = new File(this.getBootstrap().getDataFolder(), "config.yml");
path.mkdir();
it.withConfigurer(new YamlSnakeYamlConfigurer());
it.withBindFile(path);
it.withRemoveOrphans(true);
it.saveDefaults();
it.load(true);
});
}
public final void enable() {
// setup storage
StorageFactory factory = new StorageFactory(this);

View file

@ -2,11 +2,14 @@ package dev.xhyrom.lighteco.common.plugin.bootstrap;
import dev.xhyrom.lighteco.common.plugin.logger.PluginLogger;
import java.io.File;
import java.nio.file.Path;
import java.util.List;
import java.util.UUID;
public interface LightEcoBootstrap {
Object getLoader();
PluginLogger getLogger();
File getDataFolder();
List<UUID> getOnlinePlayers();
}