Skip to content

Commit

Permalink
feat(time): 尝试支持按日/周/月/总时长判别奖励。
Browse files Browse the repository at this point in the history
  • Loading branch information
CarmJos committed Sep 2, 2023
1 parent e1f4172 commit dd80c5c
Show file tree
Hide file tree
Showing 24 changed files with 586 additions and 369 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

```text
# claim [奖励ID]
@ 玩家指令
@ 玩家指令 (TimeReward.claim)
- 为自己手动领取对应奖励。
- 若不填写奖励ID,则自动领取全部可领取的奖励。
Expand Down Expand Up @@ -72,8 +72,9 @@
变量如下:

```text
# %TimeReward_time%
- 得到玩家总共的在线时长(秒)。
# %TimeReward_time_<时间类型>%
- 得到玩家的在线时长(秒)。
- 时间类型可选 TOTAL, DAILY, WEEKLY, MONTHLY 。
# %TimeReward_reward_<奖励ID>%
- 得到某个奖励配置的名称。
Expand Down
40 changes: 24 additions & 16 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
<maven.compiler.target>${project.jdk.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<deps.easyplugin.version>1.5.5</deps.easyplugin.version>

<deps.easyplugin.version>1.5.7</deps.easyplugin.version>
<deps.easysql.version>0.4.7</deps.easysql.version>
<deps.mineconfig.version>2.8.0</deps.mineconfig.version>
</properties>
Expand Down Expand Up @@ -49,7 +49,7 @@
<name>Carm's Repo</name>
<url>https://repo.carm.cc/repository/maven-public/</url>
</repository>

<repository>
<id>github</id>
<name>GitHub Packages</name>
Expand All @@ -66,7 +66,7 @@
<url>https://mvn.lumine.io/repository/maven-public/</url>
</repository>
</repositories>

<distributionManagement>
<downloadUrl>https://github.com/CarmJos/TimeReward/releases</downloadUrl>
<repository>
Expand All @@ -84,15 +84,23 @@
<scope>compile</scope>
<optional>true</optional>
</dependency>

<dependency>
<groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-main</artifactId>
<version>${deps.easyplugin.version}</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>


<dependency>
<groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-user</artifactId>
<version>${deps.easyplugin.version}</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>

<dependency>
<groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-command</artifactId>
Expand All @@ -117,15 +125,15 @@
<scope>compile</scope>
<optional>true</optional>
</dependency>

<dependency>
<groupId>cc.carm.lib</groupId>
<artifactId>easysql-beecp</artifactId>
<version>${deps.easysql.version}</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>

<dependency>
<groupId>de.themoep</groupId>
<artifactId>minedown</artifactId>
Expand All @@ -141,7 +149,7 @@
<scope>compile</scope>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
Expand Down Expand Up @@ -174,7 +182,7 @@

<build>
<defaultGoal>clean package</defaultGoal>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand All @@ -199,7 +207,7 @@
</filesets>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand All @@ -211,13 +219,13 @@
<compilerArgument>-parameters</compilerArgument>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
Expand All @@ -231,7 +239,7 @@
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
Expand Down Expand Up @@ -277,7 +285,7 @@
</relocations>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
Expand All @@ -287,7 +295,7 @@
</configuration>
</plugin>
</plugins>

<resources>
<resource>
<directory>src/main/resources</directory>
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/cc/carm/plugin/timereward/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@
import cc.carm.plugin.timereward.command.MainCommand;
import cc.carm.plugin.timereward.conf.PluginConfig;
import cc.carm.plugin.timereward.conf.PluginMessages;
import cc.carm.plugin.timereward.conf.RewardsConfig;
import cc.carm.plugin.timereward.hooker.PAPIExpansion;
import cc.carm.plugin.timereward.listener.UserListener;
import cc.carm.plugin.timereward.manager.RewardManager;
import cc.carm.plugin.timereward.manager.UserManager;
import cc.carm.plugin.timereward.storage.database.MySQLStorage;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;

public class Main extends EasyPlugin {
private static Main instance;

protected ConfigurationProvider<?> configProvider;
protected ConfigurationProvider<?> messageProvider;
protected ConfigurationProvider<?> rewardProvider;

protected MySQLStorage storage;
protected UserManager userManager;
Expand All @@ -38,6 +41,9 @@ protected void load() {

this.messageProvider = MineConfiguration.from(this, "messages.yml");
this.messageProvider.initialize(PluginMessages.class);

this.rewardProvider = MineConfiguration.from(this, "rewards.yml");
this.rewardProvider.initialize(RewardsConfig.class);
}

@Override
Expand All @@ -53,10 +59,10 @@ protected boolean initialize() {
}

log("加载用户管理器...");
this.userManager = new UserManager();
if (Bukkit.getOnlinePlayers().size() > 0) {
this.userManager = new UserManager(this);
if (!Bukkit.getOnlinePlayers().isEmpty()) {
log("加载现有用户数据...");
this.userManager.loadAll();
this.userManager.loadOnline(Entity::getUniqueId);
}

log("加载奖励管理器...");
Expand Down Expand Up @@ -139,4 +145,7 @@ public ConfigurationProvider<?> getMessageProvider() {
return messageProvider;
}

public ConfigurationProvider<?> getRewardProvider() {
return rewardProvider;
}
}
5 changes: 4 additions & 1 deletion src/main/java/cc/carm/plugin/timereward/TimeRewardAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@

public class TimeRewardAPI {

private TimeRewardAPI() {
}

public static UserManager getUserManager() {
return Main.getInstance().userManager;
}

public static RewardManager getRewardManager() {
return Main.getInstance().rewardManager;
}

public static void executeCommands(Player player, List<String> commands) {
if (commands == null || commands.isEmpty()) return;
for (String command : commands) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package cc.carm.plugin.timereward.command.sub;

import cc.carm.lib.easyplugin.command.SubCommand;
import cc.carm.plugin.timereward.Main;
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.manager.RewardManager;
import cc.carm.plugin.timereward.data.RewardContents;
import cc.carm.plugin.timereward.manager.RewardManager;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
Expand Down Expand Up @@ -40,11 +39,8 @@ public Void execute(JavaPlugin plugin, CommandSender sender, String[] args) {
return null;
}

Main.getInstance().getScheduler().run(() -> unclaimedRewards.forEach(
// 在同步进程中为玩家发放奖励
unclaimedReward -> manager.claimReward(player, unclaimedReward, false)
));

// 为玩家发放奖励
manager.claimRewards(player, unclaimedRewards, false);
} else {

RewardContents reward = manager.getReward(rewardID);
Expand All @@ -57,8 +53,16 @@ public Void execute(JavaPlugin plugin, CommandSender sender, String[] args) {
PluginMessages.NOT_CLAIMABLE.send(sender, reward.getDisplayName());
return null;
}
Main.getInstance().getScheduler().run(() -> manager.claimReward(player, reward, false));

// 为玩家发放奖励
manager.claimReward(player, reward, false);
}
return null;
}

@Override
public boolean hasPermission(@NotNull CommandSender sender) {
return sender.hasPermission("TimeReward.claim");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public Void execute(JavaPlugin plugin, CommandSender sender, String[] args) {
for (RewardContents reward : awards) {
if (reward.getPermission() != null) {
PluginMessages.LIST.OBJECT_PERM.send(sender,
reward.getRewardID(), reward.getDisplayName(),
reward.getRewardID(), reward.getDisplayName(), reward.getType().name(),
reward.getTime(), reward.getPermission()
);
} else {
PluginMessages.LIST.OBJECT.send(sender,
reward.getRewardID(), reward.getDisplayName(), reward.getTime()
reward.getRewardID(), reward.getDisplayName(), reward.getType().name(), reward.getTime()
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public Void execute(JavaPlugin plugin, CommandSender sender, String[] args) thro
try {
Main.getInstance().getConfigProvider().reload();
Main.getInstance().getMessageProvider().reload();
Main.getInstance().getRewardProvider().reload();

PluginMessages.RELOAD.COMPLETE.send(sender, System.currentTimeMillis() - s1, TimeRewardAPI.getRewardManager().listRewards().size());
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
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.data.UserData;
import cc.carm.plugin.timereward.data.IntervalType;
import cc.carm.plugin.timereward.user.UserRewardData;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;

import java.time.format.DateTimeFormatter;
import java.util.List;

public class UserCommand extends SubCommand<MainCommand> {
Expand All @@ -30,11 +32,21 @@ public Void execute(JavaPlugin plugin, CommandSender sender, String[] args) thro
return null;
}

UserData user = TimeRewardAPI.getUserManager().getData(player);
UserRewardData user = TimeRewardAPI.getUserManager().get(player);
PluginMessages.USER_INFO.send(sender,
player.getName(), user.getAllSeconds(),
user.getClaimedRewards().size(), String.join("&8, &f", user.getClaimedRewards())
player.getName(),
user.getOnlineDuration(IntervalType.DAILY).getSeconds(),
user.getOnlineDuration(IntervalType.WEEKLY).getSeconds(),
user.getOnlineDuration(IntervalType.MONTHLY).getSeconds(),
user.getOnlineDuration(IntervalType.TOTAL).getSeconds(),
user.getClaimedRewards().size()
);

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
user.getClaimedRewards().forEach((id, time) -> {
PluginMessages.USER_RECEIVED.send(sender, id, time.format(formatter));
});

return null;
}

Expand Down
21 changes: 0 additions & 21 deletions src/main/java/cc/carm/plugin/timereward/conf/PluginConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,6 @@ public class PluginConfig extends ConfigurationRoot {
.serializeValue(DayOfWeek::getValue)
.defaults(DayOfWeek.MONDAY).build();

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

}


Loading

0 comments on commit dd80c5c

Please sign in to comment.