Search in sources :

Example 1 with GuildAcceptAllyInvitationEvent

use of net.dzikoysk.funnyguilds.event.guild.ally.GuildAcceptAllyInvitationEvent in project FunnyGuilds by FunnyGuilds.

the class ExcAlly method execute.

@Override
public void execute(CommandSender sender, String[] args) {
    MessagesConfig messages = Messages.getInstance();
    Player player = (Player) sender;
    User user = User.get(player);
    if (!user.hasGuild()) {
        player.sendMessage(messages.generalHasNoGuild);
        return;
    }
    if (!user.isOwner()) {
        player.sendMessage(messages.generalIsNotOwner);
        return;
    }
    Guild guild = user.getGuild();
    List<InvitationList.Invitation> invitations = InvitationList.getInvitationsFor(guild);
    if (args.length < 1) {
        if (invitations.size() == 0) {
            player.sendMessage(messages.allyHasNotInvitation);
            return;
        }
        List<String> list = messages.allyInvitationList;
        String guildNames = StringUtils.toString(InvitationList.getInvitationGuildNames(guild), false);
        for (String msg : list) {
            player.sendMessage(msg.replace("{GUILDS}", guildNames));
        }
        return;
    }
    String tag = args[0];
    if (!GuildUtils.tagExists(tag)) {
        player.sendMessage(StringUtils.replace(messages.generalGuildNotExists, "{TAG}", tag));
        return;
    }
    Guild invitedGuild = GuildUtils.getByTag(tag);
    if (guild.equals(invitedGuild)) {
        player.sendMessage(messages.allySame);
        return;
    }
    if (guild.getAllies().contains(invitedGuild)) {
        player.sendMessage(messages.allyAlly);
        return;
    }
    if (InvitationList.hasInvitationFrom(guild, invitedGuild)) {
        if (!SimpleEventHandler.handle(new GuildAcceptAllyInvitationEvent(EventCause.USER, user, guild, invitedGuild))) {
            return;
        }
        InvitationList.expireInvitation(invitedGuild, guild);
        guild.addAlly(invitedGuild);
        invitedGuild.addAlly(guild);
        String allyDoneMessage = messages.allyDone;
        allyDoneMessage = StringUtils.replace(allyDoneMessage, "{GUILD}", invitedGuild.getName());
        allyDoneMessage = StringUtils.replace(allyDoneMessage, "{TAG}", invitedGuild.getTag());
        player.sendMessage(allyDoneMessage);
        Player owner = invitedGuild.getOwner().getPlayer();
        if (owner != null) {
            String allyIDoneMessage = messages.allyIDone;
            allyIDoneMessage = StringUtils.replace(allyIDoneMessage, "{GUILD}", guild.getName());
            allyIDoneMessage = StringUtils.replace(allyIDoneMessage, "{TAG}", guild.getTag());
            owner.sendMessage(allyIDoneMessage);
        }
        ConcurrencyManager concurrencyManager = FunnyGuilds.getInstance().getConcurrencyManager();
        ConcurrencyTaskBuilder taskBuilder = ConcurrencyTask.builder();
        for (User member : guild.getMembers()) {
            // IndependentThread.action(ActionType.PREFIX_UPDATE_GUILD, member, invitedGuild);
            taskBuilder.delegate(new PrefixUpdateGuildRequest(member, invitedGuild));
        }
        for (User member : invitedGuild.getMembers()) {
            // IndependentThread.action(ActionType.PREFIX_UPDATE_GUILD, member, guild);
            taskBuilder.delegate(new PrefixUpdateGuildRequest(member, guild));
        }
        ConcurrencyTask task = taskBuilder.build();
        concurrencyManager.postTask(task);
        return;
    }
    if (InvitationList.hasInvitationFrom(invitedGuild, guild)) {
        if (!SimpleEventHandler.handle(new GuildRevokeAllyInvitationEvent(EventCause.USER, user, guild, invitedGuild))) {
            return;
        }
        InvitationList.expireInvitation(guild, invitedGuild);
        String allyReturnMessage = messages.allyReturn;
        allyReturnMessage = StringUtils.replace(allyReturnMessage, "{GUILD}", invitedGuild.getName());
        allyReturnMessage = StringUtils.replace(allyReturnMessage, "{TAG}", invitedGuild.getTag());
        player.sendMessage(allyReturnMessage);
        Player owner = invitedGuild.getOwner().getPlayer();
        if (owner != null) {
            String allyIReturnMessage = messages.allyIReturn;
            allyIReturnMessage = StringUtils.replace(allyIReturnMessage, "{GUILD}", guild.getName());
            allyIReturnMessage = StringUtils.replace(allyIReturnMessage, "{TAG}", guild.getTag());
            owner.sendMessage(allyIReturnMessage);
        }
        return;
    }
    if (!SimpleEventHandler.handle(new GuildSendAllyInvitationEvent(EventCause.USER, user, guild, invitedGuild))) {
        return;
    }
    InvitationList.createInvitation(guild, invitedGuild);
    String allyInviteDoneMessage = messages.allyInviteDone;
    allyInviteDoneMessage = StringUtils.replace(allyInviteDoneMessage, "{GUILD}", invitedGuild.getName());
    allyInviteDoneMessage = StringUtils.replace(allyInviteDoneMessage, "{TAG}", invitedGuild.getTag());
    player.sendMessage(allyInviteDoneMessage);
    Player owner = invitedGuild.getOwner().getPlayer();
    if (owner != null) {
        String allyToInvitedMessage = messages.allyToInvited;
        allyToInvitedMessage = StringUtils.replace(allyToInvitedMessage, "{GUILD}", guild.getName());
        allyToInvitedMessage = StringUtils.replace(allyToInvitedMessage, "{TAG}", guild.getTag());
        owner.sendMessage(allyToInvitedMessage);
    }
}
Also used : Player(org.bukkit.entity.Player) User(net.dzikoysk.funnyguilds.basic.User) ConcurrencyManager(net.dzikoysk.funnyguilds.concurrency.ConcurrencyManager) GuildRevokeAllyInvitationEvent(net.dzikoysk.funnyguilds.event.guild.ally.GuildRevokeAllyInvitationEvent) PrefixUpdateGuildRequest(net.dzikoysk.funnyguilds.concurrency.requests.prefix.PrefixUpdateGuildRequest) Guild(net.dzikoysk.funnyguilds.basic.Guild) ConcurrencyTask(net.dzikoysk.funnyguilds.concurrency.ConcurrencyTask) MessagesConfig(net.dzikoysk.funnyguilds.data.configs.MessagesConfig) GuildSendAllyInvitationEvent(net.dzikoysk.funnyguilds.event.guild.ally.GuildSendAllyInvitationEvent) GuildAcceptAllyInvitationEvent(net.dzikoysk.funnyguilds.event.guild.ally.GuildAcceptAllyInvitationEvent) ConcurrencyTaskBuilder(net.dzikoysk.funnyguilds.concurrency.ConcurrencyTaskBuilder)

Aggregations

Guild (net.dzikoysk.funnyguilds.basic.Guild)1 User (net.dzikoysk.funnyguilds.basic.User)1 ConcurrencyManager (net.dzikoysk.funnyguilds.concurrency.ConcurrencyManager)1 ConcurrencyTask (net.dzikoysk.funnyguilds.concurrency.ConcurrencyTask)1 ConcurrencyTaskBuilder (net.dzikoysk.funnyguilds.concurrency.ConcurrencyTaskBuilder)1 PrefixUpdateGuildRequest (net.dzikoysk.funnyguilds.concurrency.requests.prefix.PrefixUpdateGuildRequest)1 MessagesConfig (net.dzikoysk.funnyguilds.data.configs.MessagesConfig)1 GuildAcceptAllyInvitationEvent (net.dzikoysk.funnyguilds.event.guild.ally.GuildAcceptAllyInvitationEvent)1 GuildRevokeAllyInvitationEvent (net.dzikoysk.funnyguilds.event.guild.ally.GuildRevokeAllyInvitationEvent)1 GuildSendAllyInvitationEvent (net.dzikoysk.funnyguilds.event.guild.ally.GuildSendAllyInvitationEvent)1 Player (org.bukkit.entity.Player)1