From 6b01e8e28dcf8b7313f4c70e398863afc43141e8 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Sat, 24 Jan 2015 01:09:43 -0500 Subject: [PATCH] =?UTF-8?q?Ajout=20de=20la=20fonctionnalit=C3=A9=20de=20me?= =?UTF-8?q?ssages=20automatiques=20(remplace=20ZavAutoMessager)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugin/pandacraftutils/ConfigManager.java | 31 ++++- .../pandacraftutils/PandacraftUtils.java | 7 +- .../modules/AutoMessagesManager.java | 125 ++++++++++++++++++ .../pandacraftutils/players/OnlinePlayer.java | 66 +++++++++ 4 files changed, 220 insertions(+), 9 deletions(-) create mode 100644 src/net/mc_pandacraft/java/plugin/pandacraftutils/modules/AutoMessagesManager.java diff --git a/src/net/mc_pandacraft/java/plugin/pandacraftutils/ConfigManager.java b/src/net/mc_pandacraft/java/plugin/pandacraftutils/ConfigManager.java index 529e3d4..df0c86a 100644 --- a/src/net/mc_pandacraft/java/plugin/pandacraftutils/ConfigManager.java +++ b/src/net/mc_pandacraft/java/plugin/pandacraftutils/ConfigManager.java @@ -84,6 +84,15 @@ public class ConfigManager { UsersCleaner_users_cleaner_group = configFile.getString("cleaner.users_cleaner_group"); + // TODO ajouter les valeurs par défaut dans config.yml + Automessage_interval_players = configFile.getInt("autoMessage.interval_player", 10*60); + Automessage_interval_default = configFile.getInt("autoMessage.interval_default", 2*60+30); + + + // TODO ajouter les valeurs par défaut dans config.yml + // TODO gérer les codes de coloration; + ServerMessages_prefix = ChatColor.YELLOW+"["+ChatColor.WHITE+"Pandacraft"+ChatColor.YELLOW+"] "; //configFile.getString("server_messages_prefix"); + initChatAnalysisBadWord(); initCommandAlias(); @@ -123,6 +132,14 @@ public class ConfigManager { * Automessages */ public List> Automessages_messages; + /** + * En seconde : pour les joueurs != visiteur + */ + public int Automessage_interval_players; + /** + * En seconde : pour les visiteurs + */ + public int Automessage_interval_default; private void initAutomessages() { Automessages_messages = new ArrayList>(); @@ -130,6 +147,12 @@ public class ConfigManager { String default_color = ChatColor.YELLOW.toString(); Map message; + + message = new HashMap(); + message.put("message", ChatColor.BLUE+"Bienvenue sur Pandacraft ! :) Pour pouvoir jouer, n'oubliez pas de vous inscrire en cliquant sur le lien en dessous%n &ahttp://mc-pandacraft.net/?p=inscription"); + message.put("permissions", "pandacraft.grade.default"); + Automessages_messages.add(message); + message = new HashMap(); message.put("message", "WorldEdit disponible en &bCréatif"+default_color+" avec le grade &f[&eUltimate&f]"+default_color+". Profitez en vite :D%n&f &a http://mc-pandacraft.net/?p=ultimate"); message.put("permissions", "*"); @@ -139,12 +162,6 @@ public class ConfigManager { message.put("message", "Launcher disponible pour Pandacraft : Optifine intégré et mise à jour automatique avec le serveur.%n &ahttp://mc-pandacraft.net/?p=launcher"); message.put("permissions", "*"); Automessages_messages.add(message); - - message = new HashMap(); - message.put("message", ChatColor.BLUE+"Bienvenue sur Pandacraft ! :) Pour pouvoir jouer, n'oubliez pas de vous inscrire en cliquant sur le lien en dessous%n &ahttp://mc-pandacraft.net/?p=inscription"); - message.put("permissions", "pandacraft.grade.default"); - Automessages_messages.add(message); - /* * Fonctionnement : * Chaque joueur a une variable indiquand la date/heure du dernier message affiché @@ -321,7 +338,7 @@ public class ConfigManager { public List CuboCommand_worlds = new ArrayList(); - + public String ServerMessages_prefix; diff --git a/src/net/mc_pandacraft/java/plugin/pandacraftutils/PandacraftUtils.java b/src/net/mc_pandacraft/java/plugin/pandacraftutils/PandacraftUtils.java index 6b34797..8caa0c3 100644 --- a/src/net/mc_pandacraft/java/plugin/pandacraftutils/PandacraftUtils.java +++ b/src/net/mc_pandacraft/java/plugin/pandacraftutils/PandacraftUtils.java @@ -2,6 +2,7 @@ package net.mc_pandacraft.java.plugin.pandacraftutils; import net.mc_pandacraft.java.plugin.pandacraftutils.commands.CommandsManager; import net.mc_pandacraft.java.plugin.pandacraftutils.modules.AfkManager; +import net.mc_pandacraft.java.plugin.pandacraftutils.modules.AutoMessagesManager; import net.mc_pandacraft.java.plugin.pandacraftutils.modules.CalculatorManager; import net.mc_pandacraft.java.plugin.pandacraftutils.modules.ChatAnalysisManager; import net.mc_pandacraft.java.plugin.pandacraftutils.modules.CommandAliasManager; @@ -43,6 +44,7 @@ public class PandacraftUtils extends JavaPlugin { public SurvivalCuboManager survivalCuboManager; public StaffQueueManager staffQueueManager; public TPSAnalysisManager tpsAnalysisManager; + public AutoMessagesManager autoMessagesManager; @@ -82,7 +84,7 @@ public class PandacraftUtils extends JavaPlugin { serverPingListener = new PacketOutServerInfoListener(this); staffQueueManager = new StaffQueueManager(); tpsAnalysisManager = new TPSAnalysisManager(); - + autoMessagesManager = new AutoMessagesManager(); @@ -111,6 +113,7 @@ public class PandacraftUtils extends JavaPlugin { serverPingListener = null; staffQueueManager = null; tpsAnalysisManager = null; + autoMessagesManager = null; ConfigManager.reloadConfig(); @@ -153,7 +156,7 @@ public class PandacraftUtils extends JavaPlugin { throw new IllegalArgumentException("Un message doit être défini"); if (prefix) - message = ChatColor.YELLOW+"["+ChatColor.WHITE+"Pandacraft"+ChatColor.YELLOW+"] " + message; + message = ConfigManager.getInstance().ServerMessages_prefix + message; for (Player p : getServer().getOnlinePlayers()) diff --git a/src/net/mc_pandacraft/java/plugin/pandacraftutils/modules/AutoMessagesManager.java b/src/net/mc_pandacraft/java/plugin/pandacraftutils/modules/AutoMessagesManager.java new file mode 100644 index 0000000..7cfeb6c --- /dev/null +++ b/src/net/mc_pandacraft/java/plugin/pandacraftutils/modules/AutoMessagesManager.java @@ -0,0 +1,125 @@ +package net.mc_pandacraft.java.plugin.pandacraftutils.modules; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.Map; + +import net.mc_pandacraft.java.plugin.pandacraftutils.ConfigManager; +import net.mc_pandacraft.java.plugin.pandacraftutils.PandacraftUtils; +import net.mc_pandacraft.java.plugin.pandacraftutils.players.OnlinePlayer; +import net.mc_pandacraft.java.plugin.pandacraftutils.players.OnlinePlayerManager; + +import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitRunnable; + +public class AutoMessagesManager extends BukkitRunnable { + private PandacraftUtils plugin = PandacraftUtils.getInstance(); + + + + public AutoMessagesManager() + { + plugin.getServer().getScheduler().runTaskTimerAsynchronously(plugin, this, 0L, 20L); + } + + + + + + @Override + public void run() { + List messages = getMessagesFromConfig(); + int interval_default = ConfigManager.getInstance().Automessage_interval_default, + interval_players = ConfigManager.getInstance().Automessage_interval_players; + // message + // permissions + + Collection oPlayers = OnlinePlayerManager.getInstance().getAll(); + + + for (OnlinePlayer op : oPlayers) { + + int interval_sec = op.getPlayer().hasPermission("pandacraft.grade.default") ? interval_default : interval_players; + + op.firstAutomessageCkeck(interval_sec); + + // on vérifie si il est temps, ou non, d'afficher un message aux joueurs + if (op.getAutomessage_last_message_time() + (interval_sec*1000) > System.currentTimeMillis()) + continue; + + // ici, il se peut que id == -1 + int id = op.getAutomessage_last_message_id(); + boolean atLeastOnePermitted = true; + int c = 0; + do { + id++; + c++; + if (id >= messages.size()) id = 0; + if (c > messages.size()) { // il parcouru au moins tout le tableau, sans trouver de message qu'il peut afficher + atLeastOnePermitted = false; + break; + } + } while(!messages.get(id).hasPlayerPermission(op.getPlayer())); + + if (atLeastOnePermitted) { + op.setAutomessage_last_message_id(id); + op.updateAutomessage_last_message_time(); + + op.sendMessageFromServer(messages.get(id).getMessage()); + } + + } + + } + + + + + public List getMessagesFromConfig() { + ArrayList ret = new ArrayList(); + + for (Map message : ConfigManager.getInstance().Automessages_messages) + ret.add(new AutoMessage(message.get("message"), message.get("permissions"))); + + return ret; + } + + + + + public class AutoMessage { + private String message; + private List permissions; + + public AutoMessage(String m, String perm) { + setMessage(m); + if (perm == null || perm.equals("*")) + permissions = null; + else + permissions = Arrays.asList(perm.split(";")); + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + if (message == null) throw new IllegalArgumentException("message ne doit pas être null"); + this.message = message; + } + + public boolean hasPlayerPermission(Player p) { + if (p == null) throw new IllegalArgumentException("le joueur ne doit pas être null"); + if (permissions == null) + return true; + + for (String pe : permissions) { + if (p.hasPermission(pe)) return true; + } + return false; + } + } + +} diff --git a/src/net/mc_pandacraft/java/plugin/pandacraftutils/players/OnlinePlayer.java b/src/net/mc_pandacraft/java/plugin/pandacraftutils/players/OnlinePlayer.java index db12ac4..96f5d73 100644 --- a/src/net/mc_pandacraft/java/plugin/pandacraftutils/players/OnlinePlayer.java +++ b/src/net/mc_pandacraft/java/plugin/pandacraftutils/players/OnlinePlayer.java @@ -2,6 +2,7 @@ package net.mc_pandacraft.java.plugin.pandacraftutils.players; import java.util.Collections; import java.util.HashSet; +import java.util.Random; import java.util.Set; import net.mc_pandacraft.java.plugin.pandacraftutils.ConfigManager; @@ -160,4 +161,69 @@ public class OnlinePlayer { + + + + + + + /* + * AutomessagesData + */ + private boolean automessage_first_since_online = false; + private long automessage_last_message_time = System.currentTimeMillis(); + private int automessage_last_message_id = -1; + + public boolean firstAutomessageCkeck(int aleaMaxDuration) { + if (automessage_first_since_online) return true; + automessage_first_since_online = true; + automessage_last_message_time = System.currentTimeMillis() - (new Random()).nextInt(aleaMaxDuration)*1000; + return false; + } + public void updateAutomessage_last_message_time() { automessage_last_message_time = System.currentTimeMillis(); } + public long getAutomessage_last_message_time() { return automessage_last_message_time; } + + public int getAutomessage_last_message_id() { return automessage_last_message_id; } + + public void setAutomessage_last_message_id(int id) { automessage_last_message_id = id; } + + + + + + + + + + + + + + + + + + + + + + + + + + + + + /* + * Utils + */ + + /** + * Envoi un message au joueur avec un prefixe. + * Fait appel à player.sendMessage() + * @param message le message à envoyer au joueur + */ + public void sendMessageFromServer(String message) { + player.sendMessage(ConfigManager.getInstance().ServerMessages_prefix + message); + } }