package org.royaldev.thehumanity.commands;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.kitteh.irc.client.library.element.User;
import org.kitteh.irc.client.library.event.channel.ChannelMessageEvent;
import org.kitteh.irc.client.library.event.helper.ActorEvent;
import org.royaldev.thehumanity.TheHumanity;
import org.royaldev.thehumanity.game.TheHumanityGame;
import org.royaldev.thehumanity.player.TheHumanityPlayer;
import xyz.cardstock.cardstock.commands.CallInfo;
import xyz.cardstock.cardstock.commands.GameChannelCommand;
import java.util.List;
/**
* A command that is only meant to be used when the user is in a game.
*/
public abstract class InGameCommand extends GameChannelCommand<TheHumanityPlayer, TheHumanityGame> {
protected final TheHumanity humanity;
public InGameCommand(@NotNull final TheHumanity humanity) {
super(humanity, (channel -> humanity.getGameRegistrar().on(channel)));
this.humanity = humanity;
}
/**
* Checks to see if the given Player is the host or a channel operator in the given TheHumanityGame.
*
* @param p Player to check
* @param g TheHumanityGame to check
* @return true if host or chanop, false if not
*/
@Contract("null, _ -> false; _, null -> false; !null, !null -> _")
public boolean isHostOrOp(final TheHumanityPlayer p, final TheHumanityGame g) {
return !(p == null || g == null) && (g.getHost().equals(p) || this.humanity.hasChannelMode(g.getChannel(), p.getUser(), 'o'));
}
@Override
public void run(@NotNull final ActorEvent<User> event, @NotNull final CallInfo callInfo, @NotNull final List<String> arguments) {
if (!(event instanceof ChannelMessageEvent)) return;
final TheHumanityGame g = this.humanity.getGameRegistrar().find(((ChannelMessageEvent) event).getChannel());
final TheHumanityPlayer p;
if (g == null || (p = g.getPlayer(event.getActor())) == null) {
event.getActor().sendNotice("You're not in a game.");
return;
}
this.run((ChannelMessageEvent) event, callInfo, g, p, arguments);
}
}