diff --git a/src/main/java/com/alttd/GUI/windows/BuyGUI.java b/src/main/java/com/alttd/GUI/windows/BuyGUI.java index 7b1dd38..6d870ed 100644 --- a/src/main/java/com/alttd/GUI/windows/BuyGUI.java +++ b/src/main/java/com/alttd/GUI/windows/BuyGUI.java @@ -3,6 +3,7 @@ package com.alttd.GUI.windows; import com.alttd.GUI.GUIMerchant; import com.alttd.VillagerUI; import com.alttd.config.Config; +import com.alttd.objects.EconUser; import com.alttd.objects.Price; import com.alttd.objects.VillagerType; import com.alttd.util.Utilities; @@ -11,6 +12,7 @@ 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.entity.Villager; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; @@ -26,28 +28,29 @@ public class BuyGUI extends GUIMerchant { Price price = Utilities.getPrice(itemStack); if (price == null) continue; - double money = price.getPrice(itemStack.getAmount()); addItem(itemStack, - getPriceItem(money), + getPriceItem(price.getPrice(itemStack.getAmount())), null, - player -> buy(player, itemStack.getType(), itemStack.getAmount(), money) + player -> buy(villagerType, player, itemStack.getType(), itemStack.getAmount(), price) ); } } - private void buy(Player player, Material material, int amount, double price) + private void buy(VillagerType villagerType, Player player, Material material, int amount, Price price) { Economy econ = VillagerUI.getEcon(); double balance = econ.getBalance(player); + double cost = price.getPrice(amount); - price *= amount; - if (balance < amount) { + if (balance < cost) { 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); + econ.withdrawPlayer(player, cost); + EconUser.users.get(player.getUniqueId()) + .addPoints(villagerType.getName(), price.getPoints()); player.sendMessage(MiniMessage.get().parse(Config.PURCHASED_ITEM, Template.of("amount", String.valueOf(amount)), Template.of("item", material.toString()), diff --git a/src/main/java/com/alttd/GUI/windows/SellGUI.java b/src/main/java/com/alttd/GUI/windows/SellGUI.java index 9e1c435..c208b1a 100644 --- a/src/main/java/com/alttd/GUI/windows/SellGUI.java +++ b/src/main/java/com/alttd/GUI/windows/SellGUI.java @@ -3,6 +3,7 @@ package com.alttd.GUI.windows; import com.alttd.GUI.GUIMerchant; import com.alttd.VillagerUI; import com.alttd.config.Config; +import com.alttd.objects.EconUser; import com.alttd.objects.Price; import com.alttd.objects.VillagerType; import com.alttd.util.Utilities; @@ -26,21 +27,22 @@ public class SellGUI extends GUIMerchant { Price price = Utilities.getPrice(itemStack); if (price == null) continue; - double money = price.getPrice(itemStack.getAmount());; addItem(itemStack, - getPriceItem(money), + getPriceItem(price.getPrice(itemStack.getAmount())), null, - player -> sell(player, itemStack.getType(), itemStack.getAmount(), money) + player -> sell(villagerType, player, itemStack.getType(), itemStack.getAmount(), price) ); } } - private void sell(Player player, Material material, int amount, double price) + private void sell(VillagerType villagerType, Player player, Material material, int amount, Price price) { Economy econ = VillagerUI.getEcon(); - price *= amount; + double cost = price.getPrice(amount); - econ.depositPlayer(player, price); + econ.depositPlayer(player, cost); + EconUser.users.get(player.getUniqueId()) + .addPoints(villagerType.getName(), -price.getPoints()); player.sendMessage(MiniMessage.get().parse(Config.PURCHASED_ITEM, Template.of("amount", String.valueOf(amount)), Template.of("item", material.toString()), diff --git a/src/main/java/com/alttd/commands/database/Queries.java b/src/main/java/com/alttd/commands/database/Queries.java index e9c6419..982131c 100644 --- a/src/main/java/com/alttd/commands/database/Queries.java +++ b/src/main/java/com/alttd/commands/database/Queries.java @@ -1,7 +1,10 @@ package com.alttd.commands.database; +import com.alttd.objects.EconUser; import com.alttd.objects.VillagerType; import com.alttd.util.Logger; +import it.unimi.dsi.fastutil.objects.Object2IntMap; +import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -9,9 +12,11 @@ import java.sql.SQLException; import java.util.UUID; public class Queries { - /** NOTE: run async + /** + * NOTE: run async * Get the points a user has for a villager type - * @param uuid Uuid for the user you want to get the points for + * + * @param uuid Uuid for the user you want to get the points for * @param villagerType Type of villager you want to get the points for * @return The amount of points a user has for the given villager type */ @@ -36,14 +41,16 @@ public class Queries { return (points); } - /** NOTE: run async + /** + * NOTE: run async * Add a specified amount of points to the user for the given villager type - * @param uuid Uuid for the user you want to add the points to + * + * @param uuid Uuid for the user you want to add the points to * @param villagerType Type of villager you want to add the points to - * @param points The amount of points to add + * @param points The amount of points to add * @return success */ - public static boolean updateUserPoints(UUID uuid, VillagerType villagerType, int points) { + public static boolean updateUserPoints(UUID uuid, String villagerType, int points) { String sql = "UPDATE Points = SET user_points = user_points + ? " + "WHERE UUID = ? " + "AND villager_type = ?;"; @@ -52,27 +59,29 @@ public class Queries { PreparedStatement preparedStatement = Database.connection.prepareStatement(sql); preparedStatement.setInt(1, points); preparedStatement.setString(2, uuid.toString()); - preparedStatement.setString(3, villagerType.getName()); + preparedStatement.setString(3, villagerType); if (preparedStatement.executeUpdate() == 0) return createUserPointsEntry(uuid, villagerType, points); } catch (SQLException e) { e.printStackTrace(); Logger.warning("Unable to add % points to %" + - " for villager type %", String.valueOf(points), uuid.toString(), villagerType.getName()); + " for villager type %", String.valueOf(points), uuid.toString(), villagerType); return (false); } return (true); } - /** NOTE: run async + /** + * NOTE: run async * Create a new user entry - * @param uuid Uuid for the user you want to create an entry for + * + * @param uuid Uuid for the user you want to create an entry for * @param villagerType Type of villager you want to use in that entry - * @param points The amount of points to set the start to + * @param points The amount of points to set the start to * @return success */ - public static boolean createUserPointsEntry(UUID uuid, VillagerType villagerType, int points) { + public static boolean createUserPointsEntry(UUID uuid, String villagerType, int points) { String sql = "INSERT INTO Points " + "(uuid, villager_type, points) " + "(?, ?, ?)"; @@ -80,15 +89,37 @@ public class Queries { try { PreparedStatement preparedStatement = Database.connection.prepareStatement(sql); preparedStatement.setString(1, uuid.toString()); - preparedStatement.setString(2, villagerType.getName()); + preparedStatement.setString(2, villagerType); preparedStatement.setInt(3, points); + return (preparedStatement.execute()); } catch (SQLException e) { e.printStackTrace(); Logger.warning("Unable to create point entry for %" + - " for villager type %", uuid.toString(), villagerType.getName()); + " for villager type %", uuid.toString(), villagerType); return (false); } - return (true); + } + + public static EconUser getEconUser(UUID uuid) { + String sql = "SELECT * FROM POINTS WHERE uuid = ?"; + + try { + PreparedStatement preparedStatement = Database.connection.prepareStatement(sql); + preparedStatement.setString(1, uuid.toString()); + + ResultSet resultSet = preparedStatement.executeQuery(); + Object2ObjectArrayMap points = new Object2ObjectArrayMap<>(); + while (resultSet.next()) { + points.put( + resultSet.getString("villager_type"), + resultSet.getInt("points")); + } + return (new EconUser(uuid, points)); + + } catch (SQLException e) { + e.printStackTrace(); + } + return (null); } } diff --git a/src/main/java/com/alttd/objects/EconUser.java b/src/main/java/com/alttd/objects/EconUser.java new file mode 100644 index 0000000..f70df6e --- /dev/null +++ b/src/main/java/com/alttd/objects/EconUser.java @@ -0,0 +1,36 @@ +package com.alttd.objects; + +import com.alttd.commands.database.Queries; +import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap; + +import java.util.UUID; + +public class EconUser { + + public static Object2ObjectArrayMap users = new Object2ObjectArrayMap<>(); + + private final UUID uuid; + private final Object2ObjectArrayMap pointsMap; + + public EconUser(UUID uuid, Object2ObjectArrayMap points) { + this.uuid = uuid; + this.pointsMap = points; + users.put(this.uuid, this); + } + + public UUID getUuid() { + return uuid; + } + + public Object2ObjectArrayMap getPointsMap() { + return pointsMap; + } + + public void addPoints(String villagerType, int points) { + if (pointsMap.containsKey(villagerType)) + pointsMap.put(villagerType, points); + else + pointsMap.put(villagerType, pointsMap.get(villagerType) + points); + Queries.updateUserPoints(uuid, villagerType, pointsMap.get(villagerType)); + } +} diff --git a/src/main/java/com/alttd/objects/Price.java b/src/main/java/com/alttd/objects/Price.java index 93715a5..2057329 100644 --- a/src/main/java/com/alttd/objects/Price.java +++ b/src/main/java/com/alttd/objects/Price.java @@ -11,7 +11,7 @@ public record Price(double price, int points) { return (Utilities.round(price * multiplier, 2)); } - public int getPoints(int multiplier) { - return (points * multiplier); + public int getPoints() { + return (points); } }