Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. import org.spongepowered.api.Sponge;
  2. import org.spongepowered.api.command.CommandResult;
  3. import org.spongepowered.api.command.args.GenericArguments;
  4. import org.spongepowered.api.command.spec.CommandSpec;
  5. import org.spongepowered.api.event.Listener;
  6. import org.spongepowered.api.event.game.GameRegistryEvent;
  7. import org.spongepowered.api.event.game.state.GameConstructionEvent;
  8. import org.spongepowered.api.event.game.state.GameInitializationEvent;
  9. import org.spongepowered.api.event.game.state.GameStartedServerEvent;
  10. import org.spongepowered.api.plugin.Plugin;
  11. import org.spongepowered.api.text.Text;
  12.  
  13. import java.util.Optional;
  14.  
  15. @Plugin(id = "myplugin")
  16. public class MyPlugin {
  17.  
  18. @Listener
  19. public void onConstruct(GameConstructionEvent event) {
  20. // We need to register the catalog module so that Sponge knows how to retrieve our titles.
  21. // CatalogRegistryModules are the backing storage for every CatalogType registered to the GameRegistry.
  22. Sponge.getRegistry().registerModule(Title.class, new TitleCatalogModule());
  23. }
  24.  
  25. /**
  26. * NOTE:
  27. * This event is only called for your catalog types when the catalog modules that you register
  28. * implement AdditionalCatalogRegistryModule (and not just CatalogRegistryModule)
  29. */
  30. @Listener
  31. public void onRegisterTitles(GameRegistryEvent.Register<Title> event) {
  32. // Lets register some extra titles.
  33. event.register(new Title("explorer", "Explorer", () -> "[Explorer]"));
  34. }
  35.  
  36. @Listener
  37. public void onInit(GameInitializationEvent event) {
  38. // Since our Title class implements CatalogType, we get GenericArguments.catalogedElement for free!
  39. CommandSpec myDebug = CommandSpec.builder()
  40. .permission("myplugin.debug")
  41. .arguments(GenericArguments.catalogedElement(Text.of("title"), Title.class))
  42. .executor((src, args) -> {
  43. Title title = args.<Title>getOne("title").get();
  44.  
  45. src.sendMessage(Text.of("The given title is " + title.getTitleSupplier().get()));
  46.  
  47. return CommandResult.success();
  48. })
  49. .build();
  50.  
  51. Sponge.getCommandManager().register(this, myDebug, "mydebug");
  52. }
  53.  
  54. @Listener
  55. public void onStarted(GameStartedServerEvent event) {
  56. // To fetch a CatalogType (Title in our case) from the registry is as simple as:
  57. Optional<Title> ownerTitle = Sponge.getRegistry().getType(Title.class, "owner");
  58.  
  59. if (ownerTitle.isPresent()) {
  60. // owner is a registered title!
  61. } else {
  62. // we couldn't find it :(
  63. }
  64.  
  65. // To get all of our registered titles:
  66. for (Title title : Sponge.getRegistry().getAllOf(Title.class)) {
  67. String supplied = title.getTitleSupplier().get();
  68. }
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement