Charge money/give money when buying/selling
This commit is contained in:
parent
4b5e09c553
commit
5ce955d6ee
|
|
@ -1,12 +1,14 @@
|
|||
package com.alttd.GUI.windows;
|
||||
|
||||
import com.alttd.GUI.GUIMerchant;
|
||||
import com.alttd.VillagerUI;
|
||||
import com.alttd.config.Config;
|
||||
import com.alttd.objects.Price;
|
||||
import com.alttd.objects.VillagerType;
|
||||
import com.alttd.util.Utilities;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.minimessage.Template;
|
||||
import net.milkbowl.vault.economy.Economy;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
|
@ -35,11 +37,21 @@ public class BuyGUI extends GUIMerchant {
|
|||
|
||||
private void buy(Player player, Material material, int amount, double price)
|
||||
{
|
||||
player.sendMessage(MiniMessage.get().parse(
|
||||
"Hi! you bought: " + amount +
|
||||
" " + material.name() +
|
||||
" for " + price +
|
||||
"."));
|
||||
Economy econ = VillagerUI.getEcon();
|
||||
double balance = econ.getBalance(player);
|
||||
|
||||
price *= amount;
|
||||
if (balance < amount) {
|
||||
player.sendMessage(MiniMessage.get().parse(Config.NOT_ENOUGH_MONEY,
|
||||
Template.of("money", String.valueOf(Utilities.round(balance, 2))),
|
||||
Template.of("price", String.valueOf(price))));
|
||||
return;
|
||||
}
|
||||
econ.withdrawPlayer(player, price);
|
||||
player.sendMessage(MiniMessage.get().parse(Config.PURCHASED_ITEM,
|
||||
Template.of("amount", String.valueOf(amount)),
|
||||
Template.of("item", material.toString()),
|
||||
Template.of("price", String.valueOf(price))));
|
||||
}
|
||||
|
||||
private ItemStack getPriceItem(double price) {
|
||||
|
|
|
|||
|
|
@ -1,12 +1,14 @@
|
|||
package com.alttd.GUI.windows;
|
||||
|
||||
import com.alttd.GUI.GUIMerchant;
|
||||
import com.alttd.VillagerUI;
|
||||
import com.alttd.config.Config;
|
||||
import com.alttd.objects.Price;
|
||||
import com.alttd.objects.VillagerType;
|
||||
import com.alttd.util.Utilities;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.minimessage.Template;
|
||||
import net.milkbowl.vault.economy.Economy;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
|
@ -35,11 +37,14 @@ public class SellGUI extends GUIMerchant {
|
|||
|
||||
private void sell(Player player, Material material, int amount, double price)
|
||||
{
|
||||
player.sendMessage(MiniMessage.get().parse(
|
||||
"Hi! you sold: " + amount +
|
||||
" " + material.name() +
|
||||
" for " + price +
|
||||
"."));
|
||||
Economy econ = VillagerUI.getEcon();
|
||||
price *= amount;
|
||||
|
||||
econ.depositPlayer(player, price);
|
||||
player.sendMessage(MiniMessage.get().parse(Config.PURCHASED_ITEM,
|
||||
Template.of("amount", String.valueOf(amount)),
|
||||
Template.of("item", material.toString()),
|
||||
Template.of("price", String.valueOf(price))));
|
||||
}
|
||||
|
||||
private ItemStack getPriceItem(double price) {
|
||||
|
|
|
|||
|
|
@ -8,16 +8,23 @@ import com.alttd.config.VillagerConfig;
|
|||
import com.alttd.config.WorthConfig;
|
||||
import com.alttd.events.VillagerInteract;
|
||||
import com.alttd.util.Logger;
|
||||
import net.milkbowl.vault.economy.Economy;
|
||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public class VillagerUI extends JavaPlugin {
|
||||
|
||||
public static VillagerUI instance;
|
||||
private static Economy econ = null;
|
||||
|
||||
public static VillagerUI getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static Economy getEcon() {
|
||||
return econ;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoad() {
|
||||
instance = this;
|
||||
|
|
@ -30,6 +37,11 @@ public class VillagerUI extends JavaPlugin {
|
|||
Config.reload();
|
||||
VillagerConfig.reload();
|
||||
WorthConfig.reload();
|
||||
if (!setupEconomy()) {
|
||||
Logger.severe("% - Unable to find vault", getDescription().getName());
|
||||
getServer().getPluginManager().disablePlugin(this);
|
||||
return;
|
||||
}
|
||||
Database.init();
|
||||
Logger.info("--------------------------------------------------");
|
||||
Logger.info("Villager UI started");
|
||||
|
|
@ -41,4 +53,13 @@ public class VillagerUI extends JavaPlugin {
|
|||
getServer().getPluginManager().registerEvents(new VillagerInteract(), this);
|
||||
}
|
||||
|
||||
private boolean setupEconomy() {
|
||||
if (getServer().getPluginManager().getPlugin("Vault") == null) return false;
|
||||
RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
|
||||
if (rsp == null) return false;
|
||||
econ = rsp.getProvider();
|
||||
|
||||
return econ != null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -80,6 +80,12 @@ public final class Config extends AbstractConfig {
|
|||
VILLAGER_NAME = config.getString("idkyet.villager-name", VILLAGER_NAME); //TODO change path
|
||||
}
|
||||
|
||||
public static final String NOT_ENOUGH_MONEY = "<red>You only have $<money>, you need at least $<price> for this purchase.</red>";
|
||||
public static final String PURCHASED_ITEM = "<green>You bought <amount> <item> for <price>!</green>";
|
||||
private static void loadMessages() {
|
||||
|
||||
}
|
||||
|
||||
private static void loadVillagerTypes() {
|
||||
VillagerType.clearVillagerTypes();
|
||||
ConfigurationSection configurationSection = config.getConfigurationSection("villager-types");
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user