mirror of
https://github.com/MiniDay/HamsterCurrency-Parent.git
synced 2025-08-22 12:15:31 +08:00
-
This commit is contained in:
@@ -10,6 +10,12 @@
|
||||
- [Vault](https://www.spigotmc.org/resources/vault.34315/) (仅开启Vault经济系统时需要)
|
||||
- [PlaceholderAPI](https://www.spigotmc.org/resources/placeholderapi.6245/) (非必须)
|
||||
|
||||
# 从其他插件导入
|
||||
|
||||
## CMI
|
||||
|
||||
|
||||
|
||||
# 开发者
|
||||
|
||||
## 依赖导入
|
||||
|
@@ -1,4 +1,5 @@
|
||||
rootProject.name = 'HamsterCurrency-Parent'
|
||||
include 'currency-plugin'
|
||||
include 'transform-essentials'
|
||||
include 'transform-CMI'
|
||||
|
||||
|
44
transform-CMI/build.gradle
Normal file
44
transform-CMI/build.gradle
Normal file
@@ -0,0 +1,44 @@
|
||||
plugins {
|
||||
id 'java'
|
||||
}
|
||||
|
||||
group 'cn.hamster3'
|
||||
version '1.0.0-SNAPSHOT'
|
||||
|
||||
repositories {
|
||||
maven {
|
||||
url = "https://maven.airgame.net/repository/maven-public/"
|
||||
}
|
||||
}
|
||||
|
||||
configurations {
|
||||
implementationShade
|
||||
implementation.extendsFrom implementationShade
|
||||
}
|
||||
|
||||
dependencies {
|
||||
// https://mvnrepository.com/artifact/com.google.code.gson/gson
|
||||
implementationShade group: 'com.google.code.gson', name: 'gson', version: '2.8.8'
|
||||
// https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc
|
||||
implementationShade group: 'org.xerial', name: 'sqlite-jdbc', version: '3.36.0.3'
|
||||
// https://mvnrepository.com/artifact/mysql/mysql-connector-java
|
||||
implementationShade group: 'mysql', name: 'mysql-connector-java', version: '8.0.26'
|
||||
}
|
||||
|
||||
java {
|
||||
sourceCompatibility = JavaVersion.VERSION_1_8
|
||||
targetCompatibility = JavaVersion.VERSION_1_8
|
||||
}
|
||||
|
||||
jar {
|
||||
archivesBaseName = "HamsterCurrency-Transform-CMI"
|
||||
manifest.attributes('Main-Class': 'cn.hamster3.transform.cmi.Main')
|
||||
from([
|
||||
configurations.implementationShade.collect {
|
||||
it.isDirectory() ? it : zipTree(it)
|
||||
},
|
||||
rootProject.file("LICENSE")
|
||||
])
|
||||
duplicatesStrategy(DuplicatesStrategy.EXCLUDE)
|
||||
destinationDir(rootProject.buildDir)
|
||||
}
|
111
transform-CMI/src/main/java/cn/hamster3/transform/cmi/Main.java
Normal file
111
transform-CMI/src/main/java/cn/hamster3/transform/cmi/Main.java
Normal file
@@ -0,0 +1,111 @@
|
||||
package cn.hamster3.transform.cmi;
|
||||
|
||||
import cn.hamster3.transform.cmi.data.PlayerData;
|
||||
|
||||
import java.io.File;
|
||||
import java.sql.*;
|
||||
import java.util.HashSet;
|
||||
import java.util.Scanner;
|
||||
import java.util.UUID;
|
||||
|
||||
public class Main {
|
||||
private static final HashSet<PlayerData> playerData = new HashSet<>();
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
System.out.println("请输入 cmi 数据库路径:");
|
||||
String databasePath = scanner.nextLine();
|
||||
System.out.println("请输入数据库主机名: ");
|
||||
String host = scanner.nextLine();
|
||||
System.out.println("请输入数据库端口号: ");
|
||||
String port = scanner.nextLine();
|
||||
System.out.println("请输入数据库用户名: ");
|
||||
String user = scanner.nextLine();
|
||||
System.out.println("请输入数据库密码: ");
|
||||
String password = scanner.nextLine();
|
||||
System.out.println("请输入数据库库名: ");
|
||||
String database = scanner.nextLine();
|
||||
|
||||
File databaseFile = new File(databasePath);
|
||||
System.out.println("开始读取 CMI 数据.");
|
||||
scanData(databaseFile);
|
||||
System.out.println("开始保存数据到 HamsterCurrency 中.");
|
||||
uploadData(host, port, user, password, database);
|
||||
System.out.printf("数据保存完成,共计转移了 %d 个玩家数据存档.%n", playerData.size());
|
||||
}
|
||||
|
||||
private static void scanData(File database) throws ClassNotFoundException, SQLException {
|
||||
System.out.println("加载 sqlite 数据库驱动...");
|
||||
Class.forName("org.sqlite.JDBC");
|
||||
System.out.println("建立 sqlite 数据库连接...");
|
||||
Connection connection = DriverManager.getConnection("jdbc:sqlite://" + database.getAbsolutePath());
|
||||
Statement statement = connection.createStatement();
|
||||
ResultSet set = statement.executeQuery("SELECT player_uuid, username, Balance FROM users;");
|
||||
while (set.next()) {
|
||||
try {
|
||||
UUID uuid = UUID.fromString(set.getString("player_uuid"));
|
||||
String username = set.getString("username");
|
||||
PlayerData data = getPlayerData(uuid, username);
|
||||
double balance = set.getDouble("Balance");
|
||||
if (data.getPlayerCurrency("金币") >= balance) {
|
||||
return;
|
||||
}
|
||||
data.setPlayerCurrency("金币", balance);
|
||||
System.out.printf("已加载 %s(%s) 的存档: %.2f金币%n", uuid, username, balance);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
statement.close();
|
||||
connection.close();
|
||||
System.out.printf("已加载 %d 个玩家的数据存档.%n", playerData.size());
|
||||
}
|
||||
|
||||
private static void uploadData(String host, String port, String user, String password, String database) throws ClassNotFoundException, SQLException {
|
||||
System.out.println("加载 MySQL 数据库驱动...");
|
||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||
System.out.println("建立 MySQL 数据库连接...");
|
||||
Connection connection = DriverManager.getConnection(
|
||||
"jdbc:mysql://" + host + ":" + port + "?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false",
|
||||
user,
|
||||
password
|
||||
);
|
||||
Statement statement = connection.createStatement();
|
||||
System.out.println("切换至数据库...");
|
||||
statement.execute(String.format("CREATE DATABASE IF NOT EXISTS %s DEFAULT CHARACTER SET ='UTF8';", database));
|
||||
statement.execute(String.format("USE %s;", database));
|
||||
System.out.println("检查数据表...");
|
||||
statement.execute("CREATE TABLE IF NOT EXISTS hamster_currency_player_data(" +
|
||||
"uuid VARCHAR(36) PRIMARY KEY," +
|
||||
"data TEXT" +
|
||||
");");
|
||||
System.out.println("开始更新数据库...");
|
||||
for (PlayerData data : playerData) {
|
||||
String sql = String.format(
|
||||
"REPLACE INTO hamster_currency_player_data VALUES('%s', '%s');",
|
||||
data.getUuid().toString(),
|
||||
data.saveToJson().toString().replace("'", "\\'")
|
||||
);
|
||||
try {
|
||||
statement.executeUpdate(sql);
|
||||
} catch (SQLException e) {
|
||||
System.out.println("执行 sql " + sql + " 时遇到了一个异常:");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
statement.close();
|
||||
connection.close();
|
||||
}
|
||||
|
||||
private static PlayerData getPlayerData(UUID uuid, String name) {
|
||||
for (PlayerData data : playerData) {
|
||||
if (data.getUuid().equals(uuid)) {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
PlayerData data = new PlayerData(uuid, name);
|
||||
playerData.add(data);
|
||||
return data;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,44 @@
|
||||
package cn.hamster3.transform.cmi.data;
|
||||
|
||||
/**
|
||||
* 货币类型
|
||||
*/
|
||||
public class CurrencyType {
|
||||
/**
|
||||
* 货币识别符
|
||||
*/
|
||||
private final String id;
|
||||
/**
|
||||
* 是否允许转账
|
||||
*/
|
||||
private final boolean canTransfer;
|
||||
|
||||
public CurrencyType(String id, boolean canTransfer) {
|
||||
this.id = id;
|
||||
this.canTransfer = canTransfer;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
|
||||
public boolean isCanTransfer() {
|
||||
return canTransfer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
CurrencyType that = (CurrencyType) o;
|
||||
|
||||
return id.equalsIgnoreCase(that.id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return id.hashCode();
|
||||
}
|
||||
}
|
@@ -0,0 +1,63 @@
|
||||
package cn.hamster3.transform.cmi.data;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
public class PlayerData {
|
||||
private final UUID uuid;
|
||||
private final String playerName;
|
||||
private final HashMap<String, Double> playerCurrencies;
|
||||
|
||||
public PlayerData(UUID uuid, String playerName) {
|
||||
this.uuid = uuid;
|
||||
this.playerName = playerName;
|
||||
playerCurrencies = new HashMap<>();
|
||||
}
|
||||
|
||||
|
||||
public JsonObject saveToJson() {
|
||||
JsonObject object = new JsonObject();
|
||||
object.addProperty("uuid", uuid.toString());
|
||||
object.addProperty("playerName", playerName);
|
||||
JsonObject playerCurrenciesJson = new JsonObject();
|
||||
for (Map.Entry<String, Double> entry : playerCurrencies.entrySet()) {
|
||||
playerCurrenciesJson.addProperty(entry.getKey(), entry.getValue());
|
||||
}
|
||||
object.add("playerCurrencies", playerCurrenciesJson);
|
||||
return object;
|
||||
}
|
||||
|
||||
public UUID getUuid() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
public String getPlayerName() {
|
||||
return playerName;
|
||||
}
|
||||
|
||||
public void setPlayerCurrency(String type, double amount) {
|
||||
playerCurrencies.put(type, amount);
|
||||
}
|
||||
|
||||
public double getPlayerCurrency(String type) {
|
||||
return playerCurrencies.getOrDefault(type, 0D);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
PlayerData that = (PlayerData) o;
|
||||
|
||||
return uuid.equals(that.uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return uuid.hashCode();
|
||||
}
|
||||
}
|
@@ -49,7 +49,6 @@ public class Main {
|
||||
System.out.println("加载 MySQL 数据库驱动...");
|
||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||
System.out.println("建立 MySQL 数据库连接...");
|
||||
|
||||
Connection connection = DriverManager.getConnection(
|
||||
"jdbc:mysql://" + host + ":" + port + "?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false",
|
||||
user,
|
||||
|
Reference in New Issue
Block a user