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;
|
package com.alttd.GUI.windows;
|
||||||
|
|
||||||
import com.alttd.GUI.GUIMerchant;
|
import com.alttd.GUI.GUIMerchant;
|
||||||
|
import com.alttd.VillagerUI;
|
||||||
import com.alttd.config.Config;
|
import com.alttd.config.Config;
|
||||||
import com.alttd.objects.Price;
|
import com.alttd.objects.Price;
|
||||||
import com.alttd.objects.VillagerType;
|
import com.alttd.objects.VillagerType;
|
||||||
import com.alttd.util.Utilities;
|
import com.alttd.util.Utilities;
|
||||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||||
import net.kyori.adventure.text.minimessage.Template;
|
import net.kyori.adventure.text.minimessage.Template;
|
||||||
|
import net.milkbowl.vault.economy.Economy;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.inventory.ItemStack;
|
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)
|
private void buy(Player player, Material material, int amount, double price)
|
||||||
{
|
{
|
||||||
player.sendMessage(MiniMessage.get().parse(
|
Economy econ = VillagerUI.getEcon();
|
||||||
"Hi! you bought: " + amount +
|
double balance = econ.getBalance(player);
|
||||||
" " + material.name() +
|
|
||||||
" for " + price +
|
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) {
|
private ItemStack getPriceItem(double price) {
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,14 @@
|
||||||
package com.alttd.GUI.windows;
|
package com.alttd.GUI.windows;
|
||||||
|
|
||||||
import com.alttd.GUI.GUIMerchant;
|
import com.alttd.GUI.GUIMerchant;
|
||||||
|
import com.alttd.VillagerUI;
|
||||||
import com.alttd.config.Config;
|
import com.alttd.config.Config;
|
||||||
import com.alttd.objects.Price;
|
import com.alttd.objects.Price;
|
||||||
import com.alttd.objects.VillagerType;
|
import com.alttd.objects.VillagerType;
|
||||||
import com.alttd.util.Utilities;
|
import com.alttd.util.Utilities;
|
||||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||||
import net.kyori.adventure.text.minimessage.Template;
|
import net.kyori.adventure.text.minimessage.Template;
|
||||||
|
import net.milkbowl.vault.economy.Economy;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.inventory.ItemStack;
|
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)
|
private void sell(Player player, Material material, int amount, double price)
|
||||||
{
|
{
|
||||||
player.sendMessage(MiniMessage.get().parse(
|
Economy econ = VillagerUI.getEcon();
|
||||||
"Hi! you sold: " + amount +
|
price *= amount;
|
||||||
" " + material.name() +
|
|
||||||
" for " + price +
|
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) {
|
private ItemStack getPriceItem(double price) {
|
||||||
|
|
|
||||||
|
|
@ -8,16 +8,23 @@ import com.alttd.config.VillagerConfig;
|
||||||
import com.alttd.config.WorthConfig;
|
import com.alttd.config.WorthConfig;
|
||||||
import com.alttd.events.VillagerInteract;
|
import com.alttd.events.VillagerInteract;
|
||||||
import com.alttd.util.Logger;
|
import com.alttd.util.Logger;
|
||||||
|
import net.milkbowl.vault.economy.Economy;
|
||||||
|
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
public class VillagerUI extends JavaPlugin {
|
public class VillagerUI extends JavaPlugin {
|
||||||
|
|
||||||
public static VillagerUI instance;
|
public static VillagerUI instance;
|
||||||
|
private static Economy econ = null;
|
||||||
|
|
||||||
public static VillagerUI getInstance() {
|
public static VillagerUI getInstance() {
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Economy getEcon() {
|
||||||
|
return econ;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onLoad() {
|
public void onLoad() {
|
||||||
instance = this;
|
instance = this;
|
||||||
|
|
@ -30,6 +37,11 @@ public class VillagerUI extends JavaPlugin {
|
||||||
Config.reload();
|
Config.reload();
|
||||||
VillagerConfig.reload();
|
VillagerConfig.reload();
|
||||||
WorthConfig.reload();
|
WorthConfig.reload();
|
||||||
|
if (!setupEconomy()) {
|
||||||
|
Logger.severe("% - Unable to find vault", getDescription().getName());
|
||||||
|
getServer().getPluginManager().disablePlugin(this);
|
||||||
|
return;
|
||||||
|
}
|
||||||
Database.init();
|
Database.init();
|
||||||
Logger.info("--------------------------------------------------");
|
Logger.info("--------------------------------------------------");
|
||||||
Logger.info("Villager UI started");
|
Logger.info("Villager UI started");
|
||||||
|
|
@ -41,4 +53,13 @@ public class VillagerUI extends JavaPlugin {
|
||||||
getServer().getPluginManager().registerEvents(new VillagerInteract(), this);
|
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
|
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() {
|
private static void loadVillagerTypes() {
|
||||||
VillagerType.clearVillagerTypes();
|
VillagerType.clearVillagerTypes();
|
||||||
ConfigurationSection configurationSection = config.getConfigurationSection("villager-types");
|
ConfigurationSection configurationSection = config.getConfigurationSection("villager-types");
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user