Only create portals block tabe when required.

This commit is contained in:
UnknownShadow200 2016-08-23 19:10:39 +10:00
parent 1b6fecc7d4
commit 0436384446
2 changed files with 24 additions and 18 deletions

View File

@ -14,14 +14,14 @@
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses. permissions and limitations under the Licenses.
*/ */
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
using MCGalaxy.SQL; using MCGalaxy.SQL;
namespace MCGalaxy.Commands.Building { namespace MCGalaxy.Commands.Building {
public sealed class CmdPortal : Command { public sealed class CmdPortal : Command {
public override string name { get { return "portal"; } } public override string name { get { return "portal"; } }
public override string shortcut { get { return "o"; } } public override string shortcut { get { return "o"; } }
public override string type { get { return CommandTypes.Building; } } public override string type { get { return CommandTypes.Building; } }
@ -58,8 +58,8 @@ namespace MCGalaxy.Commands.Building {
p.Blockchange += EntryChange; p.Blockchange += EntryChange;
} }
void EntryChange(Player p, ushort x, ushort y, ushort z, byte type, byte extType) { void EntryChange(Player p, ushort x, ushort y, ushort z, byte type, byte extType) {
PortalData bp = (PortalData)p.blockchangeObject; PortalData bp = (PortalData)p.blockchangeObject;
byte old = p.level.GetTile(x, y, z); byte old = p.level.GetTile(x, y, z);
if (!p.level.CheckAffectPermissions(p, x, y, z, old, type, extType)) { if (!p.level.CheckAffectPermissions(p, x, y, z, old, type, extType)) {
p.RevertBlock(x, y, z); return; p.RevertBlock(x, y, z); return;
@ -90,25 +90,32 @@ namespace MCGalaxy.Commands.Building {
RevertAndClearState(p, x, y, z); RevertAndClearState(p, x, y, z);
PortalData bp = (PortalData)p.blockchangeObject; PortalData bp = (PortalData)p.blockchangeObject;
foreach (PortalPos pos in bp.entries) { foreach (PortalPos P in bp.entries) {
//safe against SQL injections because no user input is given here //safe against SQL injections because no user input is given here
DataTable Portals = Database.Fill("SELECT * FROM `Portals" + pos.mapName + string lvlName = P.mapName;
"` WHERE EntryX=@0 AND EntryY=@1 AND EntryZ=@2", pos.x, pos.y, pos.z); object locker = ThreadSafeCache.DBCache.Get(lvlName);
Portals.Dispose();
string syntax = Portals.Rows.Count == 0 ? lock (locker) {
"INSERT INTO `Portals" + pos.mapName + "` (EntryX, EntryY, EntryZ, ExitX, ExitY, ExitZ, ExitMap) VALUES (@0, @1, @2, @3, @4, @5, @6)" Database.Execute(String.Format(LevelDB.createPortals, lvlName));
: "UPDATE `Portals" + pos.mapName + "` SET ExitMap=@6, ExitX=@3, ExitY=@4, ExitZ=@5 WHERE EntryX=@0 AND EntryY=@1 AND EntryZ=@2";
Database.Execute(syntax, pos.x, pos.y, pos.z, x, y, z, p.level.name);
if (pos.mapName == p.level.name) int count = 0;
p.SendBlockchange(pos.x, pos.y, pos.z, bp.type); string syntax = "SELECT * FROM `Portals" + lvlName + "` WHERE EntryX=@0 AND EntryY=@1 AND EntryZ=@2";
using (DataTable portals = Database.Fill(syntax, P.x, P.y, P.z))
count = portals.Rows.Count;
syntax = count == 0 ?
"INSERT INTO `Portals" + lvlName + "` (EntryX, EntryY, EntryZ, ExitX, ExitY, ExitZ, ExitMap) VALUES (@0, @1, @2, @3, @4, @5, @6)"
: "UPDATE `Portals" + lvlName + "` SET ExitMap=@6, ExitX=@3, ExitY=@4, ExitZ=@5 WHERE EntryX=@0 AND EntryY=@1 AND EntryZ=@2";
Database.Execute(syntax, P.x, P.y, P.z, x, y, z, p.level.name);
}
if (P.mapName == p.level.name)
p.SendBlockchange(P.x, P.y, P.z, bp.type);
} }
Player.Message(p, "&3Exit %Sblock placed"); Player.Message(p, "&3Exit %Sblock placed");
if (!p.staticCommands) return; if (!p.staticCommands) return;
bp.entries.Clear(); bp.entries.Clear();
p.blockchangeObject = bp; p.blockchangeObject = bp;
p.Blockchange += EntryChange; p.Blockchange += EntryChange;
} }

View File

@ -103,7 +103,6 @@ namespace MCGalaxy {
public static void CreateTables(string givenName) { public static void CreateTables(string givenName) {
Database.Execute(String.Format(createBlock, givenName, Server.useMySQL ? "BOOL" : "INT")); Database.Execute(String.Format(createBlock, givenName, Server.useMySQL ? "BOOL" : "INT"));
Database.Execute(String.Format(createPortals, givenName));
} }
internal static void LoadZones(Level level, string name) { internal static void LoadZones(Level level, string name) {