Search in sources :

Example 1 with ConcurrencyTask

use of net.dzikoysk.funnyguilds.concurrency.ConcurrencyTask in project FunnyGuilds by FunnyGuilds.

the class PlayerDeath method onDeath.

@EventHandler
public void onDeath(PlayerDeathEvent event) {
    Player v = event.getEntity();
    Player a = event.getEntity().getKiller();
    User victim = User.get(v);
    victim.getRank().addDeath();
    if (a == null) {
        return;
    }
    User attacker = User.get(a);
    if (victim.equals(attacker)) {
        return;
    }
    if (PluginHook.isPresent(PluginHook.PLUGIN_WORLDGUARD)) {
        if (WorldGuardHook.isOnNonPointsRegion(v.getLocation()) || WorldGuardHook.isOnNonPointsRegion(a.getLocation())) {
            return;
        }
    }
    PluginConfig config = Settings.getConfig();
    MessagesConfig messages = Messages.getInstance();
    if (config.rankFarmingProtect) {
        if (attacker.getLastVictim() != null && attacker.getLastVictim().equals(victim)) {
            if (attacker.getLastVictimTime() + (config.rankFarmingCooldown * 1000) >= System.currentTimeMillis()) {
                v.sendMessage(messages.rankLastVictimV);
                a.sendMessage(messages.rankLastVictimA);
                event.setDeathMessage(null);
                return;
            }
        } else if (victim.getLastAttacker() != null && victim.getLastAttacker().equals(attacker)) {
            if (victim.getLastVictimTime() + (config.rankFarmingCooldown * 1000) >= System.currentTimeMillis()) {
                v.sendMessage(messages.rankLastAttackerV);
                a.sendMessage(messages.rankLastAttackerA);
                event.setDeathMessage(null);
                return;
            }
        }
    }
    if (config.rankIPProtect) {
        if (a.getAddress().getHostString().equalsIgnoreCase(v.getAddress().getHostString())) {
            v.sendMessage(messages.rankIPVictim);
            a.sendMessage(messages.rankIPAttacker);
            event.setDeathMessage(null);
            return;
        }
    }
    int[] rankChanges = new int[2];
    int aP = attacker.getRank().getPoints();
    int vP = victim.getRank().getPoints();
    switch(config.rankSystem) {
        case ELO:
            rankChanges = getEloValues(vP, aP);
            break;
        case PERCENT:
            Double d = victim.getRank().getPoints() * (config.percentRankChange / 100);
            rankChanges[0] = d.intValue();
            rankChanges[1] = d.intValue();
            break;
        case STATIC:
            rankChanges[0] = config.staticAttackerChange;
            rankChanges[1] = config.staticVictimChange;
            break;
        default:
            rankChanges = getEloValues(vP, aP);
            break;
    }
    RankChangeEvent attackerEvent = new PointsChangeEvent(EventCause.USER, attacker.getRank(), attacker, rankChanges[0]);
    RankChangeEvent victimEvent = new PointsChangeEvent(EventCause.USER, victim.getRank(), attacker, rankChanges[1]);
    List<String> assistEntries = new ArrayList<>();
    if (SimpleEventHandler.handle(attackerEvent) && SimpleEventHandler.handle(victimEvent)) {
        double attackerDamage = victim.killedBy(attacker);
        if (config.assistEnable && victim.isAssisted()) {
            double toShare = attackerEvent.getChange() * (1 - config.assistKillerShare);
            double totalDamage = victim.getTotalDamage() + attackerDamage;
            int givenPoints = 0;
            Map<User, Double> damage = MapUtil.sortByValue(victim.getDamage());
            int assists = 0;
            for (Entry<User, Double> assist : damage.entrySet()) {
                double assistFraction = assist.getValue() / totalDamage;
                int addedPoints = (int) Math.round(assistFraction * toShare);
                if (addedPoints <= 0) {
                    continue;
                }
                if (config.assistsLimit > 0) {
                    if (assists >= config.assistsLimit) {
                        continue;
                    }
                    assists++;
                }
                givenPoints += addedPoints;
                String assistEntry = StringUtils.replace(messages.rankAssistEntry, "{PLAYER}", assist.getKey().getName());
                assistEntry = StringUtils.replace(assistEntry, "{+}", Integer.toString(addedPoints));
                assistEntry = StringUtils.replace(assistEntry, "{SHARE}", StringUtils.getPercent(assistFraction));
                assistEntries.add(assistEntry);
                assist.getKey().getRank().addPoints(addedPoints);
            }
            double attackerPoints = attackerEvent.getChange() - toShare + (givenPoints < toShare ? toShare - givenPoints : 0);
            attackerEvent.setChange((int) Math.round(attackerPoints));
        }
        attacker.getRank().addKill();
        attacker.getRank().addPoints(attackerEvent.getChange());
        attacker.setLastVictim(victim);
        victim.getRank().removePoints(victimEvent.getChange());
        victim.setLastAttacker(attacker);
        victim.clearDamage();
    }
    ConcurrencyManager concurrencyManager = FunnyGuilds.getInstance().getConcurrencyManager();
    ConcurrencyTaskBuilder taskBuilder = ConcurrencyTask.builder();
    if (config.dataType.mysql) {
        if (victim.hasGuild()) {
            // IndependentThread.actions(ActionType.MYSQL_UPDATE_GUILD_POINTS, victim.getGuild());
            taskBuilder.delegate(new DatabaseUpdateGuildPointsRequest(victim.getGuild()));
        }
        if (attacker.hasGuild()) {
            // IndependentThread.actions(ActionType.MYSQL_UPDATE_GUILD_POINTS, attacker.getGuild());
            taskBuilder.delegate(new DatabaseUpdateGuildPointsRequest(attacker.getGuild()));
        }
        // IndependentThread.actions(ActionType.MYSQL_UPDATE_USER_POINTS, victim);
        // IndependentThread.actions(ActionType.MYSQL_UPDATE_USER_POINTS, attacker);
        taskBuilder.delegate(new DatabaseUpdateUserPointsRequest(victim));
        taskBuilder.delegate(new DatabaseUpdateUserPointsRequest(attacker));
    }
    /*
        IndependentThread.actions(ActionType.DUMMY_GLOBAL_UPDATE_USER, victim);
        IndependentThread.actions(ActionType.DUMMY_GLOBAL_UPDATE_USER, attacker);
        IndependentThread.actions(ActionType.RANK_UPDATE_USER, victim);
        IndependentThread.action(ActionType.RANK_UPDATE_USER, attacker);
        */
    ConcurrencyTask task = taskBuilder.delegate(new DummyGlobalUpdateUserRequest(victim)).delegate(new DummyGlobalUpdateUserRequest(attacker)).delegate(new RankUpdateUserRequest(victim)).delegate(new RankUpdateUserRequest(attacker)).build();
    concurrencyManager.postTask(task);
    String deathMessage = messages.rankDeathMessage;
    deathMessage = StringUtils.replace(deathMessage, "{ATTACKER}", attacker.getName());
    deathMessage = StringUtils.replace(deathMessage, "{VICTIM}", victim.getName());
    deathMessage = StringUtils.replace(deathMessage, "{+}", Integer.toString(attackerEvent.getChange()));
    deathMessage = StringUtils.replace(deathMessage, "{-}", Integer.toString(victimEvent.getChange()));
    deathMessage = StringUtils.replace(deathMessage, "{POINTS-FORMAT}", IntegerRange.inRange(vP, config.pointsFormat));
    deathMessage = StringUtils.replace(deathMessage, "{POINTS}", String.valueOf(victim.getRank().getPoints()));
    deathMessage = StringUtils.replace(deathMessage, "{WEAPON}", MaterialUtil.getMaterialName(a.getItemInHand().getType()));
    deathMessage = StringUtils.replace(deathMessage, "{REMAINING-HEALTH}", Double.toString(a.getHealth()));
    deathMessage = StringUtils.replace(deathMessage, "{REMAINING-HEARTS}", Double.toString(a.getHealth() / 2));
    if (victim.hasGuild()) {
        deathMessage = StringUtils.replace(deathMessage, "{VTAG}", StringUtils.replace(config.chatGuild, "{TAG}", victim.getGuild().getTag()));
    }
    if (attacker.hasGuild()) {
        deathMessage = StringUtils.replace(deathMessage, "{ATAG}", StringUtils.replace(config.chatGuild, "{TAG}", attacker.getGuild().getTag()));
    }
    deathMessage = StringUtils.replace(deathMessage, "{VTAG}", "");
    deathMessage = StringUtils.replace(deathMessage, "{ATAG}", "");
    event.setDeathMessage(deathMessage);
}
Also used : Player(org.bukkit.entity.Player) User(net.dzikoysk.funnyguilds.basic.User) RankChangeEvent(net.dzikoysk.funnyguilds.event.rank.RankChangeEvent) ConcurrencyManager(net.dzikoysk.funnyguilds.concurrency.ConcurrencyManager) ArrayList(java.util.ArrayList) RankUpdateUserRequest(net.dzikoysk.funnyguilds.concurrency.requests.rank.RankUpdateUserRequest) DummyGlobalUpdateUserRequest(net.dzikoysk.funnyguilds.concurrency.requests.dummy.DummyGlobalUpdateUserRequest) PointsChangeEvent(net.dzikoysk.funnyguilds.event.rank.PointsChangeEvent) DatabaseUpdateUserPointsRequest(net.dzikoysk.funnyguilds.concurrency.requests.database.DatabaseUpdateUserPointsRequest) PluginConfig(net.dzikoysk.funnyguilds.data.configs.PluginConfig) DatabaseUpdateGuildPointsRequest(net.dzikoysk.funnyguilds.concurrency.requests.database.DatabaseUpdateGuildPointsRequest) ConcurrencyTask(net.dzikoysk.funnyguilds.concurrency.ConcurrencyTask) MessagesConfig(net.dzikoysk.funnyguilds.data.configs.MessagesConfig) ConcurrencyTaskBuilder(net.dzikoysk.funnyguilds.concurrency.ConcurrencyTaskBuilder) EventHandler(org.bukkit.event.EventHandler) SimpleEventHandler(net.dzikoysk.funnyguilds.event.SimpleEventHandler)

Example 2 with ConcurrencyTask

use of net.dzikoysk.funnyguilds.concurrency.ConcurrencyTask 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)

Example 3 with ConcurrencyTask

use of net.dzikoysk.funnyguilds.concurrency.ConcurrencyTask in project FunnyGuilds by FunnyGuilds.

the class ExcBreak 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();
    if (guild.getAllies() == null || guild.getAllies().isEmpty()) {
        player.sendMessage(messages.breakHasNotAllies);
        return;
    }
    if (args.length < 1) {
        List<String> list = messages.breakAlliesList;
        String iss = StringUtils.toString(GuildUtils.getNames(guild.getAllies()), true);
        for (String msg : list) {
            player.sendMessage(msg.replace("{GUILDS}", iss));
        }
        return;
    }
    String tag = args[0];
    Guild oppositeGuild = GuildUtils.getByTag(tag);
    if (oppositeGuild == null) {
        player.sendMessage(messages.generalGuildNotExists.replace("{TAG}", tag));
        return;
    }
    if (!guild.getAllies().contains(oppositeGuild)) {
        player.sendMessage(messages.breakAllyExists.replace("{GUILD}", oppositeGuild.getName()).replace("{TAG}", tag));
    }
    if (!SimpleEventHandler.handle(new GuildBreakAllyEvent(EventCause.USER, user, guild, oppositeGuild))) {
        return;
    }
    Player owner = oppositeGuild.getOwner().getPlayer();
    if (owner != null) {
        owner.sendMessage(messages.breakIDone.replace("{GUILD}", guild.getName()).replace("{TAG}", guild.getTag()));
    }
    guild.removeAlly(oppositeGuild);
    oppositeGuild.removeAlly(guild);
    ConcurrencyManager concurrencyManager = FunnyGuilds.getInstance().getConcurrencyManager();
    ConcurrencyTaskBuilder taskBuilder = ConcurrencyTask.builder();
    for (User member : guild.getMembers()) {
        // IndependentThread.action(ActionType.PREFIX_UPDATE_GUILD, member, oppositeGuild);
        taskBuilder.delegate(new PrefixUpdateGuildRequest(member, oppositeGuild));
    }
    for (User member : oppositeGuild.getMembers()) {
        // IndependentThread.action(ActionType.PREFIX_UPDATE_GUILD, member, guild);
        taskBuilder.delegate(new PrefixUpdateGuildRequest(member, guild));
    }
    ConcurrencyTask task = taskBuilder.build();
    concurrencyManager.postTask(task);
    player.sendMessage(messages.breakDone.replace("{GUILD}", oppositeGuild.getName()).replace("{TAG}", oppositeGuild.getTag()));
}
Also used : Player(org.bukkit.entity.Player) User(net.dzikoysk.funnyguilds.basic.User) ConcurrencyManager(net.dzikoysk.funnyguilds.concurrency.ConcurrencyManager) ConcurrencyTask(net.dzikoysk.funnyguilds.concurrency.ConcurrencyTask) MessagesConfig(net.dzikoysk.funnyguilds.data.configs.MessagesConfig) GuildBreakAllyEvent(net.dzikoysk.funnyguilds.event.guild.ally.GuildBreakAllyEvent) PrefixUpdateGuildRequest(net.dzikoysk.funnyguilds.concurrency.requests.prefix.PrefixUpdateGuildRequest) Guild(net.dzikoysk.funnyguilds.basic.Guild) ConcurrencyTaskBuilder(net.dzikoysk.funnyguilds.concurrency.ConcurrencyTaskBuilder)

Aggregations

User (net.dzikoysk.funnyguilds.basic.User)3 ConcurrencyManager (net.dzikoysk.funnyguilds.concurrency.ConcurrencyManager)3 ConcurrencyTask (net.dzikoysk.funnyguilds.concurrency.ConcurrencyTask)3 ConcurrencyTaskBuilder (net.dzikoysk.funnyguilds.concurrency.ConcurrencyTaskBuilder)3 MessagesConfig (net.dzikoysk.funnyguilds.data.configs.MessagesConfig)3 Player (org.bukkit.entity.Player)3 Guild (net.dzikoysk.funnyguilds.basic.Guild)2 PrefixUpdateGuildRequest (net.dzikoysk.funnyguilds.concurrency.requests.prefix.PrefixUpdateGuildRequest)2 ArrayList (java.util.ArrayList)1 DatabaseUpdateGuildPointsRequest (net.dzikoysk.funnyguilds.concurrency.requests.database.DatabaseUpdateGuildPointsRequest)1 DatabaseUpdateUserPointsRequest (net.dzikoysk.funnyguilds.concurrency.requests.database.DatabaseUpdateUserPointsRequest)1 DummyGlobalUpdateUserRequest (net.dzikoysk.funnyguilds.concurrency.requests.dummy.DummyGlobalUpdateUserRequest)1 RankUpdateUserRequest (net.dzikoysk.funnyguilds.concurrency.requests.rank.RankUpdateUserRequest)1 PluginConfig (net.dzikoysk.funnyguilds.data.configs.PluginConfig)1 SimpleEventHandler (net.dzikoysk.funnyguilds.event.SimpleEventHandler)1 GuildAcceptAllyInvitationEvent (net.dzikoysk.funnyguilds.event.guild.ally.GuildAcceptAllyInvitationEvent)1 GuildBreakAllyEvent (net.dzikoysk.funnyguilds.event.guild.ally.GuildBreakAllyEvent)1 GuildRevokeAllyInvitationEvent (net.dzikoysk.funnyguilds.event.guild.ally.GuildRevokeAllyInvitationEvent)1 GuildSendAllyInvitationEvent (net.dzikoysk.funnyguilds.event.guild.ally.GuildSendAllyInvitationEvent)1 PointsChangeEvent (net.dzikoysk.funnyguilds.event.rank.PointsChangeEvent)1