diff --git a/VotingPlugin/src/main/java/com/bencodez/votingplugin/user/VotingPluginUser.java b/VotingPlugin/src/main/java/com/bencodez/votingplugin/user/VotingPluginUser.java index b2de62d38..cb508f998 100644 --- a/VotingPlugin/src/main/java/com/bencodez/votingplugin/user/VotingPluginUser.java +++ b/VotingPlugin/src/main/java/com/bencodez/votingplugin/user/VotingPluginUser.java @@ -2362,7 +2362,7 @@ private CompletionStage offVoteWithCapturedTopVoterIgnoreAsync(boolean cur beforeRewardEffects.run(); throw new IllegalStateException("Earlier offline-vote rewards require reconciliation for " + uuid); } - ArrayList offlineVotes = getOfflineVotes(); + ArrayList offlineVotes = new ArrayList<>(getOfflineVotes()); if (offlineVotes.isEmpty()) { confirmed.complete(null); return confirmed; @@ -2373,7 +2373,8 @@ private CompletionStage offVoteWithCapturedTopVoterIgnoreAsync(boolean cur CompletionStage rewards = plugin.getRewardHandler().giveRewardAsync(this, plugin.getSpecialRewardsConfig().getData(), plugin.getSpecialRewardsConfig().getAnySiteRewardsPath(), - new RewardOptions().setOnline(false)); + new RewardOptions().setOnline(false) + .addPlaceholder("votes", Integer.toString(offlineVotes.size()))); if (rewards == null) throw new IllegalStateException("Missing offline reward completion"); for (String name : offlineVotes) { VoteSite site = plugin.getVoteSiteManager().resolveVoteSite(name, true); @@ -2676,7 +2677,8 @@ public void resetTotals(TopVoter topVoter) { */ public void sendVoteEffects(boolean online) { plugin.getRewardHandler().giveReward(this, plugin.getSpecialRewardsConfig().getData(), - plugin.getSpecialRewardsConfig().getAnySiteRewardsPath(), new RewardOptions().setOnline(online)); + plugin.getSpecialRewardsConfig().getAnySiteRewardsPath(), + new RewardOptions().setOnline(online).addPlaceholder("votes", "1")); } /** diff --git a/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/user/VotingPluginUserOfflineVoteReplayTest.java b/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/user/VotingPluginUserOfflineVoteReplayTest.java index d131a3a74..5042c158b 100644 --- a/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/user/VotingPluginUserOfflineVoteReplayTest.java +++ b/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/user/VotingPluginUserOfflineVoteReplayTest.java @@ -40,6 +40,106 @@ import com.bencodez.votingplugin.votesites.VoteSiteManager; class VotingPluginUserOfflineVoteReplayTest { + @Test + void deniedAnySiteCompletesWithoutChangingSiteRewardSelectionOrDefaultOptions() { + AsyncReplayFixture fixture = asyncFixture(); + org.mockito.ArgumentCaptor options = org.mockito.ArgumentCaptor.forClass(RewardOptions.class); + CompletableFuture result = fixture.user.offVoteWithCapturedTopVoterIgnoreAsync(false).toCompletableFuture(); + verify(fixture.plugin.getRewardHandler()).giveRewardAsync(eq(fixture.user), + org.mockito.ArgumentMatchers.any(YamlConfiguration.class), eq("AnySiteRewards"), options.capture()); + assertFalse(options.getValue().isOnline()); + assertFalse(options.getValue().isForceOffline()); + assertFalse(options.getValue().isIgnoreRequirements()); + // AdvancedCore represents a denied/cancelled reward as normal completion. + // No command effect is fabricated here: the caller continues the site chain. + fixture.anySiteRewards.complete(null); + result.join(); + verify(fixture.site).giveRewardsAsync(fixture.user, false, false); + assertTrue(fixture.queued.get().isEmpty()); + } + + @Test + void anySiteOptionsDoNotShareCountBetweenPlayersOrLaterImmediateInvocations() { + AsyncReplayFixture first = asyncFixture(); + AsyncReplayFixture second = asyncFixture(); + doReturn("00000000-0000-0000-0000-000000000002").when(second.user).getUUID(); + first.queued.set(new ArrayList<>(List.of("Site1", "Site1"))); + first.user.offVoteWithCapturedTopVoterIgnoreAsync(false); + org.mockito.ArgumentCaptor firstOptions = org.mockito.ArgumentCaptor.forClass(RewardOptions.class); + verify(first.plugin.getRewardHandler()).giveRewardAsync(eq(first.user), + org.mockito.ArgumentMatchers.any(YamlConfiguration.class), eq("AnySiteRewards"), firstOptions.capture()); + second.user.offVoteWithCapturedTopVoterIgnoreAsync(false); + org.mockito.ArgumentCaptor secondOptions = org.mockito.ArgumentCaptor.forClass(RewardOptions.class); + verify(second.plugin.getRewardHandler()).giveRewardAsync(eq(second.user), + org.mockito.ArgumentMatchers.any(YamlConfiguration.class), eq("AnySiteRewards"), secondOptions.capture()); + assertEquals("2", firstOptions.getValue().getPlaceholders().get("votes")); + assertEquals("1", secondOptions.getValue().getPlaceholders().get("votes")); + first.anySiteRewards.complete(null); + second.anySiteRewards.complete(null); + first.user.sendVoteEffects(true); + org.mockito.ArgumentCaptor immediate = org.mockito.ArgumentCaptor.forClass(RewardOptions.class); + verify(first.plugin.getRewardHandler()).giveReward(eq(first.user), + org.mockito.ArgumentMatchers.any(YamlConfiguration.class), eq("AnySiteRewards"), immediate.capture()); + assertEquals("1", immediate.getValue().getPlaceholders().get("votes")); + assertEquals("2", firstOptions.getValue().getPlaceholders().get("votes")); + } + + @Test + void capturedCountExpandsOneAnySiteCommandAndWaitsBeforeCommitting149MixedVotes() { + AsyncReplayFixture fixture = asyncFixture(); + doReturn(true).when(fixture.user).isOnline(); + ArrayList captured = new ArrayList<>(); + for (int i = 0; i < 149; i++) captured.add(i % 2 == 0 ? "Site1" : "Site2"); + fixture.queued.set(captured); + when(fixture.plugin.getVoteSiteManager().resolveVoteSite("Site2", true)).thenReturn(fixture.site); + RewardHandler handler = fixture.plugin.getRewardHandler(); + AtomicReference options = new AtomicReference<>(); + ArrayList dispatched = new ArrayList<>(); + doAnswer(call -> { + options.set(call.getArgument(3)); + dispatched.add(com.bencodez.advancedcore.api.messages.PlaceholderUtils.replacePlaceHolder( + "crate give %votes%", options.get().getPlaceholders())); + return fixture.anySiteRewards; + }).when(handler).giveRewardAsync(eq(fixture.user), + org.mockito.ArgumentMatchers.any(YamlConfiguration.class), eq("AnySiteRewards"), + org.mockito.ArgumentMatchers.any(RewardOptions.class)); + CompletableFuture result = fixture.user.offVoteWithCapturedTopVoterIgnoreAsync(false).toCompletableFuture(); + assertEquals(List.of("crate give 149"), dispatched); + assertEquals(captured, fixture.pending.get()); + assertFalse(result.isDone()); + verify(fixture.site, never()).giveRewardsAsync(fixture.user, false, false); + ArrayList withArrival = new ArrayList<>(captured); + withArrival.add("Site1"); + fixture.queued.set(withArrival); + assertEquals("149", options.get().getPlaceholders().get("votes")); + fixture.anySiteRewards.complete(null); + result.join(); + assertEquals(List.of("Site1"), fixture.queued.get()); + assertTrue(fixture.pending.get().isEmpty()); + verify(fixture.site, org.mockito.Mockito.times(149)).giveRewardsAsync(fixture.user, false, false); + } + + @Test + void immediateAnySiteCountIsOneWithoutChangingOnlineFlag() { + AsyncReplayFixture fixture = asyncFixture(); + fixture.user.sendVoteEffects(true); + org.mockito.ArgumentCaptor options = org.mockito.ArgumentCaptor.forClass(RewardOptions.class); + verify(fixture.plugin.getRewardHandler()).giveReward(eq(fixture.user), + org.mockito.ArgumentMatchers.any(YamlConfiguration.class), eq("AnySiteRewards"), options.capture()); + assertEquals("1", options.getValue().getPlaceholders().get("votes")); + assertTrue(options.getValue().isOnline()); + } + + @Test + void emptyQueueDoesNotEvaluateAnySite() { + AsyncReplayFixture fixture = asyncFixture(); + fixture.queued.set(new ArrayList<>()); + fixture.user.offVoteWithCapturedTopVoterIgnoreAsync(false).toCompletableFuture().join(); + verify(fixture.plugin.getRewardHandler(), never()).giveRewardAsync(eq(fixture.user), + org.mockito.ArgumentMatchers.any(YamlConfiguration.class), eq("AnySiteRewards"), + org.mockito.ArgumentMatchers.any(RewardOptions.class)); + } + @Test void legacyEntryRetainsQueueAndPendingMarkerWhenRewardDeliveryFails() { AsyncReplayFixture fixture = asyncFixture(); diff --git a/docs/anysite-vote-count.md b/docs/anysite-vote-count.md new file mode 100644 index 000000000..9edb0a1d9 --- /dev/null +++ b/docs/anysite-vote-count.md @@ -0,0 +1,26 @@ +# AnySite reward vote count + +In Bukkit/Paper/Folia AnySite rewards, `%votes%` is the number of vote entries represented by that invocation: + +- An ordinary immediate AnySite invocation receives `1`. +- A nonempty queued offline-vote batch receives the size of its captured queue, across all sites. The player receiving this batch may now be online. Later arrivals belong to a subsequent batch. +- Empty queues do not invoke AnySite. Grouped immediate vote processing retains its existing rule of invoking AnySite only when `voteNumber == 1`; this placeholder does not change that rule. + +For example, configure an explicitly supported quantity command in `SpecialRewards.yml`: + +```yaml +AnySiteRewards: + Commands: + Console: + - 'crazycrates give virtual Vote %votes% %player%' +``` + +Confirm the command syntax and quantity limits for your installed recipient plugin. With 149 captured entries, this requests 149 keys through one command. It does not automatically combine other commands or verify that the recipient plugin delivered or persisted the keys. + +Move the equivalent uniform grant out of `EverySiteReward` and individual site rewards to avoid duplicate grants. Existing configurations and per-site commands remain unchanged unless you explicitly edit them. + +AnySite conditions and random rolls still evaluate once for that AnySite invocation, not once per captured vote. Its count is the captured input, not the number of condition-qualified rewards, successful commands or votes at a particular site. Even an entry whose site no longer resolves is included. Keep site-specific and independently randomized rewards in their existing per-vote paths. + +The queued AnySite reward still finishes before the sequential site rewards. The queue is committed only after the existing asynchronous reward chain completes. A denied/cancelled AnySite reward may complete without granting anything, under existing reward rules. Failed or ambiguous delivery retains the existing pending recovery fence; this placeholder adds no automatic retry or recovery guarantee. A crash after a command effect but before completion storage can still require audited reconciliation. + +The value is local to the reward options for this invocation; it is not a live queue lookup or a global PlaceholderAPI vote total. Existing top-voter, VoteParty and GUI uses of `%votes%` retain their own meanings. No claim is made for separate proxy/native-loader reward engines or external calls constructing unrelated reward options.