Mess with the plugin - Checkin streak
This commit is contained in:
parent
8e9e5d6883
commit
167e54be4b
@ -10,13 +10,14 @@ import org.jetbrains.annotations.NotNull;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.util.HashMap;
|
||||
import java.time.ZoneId;
|
||||
import java.util.UUID;
|
||||
|
||||
public class CheckinCommand implements CommandExecutor {
|
||||
private final HashMap<UUID, LocalDate> lastCheckin = new HashMap<>();
|
||||
private final HelloPlugin plugin;
|
||||
private final Economy econ = HelloPlugin.getEconomy();
|
||||
|
||||
public CheckinCommand(HelloPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
@ -36,44 +37,56 @@ public class CheckinCommand implements CommandExecutor {
|
||||
|
||||
try {
|
||||
// 查询玩家上次签到时间
|
||||
PreparedStatement ps = plugin.getConnection().prepareStatement("SELECT last_checkin FROM checkin WHERE uuid = ?");
|
||||
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()) {
|
||||
String lastDate = rs.getString("last_checkin");
|
||||
if (today.toString().equals(lastDate)) {
|
||||
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)) {
|
||||
player.sendMessage("§e你今天已经签到过了!");
|
||||
rs.close();
|
||||
ps.close();
|
||||
return true;
|
||||
} 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);
|
||||
player.sendMessage("§a签到成功!已连续签到§b" + streak + "§a天,获得§c$" + reward + "§c奖励");
|
||||
}
|
||||
|
||||
rs.close();
|
||||
ps.close();
|
||||
|
||||
// 更新或插入签到记录
|
||||
PreparedStatement update = plugin.getConnection().prepareStatement(
|
||||
"INSERT OR REPLACE INTO checkin (uuid, last_checkin) VALUES (?, ?)");
|
||||
update.setString(1, uuid.toString());
|
||||
update.setString(2, today.toString());
|
||||
update.executeUpdate();
|
||||
update.close();
|
||||
|
||||
// 发放经济奖励
|
||||
Economy econ = HelloPlugin.getEconomy();
|
||||
double reward = 100.0;
|
||||
econ.depositPlayer(player, reward);
|
||||
|
||||
player.sendMessage("§a签到成功!你获得了 §6" + reward + " §a金币。");
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
player.sendMessage("§c签到时发生错误,请联系管理员。");
|
||||
plugin.getLogger().severe(e.getMessage());
|
||||
player.sendMessage("§c未知错误。");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
sender.sendMessage("§6没活可以咬打火机");
|
||||
sender.sendMessage("§6没活可以咬打火机!");
|
||||
return false;
|
||||
}
|
||||
|
||||
public double getReward(int streak) {
|
||||
if (streak >= 7) {
|
||||
return 200.0;
|
||||
} else {
|
||||
return 25.0 * streak;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -27,12 +27,12 @@ public class HelloPlugin extends JavaPlugin {
|
||||
@Override
|
||||
public void onEnable() {
|
||||
if (!setupEconomy()) {
|
||||
getLogger().severe("No compatible economy provider found!");
|
||||
getLogger().severe("未找到任何支持的经济系统!");
|
||||
getServer().getPluginManager().disablePlugin(this);
|
||||
return;
|
||||
}
|
||||
setupDatabase();
|
||||
getLogger().info("Plugin activated successfully");
|
||||
getLogger().info("插件启用成功。");
|
||||
PluginManager pm = getServer().getPluginManager();
|
||||
pm.addPermission(CHECKIN_PERM);
|
||||
pm.registerEvents(new WelcomeMessageListener(), this);
|
||||
@ -41,7 +41,7 @@ public class HelloPlugin extends JavaPlugin {
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
getLogger().info("Plugin disabled successfully");
|
||||
getLogger().info("插件禁用成功。");
|
||||
}
|
||||
|
||||
public static Economy getEconomy() {
|
||||
@ -68,20 +68,25 @@ public class HelloPlugin extends JavaPlugin {
|
||||
try {
|
||||
File dbFile = new File(getDataFolder(), "data.db");
|
||||
if (!getDataFolder().exists()) {
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
getDataFolder().mkdirs();
|
||||
}
|
||||
if (dbFile.exists()) {
|
||||
return;
|
||||
}
|
||||
|
||||
connection = DriverManager.getConnection("jdbc:sqlite:" + dbFile.getAbsolutePath());
|
||||
Statement stmt = connection.createStatement();
|
||||
stmt.executeUpdate("CREATE TABLE IF NOT EXISTS checkin (" +
|
||||
stmt.executeUpdate("CREATE TABLE IF NOT EXISTS checkins (" +
|
||||
"uuid TEXT PRIMARY KEY," +
|
||||
"last_checkin TEXT" +
|
||||
")");
|
||||
"last_checkin INTEGER," +
|
||||
"streak INTEGER," +
|
||||
"total_checkin INTEGER)");
|
||||
stmt.close();
|
||||
|
||||
getLogger().info("SQLite 数据库初始化成功。");
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
getLogger().severe(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user