Hub / src / main / java / com / lifeknight / relaymchub / commands / CustomMapCommand.java
CustomMapCommand.java
Raw
package com.lifeknight.relaymchub.commands;

import com.lifeknight.relaymchub.player.HubPlayer;
import com.lifeknight.relaymcutils.player.SmartPlayer;
import com.lifeknight.relaymcutils.player.commands.CommandUtilities;
import com.lifeknight.relayutils.basic.Miscellaneous;
import com.lifeknight.relayutils.basic.Text;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;

import java.util.List;

public class CustomMapCommand implements CommandExecutor, TabExecutor {

    private static final List<String> COMMANDS = Miscellaneous.getList("invite", "join", "kick", "leave");

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!(sender instanceof Player) || !command.getName().equalsIgnoreCase("custommap")) {
            return true;
        }

        HubPlayer hubPlayer = HubPlayer.getHubPlayer((Player) sender);

        if (args.length > 1) {
            HubPlayer player = HubPlayer.getHubPlayer(SmartPlayer.getPlayerOrNick(args[1]));
            if (player == null) {
                CommandUtilities.noPlayerFound(sender, args[1]);
            } else {
                switch (COMMANDS.indexOf(args[0].toLowerCase())) {
                    case 0:
                        this.tryInvite(hubPlayer, player);
                        break;
                    case 1:
                        this.tryJoin(hubPlayer, player);
                        break;
                    case 2:
                        this.tryKick(hubPlayer, player);
                        break;
                    default:
                        CommandUtilities.sendUsageMessage(sender, "/custommap [invite|join|kick|leave] [player]");
                }
            }
        } else {
            if (args.length == 1 && COMMANDS.get(3).equalsIgnoreCase(args[0])) {
                if (hubPlayer.isCreatingCustomMap() && !hubPlayer.isCreatingOwnedCustomMap()) {
                    hubPlayer.leaveCustomMap();
                } else {
                    hubPlayer.sendErrorMessage("You can only use this command while editing someone else's map!");
                }
            } else {
                CommandUtilities.sendUsageMessage(sender, "/custommap [invite|join|kick] [player]");
            }
        }
        return true;
    }

    private void tryKick(HubPlayer hubPlayer, HubPlayer player) {
        if (hubPlayer.isCreatingOwnedCustomMap()) {
            if (hubPlayer.hasCollaborator(player)) {
                hubPlayer.kickCollaborator(player);
            } else {
                hubPlayer.sendErrorMessage("That player is not collaborating with you!");
            }
        } else {
            hubPlayer.sendErrorMessage("You must be the owner of a custom map you are creating to use this command!");
        }
    }

    private void tryJoin(HubPlayer hubPlayer, HubPlayer toJoin) {
        if (hubPlayer.isCreatingCustomMap()) {
            hubPlayer.sendErrorMessage("You are already editing a custom map!");
        } else {
            if (hubPlayer.collaborationInvites.contains(toJoin) || hubPlayer.isAdministrator()) {
                hubPlayer.collaborate(toJoin);
            } else {
                hubPlayer.sendErrorMessage("That player has not invited you to collaborate, or your time to accept has expired!");
            }
        }
    }

    private void tryInvite(HubPlayer hubPlayer, HubPlayer player) {
        if (hubPlayer.isCreatingOwnedCustomMap()) {
            if (hubPlayer.hasCollaborator(player)) {
                hubPlayer.sendErrorMessage("You are already collaborating with that player!");
            } else {
                hubPlayer.inviteCollaborator(player);
            }
        } else {
            hubPlayer.sendErrorMessage("You must be the owner of a custom map you are creating to use this command!");
        }
    }

    @Override
    public List<String> onTabComplete(CommandSender commandSender, Command command, String s, String[] args) {
        if (args.length > 1) {
            return Text.returnStartingEntries(Miscellaneous.processList(HubPlayer.getOnlineHubPlayers(), HubPlayer::getName), args[1], true);
        }

        return Text.returnStartingEntries(COMMANDS, args.length > 0 ? args[0] : null, true);
    }
}