package com.github.danielpbarron.EconomyCraft; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Set; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.AsyncPlayerChatEvent; public class ChatListener implements Listener { public ChatListener (EconomyCraft plugin) { plugin.getServer().getPluginManager().registerEvents(this, plugin); } @EventHandler public void onPlayerChat (AsyncPlayerChatEvent event) { Player sender = event.getPlayer(); Set recipients = event.getRecipients(); String message = event.getMessage(); switch (message.charAt(message.length()-1)) { case '.': break; case '!': break; case '?': break; case ',': break; case ';': break; case ':': break; default: message += "."; break; } event.setCancelled(true); int max_distance = (int) Math.pow(16*8, 2); try { Class.forName("com.mysql.jdbc.Driver").newInstance(); } catch (Exception ex) { } Connection connection = null; Statement statement = null; try { connection = DriverManager.getConnection(EconomyCraft.MYSQL); statement = connection.createStatement(); int speaker_id = Db.getPlayerId(statement, sender); String speaker_nick = Db.getPlayerNick(statement, speaker_id); ResultSet results = null; int incomprehensible = 0; int too_far = 0; int listeners = 0; for (Player recipient : recipients) { if (sender.getName().equals(recipient.getName())) { recipient.sendMessage(String.format("You say, \"%s\"", message)); } else { listeners++; if (sender.getWorld().equals(recipient.getWorld())) { String new_message = ""; int distance = (int) Math.floor(sender.getLocation().distanceSquared(recipient.getLocation())); if (distance < max_distance) { String[] alphabet = message.replaceAll("[^a-z]", "").split(""); int listener_id = Db.getPlayerId(statement, recipient); int characters = 0; results = statement.executeQuery(String.format("select characters from language where speaker=%s and listener=%s", speaker_id, listener_id)); boolean update = false; while (results.next()) { characters = results.getInt("characters"); update = true; } results.close(); int unchanged = 0; int changed = 0; for (String character : message.split("")) { if (character.equals("") || character.equals(" ") || character.equals(".") || character.equals("?") || character.equals("!") || character.equals(",") || character.equals(";") || character.equals(":")) { new_message += character; } else if (Math.random()*max_distance < distance) { new_message += "..."; } else { if (Math.random()*characters > Math.pow(message.length(),2)) { new_message += character; unchanged++; } else { new_message += alphabet[(int)Math.floor(Math.random()*alphabet.length)]; changed++; } } } if (new_message.length() > 1 && (unchanged > 0 || changed < 6)) { if (changed < 6 && unchanged < 1) { characters += changed; } else { characters += unchanged; } } else { incomprehensible++; } recipient.sendMessage(String.format("%s says, \"%s\"", speaker_nick, new_message)); if (update) { statement.execute(String.format("update language set characters=%s where speaker=%s and listener=%s", characters, speaker_id, listener_id)); } else { statement.execute(String.format("insert into language (characters, speaker, listener) values (%s, %s, %s)", characters, speaker_id, listener_id)); } } else { too_far++; } } else { too_far++; } } } if (too_far == listeners) { sender.sendMessage("Nobody can hear you.."); } else if (incomprehensible == listeners - too_far) { sender.sendMessage("Nobody can understand you.."); } } catch (SQLException ex) { System.out.println("SQLException: "+ex.getMessage()); System.out.println("SQLState: "+ex.getSQLState()); System.out.println("VendorError: "+ex.getErrorCode()); event.setCancelled(false); } finally { if (connection != null) { try { connection.close(); } catch (SQLException sqlEx) { } connection = null; } if (statement != null) { try { statement.close(); } catch (SQLException sqlEx) { } statement = null; } } } }