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
26 changes: 23 additions & 3 deletions src/main/java/net/tfminecraft/activitytf/gui/ActivityGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -335,29 +335,49 @@ private ItemStack dailyBarItem(ActivityConfiguration config, Messages messages,

return item(Material.EXPERIENCE_BOTTLE,
messages.get("gui.daily-bar-name", "%points%", dailyPoints, "%max%", config.dailyMax()),
List.of(Utils.colorize(bar)));
List.of(Utils.colorize(bar), dailyBarLore(messages, dailyPoints, config.dailyMax())));
}

static String dailyBarLore(Messages messages, int dailyPoints, int dailyMax) {
return messages.get(dailyPoints >= dailyMax ? "gui.daily-bar-lore-full" : "gui.daily-bar-lore",
"%max%", dailyMax, "%zone%", Utils.serverZone());
}

private ItemStack activityItem(ActivityConfiguration config, Messages messages, ActivityDef def, PlayerData data) {
return item(iconStack(config, def.icon(), def.iconPath()), Utils.colorize(def.display()),
activityLore(messages, def, data.count(def.id())));
activityLore(messages, def, data.count(def.id()), data.isUnpaid(def.id()),
data.dailyPoints() >= config.dailyMax()));
}

static List<String> activityLore(Messages messages, ActivityDef def, int count) {
return activityLore(messages, def, count, false, false);
}

/**
* {@code unpaid}: a completion today paid nothing because today's points were full; a finished task then says so
* instead of showing a full bar. {@code dayFull}: today's points are full, so an unfinished task pays nothing more.
*/
static List<String> activityLore(Messages messages, ActivityDef def, int count, boolean unpaid, boolean dayFull) {
List<String> lore = new ArrayList<>();
for (String line : def.description()) {
lore.add(Utils.colorize(line
.replace("%every%", Integer.toString(def.every()))
.replace("%points%", Integer.toString(def.points()))
.replace("%daily-cap%", Integer.toString(def.dailyCap()))));
}
boolean finished = def.dailyCap() > 0 && def.worth(count) >= def.capPoints();
String progress = progressBar(def, count);
if (progress != null) {
if (finished && unpaid) {
lore.add(messages.get("gui.activity-lore-done-unpaid"));
} else if (progress != null) {
lore.add(messages.get("gui.activity-lore-progress", "%bar%", Utils.colorize(progress)));
}
lore.add(def.dailyCap() > 0
? messages.get("gui.activity-lore-today-capped", "%today%", def.completions(count), "%cap%", def.dailyCap())
: messages.get("gui.activity-lore-today", "%today%", def.completions(count)));
if (dayFull && !finished) {
lore.add(messages.get("gui.activity-lore-day-full"));
}
return lore;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,14 @@ private RecordResult record(UUID uuid, String activityId, int amount, boolean ga
return new RecordResult(0, 0, Recorded.NOT_A_TASK);
}

int dailyBefore = data.dailyPoints();
RecordResult result = gated
? data.record(amount, def, config.barMax(), config.dailyMax(),
config.activity("vote") == null ? config.dailyMax() : config.nonVoteDailyMax(),
config.milestones())
: data.recordForced(amount, def, config.barMax(), config.milestones());
return credited(uuid, data, result, Utils.colorize(def.display()));
boolean filledToday = gated && dailyBefore < config.dailyMax() && data.dailyPoints() >= config.dailyMax();
return credited(uuid, data, result, Utils.colorize(def.display()), filledToday);
}

public RecordResult recordPoints(UUID uuid, int points) {
Expand All @@ -202,10 +204,11 @@ public RecordResult recordPoints(UUID uuid, int points) {
}
PlayerData data = store.get(uuid);
return credited(uuid, data, data.creditForced(points, config.barMax(), config.milestones()),
config.messages().get("points-granted-source"));
config.messages().get("points-granted-source"), false);
}

private RecordResult credited(UUID uuid, PlayerData data, RecordResult result, String source) {
private RecordResult credited(UUID uuid, PlayerData data, RecordResult result, String source,
boolean filledToday) {
store.markDirty();

if (result.pointsAwarded() <= 0) {
Expand All @@ -224,7 +227,11 @@ private RecordResult credited(UUID uuid, PlayerData data, RecordResult result, S
"%total%", data.points(),
"%max%", config.barMax()));
if (data.dailyPoints() == result.pointsAwarded()) {
player.sendMessage(messages.get("first-points-today"));
player.sendMessage(messages.get("first-points-today", "%max%", config.dailyMax()));
}
if (filledToday) {
player.sendMessage(messages.get("daily-max-reached", "%max%", config.dailyMax(),
"%zone%", Utils.serverZone()));
}
playSound(player, config.goalCompleteSound());

Expand Down
24 changes: 24 additions & 0 deletions src/main/java/net/tfminecraft/activitytf/models/PlayerData.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public class PlayerData {
/** Credit held today for tasks that only count once revealed (votes cast before Voting is clicked). */
private final Map<String, Integer> pending = new ConcurrentHashMap<>();

/** Tasks with a completion today that paid nothing (or only part) because today's points were full. */
private final Set<String> unpaid = ConcurrentHashMap.newKeySet();

private volatile int points;
private volatile int dailyPoints;
private volatile int votePoints;
Expand Down Expand Up @@ -120,8 +123,14 @@ public RecordResult record(int amount, ActivityDef def, int max, int dailyMax, i
int budget = isVote ? dailyBudget
: Math.min(dailyBudget, Math.max(0, nonVoteMax - (dailyPoints - votePoints)));
if (earned > 0 && budget <= 0) {
if (dailyBudget <= 0) {
unpaid.add(def.id());
}
return new RecordResult(0, 0, dailyBudget <= 0 ? Recorded.DAILY_MAX : Recorded.VOTE_SHARE);
}
if (earned > dailyBudget && budget == dailyBudget) {
unpaid.add(def.id());
}
earned = Math.min(earned, budget);
if (earned <= 0) {
return new RecordResult(0, 0,
Expand Down Expand Up @@ -224,6 +233,7 @@ public void reroll(List<String> newTasks, int max) {
private void clearTasks() {
tasks.clear();
revealed.clear();
unpaid.clear();
}

public static List<String> draw(Collection<String> ids, Collection<String> guaranteed, Random random) {
Expand Down Expand Up @@ -464,6 +474,20 @@ public void setPending(Map<String, Integer> held) {
});
}

/** Whether a completion of this task today went unpaid because today's points were full. */
public boolean isUnpaid(String id) {
return unpaid.contains(id);
}

public Set<String> unpaid() {
return Collections.unmodifiableSet(unpaid);
}

public void setUnpaid(Collection<String> ids) {
unpaid.clear();
unpaid.addAll(ids);
}

public boolean reveal(int slot) {
if (slot < 0 || slot >= tasks.size()) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ private static PlayerData parse(ConfigurationSection entry, int barMax, int dail
}
}
data.setPending(pending);
List<String> unpaid = new ArrayList<>(entry.getStringList("unpaid"));
unpaid.removeIf(known.negate());
data.setUnpaid(unpaid);
data.setVotePoints(entry.contains("vote-points") ? entry.getInt("vote-points")
: vote == null ? 0 : vote.worth(daily.getOrDefault(vote.id(), 0)));
return data;
Expand Down Expand Up @@ -300,6 +303,9 @@ static YamlConfiguration snapshot(Map<UUID, PlayerData> players) {
if (!data.pending().isEmpty()) {
yaml.set(path + ".pending", new TreeMap<>(data.pending()));
}
if (!data.unpaid().isEmpty()) {
yaml.set(path + ".unpaid", new ArrayList<>(new TreeSet<>(data.unpaid())));
}
}
return yaml;
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/net/tfminecraft/activitytf/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ public static String colorize(String msg) {
return ChatColor.translateAlternateColorCodes('&', msg);
}

/** The server's time zone as players read it ("UTC"); the day resets at midnight in this zone. */
public static String serverZone() {
java.util.TimeZone zone = java.util.TimeZone.getDefault();
return zone.getDisplayName(zone.inDaylightTime(new java.util.Date()), java.util.TimeZone.SHORT,
java.util.Locale.ROOT);
}

public static String safeForLog(String name) {
return name == null ? "null" : name.replaceAll("[\\p{Cc}\\p{Cf}\\p{Zl}\\p{Zp}]", "?");
}
Expand Down
8 changes: 7 additions & 1 deletion src/main/resources/messages.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
points-earned: "&a+%points% &f%activity% &7(&a%total%&7/%max%)"
first-points-today: "&7Your daily tasks fill your weekly bar for free rewards. Open &e/activity &7to see them."
first-points-today: "&7Your daily tasks fill your weekly bar for free rewards, up to &f%max%&7 points a day. Open &e/activity &7to see them."
# Sent once, when today's points reach bar.daily-max. The day resets at midnight, server time; %zone% is the server's zone.
daily-max-reached: "&a&lDaily tasks done! &7You've earned today's &f%max%&7 points. Tasks still tick off, but they give points again after the daily reset (&f00:00 %zone%&7). Come back tomorrow for more."
points-granted-source: "Bonus"
reward-ready: "&a&lREWARD READY! &7Open &f/activity &7to claim."
reward-claimed: "&a&lCLAIMED &7You received %reward%&7."
Expand All @@ -15,6 +17,8 @@ reroll-locked: "&7Rerolling daily tasks is an #823FFF&l[#8939EE&lA#9033DD&ls#972
gui:
bar-name: "#e6ca40&lWeekly Activity&e: #50d990%points%&7/&f%max%"
daily-bar-name: "#e6ca40&lDaily Activity&e: #50d990%points%&7/&f%max%"
daily-bar-lore: "&7Up to &f%max%&7 points a day count toward your weekly bar."
daily-bar-lore-full: "&aToday's points are full. &7New points from &f00:00 %zone%&7."
bar-lore-reward: "#d97b66Next reward&e: %reward% #b8906eat &6%points% #b8906epoints"
bar-lore-next-options: "#d97b66Next reward at&e: &6%points% #b8906epoints"
bar-lore-none: "&7No more rewards this week"
Expand All @@ -34,6 +38,8 @@ gui:
activity-lore-progress: "#af97bdProgress&e: %bar%"
activity-lore-today: "#d97b66Today&e: &6%today%"
activity-lore-today-capped: "#d97b66Today&e: &6%today%&7/&f%cap%"
activity-lore-done-unpaid: "&eDone, but today's points were already full"
activity-lore-day-full: "&8Today's points are full - no points until the reset"
hidden-task-name: "#c9c14fClick to reveal"
reroll-name: "#c26ef0&lReroll Daily Tasks"
reroll-lore-left: "#d97b66Rerolls left today&e: &6%left%&7/&f%max%"
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/net/tfminecraft/activitytf/gui/ActivityGuiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,34 @@ void theTodayLineShowsCompletionsNotPoints() {
ActivityGui.activityLore(messages, def, 3));
}

@Test
void aTaskFinishedAfterTodaysMaximumSaysItPaidNothing() {
Messages messages = shippedMessages();
ActivityDef def = activity("injured", 1, 5, 1);

assertEquals(List.of(
messages.get("gui.activity-lore-done-unpaid"),
messages.get("gui.activity-lore-today-capped", "%today%", 1, "%cap%", 1)),
ActivityGui.activityLore(messages, def, 1, true, true));
assertEquals(List.of(
messages.get("gui.activity-lore-today-capped", "%today%", 0, "%cap%", 1),
messages.get("gui.activity-lore-day-full")),
ActivityGui.activityLore(messages, def, 0, false, true));
assertEquals(ActivityGui.activityLore(messages, def, 1), ActivityGui.activityLore(messages, def, 1, false, true),
"a task finished before the maximum changed");
}

@Test
void theDailyBarSaysHowManyPointsADayCountAndWhenTheyAreFull() {
Messages messages = shippedMessages();

assertEquals(messages.get("gui.daily-bar-lore", "%max%", 10), ActivityGui.dailyBarLore(messages, 9, 10));
assertTrue(ActivityGui.dailyBarLore(messages, 9, 10).contains("10"));
String full = ActivityGui.dailyBarLore(messages, 10, 10);
assertEquals(messages.get("gui.daily-bar-lore-full", "%zone%", Utils.serverZone()), full);
assertFalse(full.contains("%zone%"), full);
}

@Test
void anActivityWithoutADescriptionRendersTheSameTwoLines() {
Messages messages = shippedMessages();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,46 @@ void votesBeforeTheClickAreHeldThenCreditedWithTheFirstPointsHint() {

assertEquals(2, data.points());
assertTrue(data.pending().isEmpty());
String hint = manager.getConfiguration().messages().get("first-points-today");
String hint = manager.getConfiguration().messages().get("first-points-today",
"%max%", manager.getConfiguration().dailyMax());
assertEquals(1, chat.stream().filter(hint::equals).count());

manager.recordAction(uuid, "cook", 1);
assertEquals(1, chat.stream().filter(hint::equals).count(), "the hint came again the same day");
assertEquals(Recorded.ACTIVITY_CAP, manager.recordAction(uuid, "vote", 1));
}

@Test
void reachingTodaysMaximumSaysSoOnceAndMarksLaterCompletionsUnpaid() {
ActivityManager manager = manager(def("vote", List.of(), true), general("cook"), general("chat"),
general("mine"));
config(manager, "dailyMax", 3);
List<String> chat = new ArrayList<>();
Player player = join(new HashSet<>(), chat);
UUID uuid = player.getUniqueId();
manager.tasks(uuid);
String full = manager.getConfiguration().messages().get("daily-max-reached", "%max%", 3,
"%zone%", net.tfminecraft.activitytf.utils.Utils.serverZone());
assertTrue(full.contains("3"), full);

manager.recordAction(uuid, "cook", 2);
assertEquals(0, chat.stream().filter(full::equals).count(), "said full before the maximum");
assertTrue(manager.getConfiguration().messages().get("first-points-today", "%max%", 3).contains("3"));

manager.recordAction(uuid, "chat", 2);
PlayerData data = manager.getStore().get(uuid);
assertEquals(3, data.dailyPoints());
assertEquals(1, chat.stream().filter(full::equals).count());
assertTrue(data.isUnpaid("chat"), "the completion cut short by the maximum was not marked");
assertFalse(data.isUnpaid("cook"));

assertEquals(Recorded.DAILY_MAX, manager.recordAction(uuid, "mine", 1));
assertTrue(data.isUnpaid("mine"));
assertEquals(1, chat.stream().filter(full::equals).count(), "said full again the same day");
manager.recordAdmin(uuid, "mine", 1, true);
assertEquals(1, chat.stream().filter(full::equals).count(), "a forced award said full");
}

@Test
void revealingVotingWithNothingHeldCreditsNothing() {
ActivityManager manager = manager(def("vote", List.of(), true), general("cook"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,41 @@ void newDayWipesOnlyTheDailyCounters() {
assertEquals(0, data.count("vote"));
}

@Test
void onlyCompletionsTheDailyMaximumCutAreMarkedUnpaidAndANewDayClearsThem() {
PlayerData data = data();
data.record(2, QUEST, MAX, 3, MILESTONES);
assertTrue(data.unpaid().isEmpty(), "a fully paid completion was marked");

data.record(2, VOTE, MAX, 3, MILESTONES);
assertEquals(3, data.dailyPoints());
assertTrue(data.isUnpaid("vote"), "a completion only partly paid was not marked");

data.record(1, INSTRUMENT, MAX, 3, MILESTONES);
assertFalse(data.isUnpaid("instrument"), "a step that earned nothing was marked");
assertEquals(Recorded.DAILY_MAX, data.record(20, INSTRUMENT, MAX, 3, MILESTONES).outcome());
assertTrue(data.isUnpaid("instrument"));
assertFalse(data.isUnpaid("quest"));

data.roll(WEEK, "2026-09-10");
assertTrue(data.unpaid().isEmpty());
}

@Test
void theNonVoteShareAloneDoesNotMarkATaskUnpaid() {
PlayerData data = data();
assertEquals(Recorded.VOTE_SHARE, data.record(2, QUEST, MAX, 4, 0, MILESTONES).outcome());
data.record(3, QUEST, MAX, 4, 2, MILESTONES);
assertEquals(2, data.dailyPoints());
assertTrue(data.unpaid().isEmpty());

PlayerData share = data();
share.record(4, UNCAPPED, MAX, 10, 5, MILESTONES);
share.record(7, UNCAPPED, MAX, 10, 5, MILESTONES);
assertEquals(5, share.dailyPoints());
assertTrue(share.unpaid().isEmpty(), "an award cut only by the non-vote share was marked");
}

@Test
void sameWeekAndDayChangeNothing() {
PlayerData data = data();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,23 @@ void heldCreditSurvivesASaveAndUnknownActivitiesAreDropped() throws Exception {
assertEquals(Map.of("vote", 3), read.pending());
}

@Test
void unpaidTasksSurviveASaveAndUnknownActivitiesAreDropped() throws Exception {
UUID uuid = UUID.randomUUID();
PlayerData data = new PlayerData("2026-11-16", "2026-11-16");
data.addPoints(1, 50);
data.setUnpaid(Set.of("cook", "gone"));

YamlConfiguration reloaded = new YamlConfiguration();
reloaded.loadFromString(PlayerStore.snapshot(Map.of(uuid, data)).saveToString());
PlayerData read = PlayerStore.readEntry(reloaded.getConfigurationSection("players"), uuid.toString(), 50, 10,
Set.of("cook")::contains);

assertEquals(Set.of("cook"), read.unpaid());
data.setUnpaid(Set.of());
assertFalse(PlayerStore.snapshot(Map.of(uuid, data)).contains("players." + uuid + ".unpaid"));
}

@Test
void nothingHeldWritesNoPendingSection() {
UUID uuid = UUID.randomUUID();
Expand Down