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
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Loading