Added points to users
This commit is contained in:
parent
7066c032cf
commit
c9987795bc
|
|
@ -3,6 +3,7 @@ package com.alttd.GUI.windows;
|
||||||
import com.alttd.GUI.GUIMerchant;
|
import com.alttd.GUI.GUIMerchant;
|
||||||
import com.alttd.VillagerUI;
|
import com.alttd.VillagerUI;
|
||||||
import com.alttd.config.Config;
|
import com.alttd.config.Config;
|
||||||
|
import com.alttd.objects.EconUser;
|
||||||
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;
|
||||||
|
|
@ -11,6 +12,7 @@ import net.kyori.adventure.text.minimessage.Template;
|
||||||
import net.milkbowl.vault.economy.Economy;
|
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.entity.Villager;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.meta.ItemMeta;
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
|
|
||||||
|
|
@ -26,28 +28,29 @@ public class BuyGUI extends GUIMerchant {
|
||||||
Price price = Utilities.getPrice(itemStack);
|
Price price = Utilities.getPrice(itemStack);
|
||||||
if (price == null)
|
if (price == null)
|
||||||
continue;
|
continue;
|
||||||
double money = price.getPrice(itemStack.getAmount());
|
|
||||||
addItem(itemStack,
|
addItem(itemStack,
|
||||||
getPriceItem(money),
|
getPriceItem(price.getPrice(itemStack.getAmount())),
|
||||||
null,
|
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();
|
Economy econ = VillagerUI.getEcon();
|
||||||
double balance = econ.getBalance(player);
|
double balance = econ.getBalance(player);
|
||||||
|
double cost = price.getPrice(amount);
|
||||||
|
|
||||||
price *= amount;
|
if (balance < cost) {
|
||||||
if (balance < amount) {
|
|
||||||
player.sendMessage(MiniMessage.get().parse(Config.NOT_ENOUGH_MONEY,
|
player.sendMessage(MiniMessage.get().parse(Config.NOT_ENOUGH_MONEY,
|
||||||
Template.of("money", String.valueOf(Utilities.round(balance, 2))),
|
Template.of("money", String.valueOf(Utilities.round(balance, 2))),
|
||||||
Template.of("price", String.valueOf(price))));
|
Template.of("price", String.valueOf(price))));
|
||||||
return;
|
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,
|
player.sendMessage(MiniMessage.get().parse(Config.PURCHASED_ITEM,
|
||||||
Template.of("amount", String.valueOf(amount)),
|
Template.of("amount", String.valueOf(amount)),
|
||||||
Template.of("item", material.toString()),
|
Template.of("item", material.toString()),
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package com.alttd.GUI.windows;
|
||||||
import com.alttd.GUI.GUIMerchant;
|
import com.alttd.GUI.GUIMerchant;
|
||||||
import com.alttd.VillagerUI;
|
import com.alttd.VillagerUI;
|
||||||
import com.alttd.config.Config;
|
import com.alttd.config.Config;
|
||||||
|
import com.alttd.objects.EconUser;
|
||||||
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;
|
||||||
|
|
@ -26,21 +27,22 @@ public class SellGUI extends GUIMerchant {
|
||||||
Price price = Utilities.getPrice(itemStack);
|
Price price = Utilities.getPrice(itemStack);
|
||||||
if (price == null)
|
if (price == null)
|
||||||
continue;
|
continue;
|
||||||
double money = price.getPrice(itemStack.getAmount());;
|
|
||||||
addItem(itemStack,
|
addItem(itemStack,
|
||||||
getPriceItem(money),
|
getPriceItem(price.getPrice(itemStack.getAmount())),
|
||||||
null,
|
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();
|
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,
|
player.sendMessage(MiniMessage.get().parse(Config.PURCHASED_ITEM,
|
||||||
Template.of("amount", String.valueOf(amount)),
|
Template.of("amount", String.valueOf(amount)),
|
||||||
Template.of("item", material.toString()),
|
Template.of("item", material.toString()),
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,10 @@
|
||||||
package com.alttd.commands.database;
|
package com.alttd.commands.database;
|
||||||
|
|
||||||
|
import com.alttd.objects.EconUser;
|
||||||
import com.alttd.objects.VillagerType;
|
import com.alttd.objects.VillagerType;
|
||||||
import com.alttd.util.Logger;
|
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.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
|
|
@ -9,9 +12,11 @@ import java.sql.SQLException;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
public class Queries {
|
public class Queries {
|
||||||
/** NOTE: run async
|
/**
|
||||||
|
* NOTE: run async
|
||||||
* Get the points a user has for a villager type
|
* 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
|
* @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
|
* @return The amount of points a user has for the given villager type
|
||||||
*/
|
*/
|
||||||
|
|
@ -36,14 +41,16 @@ public class Queries {
|
||||||
return (points);
|
return (points);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** NOTE: run async
|
/**
|
||||||
|
* NOTE: run async
|
||||||
* Add a specified amount of points to the user for the given villager type
|
* 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 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
|
* @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 + ? " +
|
String sql = "UPDATE Points = SET user_points = user_points + ? " +
|
||||||
"WHERE UUID = ? " +
|
"WHERE UUID = ? " +
|
||||||
"AND villager_type = ?;";
|
"AND villager_type = ?;";
|
||||||
|
|
@ -52,27 +59,29 @@ public class Queries {
|
||||||
PreparedStatement preparedStatement = Database.connection.prepareStatement(sql);
|
PreparedStatement preparedStatement = Database.connection.prepareStatement(sql);
|
||||||
preparedStatement.setInt(1, points);
|
preparedStatement.setInt(1, points);
|
||||||
preparedStatement.setString(2, uuid.toString());
|
preparedStatement.setString(2, uuid.toString());
|
||||||
preparedStatement.setString(3, villagerType.getName());
|
preparedStatement.setString(3, villagerType);
|
||||||
|
|
||||||
if (preparedStatement.executeUpdate() == 0)
|
if (preparedStatement.executeUpdate() == 0)
|
||||||
return createUserPointsEntry(uuid, villagerType, points);
|
return createUserPointsEntry(uuid, villagerType, points);
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
Logger.warning("Unable to add % points to %" +
|
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 (false);
|
||||||
}
|
}
|
||||||
return (true);
|
return (true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** NOTE: run async
|
/**
|
||||||
|
* NOTE: run async
|
||||||
* Create a new user entry
|
* 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 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
|
* @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 " +
|
String sql = "INSERT INTO Points " +
|
||||||
"(uuid, villager_type, points) " +
|
"(uuid, villager_type, points) " +
|
||||||
"(?, ?, ?)";
|
"(?, ?, ?)";
|
||||||
|
|
@ -80,15 +89,37 @@ public class Queries {
|
||||||
try {
|
try {
|
||||||
PreparedStatement preparedStatement = Database.connection.prepareStatement(sql);
|
PreparedStatement preparedStatement = Database.connection.prepareStatement(sql);
|
||||||
preparedStatement.setString(1, uuid.toString());
|
preparedStatement.setString(1, uuid.toString());
|
||||||
preparedStatement.setString(2, villagerType.getName());
|
preparedStatement.setString(2, villagerType);
|
||||||
preparedStatement.setInt(3, points);
|
preparedStatement.setInt(3, points);
|
||||||
|
|
||||||
|
return (preparedStatement.execute());
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
Logger.warning("Unable to create point entry for %" +
|
Logger.warning("Unable to create point entry for %" +
|
||||||
" for villager type %", uuid.toString(), villagerType.getName());
|
" for villager type %", uuid.toString(), villagerType);
|
||||||
return (false);
|
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<String, Integer> 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
36
src/main/java/com/alttd/objects/EconUser.java
Normal file
36
src/main/java/com/alttd/objects/EconUser.java
Normal file
|
|
@ -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<UUID, EconUser> users = new Object2ObjectArrayMap<>();
|
||||||
|
|
||||||
|
private final UUID uuid;
|
||||||
|
private final Object2ObjectArrayMap<String, Integer> pointsMap;
|
||||||
|
|
||||||
|
public EconUser(UUID uuid, Object2ObjectArrayMap<String, Integer> points) {
|
||||||
|
this.uuid = uuid;
|
||||||
|
this.pointsMap = points;
|
||||||
|
users.put(this.uuid, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UUID getUuid() {
|
||||||
|
return uuid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object2ObjectArrayMap<String, Integer> 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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -11,7 +11,7 @@ public record Price(double price, int points) {
|
||||||
return (Utilities.round(price * multiplier, 2));
|
return (Utilities.round(price * multiplier, 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getPoints(int multiplier) {
|
public int getPoints() {
|
||||||
return (points * multiplier);
|
return (points);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user