Skip to content

Commit

Permalink
feat(time): 尝试支持按日/周/月/总时长判别奖励。
Browse files Browse the repository at this point in the history
  • Loading branch information
CarmJos committed Aug 29, 2023
1 parent 85b7fba commit e1f4172
Show file tree
Hide file tree
Showing 17 changed files with 251 additions and 69 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@

若您觉得本插件做的不错,您可以捐赠支持我,感谢您成为开源项目的支持者!

万分感谢 [egg_拿]() 赞助本项目的开发与维护!

Many thanks to Jetbrains for kindly providing a license for me to work on this and other open-source projects.
[![](https://resources.jetbrains.com/storage/products/company/brand/logos/jb_beam.svg)](https://www.jetbrains.com/?from=https://github.com/CarmJos/UserPrefix)

Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

<deps.easyplugin.version>1.5.5</deps.easyplugin.version>
<deps.easysql.version>0.4.7</deps.easysql.version>
<deps.mineconfig.version>2.5.1</deps.mineconfig.version>
<deps.mineconfig.version>2.8.0</deps.mineconfig.version>
</properties>

<groupId>cc.carm.plugin</groupId>
<artifactId>timereward</artifactId>
<version>2.2.1</version>
<version>3.0.0</version>

<name>TimeReward</name>
<description>在线时长自动领奖插件,通过指令发放奖励,基于EasyPlugin实现。</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import cc.carm.plugin.timereward.command.MainCommand;
import cc.carm.plugin.timereward.conf.PluginMessages;
import cc.carm.plugin.timereward.manager.RewardManager;
import cc.carm.plugin.timereward.storage.RewardContents;
import cc.carm.plugin.timereward.data.RewardContents;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import cc.carm.plugin.timereward.TimeRewardAPI;
import cc.carm.plugin.timereward.command.MainCommand;
import cc.carm.plugin.timereward.conf.PluginMessages;
import cc.carm.plugin.timereward.storage.RewardContents;
import cc.carm.plugin.timereward.data.RewardContents;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import cc.carm.plugin.timereward.TimeRewardAPI;
import cc.carm.plugin.timereward.command.MainCommand;
import cc.carm.plugin.timereward.conf.PluginMessages;
import cc.carm.plugin.timereward.storage.RewardContents;
import cc.carm.plugin.timereward.data.RewardContents;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import cc.carm.plugin.timereward.TimeRewardAPI;
import cc.carm.plugin.timereward.command.MainCommand;
import cc.carm.plugin.timereward.conf.PluginMessages;
import cc.carm.plugin.timereward.storage.UserData;
import cc.carm.plugin.timereward.data.UserData;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
Expand Down
22 changes: 15 additions & 7 deletions src/main/java/cc/carm/plugin/timereward/conf/PluginConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import cc.carm.lib.configuration.core.annotation.HeaderComment;
import cc.carm.lib.configuration.core.value.ConfigValue;
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
import cc.carm.plugin.timereward.storage.RewardContents;

import java.time.DayOfWeek;

public class PluginConfig extends ConfigurationRoot {

Expand All @@ -23,7 +24,14 @@ public class PluginConfig extends ConfigurationRoot {
"检查更新为异步操作,绝不会影响性能与使用体验。"
})
public static final ConfigValue<Boolean> CHECK_UPDATE = ConfiguredValue.of(Boolean.class, true);


@HeaderComment("周起始日,用于判断周度奖励的结算日期。")
public static final ConfigValue<DayOfWeek> WEEK_FIRST_DAY = ConfiguredValue.builderOf(DayOfWeek.class)
.from(Integer.class).serializeSource(i -> i).parseSource(i -> (Integer) i)
.parseValue((v, d) -> DayOfWeek.of(v))
.serializeValue(DayOfWeek::getValue)
.defaults(DayOfWeek.MONDAY).build();

@HeaderComment({"奖励相关设定,包含以下设定:",
" [id] 配置键名即奖励ID,支持英文、数字与下划线。",
" | 确定后请不要更改,因为该键值用于存储玩家是否领取的数据",
Expand All @@ -38,11 +46,11 @@ public class PluginConfig extends ConfigurationRoot {
" [auto] 该奖励是否自动领取,可以不设置,默认为true。",
" | 若关闭自动领取,则需要玩家手动输入/tr claim 领取奖励。",
})
public static final ConfigValue<RewardContents.Group> REWARDS = ConfigValue.builder()
.asValue(RewardContents.Group.class).fromSection()
.parseValue((v, d) -> RewardContents.Group.parse(v))
.serializeValue(RewardContents.Group::serialize)
.defaults(RewardContents.Group.defaults())
public static final ConfigValue<RewardsConfig.RewardGroup> REWARDS = ConfigValue.builder()
.asValue(RewardsConfig.RewardGroup.class).fromSection()
.parseValue((v, d) -> RewardsConfig.RewardGroup.parse(v))
.serializeValue(RewardsConfig.RewardGroup::serialize)
.defaults(RewardsConfig.RewardGroup.defaults())
.build();

}
Expand Down
90 changes: 90 additions & 0 deletions src/main/java/cc/carm/plugin/timereward/conf/RewardsConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package cc.carm.plugin.timereward.conf;

import cc.carm.lib.configuration.core.ConfigurationRoot;
import cc.carm.lib.configuration.core.annotation.HeaderComment;
import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
import cc.carm.lib.configuration.core.value.ConfigValue;
import cc.carm.plugin.timereward.data.RewardContents;
import org.jetbrains.annotations.NotNull;

import java.util.LinkedHashMap;
import java.util.Map;

@HeaderComment({"奖励相关设定,包含以下设定:",
" [id] 配置键名即奖励ID,支持英文、数字与下划线。",
" | 确定后请不要更改,因为该键值用于存储玩家是否领取的数据",
" | 如果更改,原先领取过该奖励的玩家将会自动再领取一次!",
" [name] 奖励的显示名称,可以是任意字符串",
" | 可以在 commands 中使用 %(name) 来获取该奖励的名称",
" | 也可以使用变量 %TimeReward_reward_<奖励ID>% 来获取对应奖励的名称",
" [permission] 领取奖励时后台执行的指令",
" | 支持PlaceholderAPI变量,指令中可以使用 %(name) 来获取该奖励的名称。",
" [commands] 该奖励领取权限,可以不设置。",
" | 若为空则所有人都可以领取;若不为空,则需要拥有该权限的玩家才能领取。",
" [auto] 该奖励是否自动领取,可以不设置,默认为true。",
" | 若关闭自动领取,则需要玩家手动输入/tr claim 领取奖励。",
})
public class RewardsConfig extends ConfigurationRoot {

@HeaderComment("每日在线时长的奖励")
public static final ConfigValue<RewardGroup> DAILY = create();

@HeaderComment("每周在线时长的奖励")
public static final ConfigValue<RewardGroup> WEEKLY = create();

@HeaderComment("每月在线时长的奖励")
public static final ConfigValue<RewardGroup> MONTHLY = create();

@HeaderComment("总在线时长的奖励")
public static final ConfigValue<RewardGroup> TOTAL = create();

private static ConfigValue<RewardGroup> create() {
return ConfigValue.builder()
.asValue(RewardGroup.class).fromSection()
.parseValue((v, d) -> RewardGroup.parse(v))
.serializeValue(RewardGroup::serialize)
.defaults(RewardGroup.defaults())
.build();
}

public static final class RewardGroup {
final @NotNull Map<String, RewardContents> contents;

public RewardGroup(@NotNull Map<String, RewardContents> contents) {
this.contents = contents;
}

public @NotNull Map<String, RewardContents> getContents() {
return contents;
}

public Map<String, Object> serialize() {
Map<String, Object> map = new LinkedHashMap<>();
for (Map.Entry<String, RewardContents> entry : contents.entrySet()) {
map.put(entry.getKey(), entry.getValue().serialize());
}
return map;
}

public static RewardGroup parse(@NotNull ConfigurationWrapper<?> section) {
Map<String, RewardContents> rewards = new LinkedHashMap<>();
for (String rewardID : section.getKeys(false)) {
ConfigurationWrapper<?> rewardSection = section.getConfigurationSection(rewardID);
if (rewardSection == null) continue;

RewardContents c = RewardContents.parse(rewardID, rewardSection);
if (c == null) continue;
rewards.put(rewardID, c);
}
return new RewardGroup(rewards);
}

public static RewardGroup defaults() {
Map<String, RewardContents> rewards = new LinkedHashMap<>();
rewards.put("example", RewardContents.defaults("example"));
return new RewardGroup(rewards);
}

}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cc.carm.plugin.timereward.storage;
package cc.carm.plugin.timereward.data;

import cc.carm.lib.configuration.core.source.ConfigurationWrapper;
import cc.carm.lib.easyplugin.utils.ColorParser;
Expand Down Expand Up @@ -116,44 +116,6 @@ public int hashCode() {
return Objects.hash(id);
}

public static final class Group {
final @NotNull Map<String, RewardContents> contents;

public Group(@NotNull Map<String, RewardContents> contents) {
this.contents = contents;
}

public @NotNull Map<String, RewardContents> getContents() {
return contents;
}

public Map<String, Object> serialize() {
Map<String, Object> map = new LinkedHashMap<>();
for (Map.Entry<String, RewardContents> entry : contents.entrySet()) {
map.put(entry.getKey(), entry.getValue().serialize());
}
return map;
}

public static Group parse(@NotNull ConfigurationWrapper<?> section) {
Map<String, RewardContents> rewards = new LinkedHashMap<>();
for (String rewardID : section.getKeys(false)) {
ConfigurationWrapper<?> rewardSection = section.getConfigurationSection(rewardID);
if (rewardSection == null) continue;

RewardContents c = RewardContents.parse(rewardID, rewardSection);
if (c == null) continue;
rewards.put(rewardID, c);
}
return new Group(rewards);
}

public static Group defaults() {
Map<String, RewardContents> rewards = new LinkedHashMap<>();
rewards.put("example", RewardContents.defaults("example"));
return new Group(rewards);
}

}

}
63 changes: 63 additions & 0 deletions src/main/java/cc/carm/plugin/timereward/data/TimeRecord.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package cc.carm.plugin.timereward.data;

import cc.carm.plugin.timereward.conf.PluginConfig;
import org.jetbrains.annotations.NotNull;

import java.time.LocalDate;
import java.time.temporal.TemporalField;
import java.time.temporal.WeekFields;

public class TimeRecord {

protected final @NotNull LocalDate date;

protected final int daily;
protected final int weekly;
protected final int monthly;

protected final int total;

public TimeRecord(@NotNull LocalDate date, int daily, int weekly, int monthly, int total) {
this.date = date;
this.daily = daily;
this.weekly = weekly;
this.monthly = monthly;
this.total = total;
}

public @NotNull LocalDate getDate() {
return date;
}

public int getDailyTime() {
return isDayUpdated() ? 0 : daily;
}

public int getWeeklyTime() {
return isWeekUpdated() ? 0 : weekly;
}

public int getMonthlyTime() {
return isMonthUpdated() ? 0 : monthly;
}

public boolean isDayUpdated() {
return !LocalDate.now().equals(getDate());
}

public boolean isWeekUpdated() {
if (!isDayUpdated()) return false; // Same day always same week

TemporalField woy = WeekFields.of(PluginConfig.WEEK_FIRST_DAY.getNotNull(), 4).weekOfWeekBasedYear();
return getDate().get(woy) != LocalDate.now().get(woy);
}

public boolean isMonthUpdated() {
if (!isDayUpdated()) return false; // Same day always same month

// Predicate current month is after the month of the date
return LocalDate.now().getMonth().compareTo(getDate().getMonth()) > 0;
}


}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cc.carm.plugin.timereward.storage;
package cc.carm.plugin.timereward.data;

import org.jetbrains.annotations.NotNull;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import cc.carm.lib.easyplugin.papi.EasyPlaceholder;
import cc.carm.lib.easyplugin.papi.handler.PlaceholderHandler;
import cc.carm.plugin.timereward.TimeRewardAPI;
import cc.carm.plugin.timereward.storage.RewardContents;
import cc.carm.plugin.timereward.storage.UserData;
import cc.carm.plugin.timereward.data.RewardContents;
import cc.carm.plugin.timereward.data.UserData;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import cc.carm.plugin.timereward.Main;
import cc.carm.plugin.timereward.TimeRewardAPI;
import cc.carm.plugin.timereward.conf.PluginConfig;
import cc.carm.plugin.timereward.storage.RewardContents;
import cc.carm.plugin.timereward.storage.UserData;
import cc.carm.plugin.timereward.data.RewardContents;
import cc.carm.plugin.timereward.data.UserData;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import cc.carm.plugin.timereward.Main;
import cc.carm.plugin.timereward.util.DataTaskRunner;
import cc.carm.plugin.timereward.storage.UserData;
import cc.carm.plugin.timereward.data.UserData;
import cc.carm.plugin.timereward.storage.database.MySQLStorage;
import com.google.common.collect.ImmutableMap;
import org.bukkit.Bukkit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,21 @@ public enum DatabaseTables implements SQLTable {
/**
* 用于记录用户在线时间的表
*/
USER_TIMES(DatabaseConfig.TABLES.USER_TIMES, (table) -> {
USER_TIMES(DatabaseConfig.TABLES.USER_TIMES, table -> {
table.addColumn("uuid", "CHAR(36) NOT NULL PRIMARY KEY"); // 用户的UUID
table.addColumn("date", "DATE NOT NULL"); // 日期

table.addColumn("time", "INT UNSIGNED NOT NULL DEFAULT 0"); // 用户在线时间(秒)
table.addColumn("day_time", "MEDIUMINT UNSIGNED NOT NULL DEFAULT 0"); // 用户日在线时间(秒)
table.addColumn("week_time", "MEDIUMINT UNSIGNED NOT NULL DEFAULT 0"); // 用户周在线时间(秒)
table.addColumn("month_time", "INT UNSIGNED NOT NULL DEFAULT 0"); // 用户月在线时间(秒)
table.addColumn("total_time", "INT UNSIGNED NOT NULL DEFAULT 0"); // 用户总在线时间(秒)

table.addColumn("update",
"DATETIME NOT NULL " +
"DEFAULT CURRENT_TIMESTAMP " +
"ON UPDATE CURRENT_TIMESTAMP"
"DEFAULT CURRENT_TIMESTAMP " +
"ON UPDATE CURRENT_TIMESTAMP"
);

}),

/**
Expand All @@ -35,12 +40,7 @@ public enum DatabaseTables implements SQLTable {
table.addColumn("uuid", "CHAR(36) NOT NULL PRIMARY KEY"); // 用户的UUID 主键

table.addColumn("value", "MEDIUMTEXT"); // 已领取的奖励ID

table.addColumn("update",
"DATETIME NOT NULL " +
"DEFAULT CURRENT_TIMESTAMP " +
"ON UPDATE CURRENT_TIMESTAMP"
);
table.addColumn("time", "DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP"); // 领取时间
});

private final Consumer<TableCreateBuilder> builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import cc.carm.lib.easysql.EasySQL;
import cc.carm.lib.easysql.api.SQLManager;
import cc.carm.plugin.timereward.Main;
import cc.carm.plugin.timereward.storage.UserData;
import cc.carm.plugin.timereward.data.UserData;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
Expand Down
Loading

0 comments on commit e1f4172

Please sign in to comment.