From 265a1d709b318313c2655f72fa73237483fa195d Mon Sep 17 00:00:00 2001 From: BenCodez <17074231+BenCodez@users.noreply.github.com> Date: Sun, 20 Sep 2026 15:31:44 -0600 Subject: [PATCH] Use shared vote counting policy in Bukkit listener --- .../core/vote/SharedVotePolicy.java | 26 ++++++++- .../listeners/PlayerVoteListener.java | 26 +++++---- .../PlayerVoteListenerCountingPolicyTest.java | 57 +++++++++++++++++++ 3 files changed, 96 insertions(+), 13 deletions(-) create mode 100644 VotingPlugin/src/test/java/com/bencodez/votingplugin/listeners/PlayerVoteListenerCountingPolicyTest.java diff --git a/VotingPlugin/src/main/java/com/bencodez/votingplugin/core/vote/SharedVotePolicy.java b/VotingPlugin/src/main/java/com/bencodez/votingplugin/core/vote/SharedVotePolicy.java index 824bcf9e2..a08654dad 100644 --- a/VotingPlugin/src/main/java/com/bencodez/votingplugin/core/vote/SharedVotePolicy.java +++ b/VotingPlugin/src/main/java/com/bencodez/votingplugin/core/vote/SharedVotePolicy.java @@ -1,5 +1,8 @@ package com.bencodez.votingplugin.core.vote; +import java.util.Objects; +import java.util.function.BooleanSupplier; + /** * Platform-neutral subset of the existing vote-processing configuration. * Native adapters should populate it from the current VotingPlugin config. @@ -8,17 +11,34 @@ public record SharedVotePolicy(boolean countFakeVotes, boolean addTotals, boolean addTotalsOffline, boolean processRewards, boolean giveOfflineRewards) { boolean shouldApplyConfiguredVoteMutation(SharedVoteInput input) { - return input.addTotals() && (input.realVote() || countFakeVotes); + return shouldApplyConfiguredVoteMutation(input.realVote(), input.addTotals()); + } + + /** The accepted Bukkit vote's existing fake-vote and add-totals gate. */ + public boolean shouldApplyConfiguredVoteMutation(boolean realVote, boolean voteAddsTotals) { + return voteAddsTotals && (realVote || countFakeVotes); } boolean shouldCountTotals(SharedVoteInput input, boolean online) { - return shouldApplyConfiguredVoteMutation(input) && addTotals && (addTotalsOffline || online); + return shouldCountTotals(input.realVote(), input.addTotals(), online); + } + + /** Online means the user's current state, including for a proxy-origin vote. */ + public boolean shouldCountTotals(boolean realVote, boolean voteAddsTotals, boolean online) { + return shouldCountTotals(realVote, voteAddsTotals, () -> online); + } + + /** Check online state only if the configuration actually requires it. */ + public boolean shouldCountTotals(boolean realVote, boolean voteAddsTotals, BooleanSupplier currentlyOnline) { + Objects.requireNonNull(currentlyOnline, "currentlyOnline"); + return shouldApplyConfiguredVoteMutation(realVote, voteAddsTotals) + && addTotals && (addTotalsOffline || currentlyOnline.getAsBoolean()); } boolean shouldAwardConfiguredPoints(SharedVoteInput input) { // Existing Bukkit behavior awards configured vote points when the accepted // vote is countable even if Config.AddTotals itself is disabled. - return shouldApplyConfiguredVoteMutation(input); + return shouldApplyConfiguredVoteMutation(input.realVote(), input.addTotals()); } boolean shouldExecuteRewardsNow(SharedVoteInput input, boolean online) { diff --git a/VotingPlugin/src/main/java/com/bencodez/votingplugin/listeners/PlayerVoteListener.java b/VotingPlugin/src/main/java/com/bencodez/votingplugin/listeners/PlayerVoteListener.java index d86873bff..da6e29a6f 100644 --- a/VotingPlugin/src/main/java/com/bencodez/votingplugin/listeners/PlayerVoteListener.java +++ b/VotingPlugin/src/main/java/com/bencodez/votingplugin/listeners/PlayerVoteListener.java @@ -16,6 +16,7 @@ import com.bencodez.advancedcore.api.user.validation.UserValidationResult; import com.bencodez.simpleapi.array.ArrayUtils; import com.bencodez.votingplugin.VotingPluginMain; +import com.bencodez.votingplugin.core.vote.SharedVotePolicy; import com.bencodez.votingplugin.events.PlayerPostVoteEvent; import com.bencodez.votingplugin.events.PlayerVoteEvent; import com.bencodez.votingplugin.topvoter.TopVoter; @@ -255,16 +256,10 @@ public void onplayerVote(PlayerVoteEvent event) { } // add to total votes - if ((plugin.getConfigFile().isCountFakeVotes() || event.isRealVote()) && event.isAddTotals()) { - if (plugin.getConfigFile().isAddTotals()) { - if (plugin.getConfigFile().isAddTotalsOffline() || user.isOnline()) { - user.addTotal(); - user.addTotalDaily(); - user.addTotalWeekly(); - } - } - user.addPoints(); - } + SharedVotePolicy countingPolicy = new SharedVotePolicy(plugin.getConfigFile().isCountFakeVotes(), + plugin.getConfigFile().isAddTotals(), plugin.getConfigFile().isAddTotalsOffline(), false, false); + applyAcceptedVoteCounts(user, countingPolicy, event.isRealVote(), event.isAddTotals()); + user.checkDayVoteStreak(event.isForceBungee()); if (plugin.getConfigFile().isLimitMonthlyVotes()) { @@ -308,4 +303,15 @@ public void onplayerVote(PlayerVoteEvent event) { plugin.extraDebug("Finished vote processing: " + playerName + "/" + uuid); } + /** Keep the production count/points calls in their existing order. */ + static void applyAcceptedVoteCounts(VotingPluginUser user, SharedVotePolicy policy, + boolean realVote, boolean voteAddsTotals) { + if (!policy.shouldApplyConfiguredVoteMutation(realVote, voteAddsTotals)) return; + if (policy.shouldCountTotals(realVote, voteAddsTotals, user::isOnline)) { + user.addTotal(); + user.addTotalDaily(); + user.addTotalWeekly(); + } + user.addPoints(); + } } diff --git a/VotingPlugin/src/test/java/com/bencodez/votingplugin/listeners/PlayerVoteListenerCountingPolicyTest.java b/VotingPlugin/src/test/java/com/bencodez/votingplugin/listeners/PlayerVoteListenerCountingPolicyTest.java new file mode 100644 index 000000000..a24b8c135 --- /dev/null +++ b/VotingPlugin/src/test/java/com/bencodez/votingplugin/listeners/PlayerVoteListenerCountingPolicyTest.java @@ -0,0 +1,57 @@ +package com.bencodez.votingplugin.listeners; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import org.junit.jupiter.api.Test; + +import com.bencodez.votingplugin.core.vote.SharedVotePolicy; +import com.bencodez.votingplugin.user.VotingPluginUser; + +class PlayerVoteListenerCountingPolicyTest { + @Test + void onlineAcceptedVoteAddsEachTotalAndPointsOnce() { + assertCounts(true, true, false, true, false, true, true, true); + } + + @Test + void offlineVoteHonorsAddTotalsOfflineWithoutLosingPoints() { + assertCounts(true, true, false, true, false, false, false, true); + assertCounts(true, true, false, true, true, false, true, true); + } + + @Test + void eventAddTotalsFalsePreventsBothTotalsAndPoints() { + assertCounts(true, false, false, true, true, true, false, false); + } + + @Test + void configAddTotalsFalseStillAwardsConfiguredPoints() { + assertCounts(true, true, false, false, true, true, false, true); + } + + @Test + void fakeVoteUsesCountFakeVotesSetting() { + assertCounts(false, true, false, true, true, true, false, false); + assertCounts(false, true, true, true, true, true, true, true); + } + + private static void assertCounts(boolean realVote, boolean eventAddsTotals, boolean countFakeVotes, + boolean configAddTotals, boolean addTotalsOffline, boolean currentlyOnline, + boolean expectedTotals, boolean expectedPoints) { + VotingPluginUser user = mock(VotingPluginUser.class); + when(user.isOnline()).thenReturn(currentlyOnline); + SharedVotePolicy policy = new SharedVotePolicy(countFakeVotes, configAddTotals, addTotalsOffline, true, true); + + PlayerVoteListener.applyAcceptedVoteCounts(user, policy, realVote, eventAddsTotals); + + verify(user, times(expectedTotals ? 1 : 0)).addTotal(); + verify(user, times(expectedTotals ? 1 : 0)).addTotalDaily(); + verify(user, times(expectedTotals ? 1 : 0)).addTotalWeekly(); + verify(user, times(expectedPoints ? 1 : 0)).addPoints(); + if (!expectedPoints || !configAddTotals || addTotalsOffline) verify(user, never()).isOnline(); + } +}