This commit is contained in:
yaohunya 2024-07-29 02:18:12 +08:00
parent 57252679b0
commit dc821de01c
8 changed files with 74 additions and 33 deletions

View File

@ -7,6 +7,7 @@ import java.util.HashMap;
public class ConfigYml { public class ConfigYml {
public static boolean mysqlState = false;
private String GameMode; private String GameMode;
private long gifts_delay; private long gifts_delay;
private boolean MainDebug; private boolean MainDebug;

View File

@ -1,5 +1,6 @@
package com.io.yutian.mclive; package com.io.yutian.mclive;
import cn.hamster3.cdapi.CDTimeAPI;
import com.io.yutian.mclive.data.GiftManage; import com.io.yutian.mclive.data.GiftManage;
import com.io.yutian.mclive.event.*; import com.io.yutian.mclive.event.*;
import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.ChatMessageType;
@ -22,7 +23,7 @@ public class LiveEvent implements Listener {
String audience = e.getUser().nickName(); String audience = e.getUser().nickName();
String gift_name = e.getName(); String gift_name = e.getName();
long gift_amount = e.getAmount(); long gift_amount = e.getAmount();
if (Main.giftStatistics) { if (Main.giftStatistics && ConfigYml.mysqlState) {
GiftManage giftManage = Main.giftManage; GiftManage giftManage = Main.giftManage;
String name = e.getPlayer().getName(); String name = e.getPlayer().getName();
int trillGift = giftManage.getGiftMoney(gift_name); int trillGift = giftManage.getGiftMoney(gift_name);

View File

@ -27,17 +27,18 @@ public class Main extends JavaPlugin {
public static boolean check_plugin = false; public static boolean check_plugin = false;
public static ConfigYml configYml; public static ConfigYml configYml;
public static GiftManage giftManage; public static GiftManage giftManage;
public static boolean giftStatistics = false; public static boolean giftStatistics = true;
@Override @Override
public void onEnable() { public void onEnable() {
plugin = this; plugin = this;
SendPluginsAuthorMessage(Bukkit.getConsoleSender());
saveDefaultConfig(); saveDefaultConfig();
SendPluginsAuthorMessage(Bukkit.getConsoleSender());
configYml = new ConfigYml(getConfig());
if (giftStatistics){ if (giftStatistics){
giftManage = new GiftManage(); giftManage = new GiftManage();
giftManage.LinkMySqlData();
} }
configYml = new ConfigYml(getConfig());
getServer().getPluginManager().registerEvents(new LiveAdminGui(),this); getServer().getPluginManager().registerEvents(new LiveAdminGui(),this);
getServer().getPluginManager().registerEvents(new LinkRoom(),this); getServer().getPluginManager().registerEvents(new LinkRoom(),this);
getServer().getPluginManager().registerEvents(new ModEvent(),this); getServer().getPluginManager().registerEvents(new ModEvent(),this);
@ -190,9 +191,9 @@ public class Main extends JavaPlugin {
return String.valueOf(firstChar) + maskedString + lastChar; return String.valueOf(firstChar) + maskedString + lastChar;
} }
public static String getTime(String format){ public static String getMinecraftVersion() {
Date date = Calendar.getInstance().getTime(); String packageName = Bukkit.getServer().getClass().getPackage().getName();
SimpleDateFormat datatime = new SimpleDateFormat(format); String version = packageName.substring(packageName.lastIndexOf('.') + 1);
return datatime.format(date); return version;
} }
} }

View File

@ -170,7 +170,7 @@ public class MessageUtil {
ksapiLiveRoomWatcher.callUser(new KuaiShouUser(nickName)); ksapiLiveRoomWatcher.callUser(new KuaiShouUser(nickName));
} else if (type.equalsIgnoreCase("live_id")) { } else if (type.equalsIgnoreCase("live_id")) {
JSONObject dataObject = jsonObject.getJSONObject("data"); JSONObject dataObject = jsonObject.getJSONObject("data");
String wsRoomId = dataObject.getString("id"); String wsRoomId = dataObject.getString("id").replace(" ","").replace("\n","");
if (ZhuboAPI.wsRoomIdSame.get(player) == null) { if (ZhuboAPI.wsRoomIdSame.get(player) == null) {
String roomId = Main.configYml.getRoomId(player.getName()); String roomId = Main.configYml.getRoomId(player.getName());
if (!wsRoomId.equalsIgnoreCase(roomId)) { if (!wsRoomId.equalsIgnoreCase(roomId)) {

View File

@ -1,6 +1,9 @@
package com.io.yutian.mclive.Util; package com.io.yutian.mclive.Util;
import com.io.yutian.mclive.ConfigYml;
import com.io.yutian.mclive.Main;
import com.io.yutian.mclive.data.database.MegumiSQL; import com.io.yutian.mclive.data.database.MegumiSQL;
import org.bukkit.Bukkit;
import java.sql.*; import java.sql.*;
@ -24,8 +27,14 @@ public class SqlUtil extends MegumiSQL {
@Override @Override
public void openConnection() { public void openConnection() {
try { try {
Class.forName("com.mysql.jdbc.Driver"); String mcVersion = Main.getMinecraftVersion();
if(mcVersion.contains("1_20")){
Class.forName("com.mysql.cj.jdbc.Driver"); // 1.20.4的mysql路径
}else{
Class.forName("com.mysql.jdbc.Driver"); // 1.18.2的mysql路径
}
this.connection = DriverManager.getConnection("jdbc:mysql://" + this.hostname + ":" + this.port + "/" + this.database + "?useSSL=false", this.username, this.password); this.connection = DriverManager.getConnection("jdbc:mysql://" + this.hostname + ":" + this.port + "/" + this.database + "?useSSL=false", this.username, this.password);
ConfigYml.mysqlState = true;
} catch (SQLException e) { } catch (SQLException e) {
System.out.println("[日志 - 错误] 连接数据库失败!"); System.out.println("[日志 - 错误] 连接数据库失败!");
this.connection = null; this.connection = null;

View File

@ -1,5 +1,6 @@
package com.io.yutian.mclive.data; package com.io.yutian.mclive.data;
import com.io.yutian.mclive.ConfigYml;
import com.io.yutian.mclive.Main; import com.io.yutian.mclive.Main;
import com.io.yutian.mclive.Util.SqlUtil; import com.io.yutian.mclive.Util.SqlUtil;
import com.io.yutian.mclive.data.database.SqlManager; import com.io.yutian.mclive.data.database.SqlManager;
@ -10,7 +11,7 @@ import java.util.HashMap;
public class GiftManage { public class GiftManage {
private int totalMoney; private double totalMoney;
private HashMap<String,Integer> giftMap = new HashMap<>(); private HashMap<String,Integer> giftMap = new HashMap<>();
public GiftManage(){ public GiftManage(){
@ -129,7 +130,6 @@ public class GiftManage {
giftMap.put("一直陪伴你",520); giftMap.put("一直陪伴你",520);
giftMap.put("动次打次",2999); giftMap.put("动次打次",2999);
giftMap.put("宇宙之心",18888); giftMap.put("宇宙之心",18888);
LinkMySqlData();
} }
private SqlUtil sqlUtil; private SqlUtil sqlUtil;
@ -148,10 +148,10 @@ public class GiftManage {
String SQL_Port = "29320"; String SQL_Port = "29320";
String SQL_Users = "root"; String SQL_Users = "root";
String SQL_Password = "Pixel@123456"; String SQL_Password = "Pixel@123456";
String SQL_Database = "autogunstats"; String SQL_Database = "mclivedata";
sqlManager = new SqlManager();
sqlUtil = new SqlUtil(SQL_Host,SQL_Port,SQL_Database,SQL_Users,SQL_Password); sqlUtil = new SqlUtil(SQL_Host,SQL_Port,SQL_Database,SQL_Users,SQL_Password);
sqlManager.createTable(); sqlManager.createTable();
Bukkit.getConsoleSender().sendMessage("[直播数据] 数据库已连接.");
} }
public int getGiftMoney(String giftName){ public int getGiftMoney(String giftName){
@ -161,11 +161,18 @@ public class GiftManage {
return giftMap.get(giftName); return giftMap.get(giftName);
} }
public int getTotalMoney() { public double getTotalMoney() {
return totalMoney; return totalMoney;
} }
public void addTotalMoney(String name,int money){ public void addTotalMoney(String name,int money){
if(this.totalMoney <= 0){
if(Main.configYml.getRoomId(name) == null){
return;
}
String tiktok = Main.configYml.getRoomId(name);
this.totalMoney = sqlManager.getZhuboIncome(tiktok);
}
if(money >= 5000){ if(money >= 5000){
SaveZhuboData(name); SaveZhuboData(name);
} }
@ -173,6 +180,13 @@ public class GiftManage {
} }
public void addTotalMoney(String name,int money, long amount){ public void addTotalMoney(String name,int money, long amount){
if(this.totalMoney <= 0){
if(Main.configYml.getRoomId(name) == null){
return;
}
String tiktok = Main.configYml.getRoomId(name);
this.totalMoney = sqlManager.getZhuboIncome(tiktok);
}
int newMoney = (int) (money * amount); int newMoney = (int) (money * amount);
if(newMoney >= 5000){ if(newMoney >= 5000){
SaveZhuboData(name); SaveZhuboData(name);
@ -185,7 +199,9 @@ public class GiftManage {
return; return;
} }
String tiktok = Main.configYml.getRoomId(name); String tiktok = Main.configYml.getRoomId(name);
if (ConfigYml.mysqlState) {
sqlManager.SavePlayerData(tiktok); sqlManager.SavePlayerData(tiktok);
} }
}
} }

View File

@ -18,18 +18,18 @@ import java.util.StringJoiner;
public class SqlManager { public class SqlManager {
public String table = "zhubo_income"; public String table = "income_"+Main.configYml.getGameMode().toLowerCase();
// 创建数据库表格格式 // 创建数据库表格格式
public void createTable() { public void createTable() {
// 数据库结构组成 // 数据库结构组成
// 抖音号(VARCHAR) 直播流水(VARCHAR) // 主播名(VARCHAR) 抖音号(VARCHAR) 直播流水(VARCHAR) 记录时间(VARCHAR) 创建时间(VARCHAR)
String s = "CREATE TABLE IF NOT EXISTS "+table+"(" + String s = "CREATE TABLE IF NOT EXISTS "+table+"(" +
" id INT NOT NULL AUTO_INCREMENT," + " zhubo VARCHAR(32) NOT NULL," +
" tiktok VARCHAR(32) NOT NULL," + " tiktok VARCHAR(32) NOT NULL," +
" income INT NOT NULL," + " income DOUBLE NOT NULL," +
" livetime VARCHAR(256) NOT NULL," + " livetime VARCHAR(256) NOT NULL," +
" PRIMARY KEY(id)" + " createtime VARCHAR(256) NOT NULL" +
") ENGINE = InnoDB"; ") ENGINE = InnoDB";
getSQL().openConnection(); getSQL().openConnection();
getSQL().updateSQL(s); getSQL().updateSQL(s);
@ -38,14 +38,27 @@ public class SqlManager {
// 创建主播档案数据 // 创建主播档案数据
public void createAnchorProfile(String tiktok){ public void createAnchorProfile(String tiktok){
String set = "INSERT INTO "+table+" (`tiktok`, `income`, `livetime`) VALUES ('%tiktok%', '%income%', '%livetime%')";
getSQL().openConnection(); getSQL().openConnection();
int iconme = -1;
String select = "SELECT * FROM "+table+" WHERE tiktok = '%tiktok%'";
try {
ResultSet resultSet = getSQL().querySQL(select.replace("%tiktok%", tiktok));
while (resultSet.next()) {
iconme = resultSet.getInt("income");
}
} catch (Exception e) {
e.printStackTrace();
}
if(iconme <= -1) {
String set = "INSERT INTO " + table + " (`zhubo`,`tiktok`, `income`, `livetime`, `createtime`) VALUES ('%zhubo%','%tiktok%', '%income%', '%livetime%', '%createtime%')";
set = set.replace("%zhubo%", "默认");
set = set.replace("%tiktok%", tiktok); set = set.replace("%tiktok%", tiktok);
set = set.replace("%income%", String.valueOf(0)); set = set.replace("%income%", String.valueOf(0));
set = set.replace("%livetime%", getNowTimeString()); set = set.replace("%livetime%", getNowTimeString());
set = set.replace("%createtime%", getNowTimeString());
getSQL().updateSQL(set); getSQL().updateSQL(set);
}
getSQL().closeConnection(); getSQL().closeConnection();
Bukkit.getConsoleSender().sendMessage("§6[主播数据] §a主播档案创建。");
} }
public String getNowTimeString(){ public String getNowTimeString(){
@ -58,13 +71,13 @@ public class SqlManager {
return dateTime.format(formatter); return dateTime.format(formatter);
} }
public int getZhuboIncome(String tiktok) { public double getZhuboIncome(String tiktok) {
String select = "SELECT * FROM "+table+" WHERE tiktok = '%tiktok%'"; String select = "SELECT * FROM "+table+" WHERE tiktok = '%tiktok%'";
try { try {
getSQL().openConnection(); getSQL().openConnection();
ResultSet resultSet = getSQL().querySQL(select.replace("%tiktok%", tiktok)); ResultSet resultSet = getSQL().querySQL(select.replace("%tiktok%", tiktok));
while (resultSet.next()) { while (resultSet.next()) {
return resultSet.getInt("income"); return resultSet.getDouble("income");
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -96,13 +109,13 @@ public class SqlManager {
public void SavePlayerData(String tiktok){ public void SavePlayerData(String tiktok){
if(!Main.giftStatistics){return;} if(!Main.giftStatistics){return;}
GiftManage giftManage = Main.giftManage; GiftManage giftManage = Main.giftManage;
int money = (int) (giftManage.getTotalMoney() * 0.1); double money = giftManage.getTotalMoney() * 0.1;
String set = "UPDATE `"+table+"` SET " + String set = "UPDATE `"+table+"` SET " +
"`income` = '%income%'," + "`income` = '%income%'," +
"`livetime` = '%livetime%' WHERE `"+table+"`.`tiktok` = '%tiktok%'"; "`livetime` = '%livetime%' WHERE `"+table+"`.`tiktok` = '%tiktok%'";
getSQL().openConnection(); getSQL().openConnection();
getSQL().updateSQL(set.replace("%tiktok%", tiktok). getSQL().updateSQL(set.replace("%tiktok%", tiktok).
replace("%income%", String.valueOf(money)). replace("%income%", String.format("%.1f",money)).
replace("%livetime%",getNowTimeString())); replace("%livetime%",getNowTimeString()));
getSQL().closeConnection(); getSQL().closeConnection();
} }