mirror of
https://github.com/Crazy-Crew/CrazyAuctions.git
synced 2025-01-07 19:28:10 +01:00
Add multiple currency boilerplate
This commit is contained in:
parent
a837010921
commit
acbf1fb589
@ -1,36 +0,0 @@
|
||||
package com.badbones69.crazyauctions.api;
|
||||
|
||||
public enum ShopType {
|
||||
|
||||
SELL("Sell"), BID("Bid");
|
||||
|
||||
private final String name;
|
||||
|
||||
/**
|
||||
* @param name name of the Shop Type.
|
||||
*/
|
||||
private ShopType(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param name name of the Type you want.
|
||||
* @return Returns the Type as an Enum.
|
||||
*/
|
||||
public static ShopType getFromName(String name) {
|
||||
for (ShopType type : ShopType.values()) {
|
||||
if (type.getName().equalsIgnoreCase(name)) {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Returns the type name as a string.
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package com.badbones69.crazyauctions.api.economy;
|
||||
|
||||
public enum Currency {
|
||||
|
||||
VAULT("Vault"),
|
||||
XP_LEVEL("XP_Level"),
|
||||
XP_TOTAL("XP_Total");
|
||||
|
||||
private final String name;
|
||||
|
||||
Currency(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if it is a compatible currency.
|
||||
* @param currency The currency name you are checking.
|
||||
* @return True if it is supported and false if not.
|
||||
*/
|
||||
public static boolean isCurrency(String currency) {
|
||||
for (Currency value : Currency.values()) {
|
||||
if (currency.equalsIgnoreCase(value.getName())) return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a currency enum.
|
||||
* @param currency The currency you want.
|
||||
* @return The currency enum.
|
||||
*/
|
||||
public static Currency getCurrency(String currency) {
|
||||
for (Currency value : Currency.values()) {
|
||||
if (currency.equalsIgnoreCase(value.getName())) return value;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the name of the currency.
|
||||
* @return The name of the currency.
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
@ -0,0 +1,158 @@
|
||||
package com.badbones69.crazyauctions.api.economy;
|
||||
|
||||
import com.badbones69.crazyauctions.CrazyAuctions;
|
||||
import com.badbones69.crazyauctions.api.economy.vault.VaultSupport;
|
||||
import com.badbones69.crazyauctions.api.enums.ShopCategories;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class CurrencyAPI {
|
||||
|
||||
private final CrazyAuctions plugin = CrazyAuctions.getPlugin();
|
||||
|
||||
private final VaultSupport vaultSupport = plugin.getStarter().getVaultSupport();
|
||||
|
||||
/**
|
||||
* Get the amount that a player has from a specific currency.
|
||||
* @param player The player you wish to get the amount from.
|
||||
* @param currency The currency you wish to get from.
|
||||
* @return The amount that the player has of that currency.
|
||||
*/
|
||||
public int getCurrency(Player player, Currency currency) {
|
||||
try {
|
||||
switch (currency) {
|
||||
case VAULT:
|
||||
vaultSupport.getVault().getBalance(player);
|
||||
break;
|
||||
case XP_LEVEL:
|
||||
player.getLevel();
|
||||
break;
|
||||
case XP_TOTAL:
|
||||
getTotalExperience(player);
|
||||
break;
|
||||
}
|
||||
} catch (Exception | NoClassDefFoundError ignored) {}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Take an amount from a player's currency.
|
||||
* @param player The player you wish to take from.
|
||||
* @param option The ShopOption you wish to use.
|
||||
*/
|
||||
public void takeCurrency(Player player, ShopCategories option) {
|
||||
// takeCurrency(player, option.getCurrency(), option.getCost());
|
||||
}
|
||||
|
||||
/**
|
||||
* Take an amount from a player's currency.
|
||||
* @param player The player you wish to take from.
|
||||
* @param currency The currency you wish to use.
|
||||
* @param amount The amount you want to take.
|
||||
*/
|
||||
public void takeCurrency(Player player, Currency currency, int amount) {
|
||||
try {
|
||||
switch (currency) {
|
||||
case VAULT:
|
||||
vaultSupport.getVault().withdrawPlayer(player, amount);
|
||||
break;
|
||||
case XP_LEVEL:
|
||||
player.setLevel(player.getLevel() - amount);
|
||||
break;
|
||||
case XP_TOTAL:
|
||||
takeTotalExperience(player, amount);
|
||||
break;
|
||||
}
|
||||
} catch (Exception | NoClassDefFoundError ignored) {}
|
||||
}
|
||||
|
||||
/**
|
||||
* Give an amount to a player's currency.
|
||||
* @param player The player you are giving to.
|
||||
* @param currency The currency you want to use.
|
||||
* @param amount The amount you are giving to the player.
|
||||
*/
|
||||
public void giveCurrency(Player player, Currency currency, int amount) {
|
||||
try {
|
||||
switch (currency) {
|
||||
case VAULT:
|
||||
vaultSupport.getVault().depositPlayer(player, amount);
|
||||
break;
|
||||
case XP_LEVEL:
|
||||
player.setLevel(player.getLevel() + amount);
|
||||
break;
|
||||
case XP_TOTAL:
|
||||
takeTotalExperience(player, -amount);
|
||||
break;
|
||||
}
|
||||
} catch (Exception | NoClassDefFoundError ignored) {}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the player has enough of a currency.
|
||||
* @param player The player you are checking.
|
||||
* @param option The ShopOption you wish to check.
|
||||
* @return True if they have enough to buy it or false if they don't.
|
||||
*/
|
||||
public boolean canBuy(Player player, ShopCategories option) {
|
||||
return canBuy(player, option.getCurrency(), option.getCost());
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the player has enough of a currency.
|
||||
* @param player The player you are checking.
|
||||
* @param currency The currency you wish to check.
|
||||
* @param cost The cost of the item you are checking.
|
||||
* @return True if they have enough to buy it or false if they don't.
|
||||
*/
|
||||
public boolean canBuy(Player player, Currency currency, int cost) {
|
||||
return getCurrency(player, currency) >= cost;
|
||||
}
|
||||
|
||||
private void takeTotalExperience(Player player, int amount) {
|
||||
int total = getTotalExperience(player) - amount;
|
||||
player.setTotalExperience(0);
|
||||
player.setTotalExperience(total);
|
||||
player.setLevel(0);
|
||||
player.setExp(0);
|
||||
|
||||
while (total > player.getExpToLevel()) {
|
||||
total -= player.getExpToLevel();
|
||||
player.setLevel(player.getLevel() + 1);
|
||||
}
|
||||
|
||||
float xp = (float) total / (float) player.getExpToLevel();
|
||||
player.setExp(xp);
|
||||
}
|
||||
|
||||
private int getTotalExperience(Player player) { // https://www.spigotmc.org/threads/72804
|
||||
int experience;
|
||||
int level = player.getLevel();
|
||||
|
||||
if (level >= 0 && level <= 15) {
|
||||
experience = (int) Math.ceil(Math.pow(level, 2) + (6 * level));
|
||||
int requiredExperience = 2 * level + 7;
|
||||
double currentExp = Double.parseDouble(Float.toString(player.getExp()));
|
||||
experience += Math.ceil(currentExp * requiredExperience);
|
||||
return experience;
|
||||
} else if (level > 15 && level <= 30) {
|
||||
experience = (int) Math.ceil((2.5 * Math.pow(level, 2) - (40.5 * level) + 360));
|
||||
int requiredExperience = 5 * level - 38;
|
||||
double currentExp = Double.parseDouble(Float.toString(player.getExp()));
|
||||
experience += Math.ceil(currentExp * requiredExperience);
|
||||
return experience;
|
||||
} else {
|
||||
experience = (int) Math.ceil((4.5 * Math.pow(level, 2) - (162.5 * level) + 2220));
|
||||
int requiredExperience = 9 * level - 158;
|
||||
double currentExp = Double.parseDouble(Float.toString(player.getExp()));
|
||||
experience += Math.ceil(currentExp * requiredExperience);
|
||||
return experience;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the vault currency if it is on the server.
|
||||
*/
|
||||
public void loadCurrency() {
|
||||
vaultSupport.loadVault();
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.badbones69.crazyauctions.api.economy.vault;
|
||||
|
||||
import com.badbones69.crazyauctions.CrazyAuctions;
|
||||
import com.badbones69.crazyauctions.utils.func.PluginSupport;
|
||||
import net.milkbowl.vault.economy.Economy;
|
||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||
|
||||
public class VaultSupport {
|
||||
|
||||
private final CrazyAuctions plugin = CrazyAuctions.getPlugin();
|
||||
|
||||
private Economy vault = null;
|
||||
|
||||
public Economy getVault() {
|
||||
return vault;
|
||||
}
|
||||
|
||||
public void loadVault() {
|
||||
if (PluginSupport.VAULT.isPluginLoaded()) {
|
||||
RegisteredServiceProvider<Economy> rsp = plugin.getServer().getServicesManager().getRegistration(Economy.class);
|
||||
|
||||
if (rsp != null) vault = rsp.getProvider();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,92 @@
|
||||
package com.badbones69.crazyauctions.api.enums;
|
||||
|
||||
import com.badbones69.crazyauctions.api.economy.Currency;
|
||||
import com.badbones69.crazyauctions.utils.ItemBuilder;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public enum ShopCategories {
|
||||
|
||||
SELL("Sell"),
|
||||
BID("Bid");
|
||||
|
||||
private final String name;
|
||||
|
||||
private final HashMap<ShopCategories, Options> shopCategories = new HashMap<>();
|
||||
|
||||
/**
|
||||
* @param name name of the Shop Type.
|
||||
*/
|
||||
ShopCategories(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param name name of the Type you want.
|
||||
* @return Returns the Type as an Enum.
|
||||
*/
|
||||
public static ShopCategories getFromName(String name) {
|
||||
for (ShopCategories type : ShopCategories.values()) {
|
||||
if (type.getName().equalsIgnoreCase(name)) return type;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public Currency getCurrency() {
|
||||
return shopCategories.get(this).currency;
|
||||
}
|
||||
|
||||
public int getCost() {
|
||||
return shopCategories.get(this).cost;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Returns the type name as a string.
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
private static class Options {
|
||||
|
||||
private final ItemBuilder itemBuilder;
|
||||
|
||||
private final int slot;
|
||||
private final boolean inMenu;
|
||||
private int cost;
|
||||
private final Currency currency;
|
||||
|
||||
public Options(ItemBuilder itemBuilder, int slot, boolean inMenu, int cost, Currency currency) {
|
||||
this.itemBuilder = itemBuilder;
|
||||
this.slot = slot;
|
||||
this.inMenu = inMenu;
|
||||
this.cost = cost;
|
||||
this.currency = currency;
|
||||
}
|
||||
|
||||
public ItemBuilder getItemBuilder() {
|
||||
return itemBuilder;
|
||||
}
|
||||
|
||||
public int getSlot() {
|
||||
return slot;
|
||||
}
|
||||
|
||||
public int getCost() {
|
||||
return cost;
|
||||
}
|
||||
|
||||
public Currency getCurrency() {
|
||||
return currency;
|
||||
}
|
||||
|
||||
public boolean isInMenu() {
|
||||
return inMenu;
|
||||
}
|
||||
|
||||
public void setCost(int cost) {
|
||||
this.cost = cost;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,57 +0,0 @@
|
||||
package com.badbones69.crazyauctions.currency;
|
||||
|
||||
import net.milkbowl.vault.economy.Economy;
|
||||
import net.milkbowl.vault.economy.EconomyResponse;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||
|
||||
public class Vault {
|
||||
|
||||
public static Economy econ = null;
|
||||
public static EconomyResponse r;
|
||||
|
||||
public static boolean hasVault() {
|
||||
return Bukkit.getServer().getPluginManager().getPlugin("Vault") != null;
|
||||
}
|
||||
|
||||
public static boolean setupEconomy() {
|
||||
if (Bukkit.getServer().getPluginManager().getPlugin("Vault") == null) {
|
||||
return false;
|
||||
}
|
||||
RegisteredServiceProvider<Economy> rsp = Bukkit.getServer().getServicesManager().getRegistration(Economy.class);
|
||||
if (rsp == null) {
|
||||
return false;
|
||||
}
|
||||
econ = rsp.getProvider();
|
||||
return econ != null;
|
||||
}
|
||||
|
||||
public static Long getMoney(Player player) {
|
||||
if (player != null) {
|
||||
try {
|
||||
return (long) econ.getBalance(player);
|
||||
} catch (NullPointerException ignore) {
|
||||
}
|
||||
}
|
||||
return 0L;
|
||||
}
|
||||
|
||||
public static void removeMoney(Player player, Long amount) {
|
||||
econ.withdrawPlayer(player, amount);
|
||||
}
|
||||
|
||||
public static void removeMoney(OfflinePlayer player, Long amount) {
|
||||
econ.withdrawPlayer(player, amount);
|
||||
}
|
||||
|
||||
public static void addMoney(Player player, Long amount) {
|
||||
econ.depositPlayer(player, amount);
|
||||
}
|
||||
|
||||
public static void addMoney(OfflinePlayer player, Long amount) {
|
||||
econ.depositPlayer(player, amount);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package com.badbones69.crazyauctions.utils.func;
|
||||
|
||||
import com.badbones69.crazyauctions.CrazyAuctions;
|
||||
|
||||
public enum PluginSupport {
|
||||
|
||||
PLACEHOLDERAPI("PlaceholderAPI"),
|
||||
HOLOGRAPHIC_DISPLAYS("HolographicDisplays"),
|
||||
DECENT_HOLOGRAMS("DecentHolograms"),
|
||||
VAULT("Vault");
|
||||
|
||||
private final String name;
|
||||
|
||||
private final CrazyAuctions plugin = CrazyAuctions.getPlugin();
|
||||
|
||||
PluginSupport(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public boolean isPluginLoaded() {
|
||||
return plugin.getServer().getPluginManager().getPlugin(name) != null;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user