-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNextOnlineTimeAPI.java
More file actions
99 lines (86 loc) · 3 KB
/
NextOnlineTimeAPI.java
File metadata and controls
99 lines (86 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.nextplugins.onlinetime.api;
import com.google.common.collect.ImmutableSet;
import com.nextplugins.onlinetime.NextOnlineTime;
import com.nextplugins.onlinetime.api.player.TimedPlayer;
import com.nextplugins.onlinetime.api.reward.Reward;
import com.nextplugins.onlinetime.manager.RewardManager;
import com.nextplugins.onlinetime.manager.TimedPlayerManager;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import java.util.Collection;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* @author Yuhtin
* Github: https://github.com/Yuhtin
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class NextOnlineTimeAPI {
@Getter private static final NextOnlineTimeAPI instance = new NextOnlineTimeAPI();
private final TimedPlayerManager timedPlayerManager = NextOnlineTime.getInstance().getTimedPlayerManager();
private final RewardManager rewardManager = NextOnlineTime.getInstance().getRewardManager();
/**
* Get player time and collected rewards
*
* @param name of player
* @return {@link TimedPlayer} of player found
*/
public TimedPlayer getUser(String name) {
return this.timedPlayerManager.getPlayers().getOrDefault(name, null);
}
/**
* Get reward by name
*
* @param name of reward
* @return {@link Reward} found by name
*/
public Reward getReward(String name) {
return this.rewardManager.getByName(name);
}
/**
* Collect all rewards that have the minimum time in millis
*
* @param millis Time the reward should be at least
* @return {@link java.util.Set} of the rewards found
*/
public Set<Reward> getRewardsByMinTime(long millis) {
return allCachedRewards().stream()
.filter(time -> time.getTime() >= millis)
.collect(Collectors.toSet());
}
/**
* Get player by filter
* Can be used to search players with more x time
* <p>
* WARNING:
* Only from cache.
* If you want to search with all players, access {@link com.nextplugins.onlinetime.dao.TimedPlayerDAO#selectAll(String)} in {@link TimedPlayerManager}
*
* @param filter custom filter to search
* @return {@link Optional} with the player found
*/
public Optional<TimedPlayer> findPlayerByFilter(Predicate<TimedPlayer> filter) {
return allCachedPlayers().stream()
.filter(filter)
.findAny();
}
/**
* Get copy of player cache
*
* @return {@link Collection} with all players in cache
*/
public Collection<TimedPlayer> allCachedPlayers() {
return ImmutableSet.copyOf(this.timedPlayerManager.getPlayers().values());
}
/**
* Get copy of rewards cache
*
* @return {@link Collection} with all rewards in cache
*/
public Collection<Reward> allCachedRewards() {
return ImmutableSet.copyOf(this.rewardManager.getRewards().values());
}
}