Skip to content

Commit

Permalink
feat(command): Implement CommandSender
Browse files Browse the repository at this point in the history
  • Loading branch information
phinner committed Mar 16, 2024
1 parent dcfa735 commit a14be64
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,47 +22,17 @@

public interface CommandSender {

default void sendWarning(final String text) {}

default void sendMessage(final String text) {}

static CommandSender player(final Player player) {
return new CommandSender() {
@Override
public boolean isPlayer() {
return true;
}

@Override
public boolean isServer() {
return false;
}

@Override
public Player getPlayer() {
throw new UnsupportedOperationException();
}
};
return new PlayerCommandSender(player);
}

static CommandSender server() {
return new CommandSender() {
@Override
public boolean isPlayer() {
return false;
}
return ServerCommandSender.INSTANCE;
}

@Override
public boolean isServer() {
return true;
}
void sendWarning(final String text);

@Override
public Player getPlayer() {
throw new UnsupportedOperationException();
}
};
}
void sendMessage(final String text);

boolean isPlayer();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Distributor, a feature-rich framework for Mindustry plugins.
*
* Copyright (C) 2024 Xpdustry
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.xpdustry.distributor.core.command;

import mindustry.gen.Player;

record PlayerCommandSender(Player player) implements CommandSender {

@Override
public void sendMessage(final String text) {
this.player.sendMessage(text);
}

@Override
public void sendWarning(final String text) {
this.player.sendMessage("[red]" + text);
}

@Override
public boolean isPlayer() {
return true;
}

@Override
public boolean isServer() {
return false;
}

@Override
public Player getPlayer() {
return this.player;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Distributor, a feature-rich framework for Mindustry plugins.
*
* Copyright (C) 2024 Xpdustry
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.xpdustry.distributor.core.command;

import arc.util.Log;
import mindustry.gen.Player;

final class ServerCommandSender implements CommandSender {

static final ServerCommandSender INSTANCE = new ServerCommandSender();

private ServerCommandSender() {}

@Override
public void sendMessage(final String text) {
for (final var line : text.split("\n", -1)) {
Log.info(line);
}
}

@Override
public void sendWarning(final String text) {
for (final var line : text.split("\n", -1)) {
Log.warn(line);
}
}

@Override
public boolean isPlayer() {
return false;
}

@Override
public boolean isServer() {
return true;
}

@Override
public Player getPlayer() {
throw new UnsupportedOperationException();
}
}

0 comments on commit a14be64

Please sign in to comment.