Mise en place de la calculatrice utilisable depuis le chat : précéder l'expression mathématique par un =

This commit is contained in:
Marc Baloup 2014-12-26 13:44:49 -05:00
parent 6f05cf681f
commit 5990672dfb
3 changed files with 139 additions and 15 deletions

View File

@ -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<Player, List<HistoryElement>> history = new HashMap<Player, List<HistoryElement>>();
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<HistoryElement>());
}
@EventHandler
public void onPlayerQuit(PlayerQuitEvent event)
{
history.remove(event.getPlayer());
}
private class HistoryElement
{
public String expression;
public String value;
}
}

View File

@ -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 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) public void onAsyncPlayerChat(AsyncPlayerChatEvent event)
{ {
try { try {

View File

@ -449,21 +449,12 @@ public class JArithmeticInterpreter {
//.................................................................................... Write_Tree //.................................................................................... Write_Tree
public String writeTree() {
StringBuffer sb = new StringBuffer();
writeTree(sb);
return sb.toString();
}
private void writeTree(StringBuffer string) { private void writeTree(StringBuffer string) {
boolean parenthese=false; boolean parenthese=false;
switch(mOperator) { switch(mOperator) {
case 0: case 0:
string.append(mValue); string.append(StringUtil.formatDouble(mValue));
break; break;
case 1: case 1:
fg.writeTree(string); 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); StringBuffer input = new StringBuffer(expr);
@ -749,16 +740,29 @@ public class JArithmeticInterpreter {
if (jai==null) if (jai==null)
throw new IllegalArgumentException("Le calcul passé en paramètre est invalide"); throw new IllegalArgumentException("Le calcul passé en paramètre est invalide");
if (writeTree != null)
{
writeTree.setLength(0);
jai.writeTree(writeTree);
}
return jai.computeTree(); return jai.computeTree();
} }
/* public static double getResultFromExpression(String expr)
{
return getResultFromExpression(expr, null);
}
public static void main(String args[]) { 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(disp_res);
} //*/ System.out.println(b);
} // */
} }