Implement basic economy using Vault.

Prices for heads can be set for each category in the config file.
Dieser Commit ist enthalten in:
Leandro 2021-07-03 13:40:05 -03:00
Ursprung 267bb78cf3
Commit 7a9b974c77
5 geänderte Dateien mit 84 neuen und 20 gelöschten Zeilen

Datei anzeigen

@ -68,6 +68,13 @@
<artifactId>anvilgui</artifactId>
<version>1.5.0-SNAPSHOT</version>
</dependency>
<!-- Vault -->
<dependency>
<groupId>com.github.MilkBowl</groupId>
<artifactId>VaultAPI</artifactId>
<version>1.7</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>

Datei anzeigen

@ -13,12 +13,15 @@ import tsp.headdb.listener.MenuListener;
import tsp.headdb.util.Log;
import tsp.headdb.util.Metrics;
import tsp.headdb.util.Utils;
import org.bukkit.plugin.RegisteredServiceProvider;
import net.milkbowl.vault.economy.Economy;
public class HeadDB extends JavaPlugin {
private static HeadDB instance;
private Config config;
private Json playerdata;
private Economy economy = null;
@Override
public void onEnable() {
@ -28,6 +31,17 @@ public class HeadDB extends JavaPlugin {
config = LightningBuilder.fromPath("config.yml", "plugins/HeadDB").createConfig().addDefaultsFromInputStream();
playerdata = LightningBuilder.fromPath("playerdata.json", "plugins/HeadDB").createJson();
if (config.getBoolean("economy.enable"))
{
Log.debug("Starting economy...");
this.economy = this.setupEconomy();
if (this.economy == null) {
Log.error("Economy support requires Vault and an economy provider plugin.");
} else {
Log.info("Economy provider: " + this.economy.getName());
}
}
Log.debug("Starting metrics...");
new Metrics(this, Utils.METRICS_ID);
@ -52,6 +66,15 @@ public class HeadDB extends JavaPlugin {
Log.info("Done!");
}
private Economy setupEconomy() {
if (!this.getServer().getPluginManager().isPluginEnabled("Vault")) return null;
RegisteredServiceProvider<Economy> economyProvider = this.getServer().getServicesManager().getRegistration(Economy.class);
if (economyProvider == null) return null;
return this.economy = economyProvider.getProvider();
}
public Config getCfg() {
return config;
}
@ -64,4 +87,7 @@ public class HeadDB extends JavaPlugin {
return instance;
}
public Economy getEconomy() {
return this.economy;
}
}

Datei anzeigen

@ -7,12 +7,14 @@ import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import tsp.headdb.HeadDB;
import tsp.headdb.api.Head;
import tsp.headdb.api.HeadAPI;
import tsp.headdb.api.LocalHead;
import tsp.headdb.database.Category;
import tsp.headdb.util.Utils;
import tsp.headdb.util.XMaterial;
import net.milkbowl.vault.economy.Economy;
import java.util.ArrayList;
import java.util.Arrays;
@ -27,13 +29,11 @@ public class InventoryUtils {
for (LocalHead localHead : heads) {
pane.addButton(new Button(localHead.getItemStack(), e -> {
if (e.getClick() == ClickType.SHIFT_LEFT) {
ItemStack item = localHead.getItemStack();
item.setAmount(64);
player.getInventory().addItem(item);
purchaseItem(player, localHead.getItemStack(), 64, "local", localHead.getName());
return;
}
if (e.getClick() == ClickType.LEFT) {
player.getInventory().addItem(localHead.getItemStack());
purchaseItem(player, localHead.getItemStack(), 1, "local", localHead.getName());
return;
}
if (e.getClick() == ClickType.RIGHT) {
@ -53,13 +53,11 @@ public class InventoryUtils {
for (Head head : heads) {
pane.addButton(new Button(head.getItemStack(), e -> {
if (e.getClick() == ClickType.SHIFT_LEFT) {
ItemStack item = head.getItemStack();
item.setAmount(64);
player.getInventory().addItem(item);
purchaseItem(player, head.getItemStack(), 64, head.getCategory().getName(), head.getName());
return;
}
if (e.getClick() == ClickType.LEFT) {
player.getInventory().addItem(head.getItemStack());
purchaseItem(player, head.getItemStack(), 1, head.getCategory().getName(), head.getName());
}
if (e.getClick() == ClickType.RIGHT) {
HeadAPI.removeFavoriteHead(player.getUniqueId(), head.getId());
@ -79,13 +77,11 @@ public class InventoryUtils {
for (Head head : heads) {
pane.addButton(new Button(head.getItemStack(), e -> {
if (e.getClick() == ClickType.SHIFT_LEFT) {
ItemStack item = head.getItemStack();
item.setAmount(64);
player.getInventory().addItem(item);
purchaseItem(player, head.getItemStack(), 64, head.getCategory().getName(), head.getName());
return;
}
if (e.getClick() == ClickType.LEFT) {
player.getInventory().addItem(head.getItemStack());
purchaseItem(player, head.getItemStack(), 1, head.getCategory().getName(), head.getName());
}
if (e.getClick() == ClickType.RIGHT) {
HeadAPI.addFavoriteHead(player.getUniqueId(), head.getId());
@ -105,13 +101,11 @@ public class InventoryUtils {
for (Head head : heads) {
pane.addButton(new Button(head.getItemStack(), e -> {
if (e.getClick() == ClickType.SHIFT_LEFT) {
ItemStack item = head.getItemStack();
item.setAmount(64);
player.getInventory().addItem(item);
purchaseItem(player, head.getItemStack(), 64, head.getCategory().getName(), head.getName());
return;
}
if (e.getClick() == ClickType.LEFT) {
player.getInventory().addItem(head.getItemStack());
purchaseItem(player, head.getItemStack(), 1, head.getCategory().getName(), head.getName());
}
if (e.getClick() == ClickType.RIGHT) {
HeadAPI.addFavoriteHead(player.getUniqueId(), head.getId());
@ -130,13 +124,11 @@ public class InventoryUtils {
for (Head head : heads) {
pane.addButton(new Button(head.getItemStack(), e -> {
if (e.getClick() == ClickType.SHIFT_LEFT) {
ItemStack item = head.getItemStack();
item.setAmount(64);
player.getInventory().addItem(item);
purchaseItem(player, head.getItemStack(), 64, head.getCategory().getName(), head.getName());
return;
}
if (e.getClick() == ClickType.LEFT) {
player.getInventory().addItem(head.getItemStack());
purchaseItem(player, head.getItemStack(), 1, head.getCategory().getName(), head.getName());
}
if (e.getClick() == ClickType.RIGHT) {
HeadAPI.addFavoriteHead(player.getUniqueId(), head.getId());
@ -222,4 +214,26 @@ public class InventoryUtils {
return item;
}
public static void purchaseItem(Player player, ItemStack item, int amount, String costCfg, String description) {
Economy economy = HeadDB.getInstance().getEconomy();
if (economy != null) {
double cost = amount * HeadDB.getInstance().getCfg().getDouble("economy.cost." + costCfg);
if (cost > 0) {
if (economy.has(player, cost)) {
economy.withdrawPlayer(player, cost);
player.sendMessage(String.format("You purchased %d x %s for %.2f %s!", amount, description, cost, economy.currencyNamePlural()));
} else {
player.sendMessage(String.format("You do not have enough %s to purchase %d x %s.", economy.currencyNamePlural(), amount, description));
return;
}
}
else
{
player.sendMessage(String.format("You purchased %d x %s for free!", amount, description));
}
}
item.setAmount(amount);
player.getInventory().addItem(item);
}
}

Datei anzeigen

@ -7,5 +7,21 @@ asyncStartup: false
# If the cached heads are older than these amount of seconds, the plugin will refresh the database
refresh: 3600
# Economy options
economy:
enable: false
cost:
alphabet: 100
animals: 100
blocks: 100
decoration: 100
food-drinks: 100
humans: 100
humanoid: 100
miscellaneous: 100
monsters: 100
plants: 100
local: 1000
# Debug Mode
debug: false

Datei anzeigen

@ -3,6 +3,7 @@ description: ${project.description}
main: tsp.headdb.HeadDB
version: ${project.version}
softdepend: [Vault]
api-version: 1.16
author: Silent