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