mirror of
https://github.com/MiniDay/HamsterCurrency-Parent.git
synced 2025-08-22 20:25:30 +08:00
Compare commits
2 Commits
bcfb866da7
...
91817db007
Author | SHA1 | Date | |
---|---|---|---|
91817db007 | |||
840cc6d238 |
@@ -5,7 +5,7 @@ plugins {
|
||||
}
|
||||
|
||||
group 'cn.hamster3'
|
||||
version '2.2.5'
|
||||
version '2.2.6'
|
||||
|
||||
repositories {
|
||||
maven {
|
||||
|
@@ -17,7 +17,7 @@ public abstract class FileManager {
|
||||
plugin.reloadConfig();
|
||||
pluginConfig = plugin.getConfig();
|
||||
useBC = pluginConfig.getBoolean("useBC", false);
|
||||
mainServer = pluginConfig.getBoolean("datasource.template");
|
||||
mainServer = pluginConfig.getBoolean("datasource.template", true);
|
||||
setPluginConfig(pluginConfig);
|
||||
}
|
||||
|
||||
|
@@ -25,7 +25,6 @@ public class SQLDataManager implements IDataManager {
|
||||
private final HamsterCurrency plugin;
|
||||
private final JsonParser parser;
|
||||
|
||||
private final String database;
|
||||
private final DataSource datasource;
|
||||
|
||||
private final Map<UUID, PlayerData> playerData;
|
||||
@@ -38,16 +37,15 @@ public class SQLDataManager implements IDataManager {
|
||||
currencyTypes = new HashSet<>();
|
||||
|
||||
ConfigurationSection datasourceConfig = FileManager.getPluginConfig().getConfigurationSection("datasource");
|
||||
database = datasourceConfig.getString("database");
|
||||
datasource = HamsterAPI.getHikariDataSource(datasourceConfig);
|
||||
|
||||
Connection connection = datasource.getConnection();
|
||||
Statement statement = connection.createStatement();
|
||||
statement.execute("CREATE TABLE IF NOT EXISTS " + database + ".hamster_currency_player_data(" +
|
||||
statement.execute("CREATE TABLE IF NOT EXISTS hamster_currency_player_data(" +
|
||||
"uuid VARCHAR(36) PRIMARY KEY," +
|
||||
"data TEXT" +
|
||||
") CHARACTER SET = utf8mb4;");
|
||||
statement.execute("CREATE TABLE IF NOT EXISTS " + database + ".hamster_currency_logs(" +
|
||||
statement.execute("CREATE TABLE IF NOT EXISTS hamster_currency_logs(" +
|
||||
"uuid VARCHAR(36) NOT NULL," +
|
||||
"player_name VARCHAR(36) NOT NULL," +
|
||||
"type VARCHAR(36) NOT NULL," +
|
||||
@@ -58,7 +56,7 @@ public class SQLDataManager implements IDataManager {
|
||||
"INDEX idx_uuid(uuid)," +
|
||||
"INDEX idx_name(player_name)" +
|
||||
") CHARACTER SET = utf8mb4;");
|
||||
statement.execute("CREATE TABLE IF NOT EXISTS " + database + ".hamster_currency_settings(" +
|
||||
statement.execute("CREATE TABLE IF NOT EXISTS hamster_currency_settings(" +
|
||||
"title VARCHAR(64) PRIMARY KEY," +
|
||||
"data TEXT" +
|
||||
") CHARACTER SET = utf8mb4;");
|
||||
@@ -78,7 +76,7 @@ public class SQLDataManager implements IDataManager {
|
||||
Statement statement = connection.createStatement();
|
||||
String data = Base64.getEncoder().encodeToString(config.saveToString().getBytes(StandardCharsets.UTF_8));
|
||||
statement.executeUpdate(String.format(
|
||||
"REPLACE INTO " + database + ".hamster_currency_settings VALUES('%s', '%s');",
|
||||
"REPLACE INTO hamster_currency_settings VALUES('%s', '%s');",
|
||||
"pluginConfig",
|
||||
data
|
||||
));
|
||||
@@ -98,7 +96,7 @@ public class SQLDataManager implements IDataManager {
|
||||
getLogUtils().info("从数据库中下载配置文件...");
|
||||
Connection connection = datasource.getConnection();
|
||||
Statement statement = connection.createStatement();
|
||||
ResultSet set = statement.executeQuery("SELECT * FROM " + database + ".hamster_currency_settings;");
|
||||
ResultSet set = statement.executeQuery("SELECT * FROM hamster_currency_settings;");
|
||||
while (set.next()) {
|
||||
String title = set.getString("title");
|
||||
String data = new String(Base64.getDecoder().decode(set.getString("data")), StandardCharsets.UTF_8);
|
||||
@@ -190,7 +188,7 @@ public class SQLDataManager implements IDataManager {
|
||||
try {
|
||||
Connection connection = datasource.getConnection();
|
||||
Statement statement = connection.createStatement();
|
||||
ResultSet set = statement.executeQuery("SELECT * FROM " + database + ".hamster_currency_player_data;");
|
||||
ResultSet set = statement.executeQuery("SELECT * FROM hamster_currency_player_data;");
|
||||
while (set.next()) {
|
||||
String uuid = set.getString("uuid");
|
||||
String string = set.getString("data");
|
||||
@@ -237,7 +235,7 @@ public class SQLDataManager implements IDataManager {
|
||||
Connection connection = datasource.getConnection();
|
||||
Statement statement = connection.createStatement();
|
||||
ResultSet set = statement.executeQuery(String.format(
|
||||
"SELECT * FROM " + database + ".hamster_currency_player_data WHERE uuid='%s';",
|
||||
"SELECT * FROM hamster_currency_player_data WHERE uuid='%s';",
|
||||
uuid
|
||||
));
|
||||
PlayerData data;
|
||||
@@ -272,7 +270,7 @@ public class SQLDataManager implements IDataManager {
|
||||
Connection connection = datasource.getConnection();
|
||||
Statement statement = connection.createStatement();
|
||||
statement.executeUpdate(String.format(
|
||||
"REPLACE INTO " + database + ".hamster_currency_player_data VALUES('%s', '%s');",
|
||||
"REPLACE INTO hamster_currency_player_data VALUES('%s', '%s');",
|
||||
data.getUuid().toString(),
|
||||
data.saveToJson().toString()
|
||||
));
|
||||
@@ -295,7 +293,7 @@ public class SQLDataManager implements IDataManager {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
try (Connection connection = datasource.getConnection()) {
|
||||
try (PreparedStatement statement = connection.prepareStatement(
|
||||
"INSERT INTO " + database + ".hamster_currency_logs VALUES(?, ?, ?, ?, ?, ?, NOW());"
|
||||
"INSERT INTO hamster_currency_logs VALUES(?, ?, ?, ?, ?, ?, NOW());"
|
||||
)) {
|
||||
statement.setString(1, log.getUuid().toString());
|
||||
statement.setString(2, log.getPlayerName());
|
||||
|
@@ -0,0 +1,50 @@
|
||||
package cn.hamster3.currency.event.vault;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class VaultEconomyGiveEvent extends Event {
|
||||
private static final HandlerList handlerList = new HandlerList();
|
||||
|
||||
@NotNull
|
||||
private final UUID uuid;
|
||||
@NotNull
|
||||
private final String currencyID;
|
||||
private double amount;
|
||||
|
||||
public VaultEconomyGiveEvent(@NotNull UUID uuid, @NotNull String currencyID, double amount) {
|
||||
super(!Bukkit.getServer().isPrimaryThread());
|
||||
this.uuid = uuid;
|
||||
this.currencyID = currencyID;
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
public static HandlerList getHandlerList() {
|
||||
return handlerList;
|
||||
}
|
||||
|
||||
public @NotNull UUID getUuid() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
public @NotNull String getCurrencyID() {
|
||||
return currencyID;
|
||||
}
|
||||
|
||||
public double getAmount() {
|
||||
return amount;
|
||||
}
|
||||
|
||||
public void setAmount(double amount) {
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HandlerList getHandlers() {
|
||||
return handlerList;
|
||||
}
|
||||
}
|
@@ -0,0 +1,50 @@
|
||||
package cn.hamster3.currency.event.vault;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class VaultEconomySeeEvent extends Event {
|
||||
private static final HandlerList handlerList = new HandlerList();
|
||||
|
||||
@NotNull
|
||||
private final UUID uuid;
|
||||
@NotNull
|
||||
private final String currencyID;
|
||||
private double result;
|
||||
|
||||
public VaultEconomySeeEvent(@NotNull UUID uuid, @NotNull String currencyID, double result) {
|
||||
super(!Bukkit.getServer().isPrimaryThread());
|
||||
this.uuid = uuid;
|
||||
this.currencyID = currencyID;
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
public static HandlerList getHandlerList() {
|
||||
return handlerList;
|
||||
}
|
||||
|
||||
public @NotNull UUID getUuid() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
public @NotNull String getCurrencyID() {
|
||||
return currencyID;
|
||||
}
|
||||
|
||||
public double getResult() {
|
||||
return result;
|
||||
}
|
||||
|
||||
public void setResult(double result) {
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HandlerList getHandlers() {
|
||||
return handlerList;
|
||||
}
|
||||
}
|
@@ -0,0 +1,50 @@
|
||||
package cn.hamster3.currency.event.vault;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class VaultEconomyTakeEvent extends Event {
|
||||
private static final HandlerList handlerList = new HandlerList();
|
||||
|
||||
@NotNull
|
||||
private final UUID uuid;
|
||||
@NotNull
|
||||
private final String currencyID;
|
||||
private double amount;
|
||||
|
||||
public VaultEconomyTakeEvent(@NotNull UUID uuid, @NotNull String currencyID, double amount) {
|
||||
super(!Bukkit.getServer().isPrimaryThread());
|
||||
this.uuid = uuid;
|
||||
this.currencyID = currencyID;
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
public static HandlerList getHandlerList() {
|
||||
return handlerList;
|
||||
}
|
||||
|
||||
public @NotNull UUID getUuid() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
public @NotNull String getCurrencyID() {
|
||||
return currencyID;
|
||||
}
|
||||
|
||||
public double getAmount() {
|
||||
return amount;
|
||||
}
|
||||
|
||||
public void setAmount(double amount) {
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HandlerList getHandlers() {
|
||||
return handlerList;
|
||||
}
|
||||
}
|
@@ -5,21 +5,21 @@ import cn.hamster3.currency.core.IDataManager;
|
||||
import cn.hamster3.currency.core.Message;
|
||||
import cn.hamster3.currency.data.CurrencyLog;
|
||||
import cn.hamster3.currency.data.PlayerData;
|
||||
import cn.hamster3.currency.event.vault.VaultEconomyGiveEvent;
|
||||
import cn.hamster3.currency.event.vault.VaultEconomySeeEvent;
|
||||
import cn.hamster3.currency.event.vault.VaultEconomyTakeEvent;
|
||||
import net.milkbowl.vault.economy.AbstractEconomy;
|
||||
import net.milkbowl.vault.economy.EconomyResponse;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class VaultEconomyHook extends AbstractEconomy {
|
||||
private static final EconomyResponse NOT_IMPLEMENTED_RESPONSE =
|
||||
new EconomyResponse(
|
||||
0,
|
||||
0,
|
||||
EconomyResponse.ResponseType.NOT_IMPLEMENTED,
|
||||
"HamsterCurrency未实现该功能~"
|
||||
);
|
||||
private static final EconomyResponse NOT_IMPLEMENTED_RESPONSE = new EconomyResponse(
|
||||
0, 0, EconomyResponse.ResponseType.NOT_IMPLEMENTED, "HamsterCurrency未实现该功能~"
|
||||
);
|
||||
private final IDataManager dataManager;
|
||||
|
||||
public VaultEconomyHook(IDataManager dataManager) {
|
||||
@@ -31,6 +31,12 @@ public class VaultEconomyHook extends AbstractEconomy {
|
||||
return new EconomyResponse(amount, 0, EconomyResponse.ResponseType.FAILURE, "玩家账户不存在");
|
||||
}
|
||||
String type = FileManager.getVaultCurrencyType();
|
||||
VaultEconomyGiveEvent event = new VaultEconomyGiveEvent(data.getUuid(), type, amount);
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
amount = event.getAmount();
|
||||
if (amount == 0) {
|
||||
return new EconomyResponse(amount, data.getPlayerCurrency(type), EconomyResponse.ResponseType.SUCCESS, null);
|
||||
}
|
||||
double balance = data.getPlayerCurrency(type) + amount;
|
||||
if (data.getPlayerCurrency(type) > 0 && balance < 0) {
|
||||
return new EconomyResponse(amount, 0, EconomyResponse.ResponseType.FAILURE, "玩家金额超出上限");
|
||||
@@ -38,7 +44,7 @@ public class VaultEconomyHook extends AbstractEconomy {
|
||||
data.setPlayerCurrency(type, balance);
|
||||
dataManager.savePlayerData(data);
|
||||
dataManager.insertLog(new CurrencyLog(data.getUuid(), data.getPlayerName(), type, "add", amount, balance));
|
||||
return new EconomyResponse(amount, data.getPlayerCurrency(type), EconomyResponse.ResponseType.SUCCESS, "");
|
||||
return new EconomyResponse(amount, data.getPlayerCurrency(type), EconomyResponse.ResponseType.SUCCESS, null);
|
||||
}
|
||||
|
||||
protected EconomyResponse withdrawPlayer(PlayerData data, double amount) {
|
||||
@@ -46,6 +52,12 @@ public class VaultEconomyHook extends AbstractEconomy {
|
||||
return new EconomyResponse(amount, 0, EconomyResponse.ResponseType.FAILURE, "玩家账户不存在");
|
||||
}
|
||||
String type = FileManager.getVaultCurrencyType();
|
||||
VaultEconomyTakeEvent event = new VaultEconomyTakeEvent(data.getUuid(), type, amount);
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
amount = event.getAmount();
|
||||
if (amount == 0) {
|
||||
return new EconomyResponse(amount, data.getPlayerCurrency(type), EconomyResponse.ResponseType.SUCCESS, null);
|
||||
}
|
||||
if (data.getPlayerCurrency(type) < amount) {
|
||||
return new EconomyResponse(amount, data.getPlayerCurrency(type), EconomyResponse.ResponseType.FAILURE, "余额不足");
|
||||
}
|
||||
@@ -53,21 +65,24 @@ public class VaultEconomyHook extends AbstractEconomy {
|
||||
data.setPlayerCurrency(type, balance);
|
||||
dataManager.savePlayerData(data);
|
||||
dataManager.insertLog(new CurrencyLog(data.getUuid(), data.getPlayerName(), type, "take", amount, balance));
|
||||
return new EconomyResponse(amount, data.getPlayerCurrency(type), EconomyResponse.ResponseType.SUCCESS, "扣款成功");
|
||||
return new EconomyResponse(amount, data.getPlayerCurrency(type), EconomyResponse.ResponseType.SUCCESS, null);
|
||||
}
|
||||
|
||||
private boolean has(PlayerData data, double amount) {
|
||||
if (data == null) {
|
||||
return false;
|
||||
}
|
||||
return data.getPlayerCurrency(FileManager.getVaultCurrencyType()) >= amount;
|
||||
return getBalance(data) >= amount;
|
||||
}
|
||||
|
||||
private double getBalance(PlayerData data) {
|
||||
if (data == null) {
|
||||
return 0;
|
||||
}
|
||||
return data.getPlayerCurrency(FileManager.getVaultCurrencyType());
|
||||
double currency = data.getPlayerCurrency(FileManager.getVaultCurrencyType());
|
||||
VaultEconomySeeEvent event = new VaultEconomySeeEvent(data.getUuid(), FileManager.getVaultCurrencyType(), currency);
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
return event.getResult();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -5,19 +5,10 @@ useBC: true
|
||||
|
||||
# 若开启跨服模式,则需要配置datasource
|
||||
datasource:
|
||||
driver: "com.mysql.cj.jdbc.Driver"
|
||||
url: "jdbc:mysql://sql.hamster3.cn:3306?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false"
|
||||
user: Test
|
||||
password: Test123..
|
||||
database: Test1
|
||||
# 是否将这个服务器的配置文件设为模板
|
||||
# 若设为true,config将会在服务器启动时自动上传至数据库
|
||||
# 其他template为false的服务器将会在启动和重载时自动从数据库上下载config
|
||||
# 可以节省一些config配置时的麻烦事情
|
||||
# 但是请先保证template为true的服务器完全启动了再启动子服
|
||||
# 如果觉得这样反而更麻烦,也可以直接把所有服务器的template设为true
|
||||
# 这样每个服务器都会使用自己本地的config文件了
|
||||
template: true
|
||||
driver: "com.mysql.jdbc.Driver"
|
||||
url: "jdbc:mysql://localhost:3306/mc1.12.2-germ?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true"
|
||||
user: root
|
||||
password: Root123...
|
||||
|
||||
currencyTypes:
|
||||
# 货币ID
|
||||
|
Reference in New Issue
Block a user