From c639b99f64807f127f581a427fe099efdc5c901e Mon Sep 17 00:00:00 2001 From: YuTian <2953516620@qq.com> Date: Wed, 17 Jul 2024 19:27:06 +0800 Subject: [PATCH] 0.1 --- pom.xml | 2 +- .../io/yutian/auchestshop/AuChestShop.java | 4 + .../auchestshop/command/CommandManager.java | 4 +- .../auchestshop/command/subs/CommandTest.java | 51 +++++++++++++ .../io/yutian/auchestshop/database/SQLIO.java | 75 +++++++++++++------ .../auchestshop/manager/ShopManager.java | 9 ++- .../com/io/yutian/auchestshop/shop/Shop.java | 28 +++---- .../yutian/auchestshop/shop/ShopLocation.java | 48 ++++++++++++ .../yutian/auchestshop/shop/log/ShopLog.java | 62 +++++++++++++++ .../auchestshop/shop/log/ShopPlayerLog.java | 34 +++++++++ src/main/resources/plugin.yml | 5 +- 11 files changed, 276 insertions(+), 46 deletions(-) create mode 100644 src/main/java/com/io/yutian/auchestshop/command/subs/CommandTest.java create mode 100644 src/main/java/com/io/yutian/auchestshop/shop/ShopLocation.java create mode 100644 src/main/java/com/io/yutian/auchestshop/shop/log/ShopLog.java create mode 100644 src/main/java/com/io/yutian/auchestshop/shop/log/ShopPlayerLog.java diff --git a/pom.xml b/pom.xml index 4a6421f..d66f709 100644 --- a/pom.xml +++ b/pom.xml @@ -46,7 +46,7 @@ com.io.yutian AuLib - 1.2.2 + 1.4.1 provided diff --git a/src/main/java/com/io/yutian/auchestshop/AuChestShop.java b/src/main/java/com/io/yutian/auchestshop/AuChestShop.java index 6ed98ce..4791a54 100644 --- a/src/main/java/com/io/yutian/auchestshop/AuChestShop.java +++ b/src/main/java/com/io/yutian/auchestshop/AuChestShop.java @@ -1,5 +1,6 @@ package com.io.yutian.auchestshop; +import com.io.yutian.auchestshop.command.CommandManager; import com.io.yutian.auchestshop.database.SQLIO; import com.io.yutian.auchestshop.manager.ShopManager; import com.io.yutian.aulib.lang.Lang; @@ -11,13 +12,16 @@ public class AuChestShop extends JavaPlugin { private static SQLIO sqlIO; private static ShopManager shopManager; + private static CommandManager commandManager; @Override public void onEnable() { instance = this; sqlIO = new SQLIO(); + commandManager = new CommandManager(); shopManager = new ShopManager(); + commandManager.registerBukkitCommand(this, "auchestshop"); Lang.reload(this); diff --git a/src/main/java/com/io/yutian/auchestshop/command/CommandManager.java b/src/main/java/com/io/yutian/auchestshop/command/CommandManager.java index 94e1784..c69ce6a 100644 --- a/src/main/java/com/io/yutian/auchestshop/command/CommandManager.java +++ b/src/main/java/com/io/yutian/auchestshop/command/CommandManager.java @@ -1,13 +1,13 @@ package com.io.yutian.auchestshop.command; +import com.io.yutian.auchestshop.command.subs.CommandTest; import com.io.yutian.aulib.command.SimpleCommandManager; -import com.io.yutian.aulib.command.list.CommandHelp; public class CommandManager extends SimpleCommandManager { public CommandManager() { super("chestshop"); - register(new CommandHelp(this)); + register(new CommandTest()); } } diff --git a/src/main/java/com/io/yutian/auchestshop/command/subs/CommandTest.java b/src/main/java/com/io/yutian/auchestshop/command/subs/CommandTest.java new file mode 100644 index 0000000..a003387 --- /dev/null +++ b/src/main/java/com/io/yutian/auchestshop/command/subs/CommandTest.java @@ -0,0 +1,51 @@ +package com.io.yutian.auchestshop.command.subs; + +import com.io.yutian.auchestshop.AuChestShop; +import com.io.yutian.auchestshop.shop.Shop; +import com.io.yutian.auchestshop.shop.ShopLocation; +import com.io.yutian.auchestshop.shop.ShopType; +import com.io.yutian.auchestshop.shop.log.ShopLog; +import com.io.yutian.aulib.command.CommandContext; +import com.io.yutian.aulib.command.ICommand; +import com.io.yutian.aulib.command.argument.Argument; +import com.io.yutian.aulib.command.argument.ArgumentTypes; +import org.bukkit.command.CommandSender; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +public class CommandTest extends ICommand { + + public CommandTest() { + super("test"); + addArgument(Argument.argument("type", ArgumentTypes.STRING)); + } + + private static List shops = new ArrayList<>(); + + @Override + public void executes(CommandContext commandContext) { + CommandSender sender = commandContext.getSender(); + String type = commandContext.getArgumentsValue("type").getString(); + if (type.equalsIgnoreCase("create")) { + Shop shop = new Shop(UUID.randomUUID(), UUID.randomUUID(), new ShopLocation("world", 0, 10, 0), null, 10, ShopType.BUYING, false, new ShopLog()); + shops.add(shop); + sender.sendMessage("Shop created!"); + } else if (type.equalsIgnoreCase("save")) { + AuChestShop.getSQLIO().saveAllShops(shops); + sender.sendMessage("Shop saved!"); + } else if (type.equalsIgnoreCase("list")) { + System.out.println(shops); + for (Shop shop : shops) { + sender.sendMessage(shop.getUUID()); + sender.sendMessage(shop.getOwner()); + } + } else if (type.equalsIgnoreCase("load")) { + shops = AuChestShop.getSQLIO().loadAllShops(); + System.out.println(shops); + sender.sendMessage("Loaded all shops!"); + } + } + +} diff --git a/src/main/java/com/io/yutian/auchestshop/database/SQLIO.java b/src/main/java/com/io/yutian/auchestshop/database/SQLIO.java index 4e698f2..c7caea0 100644 --- a/src/main/java/com/io/yutian/auchestshop/database/SQLIO.java +++ b/src/main/java/com/io/yutian/auchestshop/database/SQLIO.java @@ -1,6 +1,8 @@ package com.io.yutian.auchestshop.database; import com.io.yutian.auchestshop.AuChestShop; +import com.io.yutian.auchestshop.shop.Shop; +import com.io.yutian.aulib.serialize.SerializeHelper; import com.io.yutian.aulib.sql.SQLHelper; import com.io.yutian.aulib.sql.api.SQLManager; import com.io.yutian.aulib.sql.api.SQLQuery; @@ -9,11 +11,13 @@ import com.io.yutian.aulib.sql.manager.SQLManagerImpl; import com.io.yutian.aulib.sql.util.DatabaseDriverType; import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; +import org.json.JSONObject; import java.io.File; import java.io.IOException; import java.sql.ResultSet; -import java.util.UUID; +import java.util.ArrayList; +import java.util.List; public class SQLIO { @@ -21,10 +25,9 @@ public class SQLIO { public void init() throws IOException { HikariConfig config = new HikariConfig(); - config.setDriverClassName(DatabaseDriverType.H2.getDriverClassName()); - config.setJdbcUrl("jdbc:h2:"+new File(AuChestShop.inst().getDataFolder(), "shops").getCanonicalFile().getAbsolutePath() + ";MODE=MYSQL"); + config.setDriverClassName(DatabaseDriverType.SQLITE.getDriverClassName()); + config.setJdbcUrl("jdbc:sqlite:"+new File(AuChestShop.inst().getDataFolder(), "shops.db").getCanonicalFile().getAbsolutePath()); sqlManager = new SQLManagerImpl(new HikariDataSource(config), "AuChestShop-Hikari-SQLManager"); - sqlManager.executeSQL("SET MODE=MYSQL;"); sqlManager.setExecutorPool(SQLExecutor.HIKARICP_EXECUTOR); createTables(); @@ -32,30 +35,60 @@ public class SQLIO { private void createTables() { sqlManager.createTable("shops") - .addColumn("uuid", "VARCHAR(32) NOT NULL") - .addColumn("owner", "VARCHAR(32) NOT NULL") - .addColumn("item", "TEXT NOT NULL") - .addColumn("type", "INT NOT NULL DEFAULT 0") - .addColumn("price", "DECIMAL(32,2) NOT NULL") - .addColumn("unlimited", "BIT NOT NULL DEFAULT 0") - .addColumn("log", "TEXT NOT NULL") - .addColumn("create_time", "LONG NOT NULL") - .setIndex("uuid", IndexType.UNIQUE_KEY) + .addColumn("uuid", "VARCHAR(48) NOT NULL") + .addColumn("owner", "VARCHAR(48) NOT NULL") + .addColumn("data", "TEXT NOT NULL") + .setIndex("uuid", IndexType.PRIMARY_KEY) + .setTableSettings("") .build().execute(null); } - private void query(UUID uuid) { - try (SQLQuery query = sqlManager.createQuery() + public List loadAllShops() { + List shops = new ArrayList<>(); + sqlManager.createQuery() .inTable("shops") - .selectColumns("uuid", "name", "item", "type", "price", "unlimited", "create_time") - .addCondition("uuid", uuid.toString()) - .build().execute(null)) { - try (ResultSet resultSet = query.getResultSet()) { - while (resultSet.next()) { - String name = resultSet.getString("name"); + .build().executeAsync(sqlQuery -> { + ResultSet resultSet = sqlQuery.getResultSet(); + while (resultSet.next()) { + String uuid = resultSet.getString("uuid"); + String owner = resultSet.getString("owner"); + String data = resultSet.getString("data"); + JSONObject dataJson = new JSONObject(data); + Shop shop = SerializeHelper.deserialize(Shop.class, dataJson); + shops.add(shop); + } + }); + return shops; + } + public void saveAllShops(List shops) { + try { + List insertedShops = new ArrayList<>(); + for (Shop shop : shops) { + try (SQLQuery sqlQuery = sqlManager.createQuery().inTable("shops").addCondition("uuid", shop.getUUID().toString()).setLimit(1).build().execute()) { + try (ResultSet resultSet = sqlQuery.getResultSet()) { + if (resultSet.next()) { + JSONObject dataJson = SerializeHelper.serialize(shop); + sqlManager.createUpdate("shops") + .addCondition("uuid", shop.getUUID().toString()) + .setLimit(1) + .setColumnValues("owner", shop.getOwner().toString()) + .setColumnValues("data", dataJson.toString()) + .build().execute(); + } else { + insertedShops.add(shop); + } + } } } + List params = new ArrayList<>(); + for (Shop shop : insertedShops) { + JSONObject dataJson = SerializeHelper.serialize(shop); + params.add(new Object[]{shop.getUUID().toString(), shop.getOwner().toString(), dataJson.toString()}); + } + if (!params.isEmpty()) { + sqlManager.createInsertBatch("shops").setIgnore(false).setColumnNames("uuid", "owner", "data").setAllParams(params).executeAsync(); + } } catch (Exception e) { e.printStackTrace(); } diff --git a/src/main/java/com/io/yutian/auchestshop/manager/ShopManager.java b/src/main/java/com/io/yutian/auchestshop/manager/ShopManager.java index f829ae5..245ec21 100644 --- a/src/main/java/com/io/yutian/auchestshop/manager/ShopManager.java +++ b/src/main/java/com/io/yutian/auchestshop/manager/ShopManager.java @@ -5,6 +5,7 @@ import com.google.common.collect.Sets; import com.io.yutian.auchestshop.shop.Shop; import com.io.yutian.auchestshop.shop.ShopChunk; import com.io.yutian.auchestshop.shop.ShopLoader; +import com.io.yutian.auchestshop.shop.ShopLocation; import com.io.yutian.auchestshop.util.ShopUtil; import org.apache.commons.lang.StringUtils; import org.bukkit.Chunk; @@ -98,8 +99,8 @@ public class ShopManager { public @NotNull List getShopsInWorld(@NotNull World world) { final List worldShops = new ArrayList<>(); for (final Shop shop : getAllShops()) { - Location location = shop.getLocation(); - if (location.isWorldLoaded() && Objects.equals(location.getWorld(), world)) { + ShopLocation location = shop.getLocation(); + if (Objects.equals(location.getWorld(), world)) { worldShops.add(shop); } } @@ -109,8 +110,8 @@ public class ShopManager { public @NotNull List getShopsInWorld(@NotNull String worldName) { final List worldShops = new ArrayList<>(); for (final Shop shop : getAllShops()) { - Location location = shop.getLocation(); - if (location.isWorldLoaded() && StringUtils.equals(worldName, location.getWorld().getName())) { + ShopLocation location = shop.getLocation(); + if (StringUtils.equals(worldName, location.getWorld())) { worldShops.add(shop); } } diff --git a/src/main/java/com/io/yutian/auchestshop/shop/Shop.java b/src/main/java/com/io/yutian/auchestshop/shop/Shop.java index 9b1a780..39cd172 100644 --- a/src/main/java/com/io/yutian/auchestshop/shop/Shop.java +++ b/src/main/java/com/io/yutian/auchestshop/shop/Shop.java @@ -1,6 +1,6 @@ package com.io.yutian.auchestshop.shop; -import org.bukkit.Location; +import com.io.yutian.auchestshop.shop.log.ShopLog; import org.bukkit.inventory.ItemStack; import java.util.Objects; @@ -9,32 +9,30 @@ import java.util.UUID; public class Shop { private final UUID uuid; - private final Location location; + private final ShopLocation location; private final UUID owner; private double price; + private ItemStack item; private ShopType shopType; private boolean unlimited; - private ItemStack item; - private ItemStack originalItem; - private ItemStack displayItem; + private ShopLog shopLog; - public Shop(UUID uuid, Location location, UUID owner, double price, ShopType shopType, boolean unlimited, ItemStack item, ItemStack originalItem, ItemStack displayItem) { + public Shop(UUID uuid, UUID owner, ShopLocation location, ItemStack item, double price, ShopType shopType, boolean unlimited, ShopLog shopLog) { this.uuid = uuid; - this.location = location; this.owner = owner; + this.location = location; + this.item = item; this.price = price; this.shopType = shopType; this.unlimited = unlimited; - this.item = item; - this.originalItem = originalItem; - this.displayItem = displayItem; + this.shopLog = shopLog; } public UUID getUUID() { return uuid; } - public Location getLocation() { + public ShopLocation getLocation() { return location; } @@ -58,12 +56,8 @@ public class Shop { return item; } - public ItemStack getOriginalItem() { - return originalItem; - } - - public ItemStack getDisplayItem() { - return displayItem; + public ShopLog getShopLog() { + return shopLog; } @Override diff --git a/src/main/java/com/io/yutian/auchestshop/shop/ShopLocation.java b/src/main/java/com/io/yutian/auchestshop/shop/ShopLocation.java new file mode 100644 index 0000000..8cd922b --- /dev/null +++ b/src/main/java/com/io/yutian/auchestshop/shop/ShopLocation.java @@ -0,0 +1,48 @@ +package com.io.yutian.auchestshop.shop; + +import org.json.JSONObject; + +public class ShopLocation { + + private final String world; + private final int x; + private final int y; + private final int z; + + public ShopLocation(String world, int x, int y, int z) { + this.world = world; + this.x = x; + this.y = y; + this.z = z; + } + + public static ShopLocation fromJSON(JSONObject json) { + return new ShopLocation(json.getString("world"), json.getInt("x"), json.getInt("y"), json.getInt("z")); + } + + public JSONObject toJSON() { + JSONObject json = new JSONObject(); + json.put("world", world); + json.put("x", x); + json.put("y", y); + json.put("z", z); + return json; + } + + public String getWorld() { + return world; + } + + public int getX() { + return x; + } + + public int getY() { + return y; + } + + public int getZ() { + return z; + } + +} diff --git a/src/main/java/com/io/yutian/auchestshop/shop/log/ShopLog.java b/src/main/java/com/io/yutian/auchestshop/shop/log/ShopLog.java new file mode 100644 index 0000000..67ff8f9 --- /dev/null +++ b/src/main/java/com/io/yutian/auchestshop/shop/log/ShopLog.java @@ -0,0 +1,62 @@ +package com.io.yutian.auchestshop.shop.log; + +import org.json.JSONArray; +import org.json.JSONObject; + +import java.util.ArrayList; +import java.util.List; + +public class ShopLog { + + private double total; + private List playerLogs; + + public ShopLog() { + this(0, new ArrayList<>()); + } + + public ShopLog(double total, List playerLogs) { + this.total = total; + this.playerLogs = playerLogs; + } + + public double getTotal() { + return total; + } + + public void setTotal(double total) { + this.total = total; + } + + public void addPlayerLog(ShopPlayerLog playerLog) { + this.playerLogs.add(playerLog); + } + + public List getPlayerLogs() { + return playerLogs; + } + + public static ShopLog fromJSON(JSONObject jsonObject) { + double total = jsonObject.getDouble("total"); + JSONArray playerLogsArray = jsonObject.getJSONArray("playerLogs"); + List shopPlayerLogs = new ArrayList<>(); + for (int i = 0; i < playerLogsArray.length(); i++) { + JSONObject playerLogObject = playerLogsArray.getJSONObject(i); + ShopPlayerLog playerLog = ShopPlayerLog.fromJSON(playerLogObject); + shopPlayerLogs.add(playerLog); + } + return new ShopLog(total, shopPlayerLogs); + } + + public JSONObject toJSON() { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("total", total); + JSONArray playerLogsArray = new JSONArray(); + for (ShopPlayerLog playerLog : playerLogs) { + JSONObject playerLogJson = playerLog.toJSON(); + playerLogsArray.put(playerLogJson); + } + jsonObject.put("playerLogs", playerLogsArray); + return jsonObject; + } +} diff --git a/src/main/java/com/io/yutian/auchestshop/shop/log/ShopPlayerLog.java b/src/main/java/com/io/yutian/auchestshop/shop/log/ShopPlayerLog.java new file mode 100644 index 0000000..784df7f --- /dev/null +++ b/src/main/java/com/io/yutian/auchestshop/shop/log/ShopPlayerLog.java @@ -0,0 +1,34 @@ +package com.io.yutian.auchestshop.shop.log; + +import org.json.JSONObject; + +import java.util.UUID; + +public class ShopPlayerLog { + + private UUID uuid; + private int amount; + private double total; + private long time; + + public ShopPlayerLog(UUID uuid, int amount, double total, long time) { + this.uuid = uuid; + this.amount = amount; + this.total = total; + this.time = time; + } + + public static ShopPlayerLog fromJSON(JSONObject json) { + return new ShopPlayerLog(UUID.fromString(json.getString("uuid")), json.getInt("amount"), json.getDouble("total"), json.getLong("time")); + } + + public JSONObject toJSON() { + JSONObject json = new JSONObject(); + json.put("uuid", uuid.toString()); + json.put("amount", amount); + json.put("total", total); + json.put("time", time); + return json; + } + +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index c496b99..27fdea4 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -4,4 +4,7 @@ version: 1.0 api-version: 1.18 author: SuperYuTian depend: - - AuLib \ No newline at end of file + - AuLib +commands: + auchestshop: + aliases: [aushop] \ No newline at end of file