3
0
Mirror von https://github.com/TheSilentPro/HeadDB.git synchronisiert 2024-12-27 11:20:05 +01:00

Change how economy is handled and version check

Dieser Commit ist enthalten in:
Silent 2022-02-20 21:38:34 +01:00
Ursprung d20538aab3
Commit dab244044a
8 geänderte Dateien mit 132 neuen und 33 gelöschten Zeilen

Datei anzeigen

@ -6,7 +6,7 @@
<groupId>tsp.headdb</groupId> <groupId>tsp.headdb</groupId>
<artifactId>HeadDB</artifactId> <artifactId>HeadDB</artifactId>
<version>3.2.4</version> <version>3.3.0</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>HeadDB</name> <name>HeadDB</name>
@ -30,8 +30,8 @@
</repository> </repository>
<!-- CodeMC --> <!-- CodeMC -->
<repository> <repository>
<id>codemc-snapshots</id> <id>CodeMC</id>
<url>https://repo.codemc.io/repository/maven-snapshots/</url> <url>https://repo.codemc.org/repository/maven-public</url>
</repository> </repository>
</repositories> </repositories>

Datei anzeigen

@ -1,12 +1,12 @@
package tsp.headdb; package tsp.headdb;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import tsp.headdb.api.HeadAPI; import tsp.headdb.api.HeadAPI;
import tsp.headdb.command.CommandHeadDB; import tsp.headdb.command.CommandHeadDB;
import tsp.headdb.database.DatabaseUpdateTask; import tsp.headdb.database.DatabaseUpdateTask;
import tsp.headdb.economy.HEconomyProvider;
import tsp.headdb.economy.VaultProvider;
import tsp.headdb.listener.JoinListener; import tsp.headdb.listener.JoinListener;
import tsp.headdb.listener.MenuListener; import tsp.headdb.listener.MenuListener;
import tsp.headdb.listener.PagedPaneListener; import tsp.headdb.listener.PagedPaneListener;
@ -14,6 +14,7 @@ import tsp.headdb.storage.PlayerDataFile;
import tsp.headdb.util.Localization; import tsp.headdb.util.Localization;
import tsp.headdb.util.Log; import tsp.headdb.util.Log;
import tsp.headdb.util.Metrics; import tsp.headdb.util.Metrics;
import tsp.headdb.util.Utils;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.io.File; import java.io.File;
@ -21,7 +22,7 @@ import java.io.File;
public class HeadDB extends JavaPlugin { public class HeadDB extends JavaPlugin {
private static HeadDB instance; private static HeadDB instance;
private Economy economy; private HEconomyProvider economyProvider;
private PlayerDataFile playerData; private PlayerDataFile playerData;
private Localization localization; private Localization localization;
@ -36,12 +37,11 @@ public class HeadDB extends JavaPlugin {
this.playerData.load(); this.playerData.load();
if (getConfig().getBoolean("economy.enable")) { if (getConfig().getBoolean("economy.enable")) {
Log.debug("Starting economy..."); String rawProvider = getConfig().getString("economy.provider", "VAULT");
this.economy = this.setupEconomy(); Log.debug("Starting economy with provider: " + rawProvider);
if (this.economy == null) { if (rawProvider.equalsIgnoreCase("vault")) {
Log.error("Economy support requires Vault and an economy provider plugin."); economyProvider = new VaultProvider();
} else { economyProvider.initProvider();
Log.info("Economy provider: " + this.economy.getName());
} }
} }
@ -58,6 +58,14 @@ public class HeadDB extends JavaPlugin {
Log.debug("Starting metrics..."); Log.debug("Starting metrics...");
new Metrics(this, 9152); new Metrics(this, 9152);
Utils.isLatestVersion(this, 84967, latest -> {
if (!latest) {
Log.warning("There is a new update available for HeadDB on spigot!");
Log.warning("Download: https://www.spigotmc.org/resources/84967");
}
});
Log.info("Done!"); Log.info("Done!");
} }
@ -70,26 +78,17 @@ public class HeadDB extends JavaPlugin {
return localization; return localization;
} }
@Nullable
public Economy getEconomy() {
return economy;
}
public PlayerDataFile getPlayerData() { public PlayerDataFile getPlayerData() {
return playerData; return playerData;
} }
public static HeadDB getInstance() { @Nullable
return instance; public HEconomyProvider getEconomyProvider() {
return economyProvider;
} }
private Economy setupEconomy() { public static HeadDB getInstance() {
if (!this.getServer().getPluginManager().isPluginEnabled("Vault")) return null; return instance;
RegisteredServiceProvider<Economy> economyProvider = this.getServer().getServicesManager().getRegistration(Economy.class);
if (economyProvider == null) return null;
return this.economy = economyProvider.getProvider();
} }
private void createLocalizationFile() { private void createLocalizationFile() {

Datei anzeigen

@ -0,0 +1,21 @@
package tsp.headdb.economy;
import org.bukkit.entity.Player;
import java.math.BigDecimal;
public interface HEconomyProvider {
default boolean canPurchase(Player player, BigDecimal cost) {
return true;
}
default void charge(Player player, BigDecimal amount) {
}
default void initProvider() {
}
}

Datei anzeigen

@ -0,0 +1,44 @@
package tsp.headdb.economy;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider;
import tsp.headdb.util.Log;
import java.math.BigDecimal;
public class VaultProvider implements HEconomyProvider {
private Economy economy;
@Override
public boolean canPurchase(Player player, BigDecimal cost) {
return economy.has(player, cost.doubleValue());
}
@Override
public void charge(Player player, BigDecimal amount) {
economy.withdrawPlayer(player, amount.doubleValue());
}
public void initProvider() {
if (!Bukkit.getServer().getPluginManager().isPluginEnabled("Vault")) {
Log.error("Vault is not installed!");
return;
}
RegisteredServiceProvider<Economy> economyProvider = Bukkit.getServer().getServicesManager().getRegistration(Economy.class);
if (economyProvider == null) {
Log.error("Could not find vault economy provider!");
return;
}
economy = economyProvider.getProvider();
}
public Economy getProvider() {
return economy;
}
}

Datei anzeigen

@ -13,12 +13,14 @@ import tsp.headdb.api.Head;
import tsp.headdb.api.HeadAPI; import tsp.headdb.api.HeadAPI;
import tsp.headdb.api.LocalHead; import tsp.headdb.api.LocalHead;
import tsp.headdb.database.Category; import tsp.headdb.database.Category;
import tsp.headdb.economy.HEconomyProvider;
import tsp.headdb.event.PlayerHeadPurchaseEvent; import tsp.headdb.event.PlayerHeadPurchaseEvent;
import tsp.headdb.util.Localization; import tsp.headdb.util.Localization;
import tsp.headdb.util.Utils; import tsp.headdb.util.Utils;
import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.economy.Economy;
import java.math.BigDecimal;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -307,26 +309,27 @@ public class InventoryUtils {
} }
public static boolean processPayment(Player player, int amount, String category, String description) { public static boolean processPayment(Player player, int amount, String category, String description) {
Economy economy = HeadDB.getInstance().getEconomy(); HEconomyProvider economyProvider = HeadDB.getInstance().getEconomyProvider();
// If economy is disabled or no plugin is present, the item is free. // If economy is disabled or no plugin is present, the item is free.
// Don't mention receiving it for free in this case, since it is always free. // Don't mention receiving it for free in this case, since it is always free.
if (economy == null) { if (economyProvider == null) {
Utils.sendMessage(player, String.format(localization.getMessage("noEconomy"), amount, description)); Utils.sendMessage(player, String.format(localization.getMessage("noEconomy"), amount, description));
Utils.playSound(player, "noEconomy"); Utils.playSound(player, "noEconomy");
return true; return true;
} }
double cost = getCategoryCost(player, category) * amount; BigDecimal cost = BigDecimal.valueOf(getCategoryCost(player, category) * amount);
// If the cost is higher than zero, attempt to charge for it. // If the cost is higher than zero, attempt to charge for it.
if (cost > 0) { if (cost.compareTo(BigDecimal.ZERO) > 0) {
if (economy.has(player, cost)) { if (economyProvider.canPurchase(player, cost)) {
economy.withdrawPlayer(player, cost); economyProvider.charge(player, cost);
Utils.sendMessage(player, String.format(localization.getMessage("purchasedHead"), amount, description, cost)); Utils.sendMessage(player, String.format(localization.getMessage("purchasedHead"), amount, description, cost));
Utils.playSound(player, "paid"); Utils.playSound(player, "paid");
return true; return true;
} }
Utils.sendMessage(player, String.format(localization.getMessage("notEnoughMoney"), amount, description)); Utils.sendMessage(player, String.format(localization.getMessage("notEnoughMoney"), amount, description));
Utils.playSound(player, "unavailable"); Utils.playSound(player, "unavailable");
return false; return false;

Datei anzeigen

@ -1,12 +1,20 @@
package tsp.headdb.util; package tsp.headdb.util;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Sound; import org.bukkit.Sound;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import tsp.headdb.HeadDB; import tsp.headdb.HeadDB;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.function.Consumer;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class Utils { public class Utils {
@ -14,6 +22,28 @@ public class Utils {
private static final FileConfiguration config = HeadDB.getInstance().getConfig(); private static final FileConfiguration config = HeadDB.getInstance().getConfig();
public static final Pattern UUID_PATTERN = Pattern.compile("[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"); public static final Pattern UUID_PATTERN = Pattern.compile("[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}");
/**
* Retrieve the latest release from spigot
*
* @param plugin The plugin
* @param id The resource id on spigot
* @param latest Whether the plugin is on the latest version
*/
public static void isLatestVersion(JavaPlugin plugin, int id, Consumer<Boolean> latest) {
Bukkit.getScheduler().runTaskAsynchronously(plugin, checkTask -> {
try {
URLConnection connection = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + id).openConnection();
connection.setConnectTimeout(5000);
connection.setRequestProperty("User-Agent", plugin.getName() + "-VersionChecker");
latest.accept(new BufferedReader(new InputStreamReader(connection.getInputStream())).readLine().equals(plugin.getDescription().getVersion()));
} catch (IOException e) {
latest.accept(true); // Assume the version is latest if checking fails
e.printStackTrace();
}
});
}
/** /**
* Validate a UUID (version 4) * Validate a UUID (version 4)
* *

Datei anzeigen

@ -11,6 +11,8 @@ requireCategoryPermission: false
# Economy options # Economy options
economy: economy:
enable: false enable: false
# Supported: VAULT
provider: "VAULT"
cost: cost:
alphabet: 100 alphabet: 100
animals: 100 animals: 100

Datei anzeigen

@ -3,7 +3,7 @@ description: ${project.description}
main: tsp.headdb.HeadDB main: tsp.headdb.HeadDB
version: ${project.version} version: ${project.version}
softdepend: [Vault] softdepend: [Vault, Treasury]
api-version: 1.13 api-version: 1.13
author: Silent author: Silent