This commit is contained in:
YuTian 2024-07-16 23:05:14 +08:00
commit 933d6c974a
14 changed files with 582 additions and 0 deletions

40
.gitignore vendored Normal file
View File

@ -0,0 +1,40 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
/.idea/
/target/

55
pom.xml Normal file
View File

@ -0,0 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.io.yutian.auchestshop</groupId>
<artifactId>AuChestShop</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<repository>
<id>papermc-repo</id>
<url>https://repo.papermc.io/repository/maven-public/</url>
</repository>
<repository>
<id>sonatype</id>
<url>https://oss.sonatype.org/content/groups/public/</url>
</repository>
<repository>
<id>public-rpg</id>
<url>https://repo.aurora-pixels.com/repository/public-rpg/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>io.papermc.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.18.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.io.yutian.pixelpaper</groupId>
<artifactId>pixelpaper-api</artifactId>
<version>1.18.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.io.yutian</groupId>
<artifactId>AuLib</artifactId>
<version>1.2.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,48 @@
package com.io.yutian.auchestshop;
import com.io.yutian.auchestshop.database.SQLIO;
import com.io.yutian.auchestshop.manager.ShopManager;
import com.io.yutian.aulib.lang.Lang;
import org.bukkit.plugin.java.JavaPlugin;
public class AuChestShop extends JavaPlugin {
private static AuChestShop instance;
private static SQLIO sqlIO;
private static ShopManager shopManager;
@Override
public void onEnable() {
instance = this;
sqlIO = new SQLIO();
shopManager = new ShopManager();
Lang.reload(this);
try {
sqlIO.init();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onDisable() {
sqlIO.close();
}
public static SQLIO getSQLIO() {
return sqlIO;
}
public static ShopManager getShopManager() {
return shopManager;
}
public static AuChestShop inst() {
return instance;
}
}

View File

@ -0,0 +1,13 @@
package com.io.yutian.auchestshop.command;
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));
}
}

View File

@ -0,0 +1,12 @@
package com.io.yutian.auchestshop.database;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class SQLExecutor {
public static ExecutorService HIKARICP_EXECUTOR = new ThreadPoolExecutor(2, 8, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
}

View File

@ -0,0 +1,71 @@
package com.io.yutian.auchestshop.database;
import com.io.yutian.auchestshop.AuChestShop;
import com.io.yutian.aulib.sql.SQLHelper;
import com.io.yutian.aulib.sql.api.SQLManager;
import com.io.yutian.aulib.sql.api.SQLQuery;
import com.io.yutian.aulib.sql.api.enums.IndexType;
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 java.io.File;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.UUID;
public class SQLIO {
private SQLManager sqlManager;
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");
sqlManager = new SQLManagerImpl(new HikariDataSource(config), "AuChestShop-Hikari-SQLManager");
sqlManager.executeSQL("SET MODE=MYSQL;");
sqlManager.setExecutorPool(SQLExecutor.HIKARICP_EXECUTOR);
createTables();
}
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)
.build().execute(null);
}
private void query(UUID uuid) {
try (SQLQuery query = 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");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void close() {
if (sqlManager == null) {
return;
}
SQLHelper.shutdownManager(sqlManager);
}
}

View File

@ -0,0 +1,14 @@
package com.io.yutian.auchestshop.listener;
import com.io.yutian.aulib.listener.IListener;
import org.bukkit.plugin.Plugin;
public class ShopListener extends IListener {
public ShopListener(Plugin plugin) {
super(plugin);
}
}

View File

@ -0,0 +1,132 @@
package com.io.yutian.auchestshop.manager;
import com.google.common.collect.Maps;
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.util.ShopUtil;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.World;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
public class ShopManager {
private final Map<String, Map<ShopChunk, Map<Location, Shop>>> shops = Maps.newConcurrentMap();
private final Set<Shop> loadedShops = Sets.newConcurrentHashSet();
public void loadAll(ShopLoader shopLoader) {
}
public @NotNull List<Shop> getAllShops() {
List<Shop> shopsCollected = new ArrayList<>();
for (Map<ShopChunk, Map<Location, Shop>> shopMapData : getShops().values()) {
for (Map<Location, Shop> shopData : shopMapData.values()) {
shopsCollected.addAll(shopData.values());
}
}
return shopsCollected;
}
public @NotNull List<Shop> getAllShops(@NotNull UUID playerUUID) {
List<Shop> playerShops = new ArrayList<>();
for (Shop shop : getAllShops()) {
UUID shopUuid = shop.getOwner();
if (playerUUID.equals(shopUuid)) {
playerShops.add(shop);
}
}
return playerShops;
}
public @Nullable Shop getShop(UUID shopId) {
for (Shop shop : getAllShops()) {
if (shop.getUUID().equals(shopId)) {
return shop;
}
}
return null;
}
public @Nullable Shop getShop(@NotNull Location location) {
if (!ShopUtil.isShoppables(location.getBlock().getType())) {
return null;
}
ShopChunk shopChunk = ShopChunk.fromLocation(location);
final Map<Location, Shop> inChunk = getShops(shopChunk);
if (inChunk == null) {
return null;
}
location = location.clone();
location.setX(location.getBlockX());
location.setY(location.getBlockY());
location.setZ(location.getBlockZ());
return inChunk.get(location);
}
public Map<String, Map<ShopChunk, Map<Location, Shop>>> getShops() {
return this.shops;
}
public Map<Location, Shop> getShops(@NotNull Chunk chunk) {
return getShops(chunk.getWorld().getName(), chunk.getX(), chunk.getZ());
}
public Map<Location, Shop> getShops(@NotNull String world, int chunkX, int chunkZ) {
final Map<ShopChunk, Map<Location, Shop>> inWorld = this.getShops(world);
if (inWorld == null) {
return null;
}
return inWorld.get(new ShopChunk(world, chunkX, chunkZ));
}
public Map<Location, Shop> getShops(@NotNull ShopChunk shopChunk) {
return getShops(shopChunk.getWorld(), shopChunk.getX(), shopChunk.getZ());
}
public Map<ShopChunk, Map<Location, Shop>> getShops(@NotNull String world) {
return this.shops.get(world);
}
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)) {
worldShops.add(shop);
}
}
return worldShops;
}
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())) {
worldShops.add(shop);
}
}
return worldShops;
}
public void loadShop(@NotNull Shop shop) {
loadedShops.add(shop);
}
public void unloadShop(@NotNull Shop shop) {
this.loadedShops.remove(shop);
}
public Set<Shop> getLoadedShops() {
return loadedShops;
}
}

View File

@ -0,0 +1,84 @@
package com.io.yutian.auchestshop.shop;
import org.bukkit.Location;
import org.bukkit.inventory.ItemStack;
import java.util.Objects;
import java.util.UUID;
public class Shop {
private final UUID uuid;
private final Location location;
private final UUID owner;
private double price;
private ShopType shopType;
private boolean unlimited;
private ItemStack item;
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) {
this.uuid = uuid;
this.location = location;
this.owner = owner;
this.price = price;
this.shopType = shopType;
this.unlimited = unlimited;
this.item = item;
this.originalItem = originalItem;
this.displayItem = displayItem;
}
public UUID getUUID() {
return uuid;
}
public Location getLocation() {
return location;
}
public UUID getOwner() {
return owner;
}
public double getPrice() {
return price;
}
public ShopType getShopType() {
return shopType;
}
public boolean isUnlimited() {
return unlimited;
}
public ItemStack getItem() {
return item;
}
public ItemStack getOriginalItem() {
return originalItem;
}
public ItemStack getDisplayItem() {
return displayItem;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Shop shop = (Shop) o;
return Objects.equals(uuid, shop.uuid) && Objects.equals(getOwner(), shop.getOwner());
}
@Override
public int hashCode() {
int result = Objects.hashCode(uuid);
result = 31 * result + Objects.hashCode(getOwner());
return result;
}
}

View File

@ -0,0 +1,63 @@
package com.io.yutian.auchestshop.shop;
import org.bukkit.Location;
import org.bukkit.World;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
public class ShopChunk {
private final String world;
private final int x;
private final int z;
public ShopChunk(String world, int x, int z) {
this.world = world;
this.x = x;
this.z = z;
}
public String getWorld() {
return world;
}
public int getX() {
return x;
}
public int getZ() {
return z;
}
public boolean equals(@NotNull String world, int x, int z) {
return this.x == x && this.z == z && this.world.equals(world);
}
public boolean equals(@NotNull World world, int x, int z) {
return equals(world.getName(), x, z);
}
public static ShopChunk fromLocation(@NotNull Location location) {
return new ShopChunk(location.getWorld().getName(), location.getBlockX() >> 4, location.getBlockZ() >> 4);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ShopChunk shopChunk = (ShopChunk) o;
return getX() == shopChunk.getX() && getZ() == shopChunk.getZ() && Objects.equals(getWorld(), shopChunk.getWorld());
}
@Override
public int hashCode() {
int result = Objects.hashCode(getWorld());
result = 31 * result + getX();
result = 31 * result + getZ();
return result;
}
}

View File

@ -0,0 +1,10 @@
package com.io.yutian.auchestshop.shop;
public class ShopLoader {
public void load() {
}
}

View File

@ -0,0 +1,8 @@
package com.io.yutian.auchestshop.shop;
public enum ShopType {
SELLING,
BUYING
}

View File

@ -0,0 +1,25 @@
package com.io.yutian.auchestshop.util;
import org.bukkit.Material;
import org.jetbrains.annotations.NotNull;
import java.util.EnumSet;
public class ShopUtil {
private static final EnumSet<Material> SHOPABLES = EnumSet.noneOf(Material.class);
static {
SHOPABLES.add(Material.CHEST);
SHOPABLES.add(Material.TRAPPED_CHEST);
}
public static boolean isShoppables(@NotNull Material material) {
return SHOPABLES.contains(material);
}
public static EnumSet<Material> getShopables() {
return SHOPABLES;
}
}

View File

@ -0,0 +1,7 @@
name: AuChestShop
main: com.io.yutian.auchestshop.AuChestShop
version: 1.0
api-version: 1.18
author: SuperYuTian
depend:
- AuLib