From 0432d1097507ff5e7ee4372952b00af0515e53b7 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Thu, 14 May 2015 18:41:32 +0200 Subject: [PATCH] =?UTF-8?q?Possibilit=C3=A9=20de=20r=C3=A9cup=C3=A9rer=20l?= =?UTF-8?q?es=20derni=C3=A8res=20discussion=20d'un=20joueur=20donn=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commands/CommandAnimal.java | 2 +- .../data_model/MPMessageTable.java | 13 ++- .../pandacraftutils/data_model/ORM.java | 12 +++ .../modules/PrivateMessagesManager.java | 90 ++++++++++++++++++- 4 files changed, 110 insertions(+), 7 deletions(-) diff --git a/src/net/mc_pandacraft/java/plugin/pandacraftutils/commands/CommandAnimal.java b/src/net/mc_pandacraft/java/plugin/pandacraftutils/commands/CommandAnimal.java index d84c0f9..5f13abd 100644 --- a/src/net/mc_pandacraft/java/plugin/pandacraftutils/commands/CommandAnimal.java +++ b/src/net/mc_pandacraft/java/plugin/pandacraftutils/commands/CommandAnimal.java @@ -161,7 +161,7 @@ public class CommandAnimal extends AbstractCommandExecutor { else p.sendMessage(ChatColor.GOLD+"Propriétaire : "+ChatColor.GRAY+"Non apprivoisé"); p.sendMessage(ChatColor.GOLD+"Nom personnalisé : "+ChatColor.RESET+((animal.getCustomName() != null) ? animal.getCustomName() : ChatColor.GRAY+"Aucun")); - p.sendMessage(ChatColor.GOLD+"Point de vie : "+ChatColor.GRAY+((int)((Damageable)animal).getHealth())+"/"+((int)((Damageable)animal).getMaxHealth())+ChatColor.GOLD+" (2 points = 1 "+ChatColor.RED+"♥"+ChatColor.GOLD+")"); + p.sendMessage(ChatColor.GOLD+"Point de vie : "+ChatColor.GRAY+((int)((Damageable)animal).getHealth())+"/"+((int)((Damageable)animal).getMaxHealth())+ChatColor.GOLD+" (2 points = 1 "+ChatColor.RED+"❤"+ChatColor.GOLD+")"); p.sendMessage(ChatColor.GOLD+"Respiration sous l'eau : "+ChatColor.GRAY+animal.getRemainingAir()+"/"+animal.getMaximumAir()); if (animal instanceof Horse) { diff --git a/src/net/mc_pandacraft/java/plugin/pandacraftutils/data_model/MPMessageTable.java b/src/net/mc_pandacraft/java/plugin/pandacraftutils/data_model/MPMessageTable.java index 3e5a042..f52c0be 100644 --- a/src/net/mc_pandacraft/java/plugin/pandacraftutils/data_model/MPMessageTable.java +++ b/src/net/mc_pandacraft/java/plugin/pandacraftutils/data_model/MPMessageTable.java @@ -58,11 +58,20 @@ public class MPMessageTable extends SQLTable { } - + public List getAllUnreadForPlayer(String player) throws SQLException { + return getForPlayer(player, true, null); + } + + + public List getForPlayer(String player, boolean onlyUnread, Integer numberLast) throws SQLException { if (!AbstractCommandExecutor.isValidPlayerName(player)) return null; - return getAll("viewerNick = '"+player+"' AND wasRead = 0", "time ASC", null, null); + String where = "viewerNick = '"+player+"'"; + if (onlyUnread) + where += " AND wasRead = 0"; + + return getAll(where, "time ASC", numberLast, null); } diff --git a/src/net/mc_pandacraft/java/plugin/pandacraftutils/data_model/ORM.java b/src/net/mc_pandacraft/java/plugin/pandacraftutils/data_model/ORM.java index d1833f0..4a70f5a 100644 --- a/src/net/mc_pandacraft/java/plugin/pandacraftutils/data_model/ORM.java +++ b/src/net/mc_pandacraft/java/plugin/pandacraftutils/data_model/ORM.java @@ -62,6 +62,18 @@ public final class ORM { return tables.get(name); } + @SuppressWarnings("rawtypes") + public synchronized static T getTable(Class c) { + if (c == null) return null; + for (SQLTable table : tables.values()) { + + if (c.isAssignableFrom(table.getClass())) { + return c.cast(table); + } + } + return null; + } + 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 daf8ec9..f6a1186 100644 --- a/src/net/mc_pandacraft/java/plugin/pandacraftutils/modules/PrivateMessagesManager.java +++ b/src/net/mc_pandacraft/java/plugin/pandacraftutils/modules/PrivateMessagesManager.java @@ -24,12 +24,15 @@ import net.mc_pandacraft.java.plugin.pandacraftutils.players.OffPlayer; import net.mc_pandacraft.java.plugin.pandacraftutils.players.OnlinePlayer; import net.mc_pandacraft.java.plugin.pandacraftutils.players.OnlinePlayerManager; +import org.apache.commons.lang.Validate; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitRunnable; +import com.google.common.collect.Lists; + import de.luricos.bukkit.xAuth.events.xAuthLoginEvent; public class PrivateMessagesManager extends BukkitRunnable { @@ -289,6 +292,84 @@ public class PrivateMessagesManager extends BukkitRunnable { + public List getLastDiscussions(Player p, Integer nbLast) { + try { + // récupère les messages, triés de la plus vieille à la plus récente + List messages = ORM.getTable(MPMessageTable.class).getForPlayer(p.getName(), false, 1000); + + // inverse, de la plus récente à la plus vieille + messages = Lists.reverse(messages); + + List discussionList = new ArrayList(); + + for(MPMessageElement message : messages) { + String name; + if (message.getDestGroup() != null) + name = "g:"+message.getDestGroupElement().getGroupName(); + else + name = message.getSourceNick(); + + PrivateDiscussion pvd = new PrivateDiscussion(name, (message.isRead())?0:1, new Date(message.getTime())); + + int index; + if ((index = discussionList.indexOf(pvd)) >= 0) { + if (!message.isRead()) + discussionList.get(index).nbUnread++; + } + else + discussionList.add(pvd); + + } + + if (nbLast != null && nbLast >= 0) { + while (discussionList.size()>nbLast.intValue()) { + discussionList.remove(discussionList.size()-1); + } + } + + + return discussionList; + + } catch (SQLException e) { + e.printStackTrace(); + return null; + } + } + + + + + public class PrivateDiscussion { + /** + * Le pseudo du joueur ou du groupe de discussion (avec "g:" devant) + */ + public final String name; + public int nbUnread; + public Date timeLastMessage; + + private PrivateDiscussion(String n, int unread, Date lastMess) { + Validate.notNull(n, "n ne doit pas être null"); + name = n; + nbUnread = unread; + timeLastMessage = lastMess; + } + + @Override + public boolean equals(Object o) { + if (o == null || !(o instanceof PrivateDiscussion)) + return false; + + return ((PrivateDiscussion) o).name.equalsIgnoreCase(name); + } + } + + + + + + + + @@ -378,11 +459,12 @@ public class PrivateMessagesManager extends BukkitRunnable { tooltipLines.add(0, ChatColor.GRAY+"(Cliquez pour répondre)"); new FancyMessage("") - .then("§6<§rmoi§6 → §r"+destAff+"§r§6>") + .then("§d<§rmoi§d → §r"+destAff+"§r§d>") .suggest("/m "+destCommand+" ") .tooltip(tooltipLines) - .then(" §6"+ChatColor.translateAlternateColorCodes('&', message.getMessage()).replace("§r", "§r§6")) + .then(" §6§o"+ChatColor.translateAlternateColorCodes('&', message.getMessage()).replace("§r", "§r§6§o")) .color(ChatColor.GOLD) + .style(ChatColor.ITALIC) .send(op.getPlayer()); } else { @@ -405,7 +487,7 @@ public class PrivateMessagesManager extends BukkitRunnable { tooltipLines.add(0, ChatColor.GRAY+"(Cliquez pour répondre)"); FancyMessage fm = new FancyMessage("") - .then("§6<§r"+senderAff+"§r§6 → §r"+destAff+"§r§6>"); + .then("§d<§r"+senderAff+"§r§d → §r"+destAff+"§r§d>"); if (message.getSourceNick() != null) fm.suggest("/m "+destCommand+" "); fm.tooltip(tooltipLines) @@ -441,7 +523,7 @@ public class PrivateMessagesManager extends BukkitRunnable { ChatColor.GOLD+"g:"+message.getDestGroupElement().getGroupName(); - plugin.getServer().getConsoleSender().sendMessage("§6<§r"+senderAff+"§r§6 → §r"+destAff+"§r§6> §6"+ChatColor.translateAlternateColorCodes('&', message.getMessage()).replace("§r", "§r§6")); + plugin.getServer().getConsoleSender().sendMessage("§d<§r"+senderAff+"§r§d → §r"+destAff+"§r§d> §6"+ChatColor.translateAlternateColorCodes('&', message.getMessage()).replace("§r", "§r§6")); } catch (SQLException e) {