Masquage des messages privés avant le /login + ajout des informations dans le tooltip des messages privés

This commit is contained in:
Marc Baloup 2015-05-12 03:13:01 +02:00
parent 19960a6dfe
commit a5d5ff78fd
2 changed files with 158 additions and 72 deletions

View File

@ -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;
@ -34,6 +36,9 @@ public class PrivateMessagesManager extends BukkitRunnable {
private PandacraftUtils plugin = PandacraftUtils.getInstance();
private static final long TIMEOUT_WEB_SESSION = 10000; // msec
public PrivateMessagesManager()
{
@ -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,14 +311,23 @@ public class PrivateMessagesManager extends BukkitRunnable {
if (isIgnoring) {
message.delete();
return;
}
else {
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);
@ -325,11 +335,27 @@ public class PrivateMessagesManager extends BukkitRunnable {
String affDate = date.format(d) + " à " + hour.format(d);
List<String> tooltipLines = new ArrayList<String>();
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();
List<String> tooltipLines = Arrays.asList(new String[] {
ChatColor.GRAY+"(Cliquez pour répondre)",
ChatColor.GRAY+affDate,
});
tooltipLines.add(ChatColor.GRAY+"vers le groupe "+ChatColor.GOLD+groupEl.getGroupName());
List<MPGroupUserElement> 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) {
@ -345,6 +371,7 @@ public class PrivateMessagesManager extends BukkitRunnable {
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>")
@ -370,6 +397,8 @@ public class PrivateMessagesManager extends BukkitRunnable {
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>");
@ -385,8 +414,6 @@ public class PrivateMessagesManager extends BukkitRunnable {
}
} catch (SQLException e) {
plugin.getLogger().severe("Impossible d'afficher un message");
e.printStackTrace();
@ -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;

View File

@ -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;
@ -79,6 +80,15 @@ public final class OnlinePlayerManager {
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<OnlinePlayer> getAll() {
return new ArrayList<OnlinePlayer>(getInstance().players.values());
}