From a5d5ff78fde80fc5c8fa3962905f1f4cf7686dc8 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Tue, 12 May 2015 03:13:01 +0200 Subject: [PATCH] =?UTF-8?q?Masquage=20des=20messages=20priv=C3=A9s=20avant?= =?UTF-8?q?=20le=20/login=20+=20ajout=20des=20informations=20dans=20le=20t?= =?UTF-8?q?ooltip=20des=20messages=20priv=C3=A9s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modules/PrivateMessagesManager.java | 218 ++++++++++++------ .../players/OnlinePlayerManager.java | 12 +- 2 files changed, 158 insertions(+), 72 deletions(-) diff --git a/src/net/mc_pandacraft/java/plugin/pandacraftutils/modules/PrivateMessagesManager.java b/src/net/mc_pandacraft/java/plugin/pandacraftutils/modules/PrivateMessagesManager.java index a4d402b..ea02db7 100644 --- a/src/net/mc_pandacraft/java/plugin/pandacraftutils/modules/PrivateMessagesManager.java +++ b/src/net/mc_pandacraft/java/plugin/pandacraftutils/modules/PrivateMessagesManager.java @@ -3,7 +3,7 @@ package net.mc_pandacraft.java.plugin.pandacraftutils.modules; import java.sql.Date; import java.sql.SQLException; import java.text.DateFormat; -import java.util.Arrays; +import java.util.ArrayList; import java.util.List; import java.util.Locale; import java.util.Random; @@ -17,6 +17,8 @@ import net.mc_pandacraft.java.plugin.pandacraftutils.data_model.MPGroupUserEleme import net.mc_pandacraft.java.plugin.pandacraftutils.data_model.MPGroupUserTable; import net.mc_pandacraft.java.plugin.pandacraftutils.data_model.MPMessageElement; import net.mc_pandacraft.java.plugin.pandacraftutils.data_model.MPMessageTable; +import net.mc_pandacraft.java.plugin.pandacraftutils.data_model.MPWebSessionElement; +import net.mc_pandacraft.java.plugin.pandacraftutils.data_model.MPWebSessionTable; import net.mc_pandacraft.java.plugin.pandacraftutils.data_model.ORM; import net.mc_pandacraft.java.plugin.pandacraftutils.players.OffPlayer; import net.mc_pandacraft.java.plugin.pandacraftutils.players.OnlinePlayer; @@ -32,6 +34,9 @@ import de.luricos.bukkit.xAuth.events.xAuthLoginEvent; public class PrivateMessagesManager extends BukkitRunnable { private PandacraftUtils plugin = PandacraftUtils.getInstance(); + + + private static final long TIMEOUT_WEB_SESSION = 10000; // msec @@ -299,10 +304,6 @@ public class PrivateMessagesManager extends BukkitRunnable { OnlinePlayer op = OnlinePlayerManager.get(new OffPlayer(message.getViewerNick()).getBukkitOnlinePlayer()); - if (op == null) return; // la cible n'est pas en ligne - - if (!op.isAuthenticated()) return; // la cible n'a pas fait son /login - MessageSender sender = new MessageSender(message.getSourceNick()); boolean isIgnoring = sender.isPlayer() && op.getEssentialsUser().isIgnoredPlayer(new OffPlayer(message.getSourceNick()).getEssentialsUser()); @@ -310,82 +311,108 @@ public class PrivateMessagesManager extends BukkitRunnable { if (isIgnoring) { message.delete(); + return; + } + + if (op == null) return; // la cible n'est pas en ligne + + if (!op.isAuthenticated()) return; // la cible n'a pas fait son /login + + + message.setRead(true); + message.save(); + + // le message est affiché sur l'écran de la source du message + boolean dispToSender = (message.getViewerNick().equalsIgnoreCase(message.getSourceNick())); + + // ------- + // tooltip + // ------- + + Date d = new Date(message.getTime()); + DateFormat date = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.FRANCE); + DateFormat hour = DateFormat.getTimeInstance(DateFormat.MEDIUM, Locale.FRANCE); + + String affDate = date.format(d) + " à " + hour.format(d); + + List tooltipLines = new ArrayList(); + tooltipLines.add(ChatColor.GRAY+affDate); + tooltipLines.add(ChatColor.GRAY+"de "+ChatColor.RESET+new OffPlayer(message.getSourceNick()).getDisplayName()+ChatColor.GRAY+" ("+displayablePlayerStatus(getPlayerStatut(message.getSourceNick()))+ChatColor.GRAY+")"); + if (message.getDestGroup() == null) // vers un joueur + tooltipLines.add(ChatColor.GRAY+"vers "+ChatColor.RESET+new OffPlayer(message.getDestNick()).getDisplayName()+ChatColor.GRAY+" ("+displayablePlayerStatus(getPlayerStatut(message.getDestNick()))+ChatColor.GRAY+")"); + else { // vers un groupe + MPGroupElement groupEl = message.getDestGroupElement(); + + tooltipLines.add(ChatColor.GRAY+"vers le groupe "+ChatColor.GOLD+groupEl.getGroupName()); + + List groupUsers = groupEl.getUsers(); + + for (MPGroupUserElement gUser : groupUsers) { + if (!gUser.getPlayerName().equalsIgnoreCase(message.getSourceNick())) + tooltipLines.add(ChatColor.GRAY+"- "+ChatColor.RESET+new OffPlayer(gUser.getPlayerName()).getDisplayName()+ChatColor.GRAY+" ("+displayablePlayerStatus(getPlayerStatut(gUser.getPlayerName()))+ChatColor.GRAY+")"); + } + + + } + + // ------- + + + if (dispToSender) { + + String destAff = (message.getDestGroup() == null)? + ( (message.getDestNick().equalsIgnoreCase(message.getViewerNick())) ? + "moi": + new OffPlayer(message.getDestNick()).getDisplayName() + ): + "g:"+message.getDestGroupElement().getGroupName(); + + String destCommand = (message.getDestGroup() == null)? + message.getDestNick(): + "g:"+message.getDestGroupElement().getGroupName(); + + tooltipLines.add(0, ChatColor.GRAY+"(Cliquez pour répondre)"); + + new FancyMessage("") + .then("§6<§rmoi§6 → §r"+destAff+"§r§6>") + .suggest("/m "+destCommand+" ") + .tooltip(tooltipLines) + .then(" §6"+ChatColor.translateAlternateColorCodes('&', message.getMessage())) + .color(ChatColor.GOLD) + .send(op.getPlayer()); } else { - message.setRead(true); - message.save(); - - // le message est affiché sur l'écran de la source du message - boolean dispToSender = (message.getViewerNick().equalsIgnoreCase(message.getSourceNick())); - Date d = new Date(message.getTime()); - DateFormat date = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.FRANCE); - DateFormat hour = DateFormat.getTimeInstance(DateFormat.MEDIUM, Locale.FRANCE); - - String affDate = date.format(d) + " à " + hour.format(d); + String senderAff = new MessageSender(message.getSourceNick()).getDisplayName(); - List tooltipLines = Arrays.asList(new String[] { - ChatColor.GRAY+"(Cliquez pour répondre)", - ChatColor.GRAY+affDate, - }); - - - if (dispToSender) { - - String destAff = (message.getDestGroup() == null)? - ( (message.getDestNick().equalsIgnoreCase(message.getViewerNick())) ? - "moi": - new OffPlayer(message.getDestNick()).getDisplayName() - ): - "g:"+message.getDestGroupElement().getGroupName(); - - String destCommand = (message.getDestGroup() == null)? - message.getDestNick(): - "g:"+message.getDestGroupElement().getGroupName(); - - - new FancyMessage("") - .then("§6<§rmoi§6 → §r"+destAff+"§r§6>") - .suggest("/m "+destCommand+" ") - .tooltip(tooltipLines) - .then(" §6"+ChatColor.translateAlternateColorCodes('&', message.getMessage())) - .color(ChatColor.GOLD) - .send(op.getPlayer()); - } - else { - - - String senderAff = new MessageSender(message.getSourceNick()).getDisplayName(); - - - String destAff = (message.getDestGroup() == null)? - ((message.getDestNick().equalsIgnoreCase(message.getViewerNick()))? - "moi": - new OffPlayer(message.getDestNick()).getDisplayName()): - "g:"+message.getDestGroupElement().getGroupName(); - - String destCommand = (message.getDestGroup() == null)? - message.getSourceNick(): - "g:"+message.getDestGroupElement().getGroupName(); - - - FancyMessage fm = new FancyMessage("") - .then("§6<§r"+senderAff+"§r§6 → §r"+destAff+"§r§6>"); - if (message.getSourceNick() != null) - fm.suggest("/m "+destCommand+" "); - fm.tooltip(tooltipLines) - .then(" §6"+ChatColor.translateAlternateColorCodes('&', message.getMessage())) - .color(ChatColor.GOLD) - .send(op.getPlayer()); - - // op.getPlayer().playSound(op.getPlayer().getLocation(), Sound.ORB_PICKUP, 1, 0.3F); - } + String destAff = (message.getDestGroup() == null)? + ((message.getDestNick().equalsIgnoreCase(message.getViewerNick()))? + "moi": + new OffPlayer(message.getDestNick()).getDisplayName()): + "g:"+message.getDestGroupElement().getGroupName(); + + String destCommand = (message.getDestGroup() == null)? + message.getSourceNick(): + "g:"+message.getDestGroupElement().getGroupName(); + if (message.getSourceNick() != null) + tooltipLines.add(0, ChatColor.GRAY+"(Cliquez pour répondre)"); + FancyMessage fm = new FancyMessage("") + .then("§6<§r"+senderAff+"§r§6 → §r"+destAff+"§r§6>"); + if (message.getSourceNick() != null) + fm.suggest("/m "+destCommand+" "); + fm.tooltip(tooltipLines) + .then(" §6"+ChatColor.translateAlternateColorCodes('&', message.getMessage())) + .color(ChatColor.GOLD) + .send(op.getPlayer()); + // op.getPlayer().playSound(op.getPlayer().getLocation(), Sound.ORB_PICKUP, 1, 0.3F); } + + } catch (SQLException e) { plugin.getLogger().severe("Impossible d'afficher un message"); @@ -458,6 +485,55 @@ public class PrivateMessagesManager extends BukkitRunnable { + private PlayerStatut getPlayerStatut(String playerName) { + if (!AbstractCommandExecutor.isValidPlayerName(playerName)) + return PlayerStatut.OFFLINE; + OnlinePlayer op = OnlinePlayerManager.get(playerName); + if (op != null) + return op.isAfk() ? PlayerStatut.AFK_IG : PlayerStatut.ONLINE_IG; + + try { + MPWebSessionElement webSession = ((MPWebSessionTable)ORM.getTable("mp_web_session")).getFirst("playerName LIKE '"+playerName+"'", null); + + if (webSession != null) { + long lastWebActivity = webSession.getLastWebActivity(); + + if (System.currentTimeMillis() - lastWebActivity < TIMEOUT_WEB_SESSION) + return PlayerStatut.ONLINE_WEB; + + } + + } catch (SQLException e) { + e.printStackTrace(); + } + return PlayerStatut.OFFLINE; + } + + + private String displayablePlayerStatus(PlayerStatut ps) { + if (ps == PlayerStatut.ONLINE_IG) + return ChatColor.GREEN+"En ligne, IG"; + if (ps == PlayerStatut.AFK_IG) + return ChatColor.YELLOW+"AFK, IG"; + if (ps == PlayerStatut.ONLINE_WEB) + return ChatColor.GOLD+"En ligne, web"; + if (ps == PlayerStatut.OFFLINE) + return ChatColor.GRAY+"Hors ligne"; + return ChatColor.WHITE+"N/A"; + } + + + + + + public enum PlayerStatut { + ONLINE_IG, AFK_IG, ONLINE_WEB, OFFLINE + } + + + + + public static class MessageSender { public final String senderName; diff --git a/src/net/mc_pandacraft/java/plugin/pandacraftutils/players/OnlinePlayerManager.java b/src/net/mc_pandacraft/java/plugin/pandacraftutils/players/OnlinePlayerManager.java index 1a60cd7..ccac29d 100644 --- a/src/net/mc_pandacraft/java/plugin/pandacraftutils/players/OnlinePlayerManager.java +++ b/src/net/mc_pandacraft/java/plugin/pandacraftutils/players/OnlinePlayerManager.java @@ -9,6 +9,7 @@ import java.util.Map; import net.mc_pandacraft.java.plugin.pandacraftutils.PandacraftUtils; +import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; @@ -74,11 +75,20 @@ public final class OnlinePlayerManager { - + public synchronized static OnlinePlayer get(Player p) { return getInstance().players.get(p); } + /** + * Insensible à la casse + * @param p le pseudo du joueur recherché + * @return + */ + public static OnlinePlayer get(String p) { + return get(Bukkit.getServer().getPlayerExact(p)); + } + public synchronized static List getAll() { return new ArrayList(getInstance().players.values()); }