Skip to content

Commit 121cca6

Browse files
committed
Add VoteStreaks reset command
1 parent c633baa commit 121cca6

3 files changed

Lines changed: 177 additions & 1 deletion

File tree

VotingPlugin/src/main/java/com/bencodez/votingplugin/commands/CommandLoader.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
import com.bencodez.votingplugin.presets.VoteSitePresetSetupHandler;
8686
import com.bencodez.votingplugin.specialrewards.votemilestones.VoteMilestonesManager;
8787
import com.bencodez.votingplugin.specialrewards.votestreak.VoteStreakDefinition;
88+
import com.bencodez.votingplugin.specialrewards.votestreak.VoteStreakType;
8889
import com.bencodez.votingplugin.topvoter.TopVoter;
8990
import com.bencodez.votingplugin.user.VotingPluginUser;
9091
import com.bencodez.votingplugin.votesites.VoteSite;
@@ -904,6 +905,46 @@ public void execute(CommandSender sender, String[] args) {
904905
}
905906
});
906907

908+
plugin.getAdminVoteCommand()
909+
.add(new CommandHandler(plugin, new String[] { "User", "(player)", "ResetVoteStreaks" },
910+
"VotingPlugin.Commands.AdminVote.ResetVoteStreaks|" + adminPerm,
911+
"Reset configured VoteStreaks state for player") {
912+
913+
@Override
914+
public void execute(CommandSender sender, String[] args) {
915+
VotingPluginUser user = plugin.getVotingPluginUserManager().getVotingPluginUser(args[1]);
916+
int reset = plugin.getVoteStreakHandler().resetVoteStreaks(user);
917+
sendMessage(sender, "&cReset " + reset + " VoteStreaks for '" + args[1] + "'");
918+
}
919+
});
920+
921+
plugin.getAdminVoteCommand()
922+
.add(new CommandHandler(plugin, new String[] { "User", "(player)", "ResetVoteStreaks", "(votestreak)" },
923+
"VotingPlugin.Commands.AdminVote.ResetVoteStreaks|" + adminPerm,
924+
"Reset configured VoteStreaks state for player by type or id") {
925+
926+
@Override
927+
public void execute(CommandSender sender, String[] args) {
928+
VotingPluginUser user = plugin.getVotingPluginUserManager().getVotingPluginUser(args[1]);
929+
String target = args[3];
930+
931+
try {
932+
VoteStreakType type = VoteStreakType.from(target);
933+
int reset = plugin.getVoteStreakHandler().resetVoteStreaks(user, type);
934+
sendMessage(sender,
935+
"&cReset " + reset + " " + type.name() + " VoteStreaks for '" + args[1] + "'");
936+
return;
937+
} catch (Exception ignored) {
938+
}
939+
940+
if (plugin.getVoteStreakHandler().resetVoteStreak(user, target)) {
941+
sendMessage(sender, "&cReset VoteStreak '" + target + "' for '" + args[1] + "'");
942+
} else {
943+
sendMessage(sender, "&cVoteStreak not found: &e" + target);
944+
}
945+
}
946+
});
947+
907948
for (final TopVoter top : TopVoter.values()) {
908949
plugin.getAdminVoteCommand()
909950
.add(new CommandHandler(plugin,
@@ -3500,4 +3541,4 @@ public void processSlotClick(Player player, VotingPluginUser user, String slot)
35003541
public void setCommands(HashMap<String, CommandHandler> commands) {
35013542
this.commands = commands;
35023543
}
3503-
}
3544+
}

VotingPlugin/src/main/java/com/bencodez/votingplugin/specialrewards/votestreak/VoteStreakHandler.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,48 @@ public boolean forceVoteStreakReward(VotingPluginUser user, String id, int strea
451451
return true;
452452
}
453453

454+
public int resetVoteStreaks(VotingPluginUser user) {
455+
if (user == null) {
456+
return 0;
457+
}
458+
459+
int reset = 0;
460+
for (VoteStreakDefinition def : ordered) {
461+
writeStateString(user, getColumnName(def), "");
462+
reset++;
463+
}
464+
return reset;
465+
}
466+
467+
public int resetVoteStreaks(VotingPluginUser user, VoteStreakType type) {
468+
if (user == null || type == null) {
469+
return 0;
470+
}
471+
472+
int reset = 0;
473+
for (VoteStreakDefinition def : ordered) {
474+
if (def.getType() == type) {
475+
writeStateString(user, getColumnName(def), "");
476+
reset++;
477+
}
478+
}
479+
return reset;
480+
}
481+
482+
public boolean resetVoteStreak(VotingPluginUser user, String id) {
483+
if (user == null || id == null || id.trim().isEmpty()) {
484+
return false;
485+
}
486+
487+
VoteStreakDefinition def = getDefinition(id);
488+
if (def == null) {
489+
return false;
490+
}
491+
492+
writeStateString(user, getColumnName(def), "");
493+
return true;
494+
}
495+
454496
private void giveRewards(VotingPluginUser user, VoteStreakDefinition def, UUID voteUUID, int streakCount) {
455497
PlayerSpecialRewardEvent event = new PlayerSpecialRewardEvent(user,
456498
SpecialRewardType.VOTESTREAKS.setType(def.getType().toString()).setAmount(def.getVotesRequired()),

VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/votestreak/VoteStreakHandlerTest.java

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,31 @@ private static YamlConfiguration rootWithLegacyListRewards() {
152152
return root;
153153
}
154154

155+
private static MemoryConfiguration rootWithThreeStreaks() {
156+
MemoryConfiguration root = new MemoryConfiguration();
157+
ConfigurationSection voteStreaks = root.createSection("VoteStreaks");
158+
159+
addStreak(voteStreaks, "Daily3", "DAILY", true, 3, 1, 1, 7);
160+
addStreak(voteStreaks, "Daily7", "DAILY", true, 7, 1, 1, 7);
161+
addStreak(voteStreaks, "Weekly2", "WEEKLY", true, 2, 1, 0, 0);
162+
163+
return root;
164+
}
165+
166+
private static void addStreak(ConfigurationSection voteStreaks, String id, String type, boolean enabled,
167+
int intervalAmount, int votesRequired, int allowMissedAmount, int allowMissedPeriod) {
168+
ConfigurationSection def = voteStreaks.createSection(id);
169+
def.set("Type", type);
170+
def.set("Enabled", enabled);
171+
172+
ConfigurationSection req = def.createSection("Requirements");
173+
req.set("Amount", intervalAmount);
174+
req.set("VotesRequired", votesRequired);
175+
176+
def.set("AllowMissedAmount", allowMissedAmount);
177+
def.set("AllowMissedPeriod", allowMissedPeriod);
178+
}
179+
155180
/**
156181
* periodKey|streakCount|votesThisPeriod|countedThisPeriod|missWindowStartKey|missesUsed
157182
*
@@ -265,6 +290,74 @@ void shouldReward_respectsRecurringFlag() throws Exception {
265290
assertTrue(shouldReward(recurring, 6));
266291
}
267292

293+
@Test
294+
void resetVoteStreaks_resetsAllConfiguredDefinitions() {
295+
loadFromRoot(rootWithThreeStreaks());
296+
297+
Map<String, String> backing = new HashMap<>();
298+
VotingPluginUser user = mapBackedUser(UUID.randomUUID(), "Ben", backing);
299+
for (VoteStreakDefinition def : handler.getDefinitions()) {
300+
backing.put(handler.getColumnName(def), "2026-01-10|5|1|true||0");
301+
}
302+
303+
int reset = handler.resetVoteStreaks(user);
304+
305+
assertEquals(3, reset);
306+
for (VoteStreakDefinition def : handler.getDefinitions()) {
307+
assertEquals("", backing.get(handler.getColumnName(def)));
308+
}
309+
}
310+
311+
@Test
312+
void resetVoteStreaks_byTypeResetsOnlyMatchingDefinitions() {
313+
loadFromRoot(rootWithThreeStreaks());
314+
315+
Map<String, String> backing = new HashMap<>();
316+
VotingPluginUser user = mapBackedUser(UUID.randomUUID(), "Ben", backing);
317+
VoteStreakDefinition daily3 = handler.getDefinition("Daily3");
318+
VoteStreakDefinition daily7 = handler.getDefinition("Daily7");
319+
VoteStreakDefinition weekly2 = handler.getDefinition("Weekly2");
320+
321+
backing.put(handler.getColumnName(daily3), "2026-01-10|3|1|true||0");
322+
backing.put(handler.getColumnName(daily7), "2026-01-10|7|1|true||0");
323+
backing.put(handler.getColumnName(weekly2), "2026-W02|2|1|true||0");
324+
325+
int reset = handler.resetVoteStreaks(user, VoteStreakType.DAILY);
326+
327+
assertEquals(2, reset);
328+
assertEquals("", backing.get(handler.getColumnName(daily3)));
329+
assertEquals("", backing.get(handler.getColumnName(daily7)));
330+
assertEquals("2026-W02|2|1|true||0", backing.get(handler.getColumnName(weekly2)));
331+
}
332+
333+
@Test
334+
void resetVoteStreak_byIdResetsOnlyMatchingDefinition() {
335+
loadFromRoot(rootWithThreeStreaks());
336+
337+
Map<String, String> backing = new HashMap<>();
338+
VotingPluginUser user = mapBackedUser(UUID.randomUUID(), "Ben", backing);
339+
VoteStreakDefinition daily3 = handler.getDefinition("Daily3");
340+
VoteStreakDefinition weekly2 = handler.getDefinition("Weekly2");
341+
342+
backing.put(handler.getColumnName(daily3), "2026-01-10|3|1|true||0");
343+
backing.put(handler.getColumnName(weekly2), "2026-W02|2|1|true||0");
344+
345+
assertTrue(handler.resetVoteStreak(user, "daily3"));
346+
assertEquals("", backing.get(handler.getColumnName(daily3)));
347+
assertEquals("2026-W02|2|1|true||0", backing.get(handler.getColumnName(weekly2)));
348+
}
349+
350+
@Test
351+
void resetVoteStreak_unknownIdReturnsFalse() {
352+
loadFromRoot(rootWithThreeStreaks());
353+
354+
Map<String, String> backing = new HashMap<>();
355+
VotingPluginUser user = mapBackedUser(UUID.randomUUID(), "Ben", backing);
356+
357+
assertFalse(handler.resetVoteStreak(user, "missing"));
358+
assertTrue(backing.isEmpty());
359+
}
360+
268361
@Test
269362
void migrateLegacyConfigManually_migratesOnlyEnabledLegacyEntries() {
270363
YamlConfiguration root = rootWithLegacyVoteStreaks();

0 commit comments

Comments
 (0)