Skip to content

Commit

Permalink
add economy wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
phybros committed Sep 14, 2022
1 parent 08b9111 commit 3edc3f9
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 42 deletions.
29 changes: 6 additions & 23 deletions src/main/java/io/servertap/PluginEntrypoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@
import io.servertap.api.v1.websockets.ConsoleListener;
import io.servertap.api.v1.websockets.WebsocketHandler;
import io.servertap.metrics.Metrics;
import io.servertap.utils.EconomyWrapper;
import io.servertap.utils.GsonJsonMapper;
import io.swagger.v3.oas.models.info.Info;
import net.milkbowl.vault.economy.Economy;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Logger;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.java.JavaPluginLoader;
import org.eclipse.jetty.server.Connector;
Expand All @@ -39,7 +38,7 @@ public class PluginEntrypoint extends JavaPlugin {

public static PluginEntrypoint instance;
private static final java.util.logging.Logger log = Bukkit.getLogger();
private static Economy econ = null;
private EconomyWrapper economyWrapper;
private static Javalin app = null;

public static final String SERVERTAP_KEY_HEADER = "key";
Expand Down Expand Up @@ -68,9 +67,12 @@ public void onEnable() {
// Tell bStats what plugin this is
Metrics metrics = new Metrics(this, 9492);

// Initialize config file + set defaults
saveDefaultConfig();
FileConfiguration bukkitConfig = getConfig();
setupEconomy();

// Initialize any economy integration (if one is installed)
EconomyWrapper.getInstance().setupEconomy();

this.authEnabled = bukkitConfig.getBoolean("useKeyAuth", true);

Expand Down Expand Up @@ -258,25 +260,6 @@ public void onDisable() {

}

private void setupEconomy() {
if (getServer().getPluginManager().getPlugin("Vault") == null) {
log.info(String.format("[%s] No Vault plugin detected", getDescription().getName()));
return;
}
RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
if (rsp == null) {
log.info(String.format("[%s] No Economy providers detected", getDescription().getName()));
return;
}

log.info(String.format("[%s] Hooked economy provider: %s", getDescription().getName(), rsp.getProvider().getName()));
econ = rsp.getProvider();
}

public static Economy getEconomy() {
return econ;
}

private OpenApiOptions getOpenApiOptions() {
Info applicationInfo = new Info()
.title(this.getDescription().getName())
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/io/servertap/WebhookEventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.servertap.api.v1.models.ItemStack;
import io.servertap.api.v1.models.Player;
import io.servertap.api.v1.models.events.*;
import io.servertap.utils.EconomyWrapper;
import io.servertap.utils.GsonSingleton;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.Bukkit;
Expand Down Expand Up @@ -217,8 +218,8 @@ private ItemStack fromBukkitItemStack(org.bukkit.inventory.ItemStack itemStack)
private Player fromBukkitPlayer(org.bukkit.entity.Player player) {
Player p = new Player();

if (PluginEntrypoint.getEconomy() != null) {
p.setBalance(PluginEntrypoint.getEconomy().getBalance(player));
if (EconomyWrapper.getInstance().getEconomy() != null) {
p.setBalance(EconomyWrapper.getInstance().getEconomy().getBalance(player));
}

p.setUuid(player.getUniqueId().toString());
Expand All @@ -239,14 +240,15 @@ private Player fromBukkitPlayer(org.bukkit.entity.Player player) {

private String normalizeMessage(String message) {
try {
if(!this.plugin.getConfig().getBoolean("normalizeMessages")){
if (!this.plugin.getConfig().getBoolean("normalizeMessages")) {
return message;
}
return ChatColor.stripColor(message);
} catch (Exception e) {
return message;
}
}

private static class PostRequestTask implements Runnable {
private final WebhookEvent webhookEvent;
private final String listener;
Expand Down
14 changes: 6 additions & 8 deletions src/main/java/io/servertap/api/v1/EconomyApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
import io.javalin.http.InternalServerErrorResponse;
import io.javalin.plugin.openapi.annotations.*;
import io.servertap.Constants;
import io.servertap.PluginEntrypoint;
import io.servertap.utils.EconomyWrapper;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.economy.EconomyResponse;
import net.milkbowl.vault.economy.EconomyResponse.ResponseType;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.plugin.Plugin;
Expand Down Expand Up @@ -37,10 +36,9 @@ private enum TransactionType {
@OpenApiResponse(status = "500", content = @OpenApiContent(type = "application/json"))
}
)

public static void getEconomyPluginInformation(Context ctx) {
Plugin econPlugin;
if (PluginEntrypoint.getEconomy() == null) {
if (EconomyWrapper.getInstance().getEconomy() == null) {
throw new HttpResponseException(424, Constants.VAULT_MISSING, new HashMap<>());
} else {
RegisteredServiceProvider<Economy> rsp = Bukkit.getServer().getServicesManager().getRegistration(Economy.class);
Expand Down Expand Up @@ -107,7 +105,7 @@ private static void accountManager(Context ctx, TransactionType action) {
throw new BadRequestResponse(Constants.VAULT_MISSING_PAY_PARAMS);
}

if (PluginEntrypoint.getEconomy() == null) {
if (EconomyWrapper.getInstance().getEconomy() == null) {
throw new HttpResponseException(424, Constants.VAULT_MISSING, new HashMap<>());
}

Expand All @@ -126,12 +124,12 @@ private static void accountManager(Context ctx, TransactionType action) {
EconomyResponse response;

if (action == TransactionType.PAY) {
response = PluginEntrypoint.getEconomy().depositPlayer(player, amount);
response = EconomyWrapper.getInstance().getEconomy().depositPlayer(player, amount);
} else {
response = PluginEntrypoint.getEconomy().withdrawPlayer(player, amount);
response = EconomyWrapper.getInstance().getEconomy().withdrawPlayer(player, amount);
}

if (response.type != ResponseType.SUCCESS) {
if (response.type != EconomyResponse.ResponseType.SUCCESS) {
throw new InternalServerErrorResponse(response.errorMessage);
}

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/io/servertap/api/v1/PlayerApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import io.javalin.http.*;
import io.javalin.plugin.openapi.annotations.*;
import io.servertap.Constants;
import io.servertap.PluginEntrypoint;
import io.servertap.api.v1.models.ItemStack;
import io.servertap.api.v1.models.Player;
import io.servertap.utils.EconomyWrapper;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
Expand Down Expand Up @@ -96,8 +96,8 @@ private static Player getPlayer(org.bukkit.entity.Player player) {
p.setBanned(player.isBanned());
p.setOp(player.isOp());

if (PluginEntrypoint.getEconomy() != null) {
p.setBalance(PluginEntrypoint.getEconomy().getBalance(player));
if (EconomyWrapper.getInstance().getEconomy() != null) {
p.setBalance(EconomyWrapper.getInstance().getEconomy().getBalance(player));
}

p.setHunger(player.getFoodLevel());
Expand Down Expand Up @@ -148,8 +148,8 @@ public static void offlinePlayersGet(Context ctx) {
p.setBanned(player.isBanned());
p.setOp(player.isOp());

if (PluginEntrypoint.getEconomy() != null) {
p.setBalance(PluginEntrypoint.getEconomy().getBalance(offlinePlayers[i]));
if (EconomyWrapper.getInstance().getEconomy() != null) {
p.setBalance(EconomyWrapper.getInstance().getEconomy().getBalance(offlinePlayers[i]));
}

p.setLastPlayed(player.getLastPlayed());
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/servertap/api/v1/ServerApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import io.javalin.plugin.openapi.annotations.*;
import io.servertap.Constants;
import io.servertap.Lag;
import io.servertap.PluginEntrypoint;
import io.servertap.ServerExecCommandSender;
import io.servertap.api.v1.models.*;
import io.servertap.mojang.api.MojangApiService;
import io.servertap.mojang.api.models.NameChange;
import io.servertap.utils.EconomyWrapper;
import io.servertap.utils.GsonSingleton;
import org.apache.commons.lang.StringUtils;
import org.bukkit.BanList;
Expand Down Expand Up @@ -476,8 +476,8 @@ public static void getOps(Context ctx) {
p.setBanned(player.isBanned());
p.setOp(player.isOp());

if (PluginEntrypoint.getEconomy() != null) {
p.setBalance(PluginEntrypoint.getEconomy().getBalance(player));
if (EconomyWrapper.getInstance().getEconomy() != null) {
p.setBalance(EconomyWrapper.getInstance().getEconomy().getBalance(player));
}

opedPlayers.add(p);
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/io/servertap/utils/EconomyWrapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.servertap.utils;

import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.plugin.RegisteredServiceProvider;

public class EconomyWrapper {

private static final java.util.logging.Logger log = Bukkit.getLogger();

private static EconomyWrapper instance;
private static Economy economy;

public static EconomyWrapper getInstance() {
if (instance == null) {
instance = new EconomyWrapper();
}

return instance;
}

public Economy getEconomy() {
return economy;
}

public void setupEconomy() {
if (Bukkit.getServer().getPluginManager().getPlugin("Vault") == null) {
log.info("[ServerTap] No Vault plugin detected");
return;
}
RegisteredServiceProvider<Economy> rsp = Bukkit.getServer().getServicesManager().getRegistration(Economy.class);
if (rsp == null) {
log.info("[ServerTap] No Economy providers detected");
return;
}

log.info(String.format("[ServerTap] Hooked economy provider: %s", rsp.getProvider().getName()));
economy = rsp.getProvider();
}
}

0 comments on commit 3edc3f9

Please sign in to comment.