Skip to content

Commit

Permalink
Use simpler ToPlainString that uses less memory to create, and doesn'…
Browse files Browse the repository at this point in the history
…t need a regex to parse it
  • Loading branch information
UnknownShadow200 committed Nov 20, 2017
1 parent 0f1f74e commit 0b5b107
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion fCraft/Commands/BuildingCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2801,7 +2801,7 @@ private static void SnakeHandler(Player player, CommandReader cmd) {
if (player.World != null && player.World.Map != null) {
int blocksDrawn = 0, blocksSkipped = 0;
UndoState undoState = player.DrawBegin(null);
for (int i = 1; i <= length; i++) {
for (int i = 0; i < length; i++) {
Vector3I nextX = pos; nextX.X += dir.Next(0, 2) * 2 - 1;
Vector3I nextY = pos; nextY.Y += dir.Next(0, 2) * 2 - 1;
Vector3I nextZ = pos; nextZ.Z += dir.Next(0, 2) * 2 - 1;
Expand Down
4 changes: 2 additions & 2 deletions fCraft/Commands/ModerationCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1605,12 +1605,12 @@ static void TeleportPHandler(Player player, CommandReader cmd) {
player.Message("Teleported to {0}'s last block change", info.Name);
return;
} else {
player.Message("That users last known position is not on this world");
player.Message("User's last block change is not on this world");
player.Message("Please use &H/j {0} &Sto go there", Color.StripColors(info.LastWorld, true));
return;
}
} else {
player.Message("That user does not have a last known position");
player.Message("That user does not have a last known block change position");
return;
}
}
Expand Down
4 changes: 2 additions & 2 deletions fCraft/Player/Player.Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ static bool RaisePlayerClickingEvent( [NotNull] PlayerClickingEventArgs e ) {
if( h == null ) return false;
h( null, e );
e.Player.Info.LastWorld = e.Player.World.ClassyName;
e.Player.Info.LastWorldPos = e.Player.Position.ToString();
e.Player.Info.LastWorldPos = e.Player.Position.ToPlainString();
return e.Cancel;
}

Expand All @@ -143,7 +143,7 @@ static void RaisePlayerClickedEvent( Player player, Vector3I coords,
handler( null, new PlayerClickedEventArgs( player, coords, action, block ) );
}
player.Info.LastWorld = player.World.ClassyName;
player.Info.LastWorldPos = player.Position.ToString();
player.Info.LastWorldPos = player.Position.ToPlainString();
}

internal static void RaisePlayerPlacedBlockEvent( Player player, Map map, Vector3I coords,
Expand Down
2 changes: 1 addition & 1 deletion fCraft/Player/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,7 @@ public void PlaceBlockWithEvents( Vector3I coords, ClickAction action, Block typ
RaisePlayerClickedEvent( this, coords, e.Action, e.Block );
PlaceBlock( coords, e.Action, e.Block );
Info.LastWorld = this.World.ClassyName;
Info.LastWorldPos = this.Position.ToString();
Info.LastWorldPos = this.Position.ToPlainString();
}
}

Expand Down
20 changes: 18 additions & 2 deletions fCraft/Player/Position.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,30 @@ public override int GetHashCode() {

#endregion

public override string ToString()
{
public override string ToString() {
return String.Format("&S(X:&f{0}&S Y:&f{1}&S Z:&f{2}&S R:&f{3}&S L:&f{4}&S)", X, Y, Z, R, L);
}

public string ToPlainString() {
return X + "_" + Y + "_" + Z + "_" + R + "_" + L;
}

public static Position FromString(string text) {
Position pos = new Position();

try {
// New ToPlainString format
if (text.IndexOf('_') >= 0) {
string[] bits = text.Split('_');
pos.X = int.Parse(bits[0]);
pos.Y = int.Parse(bits[1]);
pos.Z = int.Parse(bits[2]);
pos.R = byte.Parse(bits[3]);
pos.L = byte.Parse(bits[4]);
return pos;
}

// Backwards compatibility with old format
string pat = @"\(X:(.*)Y:(.*) Z:(.*) R:(.*) L:(.*)\)";
Regex r = new Regex(pat, RegexOptions.IgnoreCase);
text = Color.StripColors(text, true);
Expand Down

0 comments on commit 0b5b107

Please sign in to comment.