CheckinSystem的首次提交
This commit is contained in:
commit
a09fcd5f2a
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
|
||||
7
.idea/encodings.xml
generated
Normal file
7
.idea/encodings.xml
generated
Normal file
@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Encoding">
|
||||
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||
</component>
|
||||
</project>
|
||||
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>CheckinSystem</artifactId>
|
||||
<version>1.0</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>
|
||||
@ -0,0 +1,26 @@
|
||||
package com.mmlsystem.CheckinSystem;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class CheckinCommand implements CommandExecutor {
|
||||
private final CheckinManager manager;
|
||||
|
||||
public CheckinCommand(CheckinManager manager) {
|
||||
this.manager = manager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
|
||||
if (!(sender instanceof Player)) return false;
|
||||
|
||||
Player player = (Player) sender;
|
||||
|
||||
player.sendMessage("签到中,请稍候...");
|
||||
manager.checkinAsync(player, message -> player.sendMessage(message));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
105
src/main/java/com/mmlsystem/CheckinSystem/CheckinManager.java
Normal file
105
src/main/java/com/mmlsystem/CheckinSystem/CheckinManager.java
Normal file
@ -0,0 +1,105 @@
|
||||
package com.mmlsystem.CheckinSystem;
|
||||
|
||||
import net.milkbowl.vault.economy.Economy;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.sql.*;
|
||||
import java.time.*;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class CheckinManager {
|
||||
private final Foundation plugin;
|
||||
private final Economy econ;
|
||||
|
||||
public CheckinManager(Foundation plugin) {
|
||||
this.plugin = plugin;
|
||||
this.econ = Foundation.getEconomy();
|
||||
}
|
||||
|
||||
public void checkinAsync(Player player, Consumer<String> callback) {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
String message;
|
||||
try {
|
||||
message = execCheckin(player);
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().severe(e.getMessage());
|
||||
message = "§c未知错误。";
|
||||
}
|
||||
|
||||
String finalMessage = message;
|
||||
Bukkit.getScheduler().runTask(plugin, () -> callback.accept(finalMessage));
|
||||
});
|
||||
}
|
||||
|
||||
private String execCheckin(Player player) {
|
||||
if (!player.hasPermission("com.mmlsystem.checkin")) {
|
||||
return "§c拒绝访问。";
|
||||
}
|
||||
UUID uuid = player.getUniqueId();
|
||||
LocalDate today = LocalDate.now();
|
||||
|
||||
try {
|
||||
PreparedStatement ps = plugin.getConnection().prepareStatement("SELECT last_checkin, streak, total_checkin FROM checkins WHERE uuid=?");
|
||||
ps.setString(1, uuid.toString());
|
||||
ResultSet rs = ps.executeQuery();
|
||||
|
||||
if (rs.next()) {
|
||||
long lastCheckin = rs.getLong("last_checkin");
|
||||
int streak = rs.getInt("streak");
|
||||
int total = rs.getInt("total_checkin");
|
||||
|
||||
LocalDate lastDate = Instant.ofEpochMilli(lastCheckin)
|
||||
.atZone(ZoneId.systemDefault()).toLocalDate();
|
||||
if (today.isEqual(lastDate)) {
|
||||
rs.close();
|
||||
ps.close();
|
||||
return "§e你今天已经签到过了!";
|
||||
} else {
|
||||
if (today.minusDays(1).isEqual(lastDate)) {
|
||||
streak += 1;
|
||||
} else {
|
||||
streak = 1;
|
||||
}
|
||||
}
|
||||
total += 1;
|
||||
ps = plugin.getConnection().prepareStatement(
|
||||
"UPDATE checkins SET last_checkin=?, streak=?, total_checkin=? WHERE uuid=?");
|
||||
ps.setLong(1, today.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli());
|
||||
ps.setInt(2, streak);
|
||||
ps.setInt(3, total);
|
||||
ps.setString(4, uuid.toString());
|
||||
ps.executeUpdate();
|
||||
double reward = getReward(streak);
|
||||
econ.depositPlayer(player, reward);
|
||||
ps.close();
|
||||
rs.close();
|
||||
return "§a签到成功!已连续签到§b" + streak + "§a天,获得§c$" + reward + "§a奖励";
|
||||
} else {
|
||||
ps = plugin.getConnection().prepareStatement(
|
||||
"INSERT INTO checkins(last_checkin,streak,total_checkin,uuid) VALUES(?,?,?,?)");
|
||||
ps.setLong(1, today.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli());
|
||||
ps.setInt(2, 1);
|
||||
ps.setInt(3, 1);
|
||||
ps.setString(4, uuid.toString());
|
||||
ps.executeUpdate();
|
||||
econ.depositPlayer(player, 25.0);
|
||||
ps.close();
|
||||
rs.close();
|
||||
return "§a签到成功!已连续签到§b1§a天,获得§c$25§a奖励";
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
plugin.getLogger().severe(e.getMessage());
|
||||
return "§c未知错误。";
|
||||
}
|
||||
}
|
||||
|
||||
public double getReward(int streak) {
|
||||
if (streak >= 7) {
|
||||
return 200.0;
|
||||
} else {
|
||||
return 25.0 * streak;
|
||||
}
|
||||
}
|
||||
}
|
||||
98
src/main/java/com/mmlsystem/CheckinSystem/Foundation.java
Normal file
98
src/main/java/com/mmlsystem/CheckinSystem/Foundation.java
Normal file
@ -0,0 +1,98 @@
|
||||
package com.mmlsystem.CheckinSystem;
|
||||
|
||||
import net.milkbowl.vault.economy.Economy;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.permissions.PermissionDefault;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.permissions.Permission;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||
|
||||
import java.io.File;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.Objects;
|
||||
|
||||
public class Foundation extends JavaPlugin {
|
||||
public static final Permission CHECKIN_PERM = new Permission(
|
||||
"com.mmlsystem.checkin",
|
||||
"允许玩家执行签到操作。",
|
||||
PermissionDefault.TRUE
|
||||
);
|
||||
private static Economy econ = null;
|
||||
private static Connection connection;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
getLogger().info("张华考上了北京大学");
|
||||
getLogger().info("李萍进了中等技术学校");
|
||||
getLogger().info("你却在这里看服务器启动日志里面有没有什么奇怪的东西");
|
||||
if (!setupEconomy()) {
|
||||
getLogger().severe("什么杂鱼服务器连个经济插件都没有(大雾)");
|
||||
getLogger().severe("请先安装Vault 1.7+或VaultUnlocked插件以继续操作");
|
||||
getServer().getPluginManager().disablePlugin(this);
|
||||
return;
|
||||
}
|
||||
setupDatabase();
|
||||
PluginManager pm = getServer().getPluginManager();
|
||||
pm.addPermission(CHECKIN_PERM);
|
||||
Objects.requireNonNull(getCommand("checkin")).setExecutor(new CheckinCommand(new CheckinManager(this)));
|
||||
getLogger().info("插件启用成功。");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
try {
|
||||
connection.close();
|
||||
} catch (SQLException e) {
|
||||
getLogger().severe(e.getMessage());
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
getLogger().info("插件禁用成功。");
|
||||
}
|
||||
|
||||
public static Economy getEconomy() {
|
||||
return econ;
|
||||
}
|
||||
|
||||
public Connection getConnection() {
|
||||
return connection;
|
||||
}
|
||||
|
||||
private boolean setupEconomy() {
|
||||
if (getServer().getPluginManager().getPlugin("Vault") == null) {
|
||||
return false;
|
||||
}
|
||||
RegisteredServiceProvider<Economy> rsp = Bukkit.getServicesManager().getRegistration(Economy.class);
|
||||
if (rsp == null) {
|
||||
return false;
|
||||
}
|
||||
econ = rsp.getProvider();
|
||||
return true;
|
||||
}
|
||||
|
||||
private void setupDatabase() {
|
||||
try {
|
||||
File dbFile = new File(getDataFolder(), "data.db");
|
||||
if (!getDataFolder().exists()) {
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
getDataFolder().mkdirs();
|
||||
}
|
||||
|
||||
connection = DriverManager.getConnection("jdbc:sqlite:" + dbFile.getAbsolutePath());
|
||||
Statement stmt = connection.createStatement();
|
||||
stmt.executeUpdate("CREATE TABLE IF NOT EXISTS checkins (" +
|
||||
"uuid TEXT PRIMARY KEY," +
|
||||
"last_checkin INTEGER," +
|
||||
"streak INTEGER," +
|
||||
"total_checkin INTEGER)");
|
||||
stmt.close();
|
||||
|
||||
getLogger().info("SQLite 数据库初始化成功。");
|
||||
} catch (SQLException e) {
|
||||
getLogger().severe(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
11
src/main/resources/plugin.yml
Normal file
11
src/main/resources/plugin.yml
Normal file
@ -0,0 +1,11 @@
|
||||
name: HelloPlugin
|
||||
main: com.mmlsystem.CheckinSystem.Foundation
|
||||
version: 1.0
|
||||
api-version: 1.21
|
||||
author: 杏川铭心
|
||||
description: 提供服务器每日签到功能。
|
||||
depend: [Vault]
|
||||
commands:
|
||||
checkin:
|
||||
description: 在服务器上签到并获取奖励。
|
||||
usage: /checkin
|
||||
Loading…
x
Reference in New Issue
Block a user