Skip to content

Commit

Permalink
(API 8) Minor updates (#982)
Browse files Browse the repository at this point in the history
* Minor updates

* Small title change

* Fixed typo
  • Loading branch information
mosemister authored Aug 31, 2023
1 parent cca4196 commit c54138d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 8 deletions.
12 changes: 9 additions & 3 deletions source/plugin/commands/commandbuilding.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Building a Command
org.spongepowered.api.command.exception.ArgumentParseException
org.spongepowered.api.event.lifecycle.RegisterCommandEvent
org.spongepowered.api.event.EventContextKeys
org.spongepowered.api.world.Locatable
org.spongepowered.plugin.PluginContainer
net.kyori.adventure.text.Component

Expand Down Expand Up @@ -83,7 +84,7 @@ a target player on the command cause.
.executor(new HelloWorldCommand())
.permission("myplugin.command.helloWorld")
.shortDescription(Component.text("Hello World Command"))
.executionRequirements(context -> context.cause().context().get(EventContextKeys.PLAYER).isPresent())
.executionRequirements(context -> context.cause().root() instanceof ServerPlayer)
.build();
}
Expand All @@ -98,7 +99,7 @@ a target player on the command cause.
.. tip::

Often times command are put in as player-only as they require the location in the world the command was
executed from. Best practise would be to check for the target location using :javadoc:`EventContextKeys#LOCATION`
executed from. Best practise would be to check if the root of the command is :javadoc:`Locatable`
instead of the player as this would allow command blocks to run the command without specifying a player.

Writing a Command Executor
Expand Down Expand Up @@ -187,6 +188,11 @@ Commands are registered on a :javadoc:`RegisterCommandEvent`. The event takes a
that is being registered to it. To register a command, the
:javadoc:`RegisterCommandEvent#register(PluginContainer, C, String, String...)` method needs to be invoked.

In the following example, we register two commands. ``helloworld`` which is from the building a simple command example
above, as well as a raw command which is explained in :doc:`rawcommand`. These two commands need to be registered in different listeners
as they are different command types, however in most cases a single listener will suffice as all commands in a single plugin
are typically of the same type.

.. code-block:: java
@Inject
Expand All @@ -200,4 +206,4 @@ that is being registered to it. To register a command, the
@Listener
public void onRegisterRawCommands(final RegisterCommandEvent<Command.Raw> event){
event.register(this.container, new MyRawCommand(), "raw");
}
}
46 changes: 41 additions & 5 deletions source/plugin/commands/commandmanager.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,56 @@ The :javadoc:`CommandManager` stands as the manager for watching what commands g
to the right command handler. To register your commands, use the :javadoc:`RegisterCommandEvent` as shown on
:doc:`./commandbuilding`.

The ``CommandManager`` can also be used to call a command programmatically.
Command Execution
~~~~~~~~~~~~~~~~~

When executing a command Sponge splits the sender into three categories.

The first category is the ``permissions subject`` which is the target to
check permissions for. The second cateogory is the ``audience`` which is
the target for all messages to be sent to. With the last category being
the actual ``sender`` who is the object that sent the command.

.. warning::

When executing a command programticly, you need to specifiy the permissions
subject and the audience but not the sender as by default the sender will be
the permissions manager.

The command sender can be changed by pushing the new sender to the root of the
cause stack.


Sending a Command As a Player
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code-block:: java
ServerPlayer player;
Sponge.server().causeStackManager().pushCause(player);
CommandManager cmdManager = Sponge.server().commandManager();
cmdManager.process(player, "msg Notch hi notch!");
You can also send a command from the server console
Sending a Command As a Console
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code-block:: java
cmdManager.process(Sponge.systemSubject(), "kill Notch");
SystemSubject console = Sponge.systemSubject();
You can also send a command as a player, but with server console permissions.
Sponge.server().causeStackManager().pushCause(console);
CommandManager cmdManager = Sponge.server().commandManager();
cmdManager.process(console, "kill Notch");
Sending a Command As a Player With Console Permissions
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code-block:: java
cmdManager.process(Sponge.systemSubject(), player, "kill Nitch");
ServerPlayer player;
SystemSubject console = Sponge.systemSubject();
Sponge.server().causeStackManager().pushCause(player);
CommandManager cmdManager = Sponge.server().commandManager();
cmdManager.process(console, player, "kill Notch");
16 changes: 16 additions & 0 deletions source/plugin/economy/implementing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Implementing the Economy API
============================

.. javadoc-import::
org.spongepowered.api.event.economy.EconomyTransactionEvent
org.spongepowered.api.service.economy.Currency
org.spongepowered.api.service.economy.EconomyService
org.spongepowered.api.service.economy.account.UniqueAccount
Expand All @@ -19,6 +20,21 @@ API, you must implement six classes:
* :javadoc:`TransferResult`
* :javadoc:`UniqueAccount`
* :javadoc:`VirtualAccount`
* :javadoc:`EconomyTransactionEvent`

Registering Your Economy Service
================================

When it comes to registering any service in Sponge, you can provide your service as an option with Sponge deciding
which service to use if requested. This means that if two Economy Service plugins are used on a server, only one
of the services will be used.

.. code-block:: java
@Listener
public void registerEconomyService(ProvideServiceEvent.EngineScoped<MyCustomEconomyService> event){
event.suggest(() -> new MyCustomEconomyService());
}
Things to consider when implementing the Economy API
====================================================
Expand Down

0 comments on commit c54138d

Please sign in to comment.