Pricing API

The interface can be obtained as Service using PriceCalculationService:

@Listener public void onChangeServiceProvider(ChangeServiceProviderEvent event) {
	if (event.getService().equals(PriceCalculationService.class)) {
		pricingService = (PriceCalculationService) event.getNewProvider();
	}
}

The API provides current prices with
pricingService.getCurrentPurchasePrice(ItemStackSnapshot item, int amount, BigDecimal staticPrice, @Nullable UUID shopID, @Nullable UUID playerID)

As soon as a player shows interest in items and due to the exponential nature of dynamic prices the next step would be to call
TransactionPreview preview = pricingService.getPurchaseInformation(ItemStackSnapshot item, int amount, BigDecimal staticPrice, Currency currency, @Nullable UUID shopID, @Nullable UUID playerID)
with the preview holding all price steps up to amount, and the amount of items the player can afford (to buy OR sell until they hit account limits).
The Transaction should be finished with a call to preview.confirm like
preview.confirm(preview.getAffordableAmount())
to actually update the prices within the trackers.

Once a price changes through a transaction, discrapency decay or reset a PriceChangeEvent will be emitted for plugins to update their dispalys.

Example Implementation

An example implementation of TooMuchStock as optional dependency is available in my plugin VillagerShops.

The abstraction is located in this package: https://github.com/DosMike/VillagerShops/tree/master/src/main/java/de/dosmike/sponge/vshop/integrations/toomuchstock

The dependency is initialized in GameInitializationEvent with PriceCalculator.get()

And it’s used in the buy and sell methods here:
https://github.com/DosMike/VillagerShops/blob/master/src/main/java/de/dosmike/sponge/vshop/shops/Purchase.java#L90

Please Note: You will have write / copy the abstraction package in your plugin.

As Dependency

repositories {
	...
	maven { url 'https://jitpack.io' }
}

dependencies {
	implementation 'com.github.DosMike:TooMuchStock:master-SNAPSHOT'
}

Category: Economy

Published on Mar 15, 2020

views

stars

watchers

total downloads

Promoted Versions

Members