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