diff --git a/Commands/building/CmdMessageBlock.cs b/Commands/building/CmdMessageBlock.cs index 1234f14d7..aeb1c507e 100644 --- a/Commands/building/CmdMessageBlock.cs +++ b/Commands/building/CmdMessageBlock.cs @@ -69,7 +69,7 @@ namespace MCGalaxy.Commands.Building { string[] parts = message.SplitSpaces(2); string alias = parts[0], cmdArgs = ""; Command.Search(ref alias, ref cmdArgs); - + foreach (Command cmd in Command.all.commands) { if (cmd.defaultRank <= p.Rank && (allCmds || !cmd.type.Contains("mod"))) continue; @@ -132,24 +132,31 @@ namespace MCGalaxy.Commands.Building { void ShowMessageBlocks(Player p) { p.showMBs = !p.showMBs; - //safe against SQL injections because no user input is given here - using (DataTable table = Database.Fill("SELECT * FROM `Messages" + p.level.name + "`")) { + using (DataTable table = Database.Backend.GetAllRows("Messages" + p.level.name, "*")) { if (p.showMBs) { - for (int i = 0; i < table.Rows.Count; i++) { - DataRow row = table.Rows[i]; - p.SendBlockchange(ushort.Parse(row["X"].ToString()), ushort.Parse(row["Y"].ToString()), ushort.Parse(row["Z"].ToString()), Block.MsgWhite); - } - Player.Message(p, "Now showing &a" + table.Rows.Count + " %SMBs."); + ShowMessageBlocks(p, table); } else { - for (int i = 0; i < table.Rows.Count; i++) { - DataRow row = table.Rows[i]; - p.RevertBlock(ushort.Parse(row["X"].ToString()), ushort.Parse(row["Y"].ToString()), ushort.Parse(row["Z"].ToString())); - } - Player.Message(p, "Now hiding MBs."); + HideMessageBlocks(p, table); } } } + static void ShowMessageBlocks(Player p, DataTable table) { + foreach (DataRow row in table.Rows) { + p.SendBlockchange(U16(row["X"]), U16(row["Y"]), U16(row["Z"]), Block.green); + } + Player.Message(p, "Now showing &a" + table.Rows.Count + " %SMBs."); + } + + static void HideMessageBlocks(Player p, DataTable table) { + foreach (DataRow row in table.Rows) { + p.RevertBlock(U16(row["X"]), U16(row["Y"]), U16(row["Z"])); + } + Player.Message(p, "Now hiding MBs."); + } + + static ushort U16(object x) { return Convert.ToUInt16(x); } + public override void Help(Player p) { Player.Message(p, "%T/mb [block] [message]"); Player.Message(p, "%HPlaces a message in your next block."); diff --git a/Commands/building/CmdPortal.cs b/Commands/building/CmdPortal.cs index cbd742880..1802c00a8 100644 --- a/Commands/building/CmdPortal.cs +++ b/Commands/building/CmdPortal.cs @@ -122,26 +122,32 @@ namespace MCGalaxy.Commands.Building { void ShowPortals(Player p) { p.showPortals = !p.showPortals; - //safe against SQL injections because no user input is given here - DataTable Portals = Database.Fill("SELECT * FROM `Portals" + p.level.name + "`"); - - if (p.showPortals) { - foreach (DataRow row in Portals.Rows) { - if (row["ExitMap"].ToString() == p.level.name) - p.SendBlockchange(U16(row["ExitX"]), U16(row["ExitY"]), U16(row["ExitZ"]), Block.red); - p.SendBlockchange(U16(row["EntryX"]), U16(row["EntryY"]), U16(row["EntryZ"]), Block.green); + using (DataTable table = Database.Backend.GetAllRows("Portals" + p.level.name, "*")) { + if (p.showPortals) { + ShowPortals(p, table); + } else { + HidePortals(p, table); } - - Player.Message(p, "Now showing &a" + Portals.Rows.Count + " %Sportals."); - } else { - foreach (DataRow row in Portals.Rows) { - if (row["ExitMap"].ToString() == p.level.name) - p.RevertBlock(U16(row["ExitX"]), U16(row["ExitY"]), U16(row["ExitZ"])); - p.RevertBlock(U16(row["EntryX"]), U16(row["EntryY"]), U16(row["EntryZ"])); - } - Player.Message(p, "Now hiding portals."); } - Portals.Dispose(); + } + + static void ShowPortals(Player p, DataTable table) { + foreach (DataRow row in table.Rows) { + if (row["ExitMap"].ToString() == p.level.name) + p.SendBlockchange(U16(row["ExitX"]), U16(row["ExitY"]), U16(row["ExitZ"]), Block.red); + p.SendBlockchange(U16(row["EntryX"]), U16(row["EntryY"]), U16(row["EntryZ"]), Block.green); + } + + Player.Message(p, "Now showing &a" + table.Rows.Count + " %Sportals."); + } + + static void HidePortals(Player p, DataTable table) { + foreach (DataRow row in table.Rows) { + if (row["ExitMap"].ToString() == p.level.name) + p.RevertBlock(U16(row["ExitX"]), U16(row["ExitY"]), U16(row["ExitZ"])); + p.RevertBlock(U16(row["EntryX"]), U16(row["EntryY"]), U16(row["EntryZ"])); + } + Player.Message(p, "Now hiding portals."); } static ushort U16(object x) { return Convert.ToUInt16(x); } diff --git a/Database/IDatabaseBackend.cs b/Database/IDatabaseBackend.cs index db1949052..83ac25a54 100644 --- a/Database/IDatabaseBackend.cs +++ b/Database/IDatabaseBackend.cs @@ -16,6 +16,7 @@ permissions and limitations under the Licenses. */ using System; +using System.Data; namespace MCGalaxy.SQL { @@ -52,9 +53,9 @@ namespace MCGalaxy.SQL { /// Removes all entries from the given table. public abstract void ClearTable(string table); - /// Inserts/Copies all the entries from the source table into the destination table. + /// Inserts/Copies all the rows from the source table into the destination table. /// Note: This may work incorrectly if the tables have different schema. - public virtual void CopyAllEntries(string srcTable, string dstTable) { + public virtual void CopyAllRows(string srcTable, string dstTable) { string syntax = "INSERT INTO `" + dstTable + "` SELECT * FROM `" + srcTable + "`"; Database.Execute(syntax); } @@ -64,5 +65,11 @@ namespace MCGalaxy.SQL { string syntax = "DROP TABLE `" + table + "`"; Database.Execute(syntax); } + + /// Retrieves all rows for the given table from the database. + public virtual DataTable GetAllRows(string table, string columns) { + string syntax = "SELECT " + columns + " FROM `" + table + "`"; + return Database.Fill(syntax); + } } } diff --git a/Economy/Economy.cs b/Economy/Economy.cs index 45e554285..9cfeacac7 100644 --- a/Economy/Economy.cs +++ b/Economy/Economy.cs @@ -57,8 +57,9 @@ PRIMARY KEY(player) public static void LoadDatabase() { Database.Execute(createTable); - DataTable eco = Database.Fill("SELECT * FROM Economy"); - foreach (DataRow row in eco.Rows) { + using (DataTable eco = Database.Backend.GetAllRows("Economy", "*")) + foreach (DataRow row in eco.Rows) + { int money = PlayerData.ParseInt(row["money"].ToString()); if (money == 0) continue; @@ -69,7 +70,7 @@ PRIMARY KEY(player) stats.Salary = row["salary"].ToString(); stats.Fine = row["fine"].ToString(); stats.TotalSpent = PlayerData.ParseInt(row["total"].ToString()); - + UpdateMoney(stats.Player, money); UpdateStats(stats); } diff --git a/Levels/LevelActions.cs b/Levels/LevelActions.cs index fcfbf8dd0..7173f76ab 100644 --- a/Levels/LevelActions.cs +++ b/Levels/LevelActions.cs @@ -208,18 +208,18 @@ namespace MCGalaxy { { if (Database.TableExists("Portals" + src)) { Database.Execute(String.Format(LevelDB.createPortals, dst)); - Database.Backend.CopyAllEntries("Portals" + src, "Portals" + dst); + Database.Backend.CopyAllRows("Portals" + src, "Portals" + dst); string updateSyntax = "UPDATE `Portals" + dst + "` SET ExitMap=@1 WHERE ExitMap=@0"; Database.Execute(updateSyntax, src, dst); } if (Database.TableExists("Messages" + src)) { Database.Execute(String.Format(LevelDB.createMessages, dst)); - Database.Backend.CopyAllEntries("Messages" + src, "Messages" + dst); + Database.Backend.CopyAllRows("Messages" + src, "Messages" + dst); } if (Database.TableExists("Zone" + src)) { Database.Execute(String.Format(LevelDB.createZones, dst)); - Database.Backend.CopyAllEntries("Zone" + src, "Zone" + dst); + Database.Backend.CopyAllRows("Zone" + src, "Zone" + dst); } } } diff --git a/Levels/LevelDB.cs b/Levels/LevelDB.cs index 0ab019326..d24a62fb1 100644 --- a/Levels/LevelDB.cs +++ b/Levels/LevelDB.cs @@ -94,7 +94,7 @@ namespace MCGalaxy { internal static void LoadZones(Level level, string name) { if (!Database.TableExists("Zone" + name)) return; - using (DataTable table = Database.Fill("SELECT * FROM `Zone" + name + "`")) { + using (DataTable table = Database.Backend.GetAllRows("Zone" + name, "*")) { Level.Zone Zn; foreach (DataRow row in table.Rows) { Zn.smallX = ushort.Parse(row["SmallX"].ToString()); @@ -111,7 +111,7 @@ namespace MCGalaxy { internal static void LoadPortals(Level level, string name) { if (!Database.TableExists("Portals" + name)) return; - using (DataTable table = Database.Fill("SELECT * FROM `Portals" + name + "`")) { + using (DataTable table = Database.Backend.GetAllRows("Portals" + name, "*")) { foreach (DataRow row in table.Rows) { byte tile = level.GetTile(ushort.Parse(row["EntryX"].ToString()), ushort.Parse(row["EntryY"].ToString()), @@ -126,7 +126,7 @@ namespace MCGalaxy { internal static void LoadMessages(Level level, string name) { if (!Database.TableExists("Messages" + name)) return; - using (DataTable table = Database.Fill("SELECT * FROM `Messages" + name + "`")) { + using (DataTable table = Database.Backend.GetAllRows("Messages" + name, "*")) { foreach (DataRow row in table.Rows) { byte tile = level.GetTile(ushort.Parse(row["X"].ToString()), ushort.Parse(row["Y"].ToString()), diff --git a/Player/Player.Timers.cs b/Player/Player.Timers.cs index 483363542..0e164a96b 100644 --- a/Player/Player.Timers.cs +++ b/Player/Player.Timers.cs @@ -59,9 +59,9 @@ namespace MCGalaxy { try { if (group.commands.Contains("inbox") && Database.TableExists("Inbox" + name) ) { //safe against SQL injections because no user input is given here - using (DataTable Inbox = Database.Fill("SELECT * FROM `Inbox" + name + "`")) { - if (Inbox.Rows.Count > 0) - SendMessage("You have &a" + Inbox.Rows.Count + " %Smessages in /inbox"); + using (DataTable table = Database.Backend.GetAllRows("Inbox" + name, "*")) { + if (table.Rows.Count > 0) + SendMessage("You have &a" + table.Rows.Count + " %Smessages in /inbox"); } } } catch {