Search in sources :

Example 1 with ChatTask

use of com.alessiodp.parties.tasks.ChatTask in project Parties by AlessioDP.

the class ChatListener method onPlayerChat.

@EventHandler(priority = EventPriority.LOW)
public void onPlayerChat(AsyncPlayerChatEvent event) {
    if (!event.isCancelled()) {
        Player p = event.getPlayer();
        PartyPlayerEntity pp = plugin.getPlayerManager().getPlayer(p.getUniqueId());
        if (!pp.getPartyName().isEmpty() && pp.isChatParty()) {
            // Make it async
            plugin.getPartiesScheduler().getEventsExecutor().execute(() -> {
                if (PartiesUtils.checkPlayerRankAlerter(pp, PartiesPermission.PRIVATE_SENDMESSAGE)) {
                    // Chat allowed
                    boolean mustWait = false;
                    if (ConfigParties.GENERAL_CHAT_CHATCD > 0 && !PartiesUtils.checkPlayerRank(pp, PartiesPermission.PRIVATE_BYPASSCOOLDOWN)) {
                        Long unixTimestamp = plugin.getPlayerManager().getChatCooldown().get(p.getUniqueId());
                        long unixNow = System.currentTimeMillis() / 1000L;
                        // Check cooldown
                        if (unixTimestamp != null) {
                            pp.sendMessage(Messages.MAINCMD_P_COOLDOWN.replace("%seconds%", String.valueOf(ConfigParties.GENERAL_CHAT_CHATCD - (unixNow - unixTimestamp))));
                            mustWait = true;
                        } else {
                            plugin.getPlayerManager().getChatCooldown().put(p.getUniqueId(), unixNow);
                            new ChatTask(p.getUniqueId(), plugin.getPlayerManager()).runTaskLater(plugin, ConfigParties.GENERAL_CHAT_CHATCD * 20);
                            LoggerManager.log(LogLevel.DEBUG, Constants.DEBUG_CMD_P_TASK.replace("{value}", Integer.toString(ConfigParties.GENERAL_CHAT_CHATCD * 20)).replace("{player}", p.getName()), true);
                        }
                    }
                    if (!mustWait) {
                        PartyEntity party = plugin.getPartyManager().getParty(pp.getPartyName());
                        // Calling API event
                        PartiesChatEvent partiesChatEvent = new PartiesChatEvent(pp, party, event.getMessage());
                        Bukkit.getServer().getPluginManager().callEvent(partiesChatEvent);
                        String newMessage = partiesChatEvent.getMessage();
                        if (!partiesChatEvent.isCancelled()) {
                            party.sendPlayerMessage(pp, newMessage);
                            if (ConfigMain.STORAGE_LOG_CHAT)
                                LoggerManager.log(LogLevel.BASIC, Constants.DEBUG_CMD_P.replace("{party}", party.getName()).replace("{player}", p.getName()).replace("{message}", newMessage), true);
                        } else
                            LoggerManager.log(LogLevel.DEBUG, Constants.DEBUG_API_CHATEVENT_DENY.replace("{player}", p.getName()).replace("{message}", event.getMessage()), true);
                    }
                } else
                    pp.sendNoPermission(PartiesPermission.PRIVATE_SENDMESSAGE);
            });
            event.setCancelled(true);
        }
    }
}
Also used : Player(org.bukkit.entity.Player) PartyPlayerEntity(com.alessiodp.parties.players.objects.PartyPlayerEntity) ChatTask(com.alessiodp.parties.tasks.ChatTask) PartyEntity(com.alessiodp.parties.parties.objects.PartyEntity) PartiesChatEvent(com.alessiodp.partiesapi.events.PartiesChatEvent) EventHandler(org.bukkit.event.EventHandler)

Example 2 with ChatTask

use of com.alessiodp.parties.tasks.ChatTask in project Parties by AlessioDP.

the class CommandP method onCommand.

@Override
public void onCommand(CommandData commandData) {
    PartyPlayerEntity pp = commandData.getPartyPlayer();
    PartyEntity party = commandData.getParty();
    /*
		 * Command handling
		 */
    if (ConfigParties.GENERAL_CHAT_CHATCD > 0 && !PartiesUtils.checkPlayerRank(pp, PartiesPermission.PRIVATE_BYPASSCOOLDOWN)) {
        Long unixTimestamp = plugin.getPlayerManager().getChatCooldown().get(pp.getPlayerUUID());
        long unixNow = System.currentTimeMillis() / 1000L;
        if (unixTimestamp != null) {
            pp.sendMessage(Messages.MAINCMD_P_COOLDOWN.replace("%seconds%", String.valueOf(ConfigParties.GENERAL_CHAT_CHATCD - (unixNow - unixTimestamp))));
            return;
        }
        plugin.getPlayerManager().getChatCooldown().put(pp.getPlayerUUID(), unixNow);
        new ChatTask(pp.getPlayerUUID(), plugin.getPlayerManager()).runTaskLater(plugin, ConfigParties.GENERAL_CHAT_CHATCD * 20);
        LoggerManager.log(LogLevel.DEBUG, Constants.DEBUG_CMD_P_TASK.replace("{value}", Integer.toString(ConfigParties.GENERAL_CHAT_CHATCD * 20)).replace("{player}", pp.getName()), true);
    }
    /*
		 * Command starts
		 */
    StringBuilder sb = new StringBuilder();
    for (String word : commandData.getArgs()) {
        if (sb.length() > 0) {
            sb.append(" ");
        }
        sb.append(word);
    }
    // Calling API event
    PartiesChatEvent partiesChatEvent = new PartiesChatEvent(pp, party, sb.toString());
    Bukkit.getServer().getPluginManager().callEvent(partiesChatEvent);
    String newMessage = partiesChatEvent.getMessage();
    if (!partiesChatEvent.isCancelled()) {
        party.sendPlayerMessage(pp, newMessage);
        if (ConfigMain.STORAGE_LOG_CHAT)
            LoggerManager.log(LogLevel.BASIC, Constants.DEBUG_CMD_P.replace("{party}", party.getName()).replace("{player}", pp.getName()).replace("{message}", newMessage), true);
    } else
        LoggerManager.log(LogLevel.DEBUG, Constants.DEBUG_API_CHATEVENT_DENY.replace("{player}", pp.getName()).replace("{message}", sb.toString()), true);
}
Also used : PartyPlayerEntity(com.alessiodp.parties.players.objects.PartyPlayerEntity) ChatTask(com.alessiodp.parties.tasks.ChatTask) PartyEntity(com.alessiodp.parties.parties.objects.PartyEntity) PartiesChatEvent(com.alessiodp.partiesapi.events.PartiesChatEvent)

Aggregations

PartyEntity (com.alessiodp.parties.parties.objects.PartyEntity)2 PartyPlayerEntity (com.alessiodp.parties.players.objects.PartyPlayerEntity)2 ChatTask (com.alessiodp.parties.tasks.ChatTask)2 PartiesChatEvent (com.alessiodp.partiesapi.events.PartiesChatEvent)2 Player (org.bukkit.entity.Player)1 EventHandler (org.bukkit.event.EventHandler)1