Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.spongepowered.api.Sponge;
- import org.spongepowered.api.command.CommandResult;
- import org.spongepowered.api.command.args.GenericArguments;
- import org.spongepowered.api.command.spec.CommandSpec;
- import org.spongepowered.api.event.Listener;
- import org.spongepowered.api.event.game.GameRegistryEvent;
- import org.spongepowered.api.event.game.state.GameConstructionEvent;
- import org.spongepowered.api.event.game.state.GameInitializationEvent;
- import org.spongepowered.api.event.game.state.GameStartedServerEvent;
- import org.spongepowered.api.plugin.Plugin;
- import org.spongepowered.api.text.Text;
- import java.util.Optional;
- @Plugin(id = "myplugin")
- public class MyPlugin {
- @Listener
- public void onConstruct(GameConstructionEvent event) {
- // We need to register the catalog module so that Sponge knows how to retrieve our titles.
- // CatalogRegistryModules are the backing storage for every CatalogType registered to the GameRegistry.
- Sponge.getRegistry().registerModule(Title.class, new TitleCatalogModule());
- }
- /**
- * NOTE:
- * This event is only called for your catalog types when the catalog modules that you register
- * implement AdditionalCatalogRegistryModule (and not just CatalogRegistryModule)
- */
- @Listener
- public void onRegisterTitles(GameRegistryEvent.Register<Title> event) {
- // Lets register some extra titles.
- event.register(new Title("explorer", "Explorer", () -> "[Explorer]"));
- }
- @Listener
- public void onInit(GameInitializationEvent event) {
- // Since our Title class implements CatalogType, we get GenericArguments.catalogedElement for free!
- CommandSpec myDebug = CommandSpec.builder()
- .permission("myplugin.debug")
- .arguments(GenericArguments.catalogedElement(Text.of("title"), Title.class))
- .executor((src, args) -> {
- Title title = args.<Title>getOne("title").get();
- src.sendMessage(Text.of("The given title is " + title.getTitleSupplier().get()));
- return CommandResult.success();
- })
- .build();
- Sponge.getCommandManager().register(this, myDebug, "mydebug");
- }
- @Listener
- public void onStarted(GameStartedServerEvent event) {
- // To fetch a CatalogType (Title in our case) from the registry is as simple as:
- Optional<Title> ownerTitle = Sponge.getRegistry().getType(Title.class, "owner");
- if (ownerTitle.isPresent()) {
- // owner is a registered title!
- } else {
- // we couldn't find it :(
- }
- // To get all of our registered titles:
- for (Title title : Sponge.getRegistry().getAllOf(Title.class)) {
- String supplied = title.getTitleSupplier().get();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement