Combine /warp and /waypoint

This commit is contained in:
UnknownShadow200 2017-08-11 12:00:48 +10:00
parent 18bdd6fd8f
commit 8cd6f83dbd
4 changed files with 99 additions and 109 deletions

View File

@ -19,7 +19,7 @@ using System;
using System.Threading;
namespace MCGalaxy.Commands.Misc {
public sealed class CmdWarp : Command {
public class CmdWarp : Command {
public override string name { get { return "Warp"; } }
public override string type { get { return CommandTypes.Other; } }
public override bool museumUsable { get { return false; } }
@ -32,49 +32,60 @@ namespace MCGalaxy.Commands.Misc {
new CommandPerm(LevelPermission.Operator, "+ can move/edit warps"),
}; }
}
protected virtual bool CheckExtraPerms { get { return true; } }
public override void Use(Player p, string message) {
WarpList warps = WarpList.Global;
UseCore(p, message, WarpList.Global, "Warp");
}
protected void UseCore(Player p, string message, WarpList warps, string group) {
string[] args = message.ToLower().SplitSpaces();
string cmd = args[0];
if (cmd.Length == 0) { Help(p); return; }
if (args.Length == 1 && (cmd == "list" || cmd == "view")) {
Player.Message(p, "Warps:");
Player.Message(p, "{0}s:", group);
foreach (Warp wr in warps.Items) {
if (LevelInfo.FindExact(wr.lvlname) != null)
Player.Message(p, wr.name + " : " + wr.lvlname);
if (LevelInfo.FindExact(wr.Level) != null)
Player.Message(p, wr.Name + " : " + wr.Level);
}
return;
} else if (args.Length == 1) {
if (!warps.Exists(cmd)) { Player.Message(p, "That warp does not exist"); }
warps.Goto(cmd, p);
Warp warp = Matcher.FindWarps(p, warps, cmd);
if (warp != null) warps.Goto(warp, p);
return;
}
string name = args[1];
if (cmd == "create" || cmd == "add") {
if (!CheckExtraPerm(p, 1)) { MessageNeedExtra(p, 1); return; }
if (warps.Exists(name)) { Player.Message(p, "That warp already exists"); return; }
if (CheckExtraPerms && !CheckExtraPerm(p, 1)) { MessageNeedExtra(p, 1); return; }
if (warps.Exists(name)) { Player.Message(p, "{0} already exists", group); return; }
Player who = args.Length == 2 ? p : PlayerInfo.FindMatches(p, args[2]);
if (who == null) return;
warps.Create(name, who);
Player.Message(p, "Warp created.");
Player.Message(p, "{0} {1} created.", group, name);
} else if (cmd == "delete" || cmd == "remove") {
if (!CheckExtraPerm(p, 2)) { MessageNeedExtra(p, 2); return; }
if (!warps.Exists(name)) { Player.Message(p, "That warp does not exist"); return; }
if (CheckExtraPerms && !CheckExtraPerm(p, 2)) { MessageNeedExtra(p, 2); return; }
Warp warp = Matcher.FindWarps(p, warps, name);
if (warp == null) return;
warps.Remove(warp, p);
Player.Message(p, "{0} {1} deleted.", group, warp.Name);
} else if (cmd == "move" || cmd == "update") {
if (CheckExtraPerms && !CheckExtraPerm(p, 3)) { MessageNeedExtra(p, 3); return; }
Warp warp = Matcher.FindWarps(p, warps, name);
if (warp == null) return;
warps.Remove(name, p);
Player.Message(p, "Warp deleted.");
} else if (cmd == "move" || cmd == "change" || cmd == "edit") {
if (!CheckExtraPerm(p, 3)) { MessageNeedExtra(p, 3); return; }
if (!warps.Exists(name)) { Player.Message(p, "Warp doesn't exist!!"); return; }
Player who = args.Length == 2 ? p : PlayerInfo.FindMatches(p, args[2]);
if (who == null) return;
warps.Update(name, who);
Player.Message(p, "Warp moved.");
warps.Update(warp, who);
Player.Message(p, "{0} {1} moved.", group, warp.Name);
} else if (cmd == "goto") {
Warp warp = Matcher.FindWarps(p, warps, name);
if (warp != null) warps.Goto(warp, p);
} else {
Help(p);
}

View File

@ -18,53 +18,20 @@
using System;
namespace MCGalaxy.Commands.Misc {
public sealed class CmdWaypoint : Command {
public sealed class CmdWaypoint : CmdWarp {
public override string name { get { return "Waypoint"; } }
public override string shortcut { get { return "wp"; } }
public override string type { get { return CommandTypes.Other; } }
public override bool museumUsable { get { return true; } }
public override LevelPermission defaultRank { get { return LevelPermission.Builder; } }
public override bool SuperUseable { get { return false; } }
public override CommandPerm[] ExtraPerms { get { return null; } }
protected override bool CheckExtraPerms { get { return false; } }
public override void Use(Player p, string message) {
string[] args = message.ToLower().SplitSpaces();
string cmd = args[0];
if (cmd.Length == 0) { Help(p); return; }
if (args.Length == 1 && cmd == "list") {
Player.Message(p, "Waypoints:");
foreach (Warp wp in p.Waypoints.Items) {
if (LevelInfo.FindExact(wp.lvlname) != null)
Player.Message(p, wp.name + " : " + wp.lvlname);
}
return;
} else if (args.Length == 1) {
if (!p.Waypoints.Exists(cmd)) { Player.Message(p, "That waypoint does not exist"); return; }
p.Waypoints.Goto(cmd, p);
return;
}
string name = args[1];
if (cmd == "create" || cmd == "new" || cmd == "add") {
if (p.Waypoints.Exists(name)) { Player.Message(p, "That waypoint already exists"); return; }
p.Waypoints.Create(name, p);
Player.Message(p, "Created waypoint");
} else if (cmd == "goto") {
if (!p.Waypoints.Exists(name)) { Player.Message(p, "That waypoint does not exist"); return; }
p.Waypoints.Goto(name, p);
} else if (cmd == "replace" || cmd == "update" || cmd == "edit") {
if (!p.Waypoints.Exists(name)) { Player.Message(p, "That waypoint does not exist"); return; }
p.Waypoints.Update(name, p);
Player.Message(p, "Updated waypoint");
} else if (cmd == "delete" || cmd == "remove") {
if (!p.Waypoints.Exists(name)) { Player.Message(p, "That waypoint does not exist"); return; }
p.Waypoints.Remove(name, p);
Player.Message(p, "Deleted waypoint");
} else {
Help(p);
}
UseCore(p, message, p.Waypoints, "Waypoint");
}
public override void Help(Player p) {
Player.Message(p, "%T/Waypoint create [name] %H- Create a new waypoint");
Player.Message(p, "%T/Waypoint update [name] %H- Update a waypoint");

View File

@ -18,14 +18,19 @@ using System.IO;
namespace MCGalaxy {
/// <summary> A named pair of position and orientation, located on a particular map. </summary>
public class Warp {
public int x, y, z;
public byte rotx, roty;
public string name;
public string lvlname;
/// <summary> Position of this warp. </summary>
public Position Pos;
/// <summary> Orientation of this warp. </summary>
public byte Yaw, Pitch;
/// <summary> The name of this warp. </summary>
public string Name;
/// <summary> The name of the level this warp is located on. </summary>
public string Level;
}
public sealed class WarpList {
public sealed class WarpList {
public static WarpList Global = new WarpList(false);
public List<Warp> Items = new List<Warp>();
@ -35,60 +40,58 @@ namespace MCGalaxy {
this.playerWarp = playerWarp;
}
/// <summary> Finds the warp whose name caselessly equals the given name. </summary>
public Warp Find(string name) {
foreach (Warp wp in Items) {
if (wp.name.CaselessEq(name)) return wp;
if (wp.Name.CaselessEq(name)) return wp;
}
return null;
}
public void Goto(string warp, Player p) {
Warp wp = Find(warp);
if (wp == null) return;
if (!p.level.name.CaselessEq(wp.lvlname)) {
PlayerActions.ChangeMap(p, wp.lvlname);
/// <summary> Attempts to move the given player to the given warp. </summary>
public void Goto(Warp warp, Player p) {
if (!p.level.name.CaselessEq(warp.Level)) {
PlayerActions.ChangeMap(p, warp.Level);
}
if (p.level.name.CaselessEq(wp.lvlname)) {
p.SendPos(Entities.SelfID,
new Position(wp.x, wp.y, wp.z),
new Orientation(wp.rotx, wp.roty));
if (p.level.name.CaselessEq(warp.Level)) {
p.SendPos(Entities.SelfID, warp.Pos, new Orientation(warp.Yaw, warp.Pitch));
Player.Message(p, "Sent you to waypoint/warp");
} else {
Player.Message(p, "Unable to send you to the warp as the map it is on is not loaded.");
}
}
public void Create(string warp, Player p) {
Warp wp = new Warp();
wp.x = p.Pos.X; wp.y = p.Pos.Y; wp.z = p.Pos.Z;
wp.rotx = p.Rot.RotY; wp.roty = p.Rot.HeadX;
wp.name = warp;
wp.lvlname = p.level.name;
Items.Add(wp);
/// <summary> Creates a new warp with the given name, located at the
/// player's current position, orientation, and level. </summary>
public void Create(string name, Player p) {
Warp warp = new Warp();
InitWarp(warp, name, p);
Items.Add(warp);
Save(p);
}
void InitWarp(Warp warp, string name, Player p) {
warp.Pos = p.Pos; warp.Name = name;
warp.Yaw = p.Rot.RotY; warp.Pitch = p.Rot.HeadX;
warp.Level = p.level.name;
}
/// <summary> Moves the given warp to the target
/// player's position, orientation, and map. </summary>
public void Update(Warp warp, Player p) {
InitWarp(warp, warp.Name, p);
Save(p);
}
public void Update(string warp, Player p) {
Warp wp = Find(warp);
Items.Remove(wp);
Create(warp, p);
}
public void Remove(string warp, Player p) {
Warp wp = Find(warp);
Items.Remove(wp);
/// <summary> Removes the given warp. </summary>
public void Remove(Warp warp, Player p) {
Items.Remove(warp);
Save(p);
}
public bool Exists(string warp) {
foreach (Warp wp in Items) {
if (wp.name.CaselessEq(warp)) return true;
}
return false;
}
/// <summary> Returns whether a warp with the given name exists. </summary>
public bool Exists(string name) { return Find(name) != null; }
public void Load(Player p) {
@ -98,19 +101,19 @@ namespace MCGalaxy {
using (StreamReader r = new StreamReader(file)) {
string line;
while ((line = r.ReadLine()) != null) {
line = line.ToLower().Trim();
line = line.Trim();
if (line.StartsWith("#") || !line.Contains(":")) continue;
string[] parts = line.ToLower().Split(':');
string[] parts = line.Split(':');
Warp wp = new Warp();
try {
wp.name = parts[0];
wp.lvlname = parts[1];
wp.x = ushort.Parse(parts[2]);
wp.y = ushort.Parse(parts[3]);
wp.z = ushort.Parse(parts[4]);
wp.rotx = byte.Parse(parts[5]);
wp.roty = byte.Parse(parts[6]);
wp.Name = parts[0];
wp.Level = parts[1];
wp.Pos.X = int.Parse(parts[2]);
wp.Pos.Y = int.Parse(parts[3]);
wp.Pos.Z = int.Parse(parts[4]);
wp.Yaw = byte.Parse(parts[5]);
wp.Pitch = byte.Parse(parts[6]);
Items.Add(wp);
} catch {
Logger.Log(LogType.Warning, "Failed loading a warp from " + file);
@ -120,10 +123,11 @@ namespace MCGalaxy {
}
public void Save(Player p) {
string file = playerWarp ? "extra/Waypoints/" + p.name + ".save" : "extra/warps.save";
string file = playerWarp ? "extra/Waypoints/" + p.name + ".save" : "extra/warps.save";
using (StreamWriter w = new StreamWriter(file)) {
foreach (Warp wp in Items) {
w.WriteLine(wp.name + ":" + wp.lvlname + ":" + wp.x + ":" + wp.y + ":" + wp.z + ":" + wp.rotx + ":" + wp.roty);
foreach (Warp warp in Items) {
w.WriteLine(warp.Name + ":" + warp.Level + ":" + warp.Pos.X + ":" +
warp.Pos.Y + ":" + warp.Pos.Z + ":" + warp.Yaw + ":" + warp.Pitch);
}
}
}

View File

@ -79,6 +79,14 @@ namespace MCGalaxy {
null, g => Colors.Strip(g.Name), "ranks");
}
/// <summary> Find partial matches of 'name' against a list of warps. </summary>
public static Warp FindWarps(Player p, WarpList warps, string name) {
string group = (warps == WarpList.Global) ? "warps" : "waypoints";
int matches;
return Find<Warp>(p, name, out matches, warps.Items,
null, wp => wp.Name, group);
}
/// <summary> Finds partial matches of 'name' against the names of the items in the 'items' enumerable. </summary>
/// <returns> If exactly one match, the matching item. </returns>