Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I use this on my java game? #85

Open
yeppiidev opened this issue Mar 3, 2021 · 9 comments
Open

How can I use this on my java game? #85

yeppiidev opened this issue Mar 3, 2021 · 9 comments

Comments

@yeppiidev
Copy link

I am making a 3D game using Java and I would like to use this on my game. But how can I do this?

@portlek
Copy link

portlek commented Mar 3, 2021

create a dispatcher with a custom command sender class.
use register method in the dispatcher class to register commands.
use execute method in the dispatcher class to execute registered commands.

@yeppiidev
Copy link
Author

Any example code?

@portlek
Copy link

portlek commented Mar 16, 2021

public interface CommandSender {

  void sendMessage(String message);
}
public final class App {

  private final CommandDispatcher<CommandSender> dispatcher = new CommandDispatcher<>();

  public static void main(final String[] args) {
    new App().exec();
  }

  public void exec() {
    this.dispatcher.register(LiteralArgumentBuilder.literal("testCommand")
      .executes(context -> {
        context.getSource().sendMessage("xxx");
        return 0;
      }));
      final var client = new Client(this.dispatcher);
      client.start();
  }
}
public final class Client implements CommandSender {

  private final CommandDispatcher<CommandSender> dispatcher;

  public Client(final CommandDispatcher<CommandSender> dispatcher) {
    this.dispatcher = dispatcher;
  }

  @Overrider
  public void sendMessage(final String message) {
    System.out.println(message);
  }

  public void start() {
    // Start client, initiate other things etc.
  }

  public int runCommand(final String command) {
    return this.dispatcher.execute(command, this);
  }
}

@yeppiidev
Copy link
Author

@portlek ok cool. I'll try it. If there is no problem with that code, I will close this issue.

@portlek
Copy link

portlek commented Mar 16, 2021

actually, it works or not, you should understand the concept.

@yeppiidev
Copy link
Author

@portlek okay

@yeppiidev
Copy link
Author

I did what you said and now I get this CommandSyntaxException
(For some context, I'm using this on the multiplayer server of my main game)

image

@yeppiidev yeppiidev reopened this Apr 20, 2021
@portlek
Copy link

portlek commented Apr 20, 2021

probably you didn't register command or typing the wrong command, I can't say anything without any single code tho .-.

@ghost
Copy link

ghost commented Aug 3, 2021

Try and catch, see what in the stacktrace is causing it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants