Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
RecursiveG committed Aug 7, 2015
1 parent 2c6f0d3 commit 486b74a
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 28 deletions.
17 changes: 11 additions & 6 deletions src/main/java/cat/nyaa/playtimetracker/Locale.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,39 @@
public class Locale {
private static ConfigurationSection lang;
private static String prefix;

public static void init(ConfigurationSection langSection) {
lang = langSection;
prefix = lang.getString("prefix");
}
public static String get(String name, String ... args) {

public static String get(String name, String... args) {
return prefix + String.format(lang.getString(name), args);
}

public static String formatTime(long ms) {
String str = "";
if (ms == 0)
return "0";

if (ms > 0) {
str=String.format("%dms", ms%1000)+str;
str = String.format("%dms", ms % 1000) + str;
ms = Math.floorDiv(ms, 1000);
}
if (ms > 0) {
str=String.format("%ds ", ms%60)+str;
str = String.format("%ds ", ms % 60) + str;
ms = Math.floorDiv(ms, 60);
}
if (ms > 0) {
str=String.format("%dm ", ms%60)+str;
str = String.format("%dm ", ms % 60) + str;
ms = Math.floorDiv(ms, 60);
}
if (ms > 0) {
str=String.format("%dh ", ms%24)+str;
str = String.format("%dh ", ms % 24) + str;
ms = Math.floorDiv(ms, 24);
}
if (ms > 0) {
str=String.format("%dd ", ms)+str;
str = String.format("%dd ", ms) + str;
}

return str;
Expand Down
37 changes: 24 additions & 13 deletions src/main/java/cat/nyaa/playtimetracker/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ public boolean isAFK(UUID id) {
else return ess.getUser(id).isAfk();
}

public boolean inGroup(UUID id, Set<String> group) {
if (ess == null || group == null || id == null) return true;
return group.contains(ess.getUser(id).getGroup());
}

private void printStatistic(CommandSender s, OfflinePlayer p) {
s.sendMessage(Locale.get("statistic-for", p.getName()));
recordMgr.printStatistic(s, p);
Expand All @@ -92,9 +97,12 @@ private void notifyReward(Player player, Collection<Rule> satisfiedRuleList) {
}

private void applyReward(Rule rule, Player p) {
rewardMap.get(rule.reward).applyTo(p);
Reward reward = rewardMap.get(rule.reward);
reward.applyTo(p);
p.sendMessage(Locale.get("rule-applied", rule.name));
p.sendMessage(rewardMap.get(rule.reward).getDescription());
if (reward.getDescription() != null && reward.getDescription().length() > 0) {
p.sendMessage(rewardMap.get(rule.reward).getDescription());
}
}

@Override
Expand All @@ -106,7 +114,6 @@ public void run() { // Save interval
recordMgr.save();
}


private void notifyAcquire(Player p) {
Set<Rule> satisfiedRules = recordMgr.getSatisfiedRules(p.getUniqueId());
Set<Rule> unacquired = new HashSet<>();
Expand All @@ -129,13 +136,15 @@ public void onPlayerJoin(PlayerJoinEvent event) {
UUID id = event.getPlayer().getUniqueId();
recordMgr.sessionStart(id, rules.values());
recordMgr.updateSingle(event.getPlayer(), false);
new BukkitRunnable() {
@Override
public void run() {
printStatistic(event.getPlayer(), event.getPlayer());
notifyAcquire(event.getPlayer());
}
}.runTaskLater(this, 20L);
if (cfg.getBoolean("display-on-login")) {
new BukkitRunnable() {
@Override
public void run() {
printStatistic(event.getPlayer(), event.getPlayer());
notifyAcquire(event.getPlayer());
}
}.runTaskLater(this, 20L);
}
}

@EventHandler
Expand Down Expand Up @@ -186,14 +195,16 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
if (!(sender instanceof Player)) {
sender.sendMessage(Locale.get("only-player-can-do"));
}
Set<Rule> satisfiedRules = recordMgr.getSatisfiedRules(((Player)sender).getUniqueId());
Set<Rule> satisfiedRules = recordMgr.getSatisfiedRules(((Player) sender).getUniqueId());
if (satisfiedRules.size() == 0) {
sender.sendMessage(Locale.get("nothing-to-acquire"));
return true;
}
breakpoint: if (args.length<=1){ // acquire all
breakpoint:
if (args.length <= 1) { // acquire all
for (Rule r : satisfiedRules) {
applyReward(r, (Player) sender);
recordMgr.setRuleAcquired(((Player)sender).getUniqueId(), r);
recordMgr.setRuleAcquired(((Player) sender).getUniqueId(), r);
}
recordMgr.save();
} else {
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/cat/nyaa/playtimetracker/OnlineRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,27 @@ public UUID getUuid() {
}

public long getLastSeen() {
parse();
return lastSeen;
}

public long getDayTime() {
parse();
return dayTime;
}

public long getWeekTime() {
parse();
return weekTime;
}

public long getMonthTime() {
parse();
return monthTime;
}

public long getTotalTime() {
parse();
return totalTime;
}

Expand All @@ -61,6 +66,10 @@ public long getLtnsDay() {
return ltnsDay;
}

public void setLtnsDay(long ltnsDay) {
this.ltnsDay = ltnsDay;
}

public static OnlineRecord fromString(UUID id, String str) {
OnlineRecord r = new OnlineRecord();
r.uuid = id;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/cat/nyaa/playtimetracker/RecordMgr.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ public Set<Rule> getSatisfiedRules(UUID id) {
Set<Rule> ret = new HashSet<>();
if (onlineRecord == null) return ret;
for (Rule rule : plugin.getRules().values()) {
if (!plugin.inGroup(id, rule.group)) continue;
String ruleName = rule.name;

switch (rule.period) {
case LONGTIMENOSEE: {
if (!onlineRecord.isCompleted(Rule.PeriodType.LONGTIMENOSEE, ruleName)) {
Expand Down Expand Up @@ -181,6 +181,7 @@ public void sessionStart(UUID id, Collection<Rule> rules) {
}
}
}
r.setLtnsDay(nowTime);
}

public void sessionEnd(UUID id) {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/cat/nyaa/playtimetracker/Rule.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import org.bukkit.configuration.ConfigurationSection;

import java.util.HashSet;
import java.util.Set;

public class Rule {
public enum PeriodType {
DAY,
Expand All @@ -18,6 +21,7 @@ public enum PeriodType {
public boolean autoGive;
public long timeout;
public String reward;
public Set<String> group;

public Rule(String name, ConfigurationSection s) {
this.name = name;
Expand Down Expand Up @@ -50,6 +54,7 @@ public Rule(String name, ConfigurationSection s) {
require = s.getLong("require");
autoGive = s.getBoolean("auto-give");
timeout = s.contains("timeout") ? s.getLong("timeout") : -1;
group = s.contains("eligible-group") ? new HashSet<>(s.getStringList("eligible-group")) : null;
reward = s.getString("reward");
}
}
27 changes: 19 additions & 8 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ message:

have-reward-redeem: "You have the following unacquired rewards, use `/ptt acquire [name]` to acquire"
have-reward-redeem-format: "- %s"
rule-applied: "Congratulations! The following rule has applies to you: %s"
rule-applied: "Congratulations! The following rule has applied to you: %s"
nothing-to-acquire: "You have nothing to acquire"
cannot-acquire: "You can not acquire %s now"
no-such-rule: "Rule not found"
Expand All @@ -23,27 +23,38 @@ message:
statistic-no-record: "No record for that player."

ignore-afk: true
save-interval: 5 #seconds
save-interval: 60 #seconds
display-on-login: false

rewards:
some-reward:
description: "You were given one stone."
command: "/give {playerName} 1 1"
command: "give {playerName} 1 1"
another-reward:
description: "Placeholder reward 2"
command: "/give {playerName} 1 2"
description: "You were give two stones"
command: "give {playerName} 1 2"
third-reward:
description: ""
command: "broadcast {playerName} has been online for one hour!"


rules:
rule1:
rule1: # rule name cannot contains space ` ` and comma `,`
period: "day" # week|month|disposable|session
require: 60 # total online *minutes* to meet the reward requirement
auto-give: false # automatically give the reward
timeout: 60 # the time player able to acquire the reward, in minutes.
# Ignored when auto-give is true. Omit this means "unlimited".
# Always timeout at the end of each period
reward: some-reward
rule2:
period: "disposable"
require: 60
auto-give: true
reward: third-reward
rule3:
period: "longtimenosee" # rewards are given when players back after a long period time of away, in hours
period: "longtimenosee" # rewards are given when players back after a long period time of away
require: 60 # how long the player left, *IN DAYS*
auto-give: true # automatically give the reward
auto-give: true
eligible-group: [group1,group2] # only players in groups listed here can acquire the rule. Any one can acquire if omitted.
reward: another-reward

0 comments on commit 486b74a

Please sign in to comment.