Mirror von
https://github.com/TheSilentPro/HeadDB.git
synchronisiert 2024-12-26 02:50:07 +01:00
Change how economy is handled and version check
Dieser Commit ist enthalten in:
Ursprung
d20538aab3
Commit
dab244044a
6
pom.xml
6
pom.xml
@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>tsp.headdb</groupId>
|
||||
<artifactId>HeadDB</artifactId>
|
||||
<version>3.2.4</version>
|
||||
<version>3.3.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>HeadDB</name>
|
||||
@ -30,8 +30,8 @@
|
||||
</repository>
|
||||
<!-- CodeMC -->
|
||||
<repository>
|
||||
<id>codemc-snapshots</id>
|
||||
<url>https://repo.codemc.io/repository/maven-snapshots/</url>
|
||||
<id>CodeMC</id>
|
||||
<url>https://repo.codemc.org/repository/maven-public</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
|
@ -1,12 +1,12 @@
|
||||
package tsp.headdb;
|
||||
|
||||
import net.milkbowl.vault.economy.Economy;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import tsp.headdb.api.HeadAPI;
|
||||
import tsp.headdb.command.CommandHeadDB;
|
||||
import tsp.headdb.database.DatabaseUpdateTask;
|
||||
import tsp.headdb.economy.HEconomyProvider;
|
||||
import tsp.headdb.economy.VaultProvider;
|
||||
import tsp.headdb.listener.JoinListener;
|
||||
import tsp.headdb.listener.MenuListener;
|
||||
import tsp.headdb.listener.PagedPaneListener;
|
||||
@ -14,6 +14,7 @@ import tsp.headdb.storage.PlayerDataFile;
|
||||
import tsp.headdb.util.Localization;
|
||||
import tsp.headdb.util.Log;
|
||||
import tsp.headdb.util.Metrics;
|
||||
import tsp.headdb.util.Utils;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.io.File;
|
||||
@ -21,7 +22,7 @@ import java.io.File;
|
||||
public class HeadDB extends JavaPlugin {
|
||||
|
||||
private static HeadDB instance;
|
||||
private Economy economy;
|
||||
private HEconomyProvider economyProvider;
|
||||
private PlayerDataFile playerData;
|
||||
private Localization localization;
|
||||
|
||||
@ -36,12 +37,11 @@ public class HeadDB extends JavaPlugin {
|
||||
this.playerData.load();
|
||||
|
||||
if (getConfig().getBoolean("economy.enable")) {
|
||||
Log.debug("Starting economy...");
|
||||
this.economy = this.setupEconomy();
|
||||
if (this.economy == null) {
|
||||
Log.error("Economy support requires Vault and an economy provider plugin.");
|
||||
} else {
|
||||
Log.info("Economy provider: " + this.economy.getName());
|
||||
String rawProvider = getConfig().getString("economy.provider", "VAULT");
|
||||
Log.debug("Starting economy with provider: " + rawProvider);
|
||||
if (rawProvider.equalsIgnoreCase("vault")) {
|
||||
economyProvider = new VaultProvider();
|
||||
economyProvider.initProvider();
|
||||
}
|
||||
}
|
||||
|
||||
@ -58,6 +58,14 @@ public class HeadDB extends JavaPlugin {
|
||||
|
||||
Log.debug("Starting metrics...");
|
||||
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!");
|
||||
}
|
||||
|
||||
@ -70,26 +78,17 @@ public class HeadDB extends JavaPlugin {
|
||||
return localization;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Economy getEconomy() {
|
||||
return economy;
|
||||
}
|
||||
|
||||
public PlayerDataFile getPlayerData() {
|
||||
return playerData;
|
||||
}
|
||||
|
||||
public static HeadDB getInstance() {
|
||||
return instance;
|
||||
@Nullable
|
||||
public HEconomyProvider getEconomyProvider() {
|
||||
return economyProvider;
|
||||
}
|
||||
|
||||
private Economy setupEconomy() {
|
||||
if (!this.getServer().getPluginManager().isPluginEnabled("Vault")) return null;
|
||||
|
||||
RegisteredServiceProvider<Economy> economyProvider = this.getServer().getServicesManager().getRegistration(Economy.class);
|
||||
if (economyProvider == null) return null;
|
||||
|
||||
return this.economy = economyProvider.getProvider();
|
||||
public static HeadDB getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
private void createLocalizationFile() {
|
||||
|
21
src/main/java/tsp/headdb/economy/HEconomyProvider.java
Normale Datei
21
src/main/java/tsp/headdb/economy/HEconomyProvider.java
Normale Datei
@ -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() {
|
||||
|
||||
}
|
||||
|
||||
}
|
44
src/main/java/tsp/headdb/economy/VaultProvider.java
Normale Datei
44
src/main/java/tsp/headdb/economy/VaultProvider.java
Normale Datei
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -13,12 +13,14 @@ import tsp.headdb.api.Head;
|
||||
import tsp.headdb.api.HeadAPI;
|
||||
import tsp.headdb.api.LocalHead;
|
||||
import tsp.headdb.database.Category;
|
||||
import tsp.headdb.economy.HEconomyProvider;
|
||||
import tsp.headdb.event.PlayerHeadPurchaseEvent;
|
||||
import tsp.headdb.util.Localization;
|
||||
import tsp.headdb.util.Utils;
|
||||
|
||||
import net.milkbowl.vault.economy.Economy;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@ -307,26 +309,27 @@ public class InventoryUtils {
|
||||
}
|
||||
|
||||
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.
|
||||
// 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.playSound(player, "noEconomy");
|
||||
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 (cost > 0) {
|
||||
if (economy.has(player, cost)) {
|
||||
economy.withdrawPlayer(player, cost);
|
||||
if (cost.compareTo(BigDecimal.ZERO) > 0) {
|
||||
if (economyProvider.canPurchase(player, cost)) {
|
||||
economyProvider.charge(player, cost);
|
||||
Utils.sendMessage(player, String.format(localization.getMessage("purchasedHead"), amount, description, cost));
|
||||
Utils.playSound(player, "paid");
|
||||
return true;
|
||||
}
|
||||
|
||||
Utils.sendMessage(player, String.format(localization.getMessage("notEnoughMoney"), amount, description));
|
||||
Utils.playSound(player, "unavailable");
|
||||
return false;
|
||||
|
@ -1,12 +1,20 @@
|
||||
package tsp.headdb.util;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Sound;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
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;
|
||||
|
||||
public class Utils {
|
||||
@ -14,6 +22,28 @@ public class Utils {
|
||||
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}");
|
||||
|
||||
/**
|
||||
* 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)
|
||||
*
|
||||
|
@ -11,6 +11,8 @@ requireCategoryPermission: false
|
||||
# Economy options
|
||||
economy:
|
||||
enable: false
|
||||
# Supported: VAULT
|
||||
provider: "VAULT"
|
||||
cost:
|
||||
alphabet: 100
|
||||
animals: 100
|
||||
|
@ -3,7 +3,7 @@ description: ${project.description}
|
||||
|
||||
main: tsp.headdb.HeadDB
|
||||
version: ${project.version}
|
||||
softdepend: [Vault]
|
||||
softdepend: [Vault, Treasury]
|
||||
api-version: 1.13
|
||||
author: Silent
|
||||
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren