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>
<groupId>com.io.yutian</groupId>
<artifactId>AuLib</artifactId>
<version>1.2.2</version>
<version>1.4.1</version>
<scope>provided</scope>
</dependency>
</dependencies>

View File

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

View File

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

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;
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<Shop> loadAllShops() {
List<Shop> 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<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) {
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.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<Shop> getShopsInWorld(@NotNull World world) {
final List<Shop> 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<Shop> getShopsInWorld(@NotNull String worldName) {
final List<Shop> 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);
}
}

View File

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

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
author: SuperYuTian
depend:
- AuLib
- AuLib
commands:
auchestshop:
aliases: [aushop]