Prepping filterChat and CoreClientManager for mysql

Removed get all clients at server start.
This commit is contained in:
Jonathan Williams 2013-10-23 16:36:13 -05:00
parent 985ce795d2
commit 21128eb153
14 changed files with 429 additions and 42 deletions

Binary file not shown.

View File

@ -11,8 +11,6 @@ public class CoreClient
private Player _player;
private Rank _rank;
private boolean _filterChat;
public CoreClient(Player player)
{
_player = player;
@ -55,16 +53,6 @@ public class CoreClient
_accountId = accountId;
}
public void SetFilterChat(Boolean filterChat)
{
_filterChat = filterChat;
}
public boolean GetFilterChat()
{
return _filterChat;
}
public Rank GetRank()
{
return _rank;

View File

@ -1,10 +1,12 @@
package mineplex.core.account;
import java.sql.Connection;
import java.util.HashSet;
import mineplex.core.account.event.AsyncClientLoadEvent;
import mineplex.core.account.event.ClientUnloadEvent;
import mineplex.core.account.event.ClientWebResponseEvent;
import mineplex.core.account.event.RetrieveClientInformationEvent;
import mineplex.core.account.repository.AccountRepository;
import mineplex.core.account.repository.token.ClientToken;
import mineplex.core.common.Rank;
@ -131,6 +133,8 @@ public class CoreClientManager implements Listener
private void LoadClient(CoreClient client, String ipAddress)
{
// Prep for mysql
ClientToken token = null;
Gson gson = new Gson();
@ -142,6 +146,31 @@ public class CoreClientManager implements Listener
Bukkit.getServer().getPluginManager().callEvent(new ClientWebResponseEvent(response));
Bukkit.getServer().getPluginManager().callEvent(new AsyncClientLoadEvent(token, client));
Connection connection = null;
try
{
Bukkit.getServer().getPluginManager().callEvent(new RetrieveClientInformationEvent(connection));
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
if (connection != null)
{
try
{
connection.close();
}
catch (Exception exception)
{
exception.printStackTrace();
}
}
}
}
@EventHandler(priority = EventPriority.LOWEST)

View File

@ -0,0 +1,33 @@
package mineplex.core.account.event;
import java.sql.Connection;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
public class RetrieveClientInformationEvent extends Event
{
private static final HandlerList handlers = new HandlerList();
private Connection _connection;
public RetrieveClientInformationEvent(Connection connection)
{
_connection = connection;
}
public Connection getConnection()
{
return _connection;
}
public HandlerList getHandlers()
{
return handlers;
}
public static HandlerList getHandlerList()
{
return handlers;
}
}

View File

@ -1,7 +1,5 @@
package mineplex.core.account.repository;
import java.util.List;
import mineplex.core.account.repository.token.LoginToken;
import mineplex.core.account.repository.token.RankUpdateToken;
import mineplex.core.common.Rank;
@ -17,12 +15,6 @@ public class AccountRepository
{
_webAddress = webAddress;
}
@SuppressWarnings("unchecked")
public List<String> GetAllClientNames()
{
return new JsonWebCall(_webAddress + "PlayerAccount/GetAccountNames").Execute(List.class);
}
public String GetClient(String name, String ipAddress)
{

View File

@ -0,0 +1,38 @@
package mineplex.core.account.repository;
import mineplex.core.mysql.RepositoryBase;
public class MysqlAccountRepository extends RepositoryBase
{
private static String CREATE_ACCOUNT_TABLE = "CREATE TABLE IF NOT EXISTS Accounts (id INT NOT NULL AUTO_INCREMENT, name VARCHAR(40), gems INT, rank VARCHAR(40), rankPerm BOOL, rankExpire LONG, lastLogin LONG, totalPlayTime LONG, PRIMARY KEY (id));";
public MysqlAccountRepository(String connectionUrl, String username, String password)
{
super(connectionUrl, username, password);
}
/*
public String GetClient(String name, String ipAddress)
{
return "";
}
public void SaveRank(Callback<Rank> callback, String name, Rank rank, boolean perm)
{
RankUpdateToken token = new RankUpdateToken();
token.Name = name;
token.Rank = rank.toString();
token.Perm = perm;
//new AsyncJsonWebCall(_webAddress + "PlayerAccount/RankUpdate").Execute(Rank.class, callback, token);
}
*/
@Override
protected void initialize()
{
executeQuery(CREATE_ACCOUNT_TABLE);
}
@Override
protected void update() { }
}

View File

@ -1,12 +1,14 @@
package mineplex.core.chat;
import mineplex.core.MiniPlugin;
import mineplex.core.MiniClientPlugin;
import mineplex.core.recharge.Recharge;
import mineplex.core.updater.event.UpdateEvent;
import mineplex.core.updater.UpdateType;
import mineplex.core.account.CoreClientManager;
import mineplex.core.account.event.RetrieveClientInformationEvent;
import mineplex.core.chat.command.BroadcastCommand;
import mineplex.core.chat.command.SilenceCommand;
import mineplex.core.chat.repository.ChatRepository;
import mineplex.core.common.Rank;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilPlayer;
@ -20,9 +22,10 @@ import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.plugin.java.JavaPlugin;
public class Chat extends MiniPlugin
public class Chat extends MiniClientPlugin<ChatClient>
{
private CoreClientManager _clientManager;
private ChatRepository _repository;
private long _silenced = 0;
@ -31,6 +34,7 @@ public class Chat extends MiniPlugin
super("Chat", plugin);
_clientManager = clientManager;
//_repository = new ChatRepository(plugin);
}
@Override
@ -40,6 +44,12 @@ public class Chat extends MiniPlugin
AddCommand(new BroadcastCommand(this));
}
@EventHandler
public void retrieveClientInformation(RetrieveClientInformationEvent event)
{
_repository.loadClientInformation(event.getConnection());
}
public void Silence(long duration, boolean inform)
{
//Set Silenced
@ -68,6 +78,11 @@ public class Chat extends MiniPlugin
event.getPlayer().sendMessage(F.main(GetName(), "Quite full of yourself aren't you? Nobody cares."));
event.setCancelled(true);
}
else if (event.getMessage().startsWith("/tell"))
{
event.getPlayer().sendMessage(F.main(GetName(), "Step back bro, thats not a command on this server!"));
event.setCancelled(true);
}
}
@EventHandler
@ -130,4 +145,10 @@ public class Chat extends MiniPlugin
{
return _silenced;
}
@Override
protected ChatClient AddPlayer(String player)
{
return new ChatClient();
}
}

View File

@ -0,0 +1,6 @@
package mineplex.core.chat.repository;
public class ChatClientToken
{
}

View File

@ -0,0 +1,163 @@
package mineplex.core.chat.repository;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.plugin.java.JavaPlugin;
import mineplex.core.mysql.AccountPreferenceRepository;
public class ChatRepository extends AccountPreferenceRepository
{
private static String ALTER_ACCOUNT_PREFERENCE_TABLE = "SET @s = (SELECT IF((SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'accountPreferences' AND table_schema = DATABASE() AND column_name = 'filterChat') > 0, 'SELECT 1', 'ALTER TABLE accountPreferences ADD filterChat BOOL')); PREPARE stmt FROM @s; EXECUTE stmt;";
private static String CREATE_FILTERED_TABLE = "CREATE TABLE IF NOT EXISTS filteredWords (id INT NOT NULL AUTO_INCREMENT, word VARCHAR(256), PRIMARY KEY (id));";
private static String RETRIEVE_FILTERED_WORDS = "SELECT word FROM filteredWords;";
private static String SAVE_FILTER_VALUE = "REPLACE INTO accountPreferences (filterChat) VALUES (?) WHERE playerName = ?;";
public ChatRepository(JavaPlugin plugin)
{
super(plugin);
}
protected void initialize()
{
super.initialize();
executeQuery(CREATE_FILTERED_TABLE);
}
protected void update()
{
super.update();
executeQuery(ALTER_ACCOUNT_PREFERENCE_TABLE);
}
public List<String> retrieveFilteredWords()
{
List<String> filteredWords = new ArrayList<String>();
Connection connection = null;
ResultSet resultSet = null;
PreparedStatement preparedStatement = null;
try
{
connection = getConnection();
preparedStatement = connection.prepareStatement(RETRIEVE_FILTERED_WORDS);
resultSet = preparedStatement.executeQuery();
while (resultSet.next())
{
filteredWords.add(resultSet.getString(1));
}
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
if (preparedStatement != null)
{
try
{
preparedStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
if (resultSet != null)
{
try
{
resultSet.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
if (connection != null)
{
try
{
connection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
return filteredWords;
}
public void saveFilterChat(String playerName, boolean filterChat)
{
Connection connection = null;
PreparedStatement preparedStatement = null;
int affectedRows = 0;
try
{
connection = getConnection();
preparedStatement = connection.prepareStatement(SAVE_FILTER_VALUE);
preparedStatement.setString(1, playerName);
affectedRows = preparedStatement.executeUpdate();
if (affectedRows == 0)
{
System.out.println("Error saving FilterChat option.");
}
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
if (preparedStatement != null)
{
try
{
preparedStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
if (connection != null)
{
try
{
connection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
public void loadClientInformation(Connection connection)
{
// TODO Auto-generated method stub
}
}

View File

@ -0,0 +1,22 @@
package mineplex.core.mysql;
import org.bukkit.plugin.java.JavaPlugin;
public class AccountPreferenceRepository extends RepositoryBase
{
private static String CREATE_ACCOUNT_PREFERENCE_TABLE = "CREATE TABLE IF NOT EXISTS AccountPreferences (id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (id));";
public AccountPreferenceRepository(JavaPlugin plugin)
{
super(plugin);
}
@Override
protected void initialize()
{
executeQuery(CREATE_ACCOUNT_PREFERENCE_TABLE);
}
@Override
protected void update() { }
}

View File

@ -0,0 +1,114 @@
package mineplex.core.mysql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.bukkit.plugin.java.JavaPlugin;
public abstract class RepositoryBase
{
private String _connectionString;
private String _userName;
private String _password;
public RepositoryBase(JavaPlugin plugin)
{
_connectionString = plugin.getConfig().getString("serverstatus.connectionurl");
_userName = plugin.getConfig().getString("serverstatus.username");
_password = plugin.getConfig().getString("serverstatus.password");
initialize();
update();
}
protected abstract void initialize();
protected abstract void update();
protected Connection getConnection() throws SQLException
{
return DriverManager.getConnection(_connectionString, _userName, _password);
}
protected int executeQuery(String query)
{
Connection connection = null;
PreparedStatement preparedStatement = null;
int affectedRows = 0;
try
{
connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = connection.prepareStatement(query);
affectedRows = preparedStatement.executeUpdate();
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
if (preparedStatement != null)
{
try
{
preparedStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
if (connection != null)
{
try
{
connection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
return affectedRows;
}
protected int executeStatement(PreparedStatement preparedStatement)
{
Connection connection = null;
int affectedRows = 0;
try
{
connection = DriverManager.getConnection(_connectionString, _userName, _password);
affectedRows = preparedStatement.executeUpdate();
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
if (connection != null)
{
try
{
connection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
return affectedRows;
}
}

View File

@ -52,25 +52,6 @@ public class Punish extends MiniPlugin
AddCommand(new PunishCommand(this));
}
@EventHandler
public void OnClientWebRequest(ClientWebRequestEvent event)
{
/*
try
{
// TODO Parse infractions/punishments here
// event.GetJsonWriter().beginObject();
// event.GetJsonWriter().name("Punish");
// event.GetJsonWriter().value("true");
// event.GetJsonWriter().endObject();
}
catch (IOException e)
{
e.printStackTrace();
}
*/
}
@EventHandler
public void OnClientWebResponse(ClientWebResponseEvent event)
{

View File

@ -72,7 +72,7 @@ public class Arcade extends JavaPlugin implements INautilusPlugin, IPlugin
private ConditionManager _condition;
private Creature _creature;
private Fire _fire;
private Logger _logger;
private Logger _logger;
private LootFactory _lootFactory;
private Observer _observer;
private PetManager _petManager;

Binary file not shown.