完成PlayerExpSystem
This commit is contained in:
commit
d7898e333f
39
.gitignore
vendored
Normal file
39
.gitignore
vendored
Normal file
@ -0,0 +1,39 @@
|
||||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
!**/src/main/**/target/
|
||||
!**/src/test/**/target/
|
||||
.kotlin
|
||||
|
||||
### 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
|
||||
3
.idea/.gitignore
generated
vendored
Normal file
3
.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
# 默认忽略的文件
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
14
.idea/misc.xml
generated
Normal file
14
.idea/misc.xml
generated
Normal file
@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="MavenProjectsManager">
|
||||
<option name="originalFiles">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/pom.xml" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_24" default="true" project-jdk-name="24" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
42
pom.xml
Normal file
42
pom.xml
Normal file
@ -0,0 +1,42 @@
|
||||
<project>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.mmlsystem</groupId>
|
||||
<artifactId>PlayerExpSystem</artifactId>
|
||||
<version>1.0.1</version>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>central</id>
|
||||
<url>https://repo.maven.apache.org/maven2/</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>spigot-repo</id>
|
||||
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>jitpack-io</id>
|
||||
<url>https://jitpack.io/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.spigotmc</groupId>
|
||||
<artifactId>spigot-api</artifactId>
|
||||
<version>1.21.8-R0.1-SNAPSHOT</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.MilkBowl</groupId>
|
||||
<artifactId>VaultAPI</artifactId>
|
||||
<version>1.7</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains</groupId>
|
||||
<artifactId>annotations</artifactId>
|
||||
<version>24.1.0</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
68
src/main/java/com/mmlsystem/PlayerExpSystem/Foundation.java
Normal file
68
src/main/java/com/mmlsystem/PlayerExpSystem/Foundation.java
Normal file
@ -0,0 +1,68 @@
|
||||
package com.mmlsystem.PlayerExpSystem;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.io.File;
|
||||
import java.sql.*;
|
||||
|
||||
public class Foundation extends JavaPlugin {
|
||||
|
||||
private Connection connection;
|
||||
private SaveManager manager;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
getLogger().info("世界上有10种人:");
|
||||
getLogger().info("一种是了解三进制的");
|
||||
getLogger().info("一种是不了解三进制的");
|
||||
getLogger().info("还有一种是觉得这是个二进制笑话的");
|
||||
setupDatabase();
|
||||
PlayerJoinListener listener = new PlayerJoinListener(this);
|
||||
manager = new SaveManager(this, listener);
|
||||
Bukkit.getServer().getPluginManager().registerEvents(listener, this);
|
||||
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, () -> {
|
||||
manager.saveAllPlayers();
|
||||
}, 1200L, 1200L);
|
||||
getLogger().info("插件启用成功。");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
closeDatabase();
|
||||
}
|
||||
|
||||
public Connection getConnection() {
|
||||
return connection;
|
||||
}
|
||||
|
||||
public void setupDatabase() {
|
||||
try {
|
||||
File dbFile = new File(getDataFolder(), "players.db");
|
||||
if (!dbFile.getParentFile().exists()) dbFile.getParentFile().mkdirs();
|
||||
|
||||
connection = DriverManager.getConnection("jdbc:sqlite:" + dbFile.getAbsolutePath());
|
||||
Statement stmt = connection.createStatement();
|
||||
stmt.executeUpdate("CREATE TABLE IF NOT EXISTS playtime (" +
|
||||
"uuid TEXT PRIMARY KEY, " +
|
||||
"name TEXT, " +
|
||||
"total BIGINT DEFAULT 0" +
|
||||
")");
|
||||
getLogger().info("初始化SQLite数据库成功。");
|
||||
} catch (SQLException e) {
|
||||
getLogger().severe(e.getMessage());
|
||||
getLogger().severe("无法连接SQLite数据库!");
|
||||
getServer().getPluginManager().disablePlugin(this);
|
||||
}
|
||||
}
|
||||
|
||||
private void closeDatabase() {
|
||||
try {
|
||||
if (connection != null && !connection.isClosed())
|
||||
connection.close();
|
||||
} catch (SQLException e) {
|
||||
getLogger().severe(e.getMessage());
|
||||
}
|
||||
getLogger().info("插件禁用成功。");
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,62 @@
|
||||
package com.mmlsystem.PlayerExpSystem;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
public class PlayerJoinListener implements Listener {
|
||||
|
||||
private final Connection connection;
|
||||
public final Map<UUID, Long> joinTimes = new HashMap<>();
|
||||
private final Foundation plugin;
|
||||
|
||||
public PlayerJoinListener(Foundation plugin) {
|
||||
this.connection = plugin.getConnection();
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onJoin(PlayerJoinEvent e) {
|
||||
joinTimes.put(e.getPlayer().getUniqueId(), System.currentTimeMillis());
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onQuit(PlayerQuitEvent e) {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
savePlayer(e.getPlayer());
|
||||
});
|
||||
joinTimes.remove(e.getPlayer().getUniqueId());
|
||||
}
|
||||
|
||||
public boolean savePlayer(Player player) {
|
||||
long joinTime = joinTimes.getOrDefault(player.getUniqueId(), System.currentTimeMillis());
|
||||
long session = System.currentTimeMillis() - joinTime;
|
||||
|
||||
try (PreparedStatement ps = connection.prepareStatement(
|
||||
"INSERT INTO playtime (uuid, name, total) VALUES (?, ?, ?) " +
|
||||
"ON CONFLICT(uuid) DO UPDATE SET " +
|
||||
"name = excluded.name, total = total + excluded.total")) {
|
||||
ps.setString(1, player.getUniqueId().toString());
|
||||
ps.setString(2, player.getName());
|
||||
ps.setLong(3, session);
|
||||
ps.executeUpdate();
|
||||
ps.close();
|
||||
joinTimes.put(player.getUniqueId(), System.currentTimeMillis());
|
||||
return true;
|
||||
} catch (SQLException e) {
|
||||
plugin.getLogger().severe(e.getMessage());
|
||||
plugin.getLogger().severe("试图保存玩家" + player.getName() + "的在线时长时发生错误。");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
30
src/main/java/com/mmlsystem/PlayerExpSystem/SaveManager.java
Normal file
30
src/main/java/com/mmlsystem/PlayerExpSystem/SaveManager.java
Normal file
@ -0,0 +1,30 @@
|
||||
package com.mmlsystem.PlayerExpSystem;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class SaveManager {
|
||||
private final Foundation foundation;
|
||||
private final PlayerJoinListener listener;
|
||||
|
||||
public SaveManager(Foundation foundation, PlayerJoinListener listener) {
|
||||
this.foundation = foundation;
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
public void saveAllPlayers() {
|
||||
boolean isFail = false;
|
||||
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||
if (!listener.savePlayer(player)) {
|
||||
isFail = true;
|
||||
}
|
||||
listener.joinTimes.put(player.getUniqueId(), System.currentTimeMillis());
|
||||
}
|
||||
if (isFail) {
|
||||
foundation.getLogger().warning("已完成自动保存例程,但发生了错误。");
|
||||
}
|
||||
else {
|
||||
foundation.getLogger().info("已完成自动保存例程。");
|
||||
}
|
||||
}
|
||||
}
|
||||
6
src/main/resources/plugin.yml
Normal file
6
src/main/resources/plugin.yml
Normal file
@ -0,0 +1,6 @@
|
||||
name: PlayerExpSystem
|
||||
main: com.mmlsystem.PlayerExpSystem.Foundation
|
||||
version: 1.0.1
|
||||
api-version: 1.21
|
||||
author: 杏川铭心
|
||||
description: 提供服务器计分板UI功能。
|
||||
Loading…
x
Reference in New Issue
Block a user