Skip to content

Add cancellable GeneratorPreBuyEvent (#137) - #153

Merged
tastybento merged 2 commits into
developfrom
137-generator-prebuy-event
Jul 4, 2026
Merged

Add cancellable GeneratorPreBuyEvent (#137)#153
tastybento merged 2 commits into
developfrom
137-generator-prebuy-event

Conversation

@tastybento

Copy link
Copy Markdown
Member

Closes #137

Problem

The existing GeneratorBuyEvent is fired after a purchase has completed, so listeners cannot use it to veto a purchase. Servers/plugins that want to add their own requirements to the buying process have no hook.

Change

Adds a new cancellable GeneratorPreBuyEvent, fired at the very start of StoneGeneratorManager.purchaseGenerator(...) — before any money is withdrawn and before the existing GeneratorBuyEvent fires. If a listener cancels it, the purchase is aborted (no withdrawal, no tier granted, no post-buy event).

The existing GeneratorBuyEvent is unchanged, so this is fully backwards compatible.

API

@EventHandler
public void onPreBuy(GeneratorPreBuyEvent e) {
    if (someCustomRequirementNotMet(e.getTargetPlayer(), e.getGeneratorID())) {
        e.setCancelled(true);
    }
}

Tests

  • GeneratorPreBuyEventTest — field population, cancellable contract, handlers.
  • StoneGeneratorManagerTest:
    • testPurchaseGeneratorFiresPreBuyEvent — event is fired on purchase.
    • testPurchaseGeneratorCancelledPreBuyEventStopsPurchase — cancelling aborts the purchase (tier not added, GeneratorBuyEvent not fired).

All 58 tests in the affected classes pass.

🤖 Generated with Claude Code

tastybento and others added 2 commits July 3, 2026 18:53
The existing GeneratorBuyEvent fires after a purchase completes, so it
cannot be used to veto a purchase. Add a new cancellable
GeneratorPreBuyEvent fired at the start of
StoneGeneratorManager.purchaseGenerator, before any money is withdrawn
and before GeneratorBuyEvent is fired. Cancelling it aborts the purchase.

This lets other plugins add their own requirements to the generator
purchasing process.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01D7NWPeGXmsUJnnX42X24Rd
SonarCloud flagged the new GeneratorPreBuyEvent as duplicated code: it
repeated the targetPlayer/islandUUID/generator/generatorID fields and
accessors that GeneratorBuyEvent, GeneratorActivationEvent and
GeneratorUnlockEvent already duplicated among themselves.

Introduce an abstract GeneratorEvent base holding those common fields and
their getters/setters, and have all four events extend it. The change is
backwards compatible: the public accessors are simply inherited, so
external listeners are unaffected. Each concrete event keeps its own
HandlerList, as Bukkit requires for correct per-type routing.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01D7NWPeGXmsUJnnX42X24Rd
@sonarqubecloud

sonarqubecloud Bot commented Jul 4, 2026

Copy link
Copy Markdown

@tastybento
tastybento merged commit 4a930e3 into develop Jul 4, 2026
4 checks passed
@tastybento
tastybento deleted the 137-generator-prebuy-event branch July 4, 2026 18:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Extend GeneratorBuyEvent to cancellable

1 participant