My first plugin

Hi all
After looking for more than 2 months a plugin like skript in bukkit or another way to create a command I have decided to create my first plugin on sponge but it’s not really easy for me and I can not finish it.I would create a command /bug which runs /kick @p you_can_reconnect with a new permission to give it to all player, unlike kick’s permission. Someone can help me for finish it.
the pastebin of my work: package io.github.blue.BugPlugin;import org.spongepowered.api.plugin.Plugin; - Pastebin.com
I use sponge_api 5.2.0 for minecraft 1.10.2.

I took a little pity, because it was clear that you had a shot and tried.

This is untested, made during my lunch break.

Hopefully you can use this to learn to see where you went wrong.

@Listener
public void onServerStart(GameStartedServerEvent event) {
// Hey! The server has started!
}
CommandSpec Bug = CommandSpec.builder()
    .description(Text.of("Bug Command"))
    .permission("BugPlugin.command.bug")
    .executor(new BugCommand())
    .build();

Sponge.getCommandManager.register(BugPlugin, Bug,"bug");

You needed to register the command inside the event, rather then when the class is first initialized (which I changed to preinitialization, which probably isn’t strictly necessary.)

 @Override
        public CommandResult execute(CommandSource(/kick @p you_can_reconnect) src, CommandContext args) throws CommandException {
            src.sendMessage(Text.of("successful!"));
            return CommandResult.success();
        }

Your code also didn’t compile with the CommandSource part.

A CommandSource is the person or console sending the command.

In order to send the command with higher permissions, I could have either used a proxy command source with the required permission or “cheat” and just send it as the console which is what I chose to do for simplicity.

Sponge.getCommandManager.register(BugPlugin, Bug,"bug");

Here you reference the plugin by it’s class name, when the register method needs the instance of the plugin that’s being registered. using this inside a class will pass in the instance of itself.

1 Like

Hi
thx so much to help me to understand how to create my plugin I have just one question, I created my plugin and test it on my server and when I execute /bug, in the console, I can see /kick but with no target, so I thought to change Player by PlayerOrSource in this line but eclipse does not recognize this argument

if (!(src instanceof PlayerOrSource)) throw new CommandException(Text.of(“Must be run by a player”));
Sponge.getCommandManager().process(console, "kick " + ((PlayerOrSource) src).getIdentifier());

Before anything, I strongly recommend that you review some of the core Java principles before trying to work with Sponge. You don’t seem to have a strong understanding of what you’re working with and that knowledge is going to be crucial to working with any aspect of Sponge, especially those that are more advanced.

I’d like to address a critical flaw in the logic of this command - running a selector (@p) through Console. Becuase a ConsoleSource has no location, it’s not possible to use a selector in your command. If a player executes your command, the actual /kick @p command is executed through Console. Without giving the Player permission to run /kick @p, it’s impossible to get the command to work in this manner.

I can offer two solutions, which would be handling the logistics of your selector and /kick internally or getting really creative with arguments. Personally, I would do the former.


Sponge.getCommandManager().process(console, "kick " + ((PlayerOrSource) src).getIdentifier());

I don’t mean to be disrespectful at all, but this line alone shows that you still have a lot to learn before you’re ready to dive into things. As much as myself, Ryan, or others want to help, we can’t push through Sponge code and ignore the Java semantics behind it - that’s something you have to learn first.

1 Like

Just learning how C-Style code works in general would be advisable. If it’s easier, starting with JS would also help you understand how things work without needing an IDE or anything.

I found how to do what I want. I have just add
Player player = (Player) src;
and changed
((Player) src).getIdentifier() by player.getName()
Thx all for your advice and I will learn core java principles and will continue to code with sponge
it was my first plugin and I needed it fastly for our server but now I have the time for learning correctly I hope can share with you in the future.

1 Like

Sorry, I expected .getIdentifier() to return the UUID (Which it does not) in my rush, and kick to be able to use that.

I was somewhat hesitant to use their name if I could help it, but if that works then go for it!

I hope you continue to learn.