Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ When the shop and/or vote plugins are installed on your Azuriom website, the int
* `%azlink_vote_sites_[id]_url%`: URL of vote site with given ID
* `%azlink_vote_top_[position]_name%`: name of player at given position in vote ranking
* `%azlink_vote_top_[position]_votes%`: vote count of player at given position in ranking
* `%azlink_vote_goal_progress%`: number of votes currently counted toward the monthly vote goal
* `%azlink_vote_goal_target%`: total number of votes required to reach the monthly vote goal

### Shop Placeholders
* `%azlink_shop_goal_progress%`: current progress of the shop goal this month
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@

import java.time.Duration;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

public class VotePlaceholderProvider implements PlaceholderProvider, Runnable, Listener {

private final Map<Integer, VoteSite> voteSites = new HashMap<>();
private final Map<String, VoteUser> users = new HashMap<>();
private final Map<Integer, VoteSite> voteSites = new ConcurrentHashMap<>();
private final Map<String, VoteUser> users = new ConcurrentHashMap<>();
private final List<TopVoteUser> topVotes = new ArrayList<>();
private volatile VoteGoal goal;

private volatile boolean pendingRefresh = true;
private volatile Instant lastUpdate = Instant.MIN;
Expand Down Expand Up @@ -64,7 +65,9 @@ public List<String> availablePlaceholders() {
"%azlink_vote_sites_[id]_name%",
"%azlink_vote_sites_[id]_url%",
"%azlink_vote_top_[position]_name%",
"%azlink_vote_top_[position]_votes%"
"%azlink_vote_top_[position]_votes%",
"%azlink_vote_goal_target%",
"%azlink_vote_goal_progress%"
);
}

Expand Down Expand Up @@ -94,6 +97,8 @@ public String evaluatePlaceholder(String[] parts, OfflinePlayer player) {
return topPlaceholder(parts);
case "sites":
return sitePlaceholder(parts);
case "goal":
return goalPlaceholder(parts[1]);
default:
return null;
}
Expand Down Expand Up @@ -134,6 +139,19 @@ private String userCanPlaceholder(String[] parts, OfflinePlayer player) throws N
return null;
}

private String goalPlaceholder(String action) {
VoteGoal goal = this.goal;

switch (action) {
case "target":
return goal != null ? Integer.toString(goal.target) : "0";
case "progress":
return goal != null ? Integer.toString(goal.progress) : "0";
default:
return null;
}
}

private String sitePlaceholder(String[] parts) throws NumberFormatException {
if (parts[1].equals("count")) {
return Integer.toString(voteSites.size());
Expand Down Expand Up @@ -229,6 +247,7 @@ private void refreshData() {
this.voteSites.clear();
this.users.clear();
this.topVotes.clear();
this.goal = response.goal;

for (VoteSite site : response.sites) {
this.voteSites.put(site.id, site);
Expand All @@ -246,7 +265,9 @@ private void refreshData() {
}

private VoteUser getUserFromPlayer(OfflinePlayer player) {
return this.users.get(player.getName().toLowerCase(Locale.ROOT));
String name = player.getName();

return name != null ? this.users.get(name.toLowerCase(Locale.ROOT)) : null;
}

private String formatDuration(Duration duration) {
Expand All @@ -260,6 +281,12 @@ public static class VoteResponse {
public List<VoteUser> users = new ArrayList<>();
@SerializedName("top_votes")
public List<TopVoteUser> topVotes = new ArrayList<>();
public VoteGoal goal;
}

public static class VoteGoal {
public int target;
public int progress;
}

public static class VoteUser {
Expand Down
Loading