Skip to content

Commit 077463b

Browse files
cleanup portals code somewhat
1 parent cc59190 commit 077463b

File tree

5 files changed

+251
-294
lines changed

5 files changed

+251
-294
lines changed

fCraft/Commands/WorldCommands.cs

Lines changed: 89 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -2843,7 +2843,7 @@ static void WorldClearSaveHandler(Player player, CommandReader cmd) {
28432843
"Whether any blocks can be placed by players in the world." },
28442844
{ "deletable", "&H/WSet <WorldName> Deletable On/Off&N&S" +
28452845
"Whether any blocks can be deleted by players in the world." },
2846-
{ "maxreach", "&H/WSet <WorldName> MaxReach <Distance>/reset&N&S" +
2846+
{ "maxreach", "&H/WSet <WorldName> MaxReach <Distance>/reset&N&S" +
28472847
"Sets maximum reach distance players may click/reach up to." },
28482848
},
28492849
Handler = WorldSetHandler
@@ -3715,7 +3715,7 @@ static void PortalCreate(Player player, CommandReader cmd) {
37153715
player.PortalName = null;
37163716
string name = cmd.Next();
37173717
if (string.IsNullOrEmpty(name)) {
3718-
} else if (!Portal.Exists(player.World, name)) {
3718+
} else if (player.World.Portals.Find(name) == null) {
37193719
player.PortalName = name;
37203720
} else {
37213721
player.Message("A portal named {0} already exists in this world.", name);
@@ -3727,22 +3727,22 @@ static void PortalCreate(Player player, CommandReader cmd) {
37273727
int x, y, z, rot = player.Position.R, lot = player.Position.L;
37283728
string next = cmd.Next();
37293729
if (next != null && next.StartsWith("#")) {
3730-
bool needsLoading = world.Map == null;
3731-
if (needsLoading) world.LoadMap();
3732-
3733-
Zone zone = world.map.Zones.FindExact(next.Remove(0,1));
3734-
if (zone == null) {
3735-
player.MessageNoZone(next.Remove(0, 1));
3736-
return;
3737-
}
3738-
3739-
player.PortalTPPos = new Position(zone.Bounds.XCentre * 32 + 16,
3740-
zone.Bounds.YCentre * 32 + 16, zone.Bounds.ZCentre * 32 + Player.CharacterHeight,
3741-
(byte)rot, (byte)lot);
3742-
player.Message("Players will be teleported to zone: " + zone.Name);
3743-
player.Message("At: " + player.PortalTPPos.ToString());
3744-
player.Message("On: " + world.Name);
3745-
if (needsLoading) world.UnloadMap(false);
3730+
bool needsLoading = world.Map == null;
3731+
if (needsLoading) world.LoadMap();
3732+
3733+
Zone zone = world.map.Zones.FindExact(next.Remove(0,1));
3734+
if (zone == null) {
3735+
player.MessageNoZone(next.Remove(0, 1));
3736+
return;
3737+
}
3738+
3739+
player.PortalTPPos = new Position(zone.Bounds.XCentre * 32 + 16,
3740+
zone.Bounds.YCentre * 32 + 16, zone.Bounds.ZCentre * 32 + Player.CharacterHeight,
3741+
(byte)rot, (byte)lot);
3742+
player.Message("Players will be teleported to zone: " + zone.Name);
3743+
player.Message("At: " + player.PortalTPPos.ToString());
3744+
player.Message("On: " + world.Name);
3745+
if (needsLoading) world.UnloadMap(false);
37463746
} else if (int.TryParse(next, out x) && cmd.NextInt(out y) && cmd.NextInt(out z)) {
37473747
if (cmd.CountRemaining >= 2 && cmd.NextInt(out rot) && cmd.NextInt(out lot)) {
37483748
if (rot > 255 || rot < 0) {
@@ -3772,8 +3772,8 @@ static void PortalRemove(Player player, CommandReader cmd) {
37723772
return;
37733773
}
37743774

3775-
string portalName = cmd.Next();
3776-
if (string.IsNullOrEmpty(portalName)) {
3775+
string name = cmd.Next();
3776+
if (string.IsNullOrEmpty(name)) {
37773777
player.Message("No portal name specified.");
37783778
return;
37793779
}
@@ -3785,31 +3785,23 @@ static void PortalRemove(Player player, CommandReader cmd) {
37853785
}
37863786
if (world == null) return;
37873787

3788-
if (world.Portals == null || world.Portals.Count == 0) {
3788+
if (world.Portals.Count == 0) {
37893789
player.Message("There are no portals in {0}", world.ClassyName);
37903790
return;
37913791
}
37923792

3793-
Portal match = null;
3794-
lock (world.Portals.SyncRoot) {
3795-
foreach (Portal portal in world.Portals) {
3796-
if (!portal.Name.CaselessEquals(portalName)) continue;
3797-
match = portal;
3798-
break;
3799-
}
3800-
3801-
if (match == null) {
3802-
player.Message("Portal {0} does not exist in {1}", portalName, world.ClassyName);
3803-
} else {
3804-
match.Remove(player, world);
3805-
player.Message("Portal was removed.");
3806-
}
3793+
Portal portal = world.Portals.Find(name);
3794+
if (portal == null) {
3795+
player.Message("Portal {0} does not exist in {1}", name, world.ClassyName);
3796+
} else {
3797+
portal.Remove(player, world);
3798+
player.Message("Portal was removed.");
38073799
}
38083800
}
38093801

38103802
static void PortalInfo(Player player, CommandReader cmd) {
3811-
string portalName = cmd.Next();
3812-
if (string.IsNullOrEmpty(portalName)) {
3803+
string name = cmd.Next();
3804+
if (string.IsNullOrEmpty(name)) {
38133805
player.Message("No portal name specified.");
38143806
return;
38153807
}
@@ -3821,25 +3813,19 @@ static void PortalInfo(Player player, CommandReader cmd) {
38213813
}
38223814
if (world == null) return;
38233815

3824-
if (world.Portals == null || world.Portals.Count == 0) {
3816+
if (world.Portals.Count == 0) {
38253817
player.Message("There are no portals in {0}", world.ClassyName);
38263818
return;
38273819
}
38283820

3829-
bool found = false;
3830-
lock (world.Portals.SyncRoot) {
3831-
foreach (Portal portal in world.Portals) {
3832-
if (!portal.Name.CaselessEquals(portalName)) continue;
3833-
3834-
string creator = PlayerDB.FindPlayerInfoExact(portal.Creator).ClassyName;
3835-
World exitWorld = WorldManager.FindWorldExact(portal.World);
3836-
player.Message("Portal {0}&S was created by {1}&S at {2} and teleports to world {3} at {4}&S.",
3837-
portal.Name, creator, portal.Created, exitWorld.ClassyName, portal.position().ToString());
3838-
found = true;
3839-
}
3840-
}
3841-
if (!found) {
3842-
player.Message("Portal {0} does not exist in {1}", portalName, world.ClassyName);
3821+
Portal portal = world.Portals.Find(name);
3822+
if (portal == null) {
3823+
player.Message("Portal {0} does not exist in {1}", name, world.ClassyName);
3824+
} else {
3825+
string creator = PlayerDB.FindPlayerInfoExact(portal.Creator).ClassyName;
3826+
World exit = WorldManager.FindWorldExact(portal.World);
3827+
player.Message("Portal {0}&S was created by {1}&S at {2} and teleports to world {3} at {4}&S.",
3828+
portal.Name, creator, portal.Created, exit.ClassyName, portal.position().ToString());
38433829
}
38443830
}
38453831

@@ -3851,73 +3837,76 @@ static void PortalList(Player player, CommandReader cmd) {
38513837
}
38523838
if (world == null) return;
38533839

3854-
if (world.Portals == null || world.Portals.Count == 0) {
3840+
if (world.Portals.Count == 0) {
38553841
player.Message("There are no portals in {0}", world.ClassyName);
38563842
return;
38573843
}
38583844

3859-
string[] portalNames = new string[world.Portals.Count];
3860-
StringBuilder output = new StringBuilder("There are " + world.Portals.Count + " portals in " + world.ClassyName + "&S: ");
3861-
for (int i = 0; i < world.Portals.Count; i++) {
3862-
portalNames[i] = ((Portal)world.Portals[i]).Name;
3845+
string list;
3846+
lock (world.Portals.locker) {
3847+
list = world.Portals.entries.JoinToString(", ", portal => portal.Name);
38633848
}
3864-
output.Append(portalNames.JoinToString(", "));
3865-
player.Message(output.ToString());
3849+
3850+
player.Message("There are {0} portals in {1}&S: {2}",
3851+
world.Portals.Count, world.ClassyName, list);
38663852
}
38673853

38683854
static void PortalCreateCallback(Player player, Vector3I[] marks, object tag) {
38693855
try {
38703856
World world = WorldManager.FindWorldExact(player.PortalWorld);
3857+
if (world == null) {
3858+
player.MessageInvalidWorldName(player.PortalWorld);
3859+
return;
3860+
}
3861+
3862+
DrawOperation op = (DrawOperation)tag;
3863+
if (!op.Prepare(marks)) return;
3864+
PortalsList portals = player.World.Portals;
3865+
3866+
if (!player.CanDraw(op.BlocksTotalEstimate)) {
3867+
player.Message("You are only allowed to run draw commands that affect up to {0} blocks. This one would affect {1} blocks.",
3868+
player.Info.Rank.DrawLimit,
3869+
op.Bounds.Volume);
3870+
op.Cancel();
3871+
return;
3872+
}
38713873

3872-
if (world != null) {
3873-
DrawOperation op = (DrawOperation)tag;
3874-
if (!op.Prepare(marks))
3875-
return;
3876-
if (!player.CanDraw(op.BlocksTotalEstimate)) {
3877-
player.Message("You are only allowed to run draw commands that affect up to {0} blocks. This one would affect {1} blocks.",
3878-
player.Info.Rank.DrawLimit,
3879-
op.Bounds.Volume);
3880-
op.Cancel();
3881-
return;
3882-
}
3874+
int Xmin = Math.Min(marks[0].X, marks[1].X);
3875+
int Xmax = Math.Max(marks[0].X, marks[1].X);
3876+
int Ymin = Math.Min(marks[0].Y, marks[1].Y);
3877+
int Ymax = Math.Max(marks[0].Y, marks[1].Y);
3878+
int Zmin = Math.Min(marks[0].Z, marks[1].Z);
3879+
int Zmax = Math.Max(marks[0].Z, marks[1].Z);
38833880

3884-
int Xmin = Math.Min(marks[0].X, marks[1].X);
3885-
int Xmax = Math.Max(marks[0].X, marks[1].X);
3886-
int Ymin = Math.Min(marks[0].Y, marks[1].Y);
3887-
int Ymax = Math.Max(marks[0].Y, marks[1].Y);
3888-
int Zmin = Math.Min(marks[0].Z, marks[1].Z);
3889-
int Zmax = Math.Max(marks[0].Z, marks[1].Z);
3890-
3891-
for (int x = Xmin; x <= Xmax; x++) {
3892-
for (int y = Ymin; y <= Ymax; y++) {
3893-
for (int z = Zmin; z <= Zmax; z++) {
3894-
if (PortalHandler.NearSpawn(player, player.World, new Vector3I(x, y, z))) {
3895-
player.Message("You cannot build a portal near the spawnpoint.");
3896-
return;
3897-
}
3881+
for (int x = Xmin; x <= Xmax; x++) {
3882+
for (int y = Ymin; y <= Ymax; y++) {
3883+
for (int z = Zmin; z <= Zmax; z++) {
3884+
if (PortalHandler.NearSpawn(player, player.World, new Vector3I(x, y, z))) {
3885+
player.Message("You cannot build a portal near the spawnpoint.");
3886+
return;
3887+
}
38983888

3899-
if (PortalHandler.GetPortal(player.World, new Vector3I(x, y, z)) != null) {
3900-
player.Message("You cannot build a portal inside a portal.");
3901-
return;
3902-
}
3889+
if (portals.Find(new Vector3I(x, y, z)) != null) {
3890+
player.Message("You cannot build a portal inside a portal.");
3891+
return;
39033892
}
39043893
}
39053894
}
3895+
}
39063896

3907-
string name = player.PortalName;
3908-
if (name == null) name = Portal.GenerateName(player.World);
3897+
string name = player.PortalName;
3898+
if (name == null) name = player.World.Portals.GenAutoName();
39093899

3910-
Portal portal = new Portal(player.PortalWorld, new PortalRange(Xmin, Xmax, Ymin, Ymax, Zmin, Zmax),
3911-
name, player.Name, player.World.Name, player.PortalTPPos);
3912-
PortalHandler.CreatePortal(portal, player.World);
3913-
op.AnnounceCompletion = false;
3914-
op.Context = BlockChangeContext.Portal;
3915-
op.Begin();
3900+
Portal portal = new Portal(player.PortalWorld, new PortalRange(Xmin, Xmax, Ymin, Ymax, Zmin, Zmax),
3901+
name, player.Name, player.World.Name, player.PortalTPPos);
3902+
portals.Add(portal);
3903+
PortalDB.Save();
3904+
3905+
op.AnnounceCompletion = false;
3906+
op.Context = BlockChangeContext.Portal;
3907+
op.Begin();
39163908

3917-
player.Message("Successfully created portal with name " + portal.Name + ".");
3918-
} else {
3919-
player.MessageInvalidWorldName(player.PortalWorld);
3920-
}
3909+
player.Message("Successfully created portal with name " + portal.Name + ".");
39213910
} catch (Exception ex) {
39223911
player.Message("Failed to create portal.");
39233912
Logger.Log(LogType.Error, "WorldCommands.PortalCreateCallback: " + ex);

0 commit comments

Comments
 (0)