This commit is contained in:
YuTian 2024-07-17 19:27:06 +08:00
parent 933d6c974a
commit c639b99f64
11 changed files with 276 additions and 46 deletions

View File

@ -46,7 +46,7 @@
<dependency> <dependency>
<groupId>com.io.yutian</groupId> <groupId>com.io.yutian</groupId>
<artifactId>AuLib</artifactId> <artifactId>AuLib</artifactId>
<version>1.2.2</version> <version>1.4.1</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -1,5 +1,6 @@
package com.io.yutian.auchestshop; package com.io.yutian.auchestshop;
import com.io.yutian.auchestshop.command.CommandManager;
import com.io.yutian.auchestshop.database.SQLIO; import com.io.yutian.auchestshop.database.SQLIO;
import com.io.yutian.auchestshop.manager.ShopManager; import com.io.yutian.auchestshop.manager.ShopManager;
import com.io.yutian.aulib.lang.Lang; import com.io.yutian.aulib.lang.Lang;
@ -11,13 +12,16 @@ public class AuChestShop extends JavaPlugin {
private static SQLIO sqlIO; private static SQLIO sqlIO;
private static ShopManager shopManager; private static ShopManager shopManager;
private static CommandManager commandManager;
@Override @Override
public void onEnable() { public void onEnable() {
instance = this; instance = this;
sqlIO = new SQLIO(); sqlIO = new SQLIO();
commandManager = new CommandManager();
shopManager = new ShopManager(); shopManager = new ShopManager();
commandManager.registerBukkitCommand(this, "auchestshop");
Lang.reload(this); Lang.reload(this);

View File

@ -1,13 +1,13 @@
package com.io.yutian.auchestshop.command; 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.SimpleCommandManager;
import com.io.yutian.aulib.command.list.CommandHelp;
public class CommandManager extends SimpleCommandManager { public class CommandManager extends SimpleCommandManager {
public CommandManager() { public CommandManager() {
super("chestshop"); super("chestshop");
register(new CommandHelp(this)); register(new CommandTest());
} }
} }

View File

@ -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<Shop> 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!");
}
}
}

View File

@ -1,6 +1,8 @@
package com.io.yutian.auchestshop.database; package com.io.yutian.auchestshop.database;
import com.io.yutian.auchestshop.AuChestShop; 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.SQLHelper;
import com.io.yutian.aulib.sql.api.SQLManager; import com.io.yutian.aulib.sql.api.SQLManager;
import com.io.yutian.aulib.sql.api.SQLQuery; 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.io.yutian.aulib.sql.util.DatabaseDriverType;
import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource; import com.zaxxer.hikari.HikariDataSource;
import org.json.JSONObject;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.util.UUID; import java.util.ArrayList;
import java.util.List;
public class SQLIO { public class SQLIO {
@ -21,10 +25,9 @@ public class SQLIO {
public void init() throws IOException { public void init() throws IOException {
HikariConfig config = new HikariConfig(); HikariConfig config = new HikariConfig();
config.setDriverClassName(DatabaseDriverType.H2.getDriverClassName()); config.setDriverClassName(DatabaseDriverType.SQLITE.getDriverClassName());
config.setJdbcUrl("jdbc:h2:"+new File(AuChestShop.inst().getDataFolder(), "shops").getCanonicalFile().getAbsolutePath() + ";MODE=MYSQL"); config.setJdbcUrl("jdbc:sqlite:"+new File(AuChestShop.inst().getDataFolder(), "shops.db").getCanonicalFile().getAbsolutePath());
sqlManager = new SQLManagerImpl(new HikariDataSource(config), "AuChestShop-Hikari-SQLManager"); sqlManager = new SQLManagerImpl(new HikariDataSource(config), "AuChestShop-Hikari-SQLManager");
sqlManager.executeSQL("SET MODE=MYSQL;");
sqlManager.setExecutorPool(SQLExecutor.HIKARICP_EXECUTOR); sqlManager.setExecutorPool(SQLExecutor.HIKARICP_EXECUTOR);
createTables(); createTables();
@ -32,30 +35,60 @@ public class SQLIO {
private void createTables() { private void createTables() {
sqlManager.createTable("shops") sqlManager.createTable("shops")
.addColumn("uuid", "VARCHAR(32) NOT NULL") .addColumn("uuid", "VARCHAR(48) NOT NULL")
.addColumn("owner", "VARCHAR(32) NOT NULL") .addColumn("owner", "VARCHAR(48) NOT NULL")
.addColumn("item", "TEXT NOT NULL") .addColumn("data", "TEXT NOT NULL")
.addColumn("type", "INT NOT NULL DEFAULT 0") .setIndex("uuid", IndexType.PRIMARY_KEY)
.addColumn("price", "DECIMAL(32,2) NOT NULL") .setTableSettings("")
.addColumn("unlimited", "BIT NOT NULL DEFAULT 0")
.addColumn("log", "TEXT NOT NULL")
.addColumn("create_time", "LONG NOT NULL")
.setIndex("uuid", IndexType.UNIQUE_KEY)
.build().execute(null); .build().execute(null);
} }
private void query(UUID uuid) { public List<Shop> loadAllShops() {
try (SQLQuery query = sqlManager.createQuery() List<Shop> shops = new ArrayList<>();
sqlManager.createQuery()
.inTable("shops") .inTable("shops")
.selectColumns("uuid", "name", "item", "type", "price", "unlimited", "create_time") .build().executeAsync(sqlQuery -> {
.addCondition("uuid", uuid.toString()) ResultSet resultSet = sqlQuery.getResultSet();
.build().execute(null)) { while (resultSet.next()) {
try (ResultSet resultSet = query.getResultSet()) { String uuid = resultSet.getString("uuid");
while (resultSet.next()) { String owner = resultSet.getString("owner");
String name = resultSet.getString("name"); 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<Shop> shops) {
try {
List<Shop> 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<Object[]> 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) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -5,6 +5,7 @@ import com.google.common.collect.Sets;
import com.io.yutian.auchestshop.shop.Shop; import com.io.yutian.auchestshop.shop.Shop;
import com.io.yutian.auchestshop.shop.ShopChunk; import com.io.yutian.auchestshop.shop.ShopChunk;
import com.io.yutian.auchestshop.shop.ShopLoader; import com.io.yutian.auchestshop.shop.ShopLoader;
import com.io.yutian.auchestshop.shop.ShopLocation;
import com.io.yutian.auchestshop.util.ShopUtil; import com.io.yutian.auchestshop.util.ShopUtil;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.Chunk; import org.bukkit.Chunk;
@ -98,8 +99,8 @@ public class ShopManager {
public @NotNull List<Shop> getShopsInWorld(@NotNull World world) { public @NotNull List<Shop> getShopsInWorld(@NotNull World world) {
final List<Shop> worldShops = new ArrayList<>(); final List<Shop> worldShops = new ArrayList<>();
for (final Shop shop : getAllShops()) { for (final Shop shop : getAllShops()) {
Location location = shop.getLocation(); ShopLocation location = shop.getLocation();
if (location.isWorldLoaded() && Objects.equals(location.getWorld(), world)) { if (Objects.equals(location.getWorld(), world)) {
worldShops.add(shop); worldShops.add(shop);
} }
} }
@ -109,8 +110,8 @@ public class ShopManager {
public @NotNull List<Shop> getShopsInWorld(@NotNull String worldName) { public @NotNull List<Shop> getShopsInWorld(@NotNull String worldName) {
final List<Shop> worldShops = new ArrayList<>(); final List<Shop> worldShops = new ArrayList<>();
for (final Shop shop : getAllShops()) { for (final Shop shop : getAllShops()) {
Location location = shop.getLocation(); ShopLocation location = shop.getLocation();
if (location.isWorldLoaded() && StringUtils.equals(worldName, location.getWorld().getName())) { if (StringUtils.equals(worldName, location.getWorld())) {
worldShops.add(shop); worldShops.add(shop);
} }
} }

View File

@ -1,6 +1,6 @@
package com.io.yutian.auchestshop.shop; package com.io.yutian.auchestshop.shop;
import org.bukkit.Location; import com.io.yutian.auchestshop.shop.log.ShopLog;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import java.util.Objects; import java.util.Objects;
@ -9,32 +9,30 @@ import java.util.UUID;
public class Shop { public class Shop {
private final UUID uuid; private final UUID uuid;
private final Location location; private final ShopLocation location;
private final UUID owner; private final UUID owner;
private double price; private double price;
private ItemStack item;
private ShopType shopType; private ShopType shopType;
private boolean unlimited; private boolean unlimited;
private ItemStack item; private ShopLog shopLog;
private ItemStack originalItem;
private ItemStack displayItem;
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.uuid = uuid;
this.location = location;
this.owner = owner; this.owner = owner;
this.location = location;
this.item = item;
this.price = price; this.price = price;
this.shopType = shopType; this.shopType = shopType;
this.unlimited = unlimited; this.unlimited = unlimited;
this.item = item; this.shopLog = shopLog;
this.originalItem = originalItem;
this.displayItem = displayItem;
} }
public UUID getUUID() { public UUID getUUID() {
return uuid; return uuid;
} }
public Location getLocation() { public ShopLocation getLocation() {
return location; return location;
} }
@ -58,12 +56,8 @@ public class Shop {
return item; return item;
} }
public ItemStack getOriginalItem() { public ShopLog getShopLog() {
return originalItem; return shopLog;
}
public ItemStack getDisplayItem() {
return displayItem;
} }
@Override @Override

View File

@ -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;
}
}

View File

@ -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<ShopPlayerLog> playerLogs;
public ShopLog() {
this(0, new ArrayList<>());
}
public ShopLog(double total, List<ShopPlayerLog> 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<ShopPlayerLog> getPlayerLogs() {
return playerLogs;
}
public static ShopLog fromJSON(JSONObject jsonObject) {
double total = jsonObject.getDouble("total");
JSONArray playerLogsArray = jsonObject.getJSONArray("playerLogs");
List<ShopPlayerLog> 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;
}
}

View File

@ -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;
}
}

View File

@ -4,4 +4,7 @@ version: 1.0
api-version: 1.18 api-version: 1.18
author: SuperYuTian author: SuperYuTian
depend: depend:
- AuLib - AuLib
commands:
auchestshop:
aliases: [aushop]