Use Vault Economy API for winner money rewards instead of eco console commands
Build / build (push) Successful in 1m16s
Build / build (push) Successful in 1m16s
This commit is contained in:
@@ -469,7 +469,7 @@ public class Arena {
|
||||
spawnCelebration(winnerPlayer);
|
||||
}
|
||||
plugin.getStatsManager().recordWin(winner);
|
||||
plugin.getRewardManager().dispatchWinnerRewards(winnerName, config.getName(), round);
|
||||
plugin.getRewardManager().dispatchWinnerRewards(winner, winnerName, config.getName(), round);
|
||||
} else {
|
||||
for (UUID uuid : allParticipants()) {
|
||||
Player p = plugin.getServer().getPlayer(uuid);
|
||||
|
||||
@@ -57,6 +57,14 @@ public class ConfigManager {
|
||||
return config.getStringList("rewards.winner.commands");
|
||||
}
|
||||
|
||||
public double getRewardMoney() {
|
||||
return config.getDouble("rewards.winner.money", 0);
|
||||
}
|
||||
|
||||
public boolean isVaultEnabled() {
|
||||
return config.getBoolean("integrations.vault", true);
|
||||
}
|
||||
|
||||
public List<String> getDefaultFloorMaterials() {
|
||||
return config.getStringList("default-floor-materials");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package us.tss3.blockparty.economy;
|
||||
|
||||
import net.milkbowl.vault.economy.Economy;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||
import us.tss3.blockparty.BlockPartyPlugin;
|
||||
|
||||
/**
|
||||
* Thin wrapper around the Vault {@link Economy} service. Only ever constructed/touched when
|
||||
* the Vault plugin is actually present (see BlockPartyPlugin#registerVaultEconomy) so a
|
||||
* missing Vault install never causes a classloading error on startup.
|
||||
*/
|
||||
public class VaultEconomyHook {
|
||||
|
||||
private final Economy economy;
|
||||
|
||||
private VaultEconomyHook(Economy economy) {
|
||||
this.economy = economy;
|
||||
}
|
||||
|
||||
/** Returns a hook if Vault is present and has a registered Economy provider, else null. */
|
||||
public static VaultEconomyHook tryCreate(BlockPartyPlugin plugin) {
|
||||
RegisteredServiceProvider<Economy> rsp = plugin.getServer().getServicesManager().getRegistration(Economy.class);
|
||||
if (rsp == null || rsp.getProvider() == null) {
|
||||
return null;
|
||||
}
|
||||
return new VaultEconomyHook(rsp.getProvider());
|
||||
}
|
||||
|
||||
public boolean deposit(OfflinePlayer player, double amount) {
|
||||
if (amount <= 0) {
|
||||
return true;
|
||||
}
|
||||
return economy.depositPlayer(player, amount).transactionSuccess();
|
||||
}
|
||||
}
|
||||
@@ -1,20 +1,33 @@
|
||||
package us.tss3.blockparty.reward;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import us.tss3.blockparty.BlockPartyPlugin;
|
||||
import us.tss3.blockparty.economy.VaultEconomyHook;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.logging.Level;
|
||||
|
||||
/** Dispatches configured console commands as rewards, substituting placeholders. No Vault required. */
|
||||
/**
|
||||
* Dispatches configured winner rewards: an optional Vault-backed money deposit
|
||||
* (rewards.winner.money) plus a list of console commands, both with %player%/%arena%/%round%
|
||||
* placeholders substituted. Vault is looked up fresh on every dispatch (rather than cached at
|
||||
* startup) so plugin load order relative to the underlying economy plugin doesn't matter; a
|
||||
* missing/absent Vault is logged once and simply skips the money portion, never crashing.
|
||||
*/
|
||||
public class RewardManager {
|
||||
|
||||
private final BlockPartyPlugin plugin;
|
||||
private boolean warnedNoVault;
|
||||
|
||||
public RewardManager(BlockPartyPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
public void dispatchWinnerRewards(String playerName, String arenaName, int round) {
|
||||
public void dispatchWinnerRewards(UUID playerId, String playerName, String arenaName, int round) {
|
||||
dispatchMoney(playerId);
|
||||
|
||||
List<String> commands = plugin.getConfigManager().getRewardCommands();
|
||||
for (String cmd : commands) {
|
||||
String parsed = cmd.replace("%player%", playerName)
|
||||
@@ -24,4 +37,37 @@ public class RewardManager {
|
||||
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), parsed));
|
||||
}
|
||||
}
|
||||
|
||||
private void dispatchMoney(UUID playerId) {
|
||||
double amount = plugin.getConfigManager().getRewardMoney();
|
||||
if (amount <= 0) {
|
||||
return;
|
||||
}
|
||||
if (!plugin.getConfigManager().isVaultEnabled() || Bukkit.getPluginManager().getPlugin("Vault") == null) {
|
||||
warnNoVaultOnce();
|
||||
return;
|
||||
}
|
||||
try {
|
||||
VaultEconomyHook economy = VaultEconomyHook.tryCreate(plugin);
|
||||
if (economy == null) {
|
||||
warnNoVaultOnce();
|
||||
return;
|
||||
}
|
||||
OfflinePlayer offline = Bukkit.getOfflinePlayer(playerId);
|
||||
boolean success = economy.deposit(offline, amount);
|
||||
if (!success) {
|
||||
plugin.getLogger().warning("Vault deposit of " + amount + " to " + offline.getName() + " reported failure.");
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
plugin.getLogger().log(Level.WARNING, "Failed to deposit winner reward via Vault", t);
|
||||
}
|
||||
}
|
||||
|
||||
private void warnNoVaultOnce() {
|
||||
if (!warnedNoVault) {
|
||||
plugin.getLogger().warning("rewards.winner.money is set but Vault (with an economy plugin) is not "
|
||||
+ "available - skipping money rewards. Install Vault + an economy plugin, or set money: 0.");
|
||||
warnedNoVault = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,9 +69,14 @@ storage:
|
||||
|
||||
rewards:
|
||||
winner:
|
||||
# Deposited via the Vault Economy API (requires Vault + an economy plugin like EssentialsX).
|
||||
# Set to 0 to disable money rewards entirely; console commands below still run either way.
|
||||
money: 100
|
||||
commands:
|
||||
- "eco give %player% 100"
|
||||
- "give %player% diamond 3"
|
||||
- "say %player% won BlockParty in %arena% after %round% rounds!"
|
||||
|
||||
integrations:
|
||||
placeholderapi: true
|
||||
# If false, money rewards are skipped even when Vault is installed (console commands are unaffected).
|
||||
vault: true
|
||||
|
||||
Reference in New Issue
Block a user