Commit 94954de1 authored by Kenix Whisperwind's avatar Kenix Whisperwind

DatabaseConnection is now a non-static class member of Server

parent b941a6f8
......@@ -12,7 +12,10 @@ import com.openrsc.server.event.rsc.GameTickEvent;
import com.openrsc.server.event.rsc.impl.BankEventNpc;
import com.openrsc.server.event.rsc.impl.ProjectileEvent;
import com.openrsc.server.event.rsc.impl.RangeEventNpc;
import com.openrsc.server.external.*;
import com.openrsc.server.external.ItemDefinition;
import com.openrsc.server.external.ItemDropDef;
import com.openrsc.server.external.ItemLoc;
import com.openrsc.server.external.NPCDef;
import com.openrsc.server.model.Point;
import com.openrsc.server.model.container.Equipment;
import com.openrsc.server.model.container.Item;
......@@ -30,7 +33,6 @@ import com.openrsc.server.model.world.region.TileValue;
import com.openrsc.server.net.rsc.ActionSender;
import com.openrsc.server.plugins.Functions;
import com.openrsc.server.plugins.listeners.action.CommandListener;
import com.openrsc.server.sql.DatabaseConnection;
import com.openrsc.server.sql.query.logs.ChatLog;
import com.openrsc.server.sql.query.logs.StaffLog;
import com.openrsc.server.util.rsc.DataConversions;
......@@ -461,7 +463,7 @@ public final class Admins implements CommandListener {
}
ItemLoc item = new ItemLoc(id, x, y, amount, respawnTime);
DatabaseConnection.getDatabase()
player.getWorld().getServer().getDatabaseConnection()
.executeUpdate("INSERT INTO `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX
+ "grounditems`(`id`, `x`, `y`, `amount`, `respawn`) VALUES ('"
+ item.getId() + "','" + item.getX() + "','" + item.getY() + "','" + item.getAmount()
......@@ -512,7 +514,7 @@ public final class Admins implements CommandListener {
}
player.message(messagePrefix + "Removed ground item from database: " + itemr.getDef().getName() + " with item ID " + itemr.getID());
DatabaseConnection.getDatabase()
player.getWorld().getServer().getDatabaseConnection()
.executeUpdate("DELETE FROM `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX
+ "grounditems` WHERE `x` = '" + itemr.getX() + "' AND `y` = '" + itemr.getY()
+ "' AND `id` = '" + itemr.getID() + "'");
......
......@@ -9,7 +9,6 @@ import com.openrsc.server.model.world.World;
import com.openrsc.server.model.world.region.TileValue;
import com.openrsc.server.net.rsc.ActionSender;
import com.openrsc.server.plugins.listeners.action.CommandListener;
import com.openrsc.server.sql.DatabaseConnection;
import com.openrsc.server.util.rsc.DataConversions;
public final class Development implements CommandListener {
......@@ -84,7 +83,7 @@ public final class Development implements CommandListener {
World.getWorld().registerNpc(n);
n.setShouldRespawn(true);
player.message(messagePrefix + "Added NPC to database: " + n.getDef().getName() + " at " + npcLoc + " with radius " + radius);
DatabaseConnection.getDatabase().executeUpdate("INSERT INTO `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX
player.getWorld().getServer().getDatabaseConnection().executeUpdate("INSERT INTO `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX
+ "npclocs`(`id`,`startX`,`minX`,`maxX`,`startY`,`minY`,`maxY`) VALUES('" + n.getLoc().getId()
+ "', '" + n.getLoc().startX() + "', '" + n.getLoc().minX() + "', '" + n.getLoc().maxX() + "','"
+ n.getLoc().startY() + "','" + n.getLoc().minY() + "','" + n.getLoc().maxY() + "')");
......@@ -112,7 +111,7 @@ public final class Development implements CommandListener {
}
player.message(messagePrefix + "Removed NPC from database: " + npc.getDef().getName() + " with instance ID " + id);
DatabaseConnection.getDatabase()
player.getWorld().getServer().getDatabaseConnection()
.executeUpdate("DELETE FROM `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX
+ "npclocs` WHERE `id` = '" + npc.getID() + "' AND startX='" + npc.getLoc().startX
+ "' AND startY='" + npc.getLoc().startY + "' AND minX='" + npc.getLoc().minX
......@@ -166,7 +165,7 @@ public final class Development implements CommandListener {
}
player.message(messagePrefix + "Removed object from database: " + object.getGameObjectDef().getName() + " with instance ID " + object.getID());
DatabaseConnection.getDatabase()
player.getWorld().getServer().getDatabaseConnection()
.executeUpdate("DELETE FROM `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX
+ "objects` WHERE `x` = '" + object.getX() + "' AND `y` = '" + object.getY()
+ "' AND `id` = '" + object.getID() + "' AND `direction` = '" + object.getDirection()
......@@ -226,7 +225,7 @@ public final class Development implements CommandListener {
GameObject newObject = new GameObject(player.getWorld(), Point.location(x, y), id, 0, 0);
World.getWorld().registerGameObject(newObject);
player.message(messagePrefix + "Added object to database: " + newObject.getGameObjectDef().getName() + " with instance ID " + newObject.getID() + " at " + newObject.getLocation());
DatabaseConnection.getDatabase()
player.getWorld().getServer().getDatabaseConnection()
.executeUpdate("INSERT INTO `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX
+ "objects`(`x`, `y`, `id`, `direction`, `type`) VALUES ('"
+ newObject.getX() + "', '" + newObject.getY() + "', '" + newObject.getID() + "', '"
......@@ -298,7 +297,7 @@ public final class Development implements CommandListener {
direction = 8;
}
DatabaseConnection.getDatabase()
player.getWorld().getServer().getDatabaseConnection()
.executeUpdate("DELETE FROM `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "objects` WHERE `x` = '"
+ object.getX() + "' AND `y` = '" + object.getY() + "' AND `id` = '" + object.getID()
+ "' AND `direction` = '" + object.getDirection() + "' AND `type` = '" + object.getType()
......@@ -308,7 +307,7 @@ public final class Development implements CommandListener {
GameObject newObject = new GameObject(player.getWorld(), Point.location(x, y), object.getID(), direction, object.getType());
World.getWorld().registerGameObject(newObject);
DatabaseConnection.getDatabase()
player.getWorld().getServer().getDatabaseConnection()
.executeUpdate("INSERT INTO `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX
+ "objects`(`x`, `y`, `id`, `direction`, `type`) VALUES ('" + newObject.getX() + "', '"
+ newObject.getY() + "', '" + newObject.getID() + "', '" + newObject.getDirection() + "', '"
......
......@@ -9,7 +9,6 @@ import com.openrsc.server.model.entity.player.Player;
import com.openrsc.server.model.world.World;
import com.openrsc.server.net.rsc.ActionSender;
import com.openrsc.server.plugins.listeners.action.CommandListener;
import com.openrsc.server.sql.DatabaseConnection;
import com.openrsc.server.sql.query.logs.StaffLog;
import com.openrsc.server.util.rsc.DataConversions;
import org.apache.logging.log4j.LogManager;
......@@ -328,7 +327,7 @@ public final class Event implements CommandListener {
player.message(
messagePrefix + "No online character found named '" + targetUsername + "'.. checking database..");
try {
PreparedStatement statement = DatabaseConnection.getDatabase()
PreparedStatement statement = player.getWorld().getServer().getDatabaseConnection()
.prepareStatement("SELECT `login_ip` FROM `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players` WHERE `username`=?");
statement.setString(1, targetUsername);
ResultSet result = statement.executeQuery();
......@@ -354,7 +353,7 @@ public final class Event implements CommandListener {
}
try {
PreparedStatement statement = DatabaseConnection.getDatabase()
PreparedStatement statement = player.getWorld().getServer().getDatabaseConnection()
.prepareStatement("SELECT `username`, `group_id` FROM `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players` WHERE `login_ip` LIKE ?");
statement.setString(1, currentIp);
ResultSet result = statement.executeQuery();
......
......@@ -11,7 +11,6 @@ import com.openrsc.server.model.world.World;
import com.openrsc.server.net.rsc.ActionSender;
import com.openrsc.server.plugins.Functions;
import com.openrsc.server.plugins.listeners.action.CommandListener;
import com.openrsc.server.sql.DatabaseConnection;
import com.openrsc.server.sql.query.logs.ChatLog;
import com.openrsc.server.util.rsc.DataConversions;
import com.openrsc.server.util.rsc.MessageType;
......@@ -62,7 +61,7 @@ public final class RegularPlayer implements CommandListener {
return;
}
try {
PreparedStatement statement = DatabaseConnection.getDatabase().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
PreparedStatement statement = player.getWorld().getServer().getDatabaseConnection().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
statement.setString(1, player.getUsername());
ResultSet result = statement.executeQuery();
if (result.next()) {
......@@ -387,7 +386,7 @@ public final class RegularPlayer implements CommandListener {
} else if (Server.getServer().getConfig().NPC_KILL_LIST && cmd.equalsIgnoreCase("kills")) {
StringBuilder kills = new StringBuilder("NPC Kill List for " + player.getUsername() + " % %");
try {
PreparedStatement statement = DatabaseConnection.getDatabase().prepareStatement(
PreparedStatement statement = player.getWorld().getServer().getDatabaseConnection().prepareStatement(
"SELECT * FROM `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "npckills` WHERE playerID = ? ORDER BY killCount DESC LIMIT 16");
statement.setInt(1, player.getDatabaseID());
ResultSet result = statement.executeQuery();
......
......@@ -7,7 +7,6 @@ import com.openrsc.server.model.Point;
import com.openrsc.server.model.entity.player.Player;
import com.openrsc.server.model.world.World;
import com.openrsc.server.plugins.listeners.action.CommandListener;
import com.openrsc.server.sql.DatabaseConnection;
import com.openrsc.server.sql.query.logs.StaffLog;
import com.openrsc.server.util.rsc.DataConversions;
......@@ -228,7 +227,7 @@ public final class SuperModerator implements CommandListener {
player.message(messagePrefix + p.getUsername() + " has stage " + p.getQuestStage(quest) + " for quest " + quest);
} else if (cmd.equalsIgnoreCase("reloaddrops")) {
try {
PreparedStatement statement = DatabaseConnection.getDatabase().prepareStatement(
PreparedStatement statement = player.getWorld().getServer().getDatabaseConnection().prepareStatement(
"SELECT * FROM `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "npcdrops` WHERE npcdef_id = ?");
for (int i = 0; i < player.getWorld().getServer().getEntityHandler().npcs.size(); i++) {
statement.setInt(1, i);
......
......@@ -11,7 +11,6 @@ import com.openrsc.server.plugins.listeners.executive.NpcCommandExecutiveListene
import com.openrsc.server.plugins.listeners.executive.TalkToNpcExecutiveListener;
import com.openrsc.server.plugins.menu.Menu;
import com.openrsc.server.plugins.menu.Option;
import com.openrsc.server.sql.DatabaseConnection;
import com.openrsc.server.util.rsc.DataConversions;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
......@@ -40,7 +39,7 @@ public class Gundai implements TalkToNpcExecutiveListener, TalkToNpcListener, Np
return;
}
try {
PreparedStatement statement = DatabaseConnection.getDatabase().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
PreparedStatement statement = player.getWorld().getServer().getDatabaseConnection().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
statement.setString(1, player.getUsername());
ResultSet result = statement.executeQuery();
if (result.next()) {
......@@ -73,7 +72,7 @@ public class Gundai implements TalkToNpcExecutiveListener, TalkToNpcListener, Np
return;
}
try {
PreparedStatement statement = DatabaseConnection.getDatabase().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
PreparedStatement statement = player.getWorld().getServer().getDatabaseConnection().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
statement.setString(1, player.getUsername());
ResultSet result = statement.executeQuery();
if (result.next()) {
......@@ -95,7 +94,7 @@ public class Gundai implements TalkToNpcExecutiveListener, TalkToNpcListener, Np
return;
}
try {
PreparedStatement statement = DatabaseConnection.getDatabase().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
PreparedStatement statement = player.getWorld().getServer().getDatabaseConnection().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
statement.setString(1, player.getUsername());
ResultSet result = statement.executeQuery();
if (result.next()) {
......@@ -110,7 +109,7 @@ public class Gundai implements TalkToNpcExecutiveListener, TalkToNpcListener, Np
}
String changeTo = getBankPinInput(player);
try {
PreparedStatement statement = DatabaseConnection.getDatabase().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
PreparedStatement statement = player.getWorld().getServer().getDatabaseConnection().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
statement.setString(1, player.getUsername());
ResultSet result = statement.executeQuery();
if (result.next()) {
......@@ -156,7 +155,7 @@ public class Gundai implements TalkToNpcExecutiveListener, TalkToNpcListener, Np
return;
}
try {
PreparedStatement statement = DatabaseConnection.getDatabase().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
PreparedStatement statement = player.getWorld().getServer().getDatabaseConnection().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
statement.setString(1, player.getUsername());
ResultSet result = statement.executeQuery();
if (result.next()) {
......@@ -222,7 +221,7 @@ public class Gundai implements TalkToNpcExecutiveListener, TalkToNpcListener, Np
return;
}
try {
PreparedStatement statement = DatabaseConnection.getDatabase().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
PreparedStatement statement = player.getWorld().getServer().getDatabaseConnection().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
statement.setString(1, player.getUsername());
ResultSet result = statement.executeQuery();
if (result.next()) {
......
......@@ -9,7 +9,6 @@ import com.openrsc.server.plugins.listeners.action.NpcCommandListener;
import com.openrsc.server.plugins.listeners.action.TalkToNpcListener;
import com.openrsc.server.plugins.listeners.executive.NpcCommandExecutiveListener;
import com.openrsc.server.plugins.listeners.executive.TalkToNpcExecutiveListener;
import com.openrsc.server.sql.DatabaseConnection;
import com.openrsc.server.util.rsc.DataConversions;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
......@@ -70,7 +69,7 @@ public class Bankers implements TalkToNpcExecutiveListener, TalkToNpcListener, N
return;
}
try {
PreparedStatement statement = DatabaseConnection.getDatabase().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
PreparedStatement statement = player.getWorld().getServer().getDatabaseConnection().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
statement.setString(1, player.getUsername());
ResultSet result = statement.executeQuery();
if (result.next()) {
......@@ -111,7 +110,7 @@ public class Bankers implements TalkToNpcExecutiveListener, TalkToNpcListener, N
return;
}
try {
PreparedStatement statement = DatabaseConnection.getDatabase().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
PreparedStatement statement = player.getWorld().getServer().getDatabaseConnection().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
statement.setString(1, player.getUsername());
ResultSet result = statement.executeQuery();
if (result.next()) {
......@@ -132,7 +131,7 @@ public class Bankers implements TalkToNpcExecutiveListener, TalkToNpcListener, N
return;
}
try {
PreparedStatement statement = DatabaseConnection.getDatabase().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
PreparedStatement statement = player.getWorld().getServer().getDatabaseConnection().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
statement.setString(1, player.getUsername());
ResultSet result = statement.executeQuery();
if (result.next()) {
......@@ -147,7 +146,7 @@ public class Bankers implements TalkToNpcExecutiveListener, TalkToNpcListener, N
}
String changeTo = getBankPinInput(player);
try {
PreparedStatement statement = DatabaseConnection.getDatabase().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
PreparedStatement statement = player.getWorld().getServer().getDatabaseConnection().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
statement.setString(1, player.getUsername());
ResultSet result = statement.executeQuery();
if (result.next()) {
......@@ -170,7 +169,7 @@ public class Bankers implements TalkToNpcExecutiveListener, TalkToNpcListener, N
return;
}
try {
PreparedStatement statement = DatabaseConnection.getDatabase().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
PreparedStatement statement = player.getWorld().getServer().getDatabaseConnection().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
statement.setString(1, player.getUsername());
ResultSet result = statement.executeQuery();
if (result.next()) {
......@@ -214,7 +213,7 @@ public class Bankers implements TalkToNpcExecutiveListener, TalkToNpcListener, N
return;
}
try {
PreparedStatement statement = DatabaseConnection.getDatabase().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
PreparedStatement statement = player.getWorld().getServer().getDatabaseConnection().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
statement.setString(1, player.getUsername());
ResultSet result = statement.executeQuery();
if (result.next()) {
......@@ -263,7 +262,7 @@ public class Bankers implements TalkToNpcExecutiveListener, TalkToNpcListener, N
return;
}
try {
PreparedStatement statement = DatabaseConnection.getDatabase().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
PreparedStatement statement = player.getWorld().getServer().getDatabaseConnection().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
statement.setString(1, player.getUsername());
ResultSet result = statement.executeQuery();
if (result.next()) {
......
......@@ -20,7 +20,6 @@ import com.openrsc.server.plugins.listeners.action.TalkToNpcListener;
import com.openrsc.server.plugins.listeners.executive.ObjectActionExecutiveListener;
import com.openrsc.server.plugins.listeners.executive.PickupExecutiveListener;
import com.openrsc.server.plugins.listeners.executive.TalkToNpcExecutiveListener;
import com.openrsc.server.sql.DatabaseConnection;
import com.openrsc.server.util.rsc.DataConversions;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
......@@ -398,7 +397,7 @@ public class ShantayPassNpcs implements ShopInterface,
return;
}
try {
PreparedStatement statement = DatabaseConnection.getDatabase().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
PreparedStatement statement = p.getWorld().getServer().getDatabaseConnection().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
statement.setString(1, p.getUsername());
ResultSet result = statement.executeQuery();
if (result.next()) {
......
......@@ -8,7 +8,6 @@ import com.openrsc.server.plugins.listeners.action.NpcCommandListener;
import com.openrsc.server.plugins.listeners.action.TalkToNpcListener;
import com.openrsc.server.plugins.listeners.executive.NpcCommandExecutiveListener;
import com.openrsc.server.plugins.listeners.executive.TalkToNpcExecutiveListener;
import com.openrsc.server.sql.DatabaseConnection;
import com.openrsc.server.util.rsc.DataConversions;
import com.openrsc.server.util.rsc.MessageType;
import org.apache.logging.log4j.LogManager;
......@@ -40,7 +39,7 @@ public class IronMan implements TalkToNpcExecutiveListener,
return;
}
try {
PreparedStatement statement = DatabaseConnection.getDatabase().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
PreparedStatement statement = p.getWorld().getServer().getDatabaseConnection().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
statement.setString(1, p.getUsername());
ResultSet result = statement.executeQuery();
if (result.next()) {
......@@ -81,7 +80,7 @@ public class IronMan implements TalkToNpcExecutiveListener,
return;
}
try {
PreparedStatement statement = DatabaseConnection.getDatabase().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
PreparedStatement statement = p.getWorld().getServer().getDatabaseConnection().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
statement.setString(1, p.getUsername());
ResultSet result = statement.executeQuery();
if (result.next()) {
......
......@@ -8,7 +8,6 @@ import com.openrsc.server.plugins.listeners.action.NpcCommandListener;
import com.openrsc.server.plugins.listeners.action.TalkToNpcListener;
import com.openrsc.server.plugins.listeners.executive.NpcCommandExecutiveListener;
import com.openrsc.server.plugins.listeners.executive.TalkToNpcExecutiveListener;
import com.openrsc.server.sql.DatabaseConnection;
import com.openrsc.server.util.rsc.DataConversions;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
......@@ -56,7 +55,7 @@ public class Auctioneers implements TalkToNpcExecutiveListener, TalkToNpcListene
return;
}
try {
PreparedStatement statement = DatabaseConnection.getDatabase().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
PreparedStatement statement = player.getWorld().getServer().getDatabaseConnection().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
statement.setString(1, player.getUsername());
ResultSet result = statement.executeQuery();
if (result.next()) {
......@@ -117,7 +116,7 @@ public class Auctioneers implements TalkToNpcExecutiveListener, TalkToNpcListene
return;
}
try {
PreparedStatement statement = DatabaseConnection.getDatabase().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
PreparedStatement statement = p.getWorld().getServer().getDatabaseConnection().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
statement.setString(1, p.getUsername());
ResultSet result = statement.executeQuery();
if (result.next()) {
......@@ -151,7 +150,7 @@ public class Auctioneers implements TalkToNpcExecutiveListener, TalkToNpcListene
return;
}
try {
PreparedStatement statement = DatabaseConnection.getDatabase().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
PreparedStatement statement = p.getWorld().getServer().getDatabaseConnection().prepareStatement("SELECT salt FROM " + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "players WHERE `username`=?");
statement.setString(1, p.getUsername());
ResultSet result = statement.executeQuery();
if (result.next()) {
......
......@@ -57,6 +57,8 @@ public final class Server implements Runnable {
private final CombatScriptLoader combatScriptLoader;
private final GameLogger gameLogger;
private final EntityHandler entityHandler;
private final DatabaseConnection databaseConnection;
private final World world;
private DelayedEvent updateEvent;
......@@ -77,6 +79,10 @@ public final class Server implements Runnable {
private Constants constants;
public static Server getServer() {
return server;
}
static {
try {
Thread.currentThread().setName("InitializationThread");
......@@ -91,27 +97,6 @@ public final class Server implements Runnable {
}
}
public Server (String configFile) throws IOException{
config = new ServerConfiguration();
getConfig().initConfig(configFile);
LOGGER.info("Server configuration loaded: " + configFile);
name = getConfig().SERVER_NAME;
pluginHandler = new PluginHandler(this);
combatScriptLoader = new CombatScriptLoader(this);
constants = new Constants(this);
discordService = new DiscordService(this);
playerDataProcessor = new PlayerDatabaseExecutor(this);
world = new World(this);
tickEventHandler = new GameTickEventHandler(this);
gameUpdater = new GameStateUpdater(this);
gameLogger = new GameLogger(this);
entityHandler = new EntityHandler(this);
monitoring = new MonitoringEvent();
scheduledExecutor = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryBuilder().setNameFormat(getName()+" : GameThread").build());
}
public static void main(String[] args) throws IOException {
LOGGER.info("Launching Game Server...");
......@@ -133,15 +118,31 @@ public final class Server implements Runnable {
}
}
public static Server getServer() {
return server;
public Server (String configFile) throws IOException{
config = new ServerConfiguration();
getConfig().initConfig(configFile);
LOGGER.info("Server configuration loaded: " + configFile);
name = getConfig().SERVER_NAME;
pluginHandler = new PluginHandler(this);
combatScriptLoader = new CombatScriptLoader(this);
constants = new Constants(this);
databaseConnection = new DatabaseConnection(this, getName()+" : Database Connection");
discordService = new DiscordService(this);
playerDataProcessor = new PlayerDatabaseExecutor(this);
world = new World(this);
tickEventHandler = new GameTickEventHandler(this);
gameUpdater = new GameStateUpdater(this);
gameLogger = new GameLogger(this);
entityHandler = new EntityHandler(this);
monitoring = new MonitoringEvent();
scheduledExecutor = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryBuilder().setNameFormat(getName()+" : GameThread").build());
}
private void initialize() {
try {
LOGGER.info("Creating database connection...");
DatabaseConnection.getDatabase();
LOGGER.info("\t Database connection created");
// TODO: We need an uninitialize process. Unloads all of these classes.
LOGGER.info("Loading Game Definitions...");
getEntityHandler().load();
......@@ -341,7 +342,7 @@ public final class Server implements Runnable {
SingleEvent up = new SingleEvent(null, 6000, "Save and Shutdown") {
public void action() {
kill();
DatabaseConnection.getDatabase().close();
getDatabaseConnection().close();
}
};
getGameEventHandler().add(up);
......@@ -542,4 +543,8 @@ public final class Server implements Runnable {
public EntityHandler getEntityHandler() {
return entityHandler;
}
public DatabaseConnection getDatabaseConnection() {
return databaseConnection;
}
}
......@@ -10,7 +10,6 @@ import com.openrsc.server.model.entity.GroundItem;
import com.openrsc.server.model.entity.npc.Npc;
import com.openrsc.server.model.entity.player.Player;
import com.openrsc.server.model.world.World;
import com.openrsc.server.sql.DatabaseConnection;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
......@@ -31,11 +30,11 @@ public class AchievementSystem {
loadedAchievements.clear();
try {
PreparedStatement fetchAchievement = DatabaseConnection.getDatabase()
PreparedStatement fetchAchievement = Server.getServer().getDatabaseConnection()
.prepareStatement("SELECT `id`, `name`, `description`, `extra`, `added` FROM `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "achievements` ORDER BY `id` ASC");
PreparedStatement fetchRewards = DatabaseConnection.getDatabase()
PreparedStatement fetchRewards = Server.getServer().getDatabaseConnection()
.prepareStatement("SELECT `item_id`, `amount`, `guaranteed`, `reward_type` FROM `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "achievement_reward` WHERE `achievement_id` = ?");
PreparedStatement fetchTasks = DatabaseConnection.getDatabase()
PreparedStatement fetchTasks = Server.getServer().getDatabaseConnection()
.prepareStatement("SELECT `type`, `do_id`, `do_amount` FROM `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "achievement_task` WHERE `achievement_id` = ?");
ResultSet result = fetchAchievement.executeQuery();
......
......@@ -2,7 +2,6 @@ package com.openrsc.server.content.clan;
import com.openrsc.server.Server;
import com.openrsc.server.model.entity.player.Player;
import com.openrsc.server.sql.DatabaseConnection;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
......@@ -75,7 +74,7 @@ public class ClanManager {
}
private static void loadClans() throws SQLException {
PreparedStatement statement = DatabaseConnection.getDatabase().prepareStatement("SELECT `id`, `name`, `tag`, `kick_setting`, `invite_setting`, `allow_search_join`, `clan_points` FROM `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "clan`");
PreparedStatement statement = Server.getServer().getDatabaseConnection().prepareStatement("SELECT `id`, `name`, `tag`, `kick_setting`, `invite_setting`, `allow_search_join`, `clan_points` FROM `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "clan`");
ResultSet result = statement.executeQuery();
while (result.next()) {
Clan clan = new Clan();
......@@ -87,7 +86,7 @@ public class ClanManager {
clan.setAllowSearchJoin(result.getInt("allow_search_join"));
clan.setClanPoints(result.getInt("clan_points"));
PreparedStatement fetchPlayers = DatabaseConnection.getDatabase()
PreparedStatement fetchPlayers = Server.getServer().getDatabaseConnection()
.prepareStatement("SELECT `username`, `rank`, `kills`, `deaths` FROM `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "clan_players` WHERE `clan_id`=?");
fetchPlayers.setInt(1, clan.getClanID());
ResultSet playersResult = fetchPlayers.executeQuery();
......@@ -114,7 +113,7 @@ public class ClanManager {
}
private static void databaseCreateClan(Clan clan) throws SQLException {
PreparedStatement statement = DatabaseConnection.getDatabase().getConnection().prepareStatement(
PreparedStatement statement = Server.getServer().getDatabaseConnection().getConnection().prepareStatement(
"INSERT INTO `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "clan`(`name`, `tag`, `leader`) VALUES (?,?,?)", Statement.RETURN_GENERATED_KEYS);
statement.setString(1, clan.getClanName());
statement.setString(2, clan.getClanTag());
......@@ -128,7 +127,7 @@ public class ClanManager {
statement.close();
statement = DatabaseConnection.getDatabase()
statement = Server.getServer().getDatabaseConnection()
.prepareStatement("INSERT INTO `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "clan_players`(`clan_id`, `username`, `rank`) VALUES (?,?,?)");
for (ClanPlayer member : clan.getPlayers()) {
statement.setInt(1, clan.getClanID());
......@@ -140,8 +139,8 @@ public class ClanManager {
}
private static void databaseDeleteClan(Clan clan) throws SQLException {
PreparedStatement deleteClan = DatabaseConnection.getDatabase().prepareStatement("DELETE FROM `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "clan` WHERE `id`=?");
PreparedStatement deleteClanPlayers = DatabaseConnection.getDatabase()
PreparedStatement deleteClan = Server.getServer().getDatabaseConnection().prepareStatement("DELETE FROM `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "clan` WHERE `id`=?");
PreparedStatement deleteClanPlayers = Server.getServer().getDatabaseConnection()
.prepareStatement("DELETE FROM `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "clan_players` WHERE `clan_id`=?");
deleteClan.setInt(1, clan.getClanID());
......@@ -152,7 +151,7 @@ public class ClanManager {
private static void saveClanPlayer(Clan clan) {
try {
PreparedStatement statement = DatabaseConnection.getDatabase().prepareStatement(
PreparedStatement statement = Server.getServer().getDatabaseConnection().prepareStatement(
"INSERT INTO `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "clan_players`(`clan_id`, `username`, `rank`, `kills`, `deaths`) VALUES (?,?,?,?,?)");
for (ClanPlayer member : clan.getPlayers()) {
statement.setInt(1, clan.getClanID());
......@@ -171,7 +170,7 @@ public class ClanManager {
private static void deleteClanPlayer(Clan clan) {
try {
PreparedStatement statement = DatabaseConnection.getDatabase()
PreparedStatement statement = Server.getServer().getDatabaseConnection()
.prepareStatement("DELETE FROM `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "clan_players` WHERE `clan_id`=?");
statement.setInt(1, clan.getClanID());
statement.executeUpdate();
......@@ -183,7 +182,7 @@ public class ClanManager {
private static void updateClan(Clan clan) {
try {
PreparedStatement statement = DatabaseConnection.getDatabase()
PreparedStatement statement = Server.getServer().getDatabaseConnection()
.prepareStatement("UPDATE `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "clan` SET `name`=?, `tag`=?, `leader`=?, `kick_setting`=?, `invite_setting`=?, `allow_search_join`=?, `clan_points`=? WHERE `id`=?");
statement.setString(1, clan.getClanName());
statement.setString(2, clan.getClanTag());
......@@ -204,7 +203,7 @@ public class ClanManager {
static void updateClanRankPlayer(ClanPlayer cp) {
try {
PreparedStatement statement = DatabaseConnection.getDatabase()
PreparedStatement statement = Server.getServer().getDatabaseConnection()
.prepareStatement("UPDATE `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX + "clan_players` SET `rank`=? WHERE `username`=?");
statement.setInt(1, cp.getRank().getRankIndex());
statement.setString(2, cp.getUsername());
......
......@@ -6,7 +6,6 @@ import com.openrsc.server.content.market.task.*;
import com.openrsc.server.external.ItemDefinition;
import com.openrsc.server.model.entity.player.Player;
import com.openrsc.server.model.world.World;
import com.openrsc.server.sql.DatabaseConnection;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
......@@ -84,7 +83,7 @@ public class Market implements Runnable {
if (auction.hasExpired()) expiredItems.add(auction);
if (expiredItems.size() != 0) {
PreparedStatement expiredItemsStatement = DatabaseConnection.getDatabase().prepareStatement(
PreparedStatement expiredItemsStatement = Server.getServer().getDatabaseConnection().prepareStatement(
"INSERT INTO `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX
+ "expired_auctions`(`item_id`, `item_amount`, `time`, `playerID`, `explanation`) VALUES (?,?,?,?,?)");
for (MarketItem expiredItem : expiredItems) {
......
package com.openrsc.server.content.market;
import com.openrsc.server.Server;
import com.openrsc.server.sql.DatabaseConnection;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
......@@ -19,7 +18,7 @@ public class MarketDatabase {
public static boolean add(MarketItem item) {
try {
PreparedStatement statement = DatabaseConnection.getDatabase().prepareStatement(
PreparedStatement statement = Server.getServer().getDatabaseConnection().prepareStatement(
"INSERT INTO `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX
+ "auctions`(`itemID`, `amount`, `amount_left`, `price`, `seller`, `seller_username`, `buyer_info`, `time`) VALUES (?,?,?,?,?,?,?,?)");
statement.setInt(1, item.getItemID());
......@@ -43,7 +42,7 @@ public class MarketDatabase {
final String finalExplanation = explanation.replaceAll("'", "");
try {
PreparedStatement statement = DatabaseConnection.getDatabase().prepareStatement(
PreparedStatement statement = Server.getServer().getDatabaseConnection().prepareStatement(
"INSERT INTO `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX
+ "expired_auctions`(`item_id`, `item_amount`, `time`, `playerID`, `explanation`) VALUES (?,?,?,?,?)");
statement.setInt(1, itemIndex);
......@@ -61,7 +60,7 @@ public class MarketDatabase {
public static boolean cancel(MarketItem item) {
try {
PreparedStatement statement = DatabaseConnection.getDatabase()
PreparedStatement statement = Server.getServer().getDatabaseConnection()
.prepareStatement("UPDATE `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX
+ "auctions` SET `sold-out`='1', `was_cancel`='1' WHERE `auctionID`=?");
statement.setInt(1, item.getAuctionID());
......@@ -75,7 +74,7 @@ public class MarketDatabase {
static int getAuctionCount() {
try {
PreparedStatement statement = DatabaseConnection.getDatabase()
PreparedStatement statement = Server.getServer().getDatabaseConnection()
.prepareStatement("SELECT count(*) as auction_count FROM `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX
+ "auctions` WHERE `sold-out`='0'");
ResultSet result = statement.executeQuery();
......@@ -91,7 +90,7 @@ public class MarketDatabase {
public static int getMyAuctionsCount(int ownerID) {
try {
PreparedStatement statement = DatabaseConnection.getDatabase()
PreparedStatement statement = Server.getServer().getDatabaseConnection()
.prepareStatement("SELECT count(*) as my_slots FROM `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX
+ "auctions` WHERE `seller`='" + ownerID + "' AND `sold-out`='0'");
ResultSet result = statement.executeQuery();
......@@ -107,7 +106,7 @@ public class MarketDatabase {
public static MarketItem getAuctionItem(int auctionID) {
try {
PreparedStatement statement = DatabaseConnection.getDatabase()
PreparedStatement statement = Server.getServer().getDatabaseConnection()
.prepareStatement("SELECT `auctionID`, `itemID`, `amount`, `amount_left`, `price`, `seller`, `seller_username`, `buyer_info`, `time` FROM `" + Server.getServer().getConfig().MYSQL_TABLE_PREFIX
+ "auctions` WHERE `auctionID`= ? AND `sold-out` = '0'");
statement.setInt(1, auctionID);
......@@ -127,7 +126,7 @@ public class MarketDatabase {
static ArrayList<MarketItem> getAuctionItemsOnSale() {
ArrayList<MarketItem> auctionItems = new ArrayList<>();
try {