From 5990672dfbcabbcab6ab5d8589147c6f20cd5b30 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Fri, 26 Dec 2014 13:44:49 -0500 Subject: [PATCH] =?UTF-8?q?Mise=20en=20place=20de=20la=20calculatrice=20ut?= =?UTF-8?q?ilisable=20depuis=20le=20chat=20:=20pr=C3=A9c=C3=A9der=20l'expr?= =?UTF-8?q?ession=20math=C3=A9matique=20par=20un=20=3D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../calculator/CalculatorManager.java | 120 ++++++++++++++++++ .../chat_analyzer/ChatAnalysisManager.java | 2 +- .../java/util/JArithmeticInterpreter.java | 32 +++-- 3 files changed, 139 insertions(+), 15 deletions(-) create mode 100644 src/net/mc_pandacraft/java/plugin/pandacraftutils/calculator/CalculatorManager.java diff --git a/src/net/mc_pandacraft/java/plugin/pandacraftutils/calculator/CalculatorManager.java b/src/net/mc_pandacraft/java/plugin/pandacraftutils/calculator/CalculatorManager.java new file mode 100644 index 0000000..64e1bbf --- /dev/null +++ b/src/net/mc_pandacraft/java/plugin/pandacraftutils/calculator/CalculatorManager.java @@ -0,0 +1,120 @@ +package net.mc_pandacraft.java.plugin.pandacraftutils.calculator; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import net.mc_pandacraft.java.plugin.pandacraftutils.PandacraftUtils; +import net.mc_pandacraft.java.util.JArithmeticInterpreter; +import net.mc_pandacraft.java.util.StringUtil; + +import org.bukkit.ChatColor; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.AsyncPlayerChatEvent; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerQuitEvent; + +public class CalculatorManager implements Listener { + + private PandacraftUtils plugin; + + private Map> history = new HashMap>(); + + + + public CalculatorManager(PandacraftUtils pl) { + plugin = pl; + + plugin.getServer().getPluginManager().registerEvents(this, plugin); + + // analyse des joueurs déjà en ligne (/reload) + for (Player p : plugin.getServer().getOnlinePlayers()) + onPlayerJoin(new PlayerJoinEvent(p, "")); // simule l'évènement d'arrivé d'un joueur, pour le rajouter + } + + + + + @EventHandler(priority=EventPriority.HIGHEST) + public void onAsyncPlayerChat(AsyncPlayerChatEvent event) + { + String message = event.getMessage(); + if (!message.startsWith("=")) + return; + + if (message.length() == 1) + { // affichage de l'historique + event.getPlayer().sendMessage(ChatColor.GRAY+"Historique d'utilisation de la calculatrice"); + int i = history.get(event.getPlayer()).size(); + for(HistoryElement el : history.get(event.getPlayer())) + { + event.getPlayer().sendMessage(ChatColor.GRAY+"res"+i+ChatColor.RESET+"="+ChatColor.GRAY+el.value+ChatColor.RESET+"="+ChatColor.GRAY+el.expression); + i--; + } + } + else + { + try { + message = message.substring(1); + + // TODO prendre en compte les variables "resX" + + + + StringBuffer expression = new StringBuffer(); + + HistoryElement calcul = new HistoryElement(); + calcul.value = StringUtil.formatDouble(JArithmeticInterpreter.getResultFromExpression(message, expression)); + calcul.expression = expression.toString(); + + history.get(event.getPlayer()).add(calcul); + if (history.get(event.getPlayer()).size() > 5) + history.get(event.getPlayer()).remove(0); + + event.getPlayer().sendMessage("="+ChatColor.GRAY+calcul.value); + } + catch (IllegalArgumentException e) { + event.getPlayer().sendMessage(ChatColor.RED+e.getMessage()); + + } + + } + + + + + event.setCancelled(true); + } + + + + + + @EventHandler + public void onPlayerJoin(PlayerJoinEvent event) + { + history.put(event.getPlayer(), new ArrayList()); + } + + + + @EventHandler + public void onPlayerQuit(PlayerQuitEvent event) + { + history.remove(event.getPlayer()); + } + + + + private class HistoryElement + { + public String expression; + public String value; + } + + +} diff --git a/src/net/mc_pandacraft/java/plugin/pandacraftutils/chat_analyzer/ChatAnalysisManager.java b/src/net/mc_pandacraft/java/plugin/pandacraftutils/chat_analyzer/ChatAnalysisManager.java index 965fdac..cc97af1 100644 --- a/src/net/mc_pandacraft/java/plugin/pandacraftutils/chat_analyzer/ChatAnalysisManager.java +++ b/src/net/mc_pandacraft/java/plugin/pandacraftutils/chat_analyzer/ChatAnalysisManager.java @@ -31,7 +31,7 @@ public class ChatAnalysisManager implements Listener { onPlayerJoin(new PlayerJoinEvent(p, "")); // simule l'évènement d'arrivé d'un joueur, pour le rajouter } - @EventHandler(priority=EventPriority.HIGHEST) + @EventHandler(priority=EventPriority.HIGH,ignoreCancelled=true) public void onAsyncPlayerChat(AsyncPlayerChatEvent event) { try { diff --git a/src/net/mc_pandacraft/java/util/JArithmeticInterpreter.java b/src/net/mc_pandacraft/java/util/JArithmeticInterpreter.java index bc91dc9..a81f04e 100644 --- a/src/net/mc_pandacraft/java/util/JArithmeticInterpreter.java +++ b/src/net/mc_pandacraft/java/util/JArithmeticInterpreter.java @@ -449,21 +449,12 @@ public class JArithmeticInterpreter { //.................................................................................... Write_Tree - public String writeTree() { - StringBuffer sb = new StringBuffer(); - writeTree(sb); - return sb.toString(); - } - - - - private void writeTree(StringBuffer string) { boolean parenthese=false; switch(mOperator) { case 0: - string.append(mValue); + string.append(StringUtil.formatDouble(mValue)); break; case 1: fg.writeTree(string); @@ -740,7 +731,7 @@ public class JArithmeticInterpreter { } - public static double getResultFromExpression(String expr) + public static double getResultFromExpression(String expr, StringBuffer writeTree) { StringBuffer input = new StringBuffer(expr); @@ -749,16 +740,29 @@ public class JArithmeticInterpreter { if (jai==null) throw new IllegalArgumentException("Le calcul passé en paramètre est invalide"); + if (writeTree != null) + { + writeTree.setLength(0); + jai.writeTree(writeTree); + } + return jai.computeTree(); } - /* + public static double getResultFromExpression(String expr) + { + return getResultFromExpression(expr, null); + } + public static void main(String args[]) { - String disp_res = StringUtil.formatDouble(JArithmeticInterpreter.getResultFromExpression("1245.25*2")); + StringBuffer b = new StringBuffer(0); + + String disp_res = StringUtil.formatDouble(JArithmeticInterpreter.getResultFromExpression("1245.25*2", b)); System.out.println(disp_res); - } //*/ + System.out.println(b); + } // */ }