Skip to content

Commit

Permalink
Add Vault support
Browse files Browse the repository at this point in the history
  • Loading branch information
nahkd123 committed Aug 13, 2021
1 parent c39ce18 commit 7da3a6b
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 5 deletions.
13 changes: 13 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
Expand All @@ -23,6 +30,12 @@
<version>0.0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.MilkBowl</groupId>
<artifactId>VaultAPI</artifactId>
<version>1.7</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/stonks/Stonks.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import featherpowders.items.ItemsDriver;
import stonks.commands.MarketCommand;
import stonks.events.ChatEventsHandler;
import stonks.hooks.VaultEconomyHook;
import stonks.players.StonksPlayer;
import stonks.stock.OfferType;
import stonks.stock.StockInfo;
Expand Down Expand Up @@ -51,6 +52,23 @@ public void onEnable() {
return;
}

// Setup economy
if (getServer().getPluginManager().getPlugin("Vault") != null) {
System.out.println("[Stonks] Vault found, hooking...");
VaultEconomyHook vault = new VaultEconomyHook();
if (!vault.serviceAvailable()) System.err.println("[Stonks] Vault Economy service is not available. Look like you don't have economy plugin :(");
else StonksHelper.economy = vault;
}
if (StonksHelper.economy == null) {
getServer().getConsoleSender().sendMessage(new String[] {
"",
"§eWARNING: §fNo economy plugin found!",
"§fStonks will ignore all balance check and allow player to create orders without",
"§ftaking money",
""
});
}

saveResource("config.yml", false);
reloadConfig();

Expand Down
13 changes: 9 additions & 4 deletions src/main/java/stonks/StonksHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,25 @@

import featherpowders.items.CustomStack;
import featherpowders.ui.chest.ChestUI;
import stonks.hooks.EconomyHook;
import stonks.stock.OfferType;
import stonks.stock.StockInfo;
import stonks.ui.StockUI;

public class StonksHelper {

// TODO: Hook with Vault Economy Service
public static EconomyHook economy;

public static boolean hasAtLeastMoney(Player player, double amount) {
return true;
return (economy != null && economy.hasMoney(player, amount)) || true;
}

public static void takeMoney(Player player, double amount) {}
public static void giveMoney(Player player, double amount) {}
public static void takeMoney(Player player, double amount) {
if (economy != null) economy.takeMoney(player, amount);
}
public static void giveMoney(Player player, double amount) {
if (economy != null) economy.giveMoney(player, amount);
}

public static HashMap<UUID, OfferType> offeringCustom = new HashMap<>();
public static HashMap<UUID, Integer> offeringUnits = new HashMap<>();
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/stonks/hooks/EconomyHook.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package stonks.hooks;

import org.bukkit.entity.Player;

public abstract class EconomyHook {

public abstract void giveMoney(Player player, double amount);
public abstract void takeMoney(Player player, double amount);
public abstract boolean hasMoney(Player player, double amount);

}
38 changes: 38 additions & 0 deletions src/main/java/stonks/hooks/VaultEconomyHook.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package stonks.hooks;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider;

import net.milkbowl.vault.economy.Economy;

public class VaultEconomyHook extends EconomyHook {

private boolean available = false;
private Economy economy;

public VaultEconomyHook() {
RegisteredServiceProvider<Economy> rsp = Bukkit.getServer().getServicesManager().getRegistration(Economy.class);
if (rsp == null) { available = false; return; }
economy = rsp.getProvider();
if (economy == null) { available = false; return; }
}

public boolean serviceAvailable() { return available; }

@Override
public void giveMoney(Player player, double amount) {
economy.depositPlayer(player, amount);
}

@Override
public void takeMoney(Player player, double amount) {
economy.withdrawPlayer(player, amount);
}

@Override
public boolean hasMoney(Player player, double amount) {
return economy.has(player, amount);
}

}
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version: 0.0.1-SNAPSHOT
api-version: 1.14
main: stonks.Stonks
depend: [FeatherPowders]
softdepend: [ManyItems]
softdepend: [ManyItems, Vault]

commands:
market:
Expand Down

0 comments on commit 7da3a6b

Please sign in to comment.