Files
src
build.gradle
manifest.kod
README.md
settings.gradle
Code
17 2,030
src/main/java/dev/kodari/beastwithdraw/WithdrawPlugin.java
package dev.kodari.beastwithdraw;
import com.cryptomorin.xseries.XMaterial;
import com.cryptomorin.xseries.XSound;
import dev.kodari.beastwithdraw.command.RedeemCommand;
import dev.kodari.beastwithdraw.command.WithdrawCommand;
import dev.kodari.beastwithdraw.economy.EconomyProvider;
import dev.kodari.beastwithdraw.economy.EconomyProviders;
import dev.kodari.beastwithdraw.item.WithdrawNoteManager;
import dev.kodari.beastwithdraw.listener.NoteListener;
import dev.kodari.beastwithdraw.storage.NoteStorage;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import java.math.BigDecimal;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
public final class WithdrawPlugin extends JavaPlugin {
private EconomyProvider economyProvider;
private NoteStorage storage;
private WithdrawNoteManager noteManager;
private NamespacedKey noteIdKey;
private NamespacedKey amountKey;
private NamespacedKey currencyKey;
private NamespacedKey ownerKey;
private NamespacedKey createdKey;
private NamespacedKey versionKey;
@Override
public void onEnable() {
saveDefaultConfig();
noteIdKey = new NamespacedKey(this, "note-id");
amountKey = new NamespacedKey(this, "amount");
currencyKey = new NamespacedKey(this, "currency");
ownerKey = new NamespacedKey(this, "owner");
createdKey = new NamespacedKey(this, "created-at");
versionKey = new NamespacedKey(this, "plugin-version");
try {
storage = createStorage();
int pending = storage.voidPendingNotes();
long redeeming = storage.incompleteRedemptionCount();
if (pending > 0) {
getLogger().warning("Voided " + pending + " unfinished note issue(s) from the previous run.");
}
if (redeeming > 0) {
getLogger().severe(redeeming + " note redemption(s) were interrupted after being claimed. "
+ "They remain locked to prevent a possible duplicate payout; inspect the economy and database.");
}
} catch (Exception exception) {
getLogger().severe("Could not open persistent note storage: " + exception.getMessage());
Bukkit.getPluginManager().disablePlugin(this);
return;
}
noteManager = new WithdrawNoteManager(this, storage);
selectEconomy();
Bukkit.getPluginManager().registerEvents(new NoteListener(this), this);
getCommand("withdraw").setExecutor(new WithdrawCommand(this));
getCommand("withdraw").setTabCompleter(new WithdrawCommand(this));
getCommand("redeem").setExecutor(new RedeemCommand(this));
getCommand("redeem").setTabCompleter(new RedeemCommand(this));
if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
registerPlaceholderExpansion();
}
if (economyProvider == null) {
getLogger().severe("No supported economy provider was found. Economy commands are unavailable until one is installed.");
} else {
getLogger().info("Using economy provider: " + economyProvider.getName());
}
}
@Override
public void onDisable() {
if (storage != null) {
try {
storage.close();
} catch (Exception exception) {
getLogger().severe("Could not close note storage cleanly: " + exception.getMessage());
}
}
}
public void reloadPluginConfig() {
reloadConfig();
selectEconomy();
}
private NoteStorage createStorage() throws Exception {
String type = getConfig().getString("storage.type", "SQLITE");
if (!type.equalsIgnoreCase("SQLITE") && !type.equalsIgnoreCase("MYSQL")) {
throw new IllegalArgumentException("storage.type must be SQLITE or MYSQL");
}
return new NoteStorage(getDataFolder(), type,
getConfig().getString("storage.sqlite-file", "notes.db"),
getConfig().getString("storage.mysql.host", "localhost"),
getConfig().getInt("storage.mysql.port", 3306),
getConfig().getString("storage.mysql.database", "beastwithdraw"),
getConfig().getString("storage.mysql.username", "root"),
getConfig().getString("storage.mysql.password", ""),
getConfig().getBoolean("storage.mysql.use-ssl", false), getLogger());
}
private void registerPlaceholderExpansion() {
try {
Class<?> expansionType = Class.forName("dev.kodari.beastwithdraw.placeholder.WithdrawExpansion", true,
getClass().getClassLoader());
Object expansion = expansionType.getConstructor(WithdrawPlugin.class).newInstance(this);
expansionType.getMethod("register").invoke(expansion);
} catch (ReflectiveOperationException | LinkageError exception) {
getLogger().warning("PlaceholderAPI was detected but the withdraw expansion could not be registered: "
+ exception.getMessage());
}
}
private void selectEconomy() {
economyProvider = EconomyProviders.select(this).orElse(null);
if (noteManager != null && economyProvider != null) {
noteManager.refreshNoteMaterial();
}
}
public EconomyProvider economy() {
return economyProvider;
}
public NoteStorage storage() {
return storage;
}
public WithdrawNoteManager notes() {
return noteManager;
}
public NamespacedKey noteIdKey() {
return noteIdKey;
}
public NamespacedKey amountKey() {
return amountKey;
}
public NamespacedKey currencyKey() {
return currencyKey;
}
public NamespacedKey ownerKey() {
return ownerKey;
}
public NamespacedKey createdKey() {
return createdKey;
}
public NamespacedKey versionKey() {
return versionKey;
}
public String pluginVersion() {
return getDescription().getVersion();
}
public Optional<BigDecimal> parseAmount(String input) {
if (input == null || !input.matches("[0-9]+(?:\\.[0-9]+)?")) {
return Optional.empty();
}
try {
BigDecimal amount = new BigDecimal(input);
if (amount.signum() <= 0 || !Double.isFinite(amount.doubleValue())) {
return Optional.empty();
}
if (economyProvider != null) {
int precision = economyProvider.getFractionalDigits();
if (precision >= 0 && amount.stripTrailingZeros().scale() > precision) {
return Optional.empty();
}
}
return Optional.of(amount.stripTrailingZeros());
} catch (NumberFormatException exception) {
return Optional.empty();
}
}
public boolean isWithinWithdrawalLimits(BigDecimal amount) {
BigDecimal minimum = BigDecimal.valueOf(getConfig().getDouble("withdraw.minimum", 1.0));
BigDecimal maximum = BigDecimal.valueOf(getConfig().getDouble("withdraw.maximum", 1_000_000_000.0));
return amount.compareTo(minimum) >= 0 && amount.compareTo(maximum) <= 0;
}
public String formatAmount(BigDecimal amount) {
return amount.stripTrailingZeros().toPlainString();