This commit is contained in:
Teriuihi 2021-12-23 22:52:13 +01:00
parent 5d7467e062
commit 1feff9e412
7 changed files with 27 additions and 21 deletions

View File

@ -25,10 +25,10 @@ public class BuyGUI extends GUIMerchant {
private static final MiniMessage miniMessage = MiniMessage.get();
public BuyGUI(VillagerType villagerType) {
public BuyGUI(VillagerType villagerType, EconUser econUser) {
super(MiniMessage.get().parse(Config.BUY_WINDOW,
Template.of("trader", villagerType.getDisplayName()),
Template.of("percentage", "100")), villagerType); //TODO get percentage from player somehow
Template.of("points", String.valueOf(Objects.requireNonNullElse(econUser.getPointsMap().get(villagerType.getName()), 0)))), villagerType);
for (ItemStack itemStack : villagerType.getBuying()) {
Price price = Utilities.getPrice(itemStack);
if (price == null)
@ -47,7 +47,7 @@ public class BuyGUI extends GUIMerchant {
int trans_pts = (int) (Math.floor(price.getPrice(amount)/ WorthConfig.POINT_MOD) * amount);
EconUser econUser = EconUser.getUser(player.getUniqueId());
int oldPoints = Objects.requireNonNullElse(econUser.getPointsMap().get(villagerType.getName()), 0);
double cost = price.calculatePriceThing(oldPoints, trans_pts);
double cost = price.calculatePriceThing(oldPoints, trans_pts, true);
if (balance < cost) {
player.sendMessage(MiniMessage.get().parse(Config.NOT_ENOUGH_MONEY,

View File

@ -3,6 +3,7 @@ package com.alttd.GUI.windows;
import com.alttd.GUI.GUIInventory;
import com.alttd.VillagerUI;
import com.alttd.config.Config;
import com.alttd.objects.EconUser;
import com.alttd.objects.VillagerType;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.Template;
@ -12,6 +13,8 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.Objects;
public class OpenGUI extends GUIInventory {
private static final ItemStack BUY = new ItemStack(Material.GOLD_INGOT);
@ -32,14 +35,14 @@ public class OpenGUI extends GUIInventory {
}
}
public OpenGUI(VillagerType villagerType) {
public OpenGUI(VillagerType villagerType, EconUser econUser) {
super(InventoryType.HOPPER, MiniMessage.get().parse(Config.INITIAL_VILLAGER_WINDOW,
Template.of("trader", villagerType.getDisplayName()),
Template.of("percentage", "100"))); //TODO get percentage from player somehow
Template.of("points", String.valueOf(Objects.requireNonNullElse(econUser.getPointsMap().get(villagerType.getName()), 0)))));
setItem(1, BUY, player -> new BukkitRunnable() {
@Override
public void run() {
BuyGUI buyGUI = new BuyGUI(villagerType);
BuyGUI buyGUI = new BuyGUI(villagerType, econUser);
new BukkitRunnable() {
@Override
public void run() {
@ -51,7 +54,7 @@ public class OpenGUI extends GUIInventory {
setItem(3, SELL, player -> new BukkitRunnable() {
@Override
public void run() {
SellGUI sellGUI = new SellGUI(villagerType);
SellGUI sellGUI = new SellGUI(villagerType, econUser);
new BukkitRunnable() {
@Override
public void run() {

View File

@ -28,10 +28,10 @@ public class SellGUI extends GUIMerchant {
private static final MiniMessage miniMessage = MiniMessage.get();
public SellGUI(VillagerType villagerType) {
public SellGUI(VillagerType villagerType, EconUser econUser) {
super(MiniMessage.get().parse(Config.SELL_WINDOW,
Template.of("trader", villagerType.getDisplayName()),
Template.of("percentage", "100")), villagerType); //TODO get percentage from player somehow
Template.of("points", String.valueOf(Objects.requireNonNullElse(econUser.getPointsMap().get(villagerType.getName()), 0)))), villagerType);
for (ItemStack itemStack : villagerType.getSelling()) {
Price price = Utilities.getPrice(itemStack);
if (price == null)
@ -47,11 +47,10 @@ public class SellGUI extends GUIMerchant {
private void sell(VillagerType villagerType, Player player, Material material, int amount, Price price) {
PlayerInventory inventory = player.getInventory();
if (!inventory.containsAtLeast(new ItemStack(material), amount))
{
if (!inventory.containsAtLeast(new ItemStack(material), amount)) {
player.sendMessage(miniMessage.parse(Config.NOT_ENOUGH_ITEMS,
Template.of("type", material.name()),
Template.of("amount",String.valueOf(amount))));
Template.of("amount", String.valueOf(amount))));
return;
}
@ -59,7 +58,7 @@ public class SellGUI extends GUIMerchant {
EconUser econUser = EconUser.getUser(player.getUniqueId());
int oldPoints = Objects.requireNonNullElse(econUser.getPointsMap().get(villagerType.getName()), 0);
int trans_pts = (int) ((Math.floor(price.getPrice(amount) / WorthConfig.POINT_MOD) + 1) * amount);
double cost = price.calculatePriceThing(oldPoints, trans_pts);
double cost = price.calculatePriceThing(oldPoints, trans_pts, false);
econ.depositPlayer(player, cost);
econUser.addPoints(villagerType.getName(), -price.getPoints());
@ -88,8 +87,7 @@ public class SellGUI extends GUIMerchant {
.forEach(itemStack -> {
if (ref.tmpAmount == 0)
return;
if (itemStack.getAmount() > ref.tmpAmount)
{
if (itemStack.getAmount() > ref.tmpAmount) {
itemStack.setAmount(itemStack.getAmount() - ref.tmpAmount);
ref.tmpAmount = 0;
} else {

View File

@ -48,9 +48,9 @@ public final class Config extends AbstractConfig {
PASSWORD = config.getString("database.password", PASSWORD);
}
public static String INITIAL_VILLAGER_WINDOW = "<trader> price: <percentage>%";
public static String BUY_WINDOW = "<trader> price: <percentage>%";
public static String SELL_WINDOW = "<trader> price: <percentage>%";
public static String INITIAL_VILLAGER_WINDOW = "<trader> points: <points>";
public static String BUY_WINDOW = "<trader> points: <points>";
public static String SELL_WINDOW = "<trader> points: <points>";
private static void loadUI() {
INITIAL_VILLAGER_WINDOW = config.getString("ui.initial-window-name", INITIAL_VILLAGER_WINDOW);
@ -90,12 +90,14 @@ public final class Config extends AbstractConfig {
public static String NOT_ENOUGH_ITEMS = "<red>You only have don't have enough <type> you need at least <amount>.</red>";
public static String PURCHASED_ITEM = "<green>You bought <amount> <item> for <price>!</green>";
public static String SOLD_ITEM = "<green>You sold <amount> <item> for <price>!</green>";
public static String REMOVED_VILLAGER = "<green>Removed villager with uuid <uuid></green>";
private static void loadMessages() {
NOT_ENOUGH_MONEY = config.getString("messages.not-enough-money", NOT_ENOUGH_MONEY);
NOT_ENOUGH_ITEMS = config.getString("messages.not-enough-items", NOT_ENOUGH_ITEMS);
PURCHASED_ITEM = config.getString("messages.purchased-item", PURCHASED_ITEM);
SOLD_ITEM = config.getString("messages.sold-item", SOLD_ITEM);
REMOVED_VILLAGER = config.getString("messages.removed-villager", REMOVED_VILLAGER);
}
public static boolean DEBUG = false;

View File

@ -68,8 +68,7 @@ public class Database {
"UUID VARCHAR(36) NOT NULL, " +
"points int NOT NULL, " +
"villager_type VARCHAR(128) NOT NULL, " +
"PRIMARY KEY (UUID), " +
"UNIQUE KEY (villager_type)" +
"PRIMARY KEY (UUID, villager_type)" +
")";
connection.prepareStatement(sql).executeUpdate();
} catch (SQLException e) {

View File

@ -4,6 +4,7 @@ import com.alttd.GUI.windows.OpenGUI;
import com.alttd.VillagerUI;
import com.alttd.config.Config;
import com.alttd.config.VillagerConfig;
import com.alttd.objects.EconUser;
import com.alttd.objects.LoadedVillagers;
import com.alttd.objects.VillagerType;
import net.kyori.adventure.text.minimessage.MiniMessage;
@ -35,7 +36,7 @@ public class VillagerEvents implements Listener {
new BukkitRunnable() {
@Override
public void run() {
OpenGUI openGUI = new OpenGUI(loadedVillager);
OpenGUI openGUI = new OpenGUI(loadedVillager, EconUser.getUser(event.getPlayer().getUniqueId()));
new BukkitRunnable() {
@Override
public void run() {

View File

@ -5,6 +5,7 @@ import com.alttd.config.Config;
import com.alttd.database.Queries;
import com.alttd.util.Logger;
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
import org.apache.commons.math3.analysis.function.Log;
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.Unmodifiable;
@ -37,6 +38,8 @@ public class EconUser {
}
public void addPoints(String villagerType, int points) {
if (Config.DEBUG)
Logger.info("Adding % points to % for %", String.valueOf(points), villagerType, uuid.toString());
if (pointsMap.containsKey(villagerType))
pointsMap.put(villagerType, points);
else