Adds Vault support for Economy and Permission rewards

This commit is contained in:
Kristian Knarvik 2023-07-09 19:21:34 +02:00
parent 2bc6c2c825
commit d7b1695cd9
7 changed files with 180 additions and 6 deletions

10
pom.xml
View File

@ -86,6 +86,10 @@
<id>knarcraft-repo</id>
<url>https://git.knarcraft.net/api/packages/EpicKnarvik97/maven</url>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
@ -125,5 +129,11 @@
<version>1.2.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.github.MilkBowl</groupId>
<artifactId>VaultAPI</artifactId>
<version>1.7</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@ -65,15 +65,21 @@ import net.knarcraft.minigames.listener.DamageListener;
import net.knarcraft.minigames.listener.InteractListener;
import net.knarcraft.minigames.listener.MoveListener;
import net.knarcraft.minigames.listener.PlayerStateChangeListener;
import net.knarcraft.minigames.manager.EconomyManager;
import net.knarcraft.minigames.manager.PermissionManager;
import net.knarcraft.minigames.placeholder.DropperRecordExpansion;
import net.knarcraft.minigames.placeholder.ParkourRecordExpansion;
import net.md_5.bungee.api.ChatColor;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.PluginCommand;
import org.bukkit.command.TabCompleter;
import org.bukkit.configuration.serialization.ConfigurationSerialization;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.ServicesManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -429,4 +435,25 @@ public final class MiniGames extends JavaPlugin {
stringFormatter.setSuccessColor(ChatColor.GREEN);
}
/**
* Sets up Vault by getting plugins from their providers
*/
private void setupVault() {
ServicesManager servicesManager = this.getServer().getServicesManager();
RegisteredServiceProvider<Permission> permissionProvider = servicesManager.getRegistration(Permission.class);
RegisteredServiceProvider<Economy> economyProvider = servicesManager.getRegistration(Economy.class);
if (permissionProvider != null) {
PermissionManager.initialize(permissionProvider.getProvider());
} else {
log(Level.WARNING, "No Vault permission provider found. Permission rewards are unavailable.");
}
if (economyProvider != null) {
EconomyManager.initialize(economyProvider.getProvider());
} else {
log(Level.WARNING, "No Vault economy provider found. Economy rewards are unavailable.");
}
}
}

View File

@ -2,11 +2,13 @@ package net.knarcraft.minigames.arena.reward;
import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.config.MiniGameMessage;
import net.knarcraft.minigames.manager.EconomyManager;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
/**
* A reward that gives an amount of currency when it's granted
@ -26,15 +28,19 @@ public class EconomyReward implements Reward {
@Override
public boolean grant(@NotNull Player player) {
//TODO: Requires Vault integration
return false;
if (!EconomyManager.isInitialized()) {
MiniGames.log(Level.SEVERE, "An economy reward has been set, but no Vault-compatible economy" +
" plugin has been initialized.");
return false;
}
EconomyManager.deposit(player, amount);
return true;
}
@Override
public @NotNull String getGrantMessage() {
//TODO: Print formatted currency amount and currency unit
return MiniGames.getInstance().getStringFormatter().replacePlaceholder(MiniGameMessage.SUCCESS_ECONOMY_REWARDED,
"{currency}", String.valueOf(amount));
"{currency}", EconomyManager.format(amount));
}
@NotNull

View File

@ -2,6 +2,7 @@ package net.knarcraft.minigames.arena.reward;
import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.config.MiniGameMessage;
import net.knarcraft.minigames.manager.PermissionManager;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
@ -9,6 +10,7 @@ import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
/**
* A reward that grants a specified permission when granted
@ -31,8 +33,17 @@ public class PermissionReward implements Reward {
@Override
public boolean grant(@NotNull Player player) {
//TODO: Vault integration is required
return false;
if (!PermissionManager.isInitialized()) {
MiniGames.log(Level.SEVERE, "A permission reward has been set, but no Vault-compatible permission" +
" plugin has been initialized.");
return false;
}
if (PermissionManager.hasPermission(player, this.permission, this.world != null ? this.world.getName() : null)) {
return false;
} else {
PermissionManager.addPermission(player, this.permission, this.world);
return true;
}
}
@Override

View File

@ -0,0 +1,55 @@
package net.knarcraft.minigames.manager;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.OfflinePlayer;
/**
* A manager that performs all Economy tasks
*/
public final class EconomyManager {
private static Economy economy;
private EconomyManager() {
}
/**
* Initializes the economy manager
*
* @param economy <p>The economy object to use for everything economy-related</p>
*/
public static void initialize(Economy economy) {
EconomyManager.economy = economy;
}
/**
* Checks whether the economy manager has been initialized
*
* @return <p>True if the economy manager has been initialized</p>
*/
public static boolean isInitialized() {
return EconomyManager.economy != null;
}
/**
* Formats the given amount of currency according to the economy plugin's format
*
* @param amount <p>The amount of currency to format</p>
* @return <p>The formatted string</p>
*/
public static String format(double amount) {
return economy.format(amount);
}
/**
* Deposits a given sum into the given player's account
*
* @param player <p>The player to deposit money to</p>
* @param sum <p>The amount of money to deposit</p>
*/
public static void deposit(OfflinePlayer player, double sum) {
economy.depositPlayer(player, sum);
}
}

View File

@ -0,0 +1,64 @@
package net.knarcraft.minigames.manager;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* A manager that performs all Permission tasks
*/
public final class PermissionManager {
private static Permission permission;
private PermissionManager() {
}
/**
* Initializes the permission manager
*
* @param permission <p>The permission object to use for everything permission-related</p>
*/
public static void initialize(Permission permission) {
PermissionManager.permission = permission;
}
/**
* Checks whether the permission manager has been initialized
*
* @return <p>True if the permission manager has been initialized</p>
*/
public static boolean isInitialized() {
return PermissionManager.permission != null;
}
/**
* Grants a permanent permission to a player
*
* @param player <p>The player to grant the permission to</p>
* @param permissionNode <p>The permission node to grant to the player</p>
*/
public static void addPermission(@NotNull Player player, @NotNull String permissionNode, @Nullable World world) {
if (world != null) {
permission.playerAdd(world.getName(), player, permissionNode);
} else {
permission.playerAdd(player, permissionNode);
}
}
/**
* Checks whether the given player has the given permission
*
* @param player <p>The player to check</p>
* @param permissionNode <p>The permission node to check for</p>
* @param world <p>The world to check for the permission</p>
* @return <p>True if the player has the permission</p>
*/
public static boolean hasPermission(@NotNull Player player, @NotNull String permissionNode, @Nullable String world) {
return permission.playerHas(world, player, permissionNode);
}
}

View File

@ -6,6 +6,7 @@ api-version: 1.19
description: A plugin that adds various mini-games
softdepend:
- PlaceholderAPI
- Vault
# Note to self: Aliases must be lowercase!
commands: