Skip to content

Commit

Permalink
simplify portals code
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Sep 6, 2018
1 parent 8c80d5b commit cc59190
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 107 deletions.
7 changes: 3 additions & 4 deletions fCraft/Commands/WorldCommands.cs
Expand Up @@ -3358,7 +3358,7 @@ static void WorldClearHandler(Player player, CommandReader cmd)
Handler = MWHandler
};

private static void MWHandler(Player player, CommandReader cmd) {
static void MWHandler(Player player, CommandReader cmd) {
switch (cmd.Next()) {
case "create":
case "c":
Expand Down Expand Up @@ -3760,8 +3760,7 @@ static void WorldClearHandler(Player player, CommandReader cmd)
}

DrawOperation op = new CuboidDrawOperation(player);
NormalBrush brush = new NormalBrush(block, block);
op.Brush = brush;
op.Brush = new NormalBrush(block, block);
player.PortalWorld = worldName;
player.SelectionStart(op.ExpectedMarks, PortalCreateCallback, op, Permission.CreatePortals);
player.Message("Click {0} blocks or use &H/Mark&S to mark the area of the portal.", op.ExpectedMarks);
Expand Down Expand Up @@ -3892,7 +3891,7 @@ static void WorldClearHandler(Player player, CommandReader cmd)
for (int x = Xmin; x <= Xmax; x++) {
for (int y = Ymin; y <= Ymax; y++) {
for (int z = Zmin; z <= Zmax; z++) {
if (PortalHandler.IsInRangeOfSpawnpoint(player, player.World, new Vector3I(x, y, z))) {
if (PortalHandler.NearSpawn(player, player.World, new Vector3I(x, y, z))) {
player.Message("You cannot build a portal near the spawnpoint.");
return;
}
Expand Down
58 changes: 35 additions & 23 deletions fCraft/Portals/Portal.cs
Expand Up @@ -53,7 +53,7 @@ public class Portal {
return new Position(TeleportPosX, TeleportPosY, TeleportPosZ + Player.CharacterHeight, TeleportPosR, TeleportPosL);
}

public bool IsInRange(Player player) {
public bool Contains(Player player) {
if ((player.Position.BlockX) <= Range.Xmax && (player.Position.BlockX) >= Range.Xmin) {
if ((player.Position.BlockY) <= Range.Ymax && (player.Position.BlockY) >= Range.Ymin) {
if (((player.Position.Z / 32) - 1) <= Range.Zmax && ((player.Position.Z / 32) - 1) >= Range.Zmin) {
Expand All @@ -65,16 +65,11 @@ public class Portal {
return false;
}

public bool IsInRange(Vector3I vector) {
if (vector.X <= Range.Xmax && vector.X >= Range.Xmin) {
if (vector.Y <= Range.Ymax && vector.Y >= Range.Ymin) {
if (vector.Z <= Range.Zmax && vector.Z >= Range.Zmin) {
return true;
}
}
}

return false;
public bool Contains(Vector3I vector) {
return
vector.X >= Range.Xmin && vector.X <= Range.Xmax &&
vector.Y >= Range.Ymin && vector.Y <= Range.Ymax &&
vector.Z >= Range.Zmin && vector.Z <= Range.Zmax;
}

public static string GenerateName(World world) {
Expand All @@ -95,24 +90,41 @@ public class Portal {
return false;
}

public void Remove(Player requester, World pWorld) {
NormalBrush brush = new NormalBrush(Block.Air, Block.Air);
DrawOperation removeOperation = new CuboidDrawOperation(requester);
removeOperation.AnnounceCompletion = false;
removeOperation.Brush = brush;
removeOperation.Context = BlockChangeContext.Portal;
public void Remove(Player requester, World world) {
DrawOperation op = new CuboidDrawOperation(requester);
op.AnnounceCompletion = false;
op.Brush = new NormalBrush(Block.Air, Block.Air);
op.Context = BlockChangeContext.Portal;

Vector3I[] affectedBlocks = {
Vector3I[] bounds = {
new Vector3I(Range.Xmin, Range.Ymin, Range.Zmin),
new Vector3I(Range.Xmax, Range.Ymax, Range.Zmax),
};
if (!removeOperation.Prepare(affectedBlocks))
throw new PortalException("Unable to remove portal.");
if (!op.Prepare(bounds))
throw new InvalidOperationException("Unable to remove portal.");

removeOperation.Begin();
lock (pWorld.Portals.SyncRoot)
pWorld.Portals.Remove(this);
op.Begin();
lock (world.Portals.SyncRoot)
world.Portals.Remove(this);
PortalDB.Save();
}
}

public class PortalRange {
public int Xmin { get; set; }
public int Xmax { get; set; }
public int Ymin { get; set; }
public int Ymax { get; set; }
public int Zmin { get; set; }
public int Zmax { get; set; }

public PortalRange(int Xmin, int Xmax, int Ymin, int Ymax, int Zmin, int Zmax) {
this.Xmin = Xmin;
this.Xmax = Xmax;
this.Ymin = Ymin;
this.Ymax = Ymax;
this.Zmin = Zmin;
this.Zmax = Zmax;
}
}
}
29 changes: 0 additions & 29 deletions fCraft/Portals/PortalException.cs

This file was deleted.

10 changes: 4 additions & 6 deletions fCraft/Portals/PortalHandler.cs
Expand Up @@ -33,7 +33,7 @@ internal static class PortalHandler {
if (e.Player.World.Portals != null && e.Player.World.Portals.Count > 0 && e.Context != BlockChangeContext.Portal) {
lock (e.Player.World.Portals.SyncRoot) {
foreach (Portal portal in e.Player.World.Portals) {
if (portal.IsInRange(e.Coords)) {
if (portal.Contains(e.Coords)) {
BlockUpdate update = new BlockUpdate(null, e.Coords, e.OldBlock);
e.Player.World.Map.QueueUpdate(update);
e.Player.Message("You can not place a block inside portal: " + portal.Name);
Expand Down Expand Up @@ -164,7 +164,7 @@ internal static class PortalHandler {
if (player.World.Portals != null && player.World.Portals.Count > 0) {
lock (player.World.Portals.SyncRoot) {
foreach (Portal candidate in player.World.Portals) {
if (candidate.IsInRange(player)) {
if (candidate.Contains(player)) {
return candidate;
}
}
Expand All @@ -181,7 +181,7 @@ internal static class PortalHandler {
if (world.Portals != null && world.Portals.Count > 0) {
lock (world.Portals.SyncRoot) {
foreach (Portal candidate in world.Portals) {
if (candidate.IsInRange(block)) {
if (candidate.Contains(block)) {
return candidate;
}
}
Expand All @@ -194,8 +194,6 @@ internal static class PortalHandler {
}

public static void CreatePortal(Portal portal, World source) {
World world = WorldManager.FindWorldExact(portal.World);

if (source.Portals == null) {
source.Portals = new ArrayList();
}
Expand All @@ -207,7 +205,7 @@ internal static class PortalHandler {
PortalDB.Save();
}

public static bool IsInRangeOfSpawnpoint(Player player, World world, Vector3I block) {
public static bool NearSpawn(Player player, World world, Vector3I block) {
if (player.Info.Rank == RankManager.HighestRank)
return false;

Expand Down
43 changes: 0 additions & 43 deletions fCraft/Portals/PortalRange.cs

This file was deleted.

2 changes: 0 additions & 2 deletions fCraft/fCraft.csproj
Expand Up @@ -164,9 +164,7 @@
<Compile Include="Plugins\Plugins\TrollPlugin.cs" />
<Compile Include="Portals\Portal.cs" />
<Compile Include="Portals\PortalDB.cs" />
<Compile Include="Portals\PortalException.cs" />
<Compile Include="Portals\PortalHandler.cs" />
<Compile Include="Portals\PortalRange.cs" />
<Compile Include="System\Utils\BitMap3D.cs" />
<Compile Include="System\Utils\ConcurrentQueue.cs" />
<Compile Include="System\Utils\Direction.cs" />
Expand Down

0 comments on commit cc59190

Please sign in to comment.