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

feat(currency-money): vault support, feat(common): messages

This commit is contained in:
Jozef Steinhübl 2023-08-27 23:10:15 +02:00
parent c82b04970b
commit f937943bdd
11 changed files with 311 additions and 9 deletions

View file

@ -54,7 +54,8 @@ public class BukkitCommandManager extends AbstractCommandManager {
new CommandAPICommand(currency.getIdentifier())
.withSubcommand(new SetCommand(this, currency, permissionBase).build())
.withSubcommand(new GiveCommand(this, currency, permissionBase).build())
.withSubcommand(new TakeCommand(this, currency, permissionBase).build());
.withSubcommand(new TakeCommand(this, currency, permissionBase).build())
.register();
new PayCommand(this, currency, permissionBase).build().register();

View file

@ -1,5 +1,6 @@
package dev.xhyrom.lighteco.common.config;
import dev.xhyrom.lighteco.common.config.message.MessageConfig;
import dev.xhyrom.lighteco.common.config.storage.StorageConfig;
import eu.okaeri.configs.OkaeriConfig;
import eu.okaeri.configs.annotation.Comment;
@ -15,4 +16,7 @@ public class Config extends OkaeriConfig {
@Comment("Storage settings.")
public StorageConfig storage = new StorageConfig();
@Comment("Messages")
public MessageConfig messages = new MessageConfig();
}

View file

@ -0,0 +1,16 @@
package dev.xhyrom.lighteco.common.config.message;
import eu.okaeri.configs.OkaeriConfig;
import eu.okaeri.configs.annotation.Variable;
public class CurrencyMessageConfig extends OkaeriConfig {
public String balance = "<currency> <dark_gray>| <gray>Your balance: <yellow><balance> </yellow></gray>";
@Variable("balance-others")
public String balanceOthers = "<currency> <dark_gray>| <gray>Balance of <yellow><target> <dark_gray>| <gray><yellow><balance> </yellow></gray>";
public String set = "<currency> <dark_gray>| <gray>Set balance of <yellow><target> <dark_gray>| <gray><yellow><balance> </yellow></gray>";
public String give = "<currency> <dark_gray>| <gray>Gave <yellow><target> <dark_gray>| <gray><yellow><balance> </yellow></gray>";
public String take = "<currency> <dark_gray>| <gray>Took <yellow><target> <dark_gray>| <gray><yellow><balance> </yellow></gray>";
public String pay = "<currency> <dark_gray>| <gray>Paid <gold><amount> <yellow>to <gold><target> <dark_gray>(<gold><taxed_amount> <yellow>after tax)</gray>";
}

View file

@ -0,0 +1,10 @@
package dev.xhyrom.lighteco.common.config.message;
import eu.okaeri.configs.OkaeriConfig;
import java.util.Collections;
import java.util.Map;
public class MessageConfig extends OkaeriConfig {
public Map<String, CurrencyMessageConfig> currency = Collections.singletonMap("default", new CurrencyMessageConfig());
}

View file

@ -1,25 +1,29 @@
package dev.xhyrom.lighteco.common.manager.command;
import dev.xhyrom.lighteco.common.config.message.CurrencyMessageConfig;
import dev.xhyrom.lighteco.common.model.chat.CommandSender;
import dev.xhyrom.lighteco.common.model.currency.Currency;
import dev.xhyrom.lighteco.common.model.user.User;
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
public abstract class AbstractCommandManager implements CommandManager {
public final LightEcoPlugin plugin;
private final MiniMessage miniMessage = MiniMessage.miniMessage();
private final Map<String, CurrencyMessageConfig> config;
private final ArrayList<UUID> mustWait = new ArrayList<>();
public AbstractCommandManager(LightEcoPlugin plugin) {
this.plugin = plugin;
this.config = this.plugin.getConfig().messages.currency;
}
@Override
@ -52,13 +56,27 @@ public abstract class AbstractCommandManager implements CommandManager {
}
}
private CurrencyMessageConfig getConfig(Currency currency) {
CurrencyMessageConfig config = this.config.get(currency.getIdentifier());
if (config == null) {
return this.config.get("default");
}
return config;
}
@Override
public void onBalance(CommandSender sender, Currency currency) {
User user = this.plugin.getUserManager().getIfLoaded(sender.getUniqueId());
BigDecimal balance = user.getBalance(currency);
sender.sendMessage(
miniMessage.deserialize("<yellow>Your balance: <gold>" + balance.toPlainString() + " <yellow>" + currency.getIdentifier())
miniMessage.deserialize(
getConfig(currency).balance,
Placeholder.parsed("currency", currency.getIdentifier()),
Placeholder.parsed("balance", balance.toPlainString())
)
);
}
@ -67,7 +85,13 @@ public abstract class AbstractCommandManager implements CommandManager {
BigDecimal balance = target.getBalance(currency);
sender.sendMessage(
miniMessage.deserialize("<yellow>" + target.getUsername() + "'s balance: <gold>" + balance.toPlainString() + " <yellow>" + currency.getIdentifier())
miniMessage.deserialize(
getConfig(currency).balanceOthers,
Placeholder.parsed("currency", currency.getIdentifier()),
Placeholder.parsed("target", target.getUsername()),
Placeholder.parsed("balance", balance.toPlainString())
)
);
}

View file

@ -4,12 +4,13 @@ plugins {
repositories {
maven("https://repo.papermc.io/repository/maven-public/")
maven("https://jitpack.io")
}
dependencies {
compileOnly(project(":lighteco-api"))
compileOnly("io.papermc.paper:paper-api:1.20.1-R0.1-SNAPSHOT")
compileOnly("com.github.MilkBowl:VaultAPI:1.7")
compileOnly("org.projectlombok:lombok:1.18.28")
annotationProcessor("org.projectlombok:lombok:1.18.28")

View file

@ -5,6 +5,7 @@ import dev.xhyrom.lighteco.api.LightEcoProvider;
import dev.xhyrom.lighteco.api.manager.CommandManager;
import dev.xhyrom.lighteco.api.manager.CurrencyManager;
import dev.xhyrom.lighteco.api.model.currency.Currency;
import dev.xhyrom.lighteco.currency.money.bukkit.hooks.vault.VaultFactory;
import dev.xhyrom.lighteco.currency.money.common.MoneyCurrency;
import org.bukkit.plugin.java.JavaPlugin;
@ -19,6 +20,11 @@ public class BukkitMCLoader extends JavaPlugin {
currencyManager.registerCurrency(currency);
commandManager.registerCurrencyCommand(currency, true);
if (getServer().getPluginManager().getPlugin("Vault") != null) {
getSLF4JLogger().info("Vault found, hooking...");
new VaultFactory(this).hook();
}
}
}

View file

@ -1,4 +0,0 @@
package dev.xhyrom.lighteco.currency.money.bukkit.hooks;
public class Vault {
}

View file

@ -0,0 +1,205 @@
package dev.xhyrom.lighteco.currency.money.bukkit.hooks.vault;
import dev.xhyrom.lighteco.api.LightEco;
import dev.xhyrom.lighteco.api.LightEcoProvider;
import dev.xhyrom.lighteco.api.model.currency.Currency;
import dev.xhyrom.lighteco.api.model.user.User;
import net.milkbowl.vault.economy.AbstractEconomy;
import net.milkbowl.vault.economy.EconomyResponse;
import org.bukkit.Bukkit;
import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;
public class Vault extends AbstractEconomy {
private final LightEco provider;
private final Currency currency;
public Vault() {
this.provider = LightEcoProvider.get();
this.currency = this.provider.getCurrencyManager().getCurrency("money");
}
@Override
public boolean isEnabled() {
return true;
}
@Override
public String getName() {
return "LightEco";
}
@Override
public boolean hasBankSupport() {
return false;
}
@Override
public int fractionalDigits() {
return currency.fractionalDigits();
}
@Override
public String format(double amount) {
return null;
}
@Override
public String currencyNamePlural() {
return null;
}
@Override
public String currencyNameSingular() {
return null;
}
@Override
public boolean hasAccount(String playerName) {
return hasAccount(playerName, null);
}
@Override
public boolean hasAccount(String playerName, String worldName) {
UUID uniqueId = Bukkit.getPlayerUniqueId(playerName);
assert uniqueId != null;
return provider.getUserManager().isLoaded(uniqueId);
}
@Override
public double getBalance(String playerName) {
return getBalance(playerName, null);
}
@Override
public double getBalance(String playerName, String world) {
UUID uniqueId = Bukkit.getPlayerUniqueId(playerName);
User user = provider.getUserManager().loadUser(uniqueId).join();
return bigDecimalToDouble(user.getBalance(currency));
}
@Override
public boolean has(String playerName, double amount) {
return has(playerName, null, amount);
}
@Override
public boolean has(String playerName, String worldName, double amount) {
UUID uniqueId = Bukkit.getPlayerUniqueId(playerName);
User user = provider.getUserManager().loadUser(uniqueId).join();
return user.getBalance(currency).compareTo(BigDecimal.valueOf(amount)) >= 0;
}
@Override
public EconomyResponse withdrawPlayer(String playerName, double amount) {
return withdrawPlayer(playerName, null, amount);
}
@Override
public EconomyResponse withdrawPlayer(String playerName, String worldName, double amount) {
UUID uniqueId = Bukkit.getPlayerUniqueId(playerName);
User user = provider.getUserManager().loadUser(uniqueId).join();
BigDecimal balance = user.getBalance(currency);
BigDecimal newBalance = balance.subtract(BigDecimal.valueOf(amount));
user.setBalance(currency, newBalance);
provider.getUserManager().saveUser(user);
return new EconomyResponse(amount, bigDecimalToDouble(newBalance), EconomyResponse.ResponseType.SUCCESS, "");
}
@Override
public EconomyResponse depositPlayer(String playerName, double amount) {
return depositPlayer(playerName, null, amount);
}
@Override
public EconomyResponse depositPlayer(String playerName, String worldName, double amount) {
UUID uniqueId = Bukkit.getPlayerUniqueId(playerName);
User user = provider.getUserManager().loadUser(uniqueId).join();
BigDecimal balance = user.getBalance(currency);
BigDecimal newBalance = balance.add(BigDecimal.valueOf(amount));
user.setBalance(currency, newBalance);
provider.getUserManager().saveUser(user);
return new EconomyResponse(amount, bigDecimalToDouble(newBalance), EconomyResponse.ResponseType.SUCCESS, "");
}
@Override
public EconomyResponse createBank(String name, String player) {
return null;
}
@Override
public EconomyResponse deleteBank(String name) {
return null;
}
@Override
public EconomyResponse bankBalance(String name) {
return null;
}
@Override
public EconomyResponse bankHas(String name, double amount) {
return null;
}
@Override
public EconomyResponse bankWithdraw(String name, double amount) {
return null;
}
@Override
public EconomyResponse bankDeposit(String name, double amount) {
return null;
}
@Override
public EconomyResponse isBankOwner(String name, String playerName) {
return null;
}
@Override
public EconomyResponse isBankMember(String name, String playerName) {
return null;
}
@Override
public List<String> getBanks() {
return null;
}
@Override
public boolean createPlayerAccount(String playerName) {
return createPlayerAccount(playerName, null);
}
@Override
public boolean createPlayerAccount(String playerName, String worldName) {
UUID uniqueId = Bukkit.getPlayerUniqueId(playerName);
provider.getUserManager().loadUser(uniqueId).join();
return true;
}
private double bigDecimalToDouble(final BigDecimal value) {
double amount = value.doubleValue();
if (new BigDecimal(amount).compareTo(value) > 0) {
amount = Math.nextAfter(amount, Double.NEGATIVE_INFINITY);
}
return amount;
}
}

View file

@ -0,0 +1,24 @@
package dev.xhyrom.lighteco.currency.money.bukkit.hooks.vault;
import dev.xhyrom.lighteco.currency.money.bukkit.BukkitMCLoader;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.plugin.ServicePriority;
import org.bukkit.plugin.ServicesManager;
public class VaultFactory {
private final BukkitMCLoader plugin;
private Vault vault;
public VaultFactory(BukkitMCLoader plugin) {
this.plugin = plugin;
}
public void hook() {
if (this.vault == null)
vault = new Vault();
ServicesManager manager = Bukkit.getServicesManager();
manager.register(Economy.class, vault, this.plugin, ServicePriority.Highest);
}
}

View file

@ -0,0 +1,15 @@
package dev.xhyrom.lighteco.currency.money.bukkit.listener;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.server.ServiceRegisterEvent;
public class BukkitServiceListener implements Listener {
@EventHandler
public void onServiceRegister(ServiceRegisterEvent event) {
if (!(event.getProvider().getProvider() instanceof Economy)) return;
}
}