Skip to content

Developer API

Nick DeGruccio edited this page Nov 4, 2020 · 6 revisions

The GTS API acts as a service, in that not only is it accessible via the API itself, but also from the respective platforms service registry. The API itself is rather simple to understand, but details regarding it will be presented below.

Hooking into the Service

With GTS 6.0.0, the service is now available following initial plugin load. In Sponge, this would be the GamePreInitializationEvent. Likewise, on BungeeCord/Waterfall, this would be during onLoad().

There will always be at least one way of connecting with the API. First, and likely the chosen method for many, will simply be to request the service from the service itself. While the service is an interface, it has a static method responsible for maintaining the implementation of the API. With this in mind, this method of retrieving the API is as simple as this:

GTSService service = GTSService.getInstance();

The second method of obtaining the API service will always be via the server platforms respective service registry. With Sponge, this is possible via their ServiceManager. An example of that looks like such:

GTSService service = Sponge.getServiceManager().provideUnchecked(GTSService.class);

Note: The above example assumes the API is registered. If you are ever unsure, use provide rather than provideUnchecked.

Registering Your Own Entry/Price

With the Service now established, you can add your own types of entries and prices via this service. For this example, we will look into how to register your own Entry. Simply create a class which extends the abstract Entry class. To help aid the Entry parent class, pass the type of object your entry will be as a generic. With this settled, you will then be able to have your entry read its type properly. Within GTS 4.0, the command system of the API will be replaced by a UI service. Until then, all extending entries must declare a subcommand for the /gts sell command. This is not the preferred strategy to handle this, but due to time constraints, the UI concept was pushed back to 4.0.

Once your class is created, you may then register it with the GTS Service. Simply call the service (lets assume we called it gts), and run either the registerEntry or registerEntries methods. An example can be seen below:

MainClass.getInstance().gts.registerEntry(MyCustomEntry.class);

If you have more than one entry, I highly recommend using the registerEntries method, to help prevent against redundant method calling.

Adding a Minimum Price Option

If an entry extends the Minable interface, then you have the option to add additional min price options to a supporting class. For instance, the default PokemonEntry class is the only default class that extends Minable as of 3.8.0. To add an additional option to that class, you'd call the service method like such:

MainClass.getInstance().gts.addMinPriceOption(PokemonEntry.class, element -> <function>);

This function uses the element typing, and returns a Price to be used for calculation.

Adding a Token

Finally, we have the TokenService. Via the GTS Service, you can add your own Nucleus Tokens right through GTS. To add a token, you can use the addToken method to allow Nucleus to be able to parse your own tokens. The Token class holds a builder for you to easily create your own tokens with named help. These methods are known as key and translator. The translator is where you define how your token is handled, and the key is just simply what is required for it to parse.

Clone this wiki locally