So I am trying to create a little mod, a block that will act like a server console inside the game, but I don’t want to use Rcon for this. I am stuck on the CommandSource interface, there are too many unexplained functions in there like getContainingCollection() or getSubjectData(). I tried to find some examples inside the sponge code but as you can guess, I didn’t learn anything from there. How can I create my own command sender class? is CommandSource the right interface to implement?
A command source is something like a player or the console or a command block, so it could be the right way to go. Try implementing or extending the command.block cs
getContainingCollection() and getSubjectData() are from the Subject interface set, and deal with permissions handling. If you want to use a console sender, Sponge has a helper method to get that sender; you can just map events to fire through that sender rather than writing your own. I assume you want the block to fire commands when clicked, so in your interaction event listener, you would simply get the block, get the mapped command(s), then run game.getCommandManager(),process(game.getServer().getConsole(), cmd).
What if I don’t want the command output to go to the server console but instead get saved in my block logs? That’s why I thought that implementing my own command sender interface would be a better idea.
I think for that you would save to file using the Asset api I believe:
That wasn’t the question.
You’re absolutely right that it would be a good way to do it. Unfortunately there isn’t much support in the API for it, especially regarding getting yourself registered to be in the proper permission groups.
So looks like the only thing I can do is to figure it out myself
You might want to implement the interface and then return the
getSubjectData() (and other methods you don’t need to use) from the sender type you are trying to use. For example, you could have a
WrappedConsoleSource, which holds the
ConsoleSource object the game is using and implements
CommandSource: you would then override the methods you want to replace with your own functionality and pass the ones you don’t care about to
ConsoleSource. For simply logging to a file, simply replacing the
sendMessage method(s) would suffice.
To use the console to send commands from blocks, you could use what I said earlier with the event listener, and then get your
WrappedConsoleSource as your
CommandSource parameter in the
Ideally you wouldn’t need to implement your own command source because registration, permissions and other complications make it challenging, however wrapping it like this would function well enough as it emulates an already existing source, eliminating the need for registration and the like.