Skip to content
Reina edited this page Jan 2, 2019 · 32 revisions

Subscription Model

BaseMod uses a subscription model to handle hooks. If you want to receive a specific hook you subscribeTo it and if you want to stop receiving the event you unsubscribeFrom it.

New Subscription Example

  • BaseMod.subscribe(this) where this is an instance of some subscriber, this method will subscribe this to all hooks it implements subscribers for.
  • BaseMod.unsubscribe(this) where this is an instance of some subscriber, this method will unsubscribe this from all hooks it implements subscribers for.
  • BaseMod.subscribe(this, Class<? extends ISubscriber> toAddClass) where this is an instance of some subscriber, this method will subscribe this to only the toAddClass hook.
  • BaseMod.unsubscribe(this, Class<? extends ISubscriber> toRemoveClass) where this is an instance of some subscriber, this method will unsubscribe this from only the toRemoveClass hook.

Old Subscription Example (Deprecated API)

  • BaseMod.subscribeTo...(this)
  • BaseMod.unsubscribeFrom...(this)

UnsubscribeLater

Sometimes you need to unsubscribe from an event after it's triggered once. Maybe you only want to listen for the first time a card is played or the first time a dungeon is set up. In that case you're going to want to unsubscribe from a hook in the callback method for the hook. However if you were to directly call BaseMod.unsubscribe in your receiveSomeReallyCoolEvent method you will throw a ConcurrentModificationException which is what happens in Java when you try and modify a list at the same time it is being iterated over. To avoid this exception you need to use BaseMod.unsubscribeLater which will register your subscriber to be unsubscribed after the bit of code that could throw the exception.

List of Hooks

This is NOT A COMPLETE LIST of hooks. Check src/main/java/basemod/interfaces for a complete list.

Cards

  • PostDrawSubscriber - After a card is drawn.
  • PostExhaustSubscriber - After a card is exhausted.
  • OnCardUseSubscriber - Directly after a card is used (can be used to add additional functionality to cards on use).

Energy

  • PostEnergyRechargeSubscriber - At the start of every player turn, after energy has recharged.

Rendering

  • PreRenderSubscriber
  • ModelRenderSubscriber - Used in conjunction with receiveCameraRender.
  • PreRoomRenderSubscriber - Under the player and monsters, over the background.
  • RenderSubscriber - Under tips and the cursor, above everything else.
  • PostRenderSubscriber - Above everything.

Gameplay

  • PostInitializeSubscriber - One time only, at the end of CardCrawlGame.initialize().
  • PostDungeonInitializeSubscriber - After dungeon initialization completes.
  • StartActSubscriber - After a new act is started - only occurs when the player transitions between acts OR starts a new game and begins the Exordium act.
  • PostCampfireSubscriber - After a campfire action is performed. Returning false will allow another action to be performed.
  • PreStartGameSubscriber - When starting a new game or continuing, before generating/loading the player.
  • StartGameSubscriber - When starting a new game or continuing, after generating/loading the player and before dungeon generation.
  • PreUpdateSubscriber - Immediately after input is read.
  • PostUpdateSubscriber - Immediately before input is disposed.
  • PreDungeonUpdateSubscriber - Called before anything happens in AbstractDungeon.update().
  • PostDungeonUpdateSubscriber - Called after everything happens in AbstractDungeon.update().
  • PrePlayerUpdateSubscriber - Called before anything happens in AbstractPlayer.update().
  • PostPlayerUpdateSubscriber - Called after everything happens in AbstractPlayer.update().
  • PostBattleSubscriber - Called after all of the logic is completed in AbstractRoom.endBattle().
  • OnPowersModifiedSubscriber - Called after AbstractDungeon.onModifyPower().
  • PostPowerApplySubscriber
  • MaxHPChangeSubscriber - Immediately before maxHealth is changed for AbstractCreature, allows you to change the amount of hp changed.
  • PostEnergyRechargeSubscriber

Starting Deck / Relics

  • PostCreateStartingDeckSubscriber - Immediately after the character's starting deck is created. Returning true will remove all the cards from the default starting deck. Add the cards you want to be in the starting deck to cardsToAdd.
  • PostCreateStartingRelicsSubscriber - Immediately after the character's starting relics are created. Returning true will remove all the default relics from the player. Add the relics you want to be in the starting deck to relicsToAdd.
  • RelicGetSubscriber

Shop

  • PostCreateShopRelicSubscriber - Immediately after the shop generates its relics. Modifying relics will change the relics in the shop. screenInstance is an instance of the ShopScreen.
  • PostCreateShopPotionSubscriber - Immediately after the shop generates its potions. Modifying potions will change the potions in the shop. screenInstance is an instance of the ShopScreen.

Potions

  • PrePotionUseSubscriber - Immediately before a potion is used.
  • PostPotionUseSubscriber - Immediately after a potion is used.
  • PotionGetSubscriber - When the player gets a potion.

Custom Content

  • EditCardsSubscriber - When you should register any cards to add or remove with BaseMod.addCard and BaseMod.removeCard. Do NOT initialize any cards or register any cards to add or remove outside of this handler. Slay the Spire needs some things to be done in certain orders and this handler ensures that happens correctly. Note that removing any cards involved in game events is undefined behavior currently.
  • EditRelicsSubscriber - When you should register any relics to add or remove with BaseMod.addRelic and BaseMod.removeRelic. Do NOT initialize any relics or register any relics to add or remove outside of this handler. Slay the Spire needs some things to be done in certain orders and this handler ensures that happens correctly. Note that removing any relics involved in game events is undefined behavior currently.
  • EditCharactersSubscriber - When you should register any characters to add or remove with BaseMod.addCharacter and BaseMod.removeCharacter. Do NOT initialize any characters or register any relics to add or remove outside of this handler. Slay the Spire needs some things to be done in certain orders and this handler ensures that happens correctly. Note that removing the default characters IS NOT supported at this time.
  • SetUnlocksSubscriber - When you should register any custom unlocks. Note that removing any unlocks that exist in the base game won't work (it shouldn't crash but it won't do anything). Do NOT set up any custom unlocks outside of this handler. Slay The Spire needs some things to be done in certain orders and this handler ensures that happens correctly.

Example

Here is a simple example that shows how you would go about doing something when the player draws a card. For example purposes all that is done is that it logs the card name to the console.

@SpireInitializer
public class MyMod implements PostDrawSubscriber {
	
        public MyMod() {
                BaseMod.subscribe(this);
        }

        public static void initialize() {
                MyMod mod = new MyMod();
        }

        @Override
	public void receivePostDraw(AbstractCard card) {
		logger.info("card ID: " + card.cardID + " was drawn!");
	}

}

Clone this wiki locally