Modularise /goto, and also load unloaded levels that exactly match the message before resolving to partially match currently loaded levels. (Thanks FabTheZen)

This commit is contained in:
UnknownShadow200 2016-01-29 16:20:55 +11:00
parent b598359545
commit 063c8794f5
2 changed files with 141 additions and 144 deletions

View File

@ -17,10 +17,10 @@
*/ */
using System; using System;
using System.IO; using System.IO;
namespace MCGalaxy.Commands namespace MCGalaxy.Commands {
{
public sealed class CmdGoto : Command public sealed class CmdGoto : Command {
{
public override string name { get { return "goto"; } } public override string name { get { return "goto"; } }
public override string shortcut { get { return "g"; } } public override string shortcut { get { return "g"; } }
public override string type { get { return CommandTypes.World; } } public override string type { get { return CommandTypes.World; } }
@ -28,30 +28,62 @@ namespace MCGalaxy.Commands
public override LevelPermission defaultRank { get { return LevelPermission.Guest; } } public override LevelPermission defaultRank { get { return LevelPermission.Guest; } }
public CmdGoto() { } public CmdGoto() { }
public override void Use(Player p, string message) public override void Use(Player p, string message) {
{
if (p == null) { MessageInGameOnly(p); return; } if (p == null) { MessageInGameOnly(p); return; }
if (message == "") { Help(p); return; } if (message == "") { Help(p); return; }
try Level lvl = Level.FindExact(message);
{ if (lvl != null) {
Level foundLevel = Level.Find(message); GoToLevel(p, lvl, message);
if (foundLevel != null) } else if (Server.AutoLoad) {
{ if (!File.Exists("levels/" + message + ".lvl")) {
Level startLevel = p.level; lvl = Level.Find(message);
if (lvl == null) {
Player.SendMessage(p, "Level \"" + message + "\" doesn't exist! Did you mean...");
Command.all.Find("search").Use(p, "levels " + message);
} else {
GoToLevel(p, lvl, message);
}
} else if (Level.CheckLoadOnGoto(message)) {
Command.all.Find("load").Use(p, message);
lvl = Level.Find(message);
if (lvl != null) {
GoToLevel(p, lvl, message);
}
} else {
if (lvl == null) {
Player.SendMessage(p, "Level \"" + message + "\" cannot be loaded using /goto!");
} else {
GoToLevel(p, lvl, message);
}
}
} else {
lvl = Level.Find(message);
if (lvl == null) {
Player.SendMessage(p, "There is no level \"" + message + "\" loaded. Did you mean..");
Command.all.Find("search").Use(p, "levels " + message);
} else {
GoToLevel(p, lvl, message);
}
}
GC.Collect(); GC.Collect();
GC.WaitForPendingFinalizers();
}
if (p.level == foundLevel) { Player.SendMessage(p, "You are already in \"" + foundLevel.name + "\"."); return; } void GoToLevel(Player p, Level lvl, string message) {
bool blacklisted = Player.BlacklistCheck(p.name, message); if (p.level == lvl) { Player.SendMessage(p, "You are already in \"" + lvl.name + "\"."); return; }
if (blacklisted) { Player.SendMessage(p, "You are blacklisted from " + foundLevel.name + "."); return; } if (Player.BlacklistCheck(p.name, message)) {
if (!p.ignorePermission) Player.SendMessage(p, "You are blacklisted from " + lvl.name + "."); return;
if (p.group.Permission < foundLevel.permissionvisit) { Player.SendMessage(p, "You're not allowed to go to " + foundLevel.name + "."); return; } }
if (!p.ignorePermission) if (!p.ignorePermission && p.group.Permission < lvl.permissionvisit) {
if (p.group.Permission > foundLevel.pervisitmax) { if (!p.group.CanExecute(Command.all.Find("pervisitmax"))) { Player.SendMessage(p, "Your rank must be " + foundLevel.pervisitmax + " or lower to go there!"); return; } } Player.SendMessage(p, "You're not allowed to go to " + lvl.name + "."); return;
{ }
if (!File.Exists("text/lockdown/map/" + message + "")) if (!p.ignorePermission && p.group.Permission > lvl.pervisitmax && !p.group.CanExecute(Command.all.Find("pervisitmax"))) {
{ Player.SendMessage(p, "Your rank must be " + lvl.pervisitmax + " or lower to go there!"); return;
}
if (File.Exists("text/lockdown/map/" + message.ToLower())) {
Player.SendMessage(p, "The level " + message + " is locked."); return;
}
p.Loading = true; p.Loading = true;
foreach (Player pl in Player.players) if (p.level == pl.level && p != pl) p.SendDespawn(pl.id); foreach (Player pl in Player.players) if (p.level == pl.level && p != pl) p.SendDespawn(pl.id);
@ -59,112 +91,76 @@ namespace MCGalaxy.Commands
Player.GlobalDespawn(p, true); Player.GlobalDespawn(p, true);
Level oldLevel = p.level; Level oldLevel = p.level;
p.level = foundLevel; p.SendUserMOTD(); p.SendMap(oldLevel); p.level = lvl; p.SendUserMOTD(); p.SendMap(oldLevel);
GC.Collect(); GC.Collect();
ushort x = (ushort)((0.5 + foundLevel.spawnx) * 32); ushort x = (ushort)((0.5 + lvl.spawnx) * 32);
ushort y = (ushort)((1 + foundLevel.spawny) * 32); ushort y = (ushort)((1 + lvl.spawny) * 32);
ushort z = (ushort)((0.5 + foundLevel.spawnz) * 32); ushort z = (ushort)((0.5 + lvl.spawnz) * 32);
if (!p.hidden) Player.GlobalSpawn(p, x, y, z, foundLevel.rotx, foundLevel.roty, true, ""); if (!p.hidden)
else p.SendPos(0xFF, x, y, z, foundLevel.rotx, foundLevel.roty); Player.GlobalSpawn(p, x, y, z, lvl.rotx, lvl.roty, true, "");
else
p.SendPos(0xFF, x, y, z, lvl.rotx, lvl.roty);
foreach (Player pl in Player.players) foreach (Player pl in Player.players)
if (pl.level == p.level && p != pl && !pl.hidden) if (pl.level == p.level && p != pl && !pl.hidden)
p.SendSpawn(pl.id, pl.color + pl.name, pl.pos[0], pl.pos[1], pl.pos[2], pl.rot[0], pl.rot[1]); p.SendSpawn(pl.id, pl.color + pl.name, pl.pos[0], pl.pos[1], pl.pos[2], pl.rot[0], pl.rot[1]);
foreach (PlayerBot b in PlayerBot.playerbots) foreach (PlayerBot b in PlayerBot.playerbots)
if (b.level == p.level) if (b.level == p.level)
p.SendSpawn(b.id, b.color + b.name, b.pos[0], b.pos[1], b.pos[2], b.rot[0], b.rot[1]); p.SendSpawn(b.id, b.color + b.name, b.pos[0], b.pos[1], b.pos[2], b.rot[0], b.rot[1]);
if (!p.hidden) Player.GlobalChat(p, p.color + "*" + p.DisplayName + Server.DefaultColor + " went to &b" + foundLevel.name, false);
p.Loading = false; p.Loading = false;
bool unloadOld = true;
bool skipUnload = false; if (oldLevel.unload && !oldLevel.name.Contains("&cMuseum ")) {
if (startLevel.unload && !startLevel.name.Contains("&cMuseum ")) foreach (Player pl in Player.players) if (pl.level == oldLevel) { unloadOld = false; break; }
{ if (unloadOld && Server.AutoLoad) oldLevel.Unload(true);
foreach (Player pl in Player.players) if (pl.level == startLevel) { skipUnload = true; break; }
if (!skipUnload && Server.AutoLoad) startLevel.Unload(true);
} }
if (Server.lava.active && !Server.lava.sendingPlayers && Server.lava.map == foundLevel) if (!p.hidden) {
{ Player.GlobalChat(p, p.color + "*" + p.DisplayName + Server.DefaultColor + " went to &b" + lvl.name, false);
if (Server.lava.roundActive) Server.IRC.Say(p.color + p.DisplayName + " %rwent to &8" + lvl.name, false, true);
{ }
}
void CheckGamesJoin(Player p, Level lvl) {
if (Server.lava.active && !Server.lava.sendingPlayers && Server.lava.map == p.level) {
if (Server.lava.roundActive) {
Server.lava.AnnounceRoundInfo(p); Server.lava.AnnounceRoundInfo(p);
Server.lava.AnnounceTimeLeft(!Server.lava.flooded, true, p); Server.lava.AnnounceTimeLeft(!Server.lava.flooded, true, p);
} } else {
else
{
Player.SendMessage(p, "Vote for the next map!"); Player.SendMessage(p, "Vote for the next map!");
Player.SendMessage(p, "Choices: " + Server.lava.VoteString); Player.SendMessage(p, "Choices: " + Server.lava.VoteString);
} }
} }
if (Server.zombie.GameInProgess()) if (Server.zombie.GameInProgess()) {
{
if (p.level.name == Server.zombie.currentLevelName) if (p.level.name == Server.zombie.currentLevelName)
Server.zombie.InfectedPlayerLogin(p); Server.zombie.InfectedPlayerLogin(p);
} }
if (p.level.name != Server.zombie.currentLevelName) if (p.level.name != Server.zombie.currentLevelName) {
{
if(ZombieGame.alive.Contains(p)) if(ZombieGame.alive.Contains(p))
ZombieGame.alive.Remove(p); ZombieGame.alive.Remove(p);
if (ZombieGame.infectd.Contains(p)) if (ZombieGame.infectd.Contains(p))
ZombieGame.infectd.Remove(p); ZombieGame.infectd.Remove(p);
} }
if (p.inTNTwarsMap) if (p.inTNTwarsMap)
{
p.canBuild = true; p.canBuild = true;
} TntWarsGame game = TntWarsGame.Find(p.level);
if (TntWarsGame.Find(p.level) != null) if (game != null) {
{ if (game.GameStatus != TntWarsGame.TntWarsGameStatus.Finished &&
if (TntWarsGame.Find(p.level).GameStatus != TntWarsGame.TntWarsGameStatus.Finished && TntWarsGame.Find(p.level).GameStatus != TntWarsGame.TntWarsGameStatus.WaitingForPlayers) game.GameStatus != TntWarsGame.TntWarsGameStatus.WaitingForPlayers) {
{
p.canBuild = false; p.canBuild = false;
Player.SendMessage(p, "TNT Wars: Disabled your building because you are in a TNT Wars map!"); Player.SendMessage(p, "TNT Wars: Disabled your building because you are in a TNT Wars map!");
} }
p.inTNTwarsMap = true; p.inTNTwarsMap = true;
} }
if (!p.hidden) { Server.IRC.Say(p.color + p.DisplayName + " %rwent to &8" + foundLevel.name, false, true); }
}
else Player.SendMessage(p, "The level " + message + " is locked.");
}
}
else if (Server.AutoLoad)
{
if (!File.Exists("levels/" + message + ".lvl"))
{
Player.SendMessage(p, "Level \"" + message + "\" doesn't exist! Did you mean...");
Command.all.Find("search").Use(p, "levels " + message);
}
else if (Level.Find(message) != null || Level.CheckLoadOnGoto(message))
{
Command.all.Find("load").Use(p, message);
foundLevel = Level.Find(message);
if (foundLevel != null) Use(p, message);
}
else
Player.SendMessage(p, "Level \"" + message + "\" cannot be loaded using /goto!");
}
else
{
Player.SendMessage(p, "There is no level \"" + message + "\" loaded. Did you mean..");
Command.all.Find("search").Use(p, "levels " + message);
} }
GC.Collect(); public override void Help(Player p) {
GC.WaitForPendingFinalizers();
}
catch (Exception e) { Server.ErrorLog(e); }
}
public override void Help(Player p)
{
Player.SendMessage(p, "/goto <mapname> - Teleports yourself to a different level."); Player.SendMessage(p, "/goto <mapname> - Teleports yourself to a different level.");
} }
} }

View File

@ -51,6 +51,7 @@ namespace MCGalaxy.Commands
p.SendMessage("Added the map settings Directory within 'text/lockdown'!"); p.SendMessage("Added the map settings Directory within 'text/lockdown'!");
} }
param[1] = param[1].ToLower();
string filepath = "text/lockdown/map/" + param[1] + ""; string filepath = "text/lockdown/map/" + param[1] + "";
bool mapIsLockedDown = File.Exists(filepath); bool mapIsLockedDown = File.Exists(filepath);