Exemple #1
0
        public static void RealmLoad(Player player, Command cmd, string fileName, string worldName, string buildRankName, string accessRankName)
        {
            if (worldName == null && player.World == null)
            {
                player.Message("When using /realm from console, you must specify the realm name.");
                return;
            }

            if (fileName == null)
            {
                // No params given at all

                return;
            }

            string fullFileName = WorldManager.FindMapFile(player, fileName);

            if (fullFileName == null)
            {
                return;
            }

            // Loading map into current realm
            if (worldName == null)
            {
                if (!cmd.IsConfirmed)
                {
                    player.Confirm(cmd, "About to replace THIS REALM with \"{0}\".", fileName);
                    return;
                }
                Map map;
                try
                {
                    map = MapUtility.Load(fullFileName);
                }
                catch (Exception ex)
                {
                    player.MessageNow("Could not load specified file: {0}: {1}", ex.GetType().Name, ex.Message);
                    return;
                }
                World realm = player.World;

                // Loading to current realm
                realm.MapChangedBy = player.Name;
                realm.ChangeMap(map);

                realm.Players.Message(player, "{0}&S loaded a new map for this realm.",
                                      player.ClassyName);
                player.MessageNow("New map loaded for the realm {0}", realm.ClassyName);

                Logger.Log(LogType.UserActivity,
                           "{0} loaded new map for realm \"{1}\" from {2}",
                           player.Name, realm.Name, fileName);
                realm.IsHidden = false;
                realm.IsRealm  = true;
                WorldManager.SaveWorldList();
            }
            else
            {
                // Loading to some other (or new) realm
                if (!World.IsValidName(worldName))
                {
                    player.MessageInvalidWorldName(worldName);
                    return;
                }

                Rank buildRank  = RankManager.DefaultBuildRank;
                Rank accessRank = null;
                if (buildRankName != null)
                {
                    buildRank = RankManager.FindRank(buildRankName);
                    if (buildRank == null)
                    {
                        player.MessageNoRank(buildRankName);
                        return;
                    }
                    if (accessRankName != null)
                    {
                        accessRank = RankManager.FindRank(accessRankName);
                        if (accessRank == null)
                        {
                            player.MessageNoRank(accessRankName);
                            return;
                        }
                    }
                }

                // Retype realm name, if needed
                if (worldName == "-")
                {
                    if (player.LastUsedWorldName != null)
                    {
                        worldName = player.LastUsedWorldName;
                    }
                    else
                    {
                        player.Message("Cannot repeat realm name: you haven't used any names yet.");
                        return;
                    }
                }

                lock (WorldManager.SyncRoot)
                {
                    World realm = WorldManager.FindWorldExact(worldName);
                    if (realm != null)
                    {
                        player.LastUsedWorldName = realm.Name;
                        // Replacing existing realm's map
                        if (!cmd.IsConfirmed)
                        {
                            player.Confirm(cmd, "About to replace realm map for {0}&S with \"{1}\".",
                                           realm.ClassyName, fileName);
                            return;
                        }

                        Map map;
                        try
                        {
                            map            = MapUtility.Load(fullFileName);
                            realm.IsHidden = false;
                            realm.IsRealm  = true;
                            WorldManager.SaveWorldList();
                        }
                        catch (Exception ex)
                        {
                            player.MessageNow("Could not load specified file: {0}: {1}", ex.GetType().Name, ex.Message);
                            return;
                        }

                        try
                        {
                            realm.MapChangedBy = player.Name;
                            realm.ChangeMap(map);
                            realm.IsHidden = false;
                            realm.IsRealm  = true;
                            WorldManager.SaveWorldList();
                        }
                        catch (WorldOpException ex)
                        {
                            Logger.Log(LogType.Error,
                                       "Could not complete RealmLoad operation: {0}", ex.Message);
                            player.Message("&WRealmLoad: {0}", ex.Message);
                            return;
                        }

                        realm.Players.Message(player, "{0}&S loaded a new map for the realm {1}",
                                              player.ClassyName, realm.ClassyName);
                        player.MessageNow("New map for the realm {0}&S has been loaded.", realm.ClassyName);
                        Logger.Log(LogType.UserActivity,
                                   "{0} loaded new map for realm \"{1}\" from {2}",
                                   player.Name, realm.Name, fullFileName);
                    }
                    else
                    {
                        // Adding a new realm
                        string targetFullFileName = Path.Combine(Paths.MapPath, worldName + ".fcm");
                        if (!cmd.IsConfirmed &&
                            File.Exists(targetFullFileName) && // target file already exists
                            !Paths.Compare(targetFullFileName, fullFileName))
                        {
                            // and is different from sourceFile
                            player.Confirm(cmd,
                                           "A map named \"{0}\" already exists, and will be overwritten with \"{1}\".",
                                           Path.GetFileName(targetFullFileName), Path.GetFileName(fullFileName));
                            return;
                        }

                        Map map;
                        try
                        {
                            map = MapUtility.Load(fullFileName);
                            //realm.IsHidden = false;
                            //realm.IsRealm = true;
                            //WorldManager.SaveWorldList();
                        }
                        catch (Exception ex)
                        {
                            player.MessageNow("Could not load \"{0}\": {1}: {2}",
                                              fileName, ex.GetType().Name, ex.Message);
                            return;
                        }

                        World newWorld;
                        try
                        {
                            newWorld = WorldManager.AddWorld(player, worldName, map, false);
                        }
                        catch (WorldOpException ex)
                        {
                            player.Message("RealmLoad: {0}", ex.Message);
                            return;
                        }

                        player.LastUsedWorldName       = worldName;
                        newWorld.BuildSecurity.MinRank = buildRank;
                        if (accessRank == null)
                        {
                            newWorld.AccessSecurity.ResetMinRank();
                        }
                        else
                        {
                            newWorld.AccessSecurity.MinRank = accessRank;
                        }
                        newWorld.BlockDB.AutoToggleIfNeeded();
                        if (BlockDB.IsEnabledGlobally && newWorld.BlockDB.IsEnabled)
                        {
                            player.Message("BlockDB is now auto-enabled on realm {0}", newWorld.ClassyName);
                        }
                        newWorld.LoadedBy = player.Name;
                        newWorld.LoadedOn = DateTime.UtcNow;
                        Server.Message("{0}&S created a new realm named {1}",
                                       player.ClassyName, newWorld.ClassyName);
                        Logger.Log(LogType.UserActivity,
                                   "{0} created a new realm named \"{1}\" (loaded from \"{2}\")",
                                   player.Name, worldName, fileName);
                        newWorld.IsHidden = false;
                        newWorld.IsRealm  = true;
                        WorldManager.SaveWorldList();
                        player.MessageNow("Access permission is {0}+&S, and build permission is {1}+",
                                          newWorld.AccessSecurity.MinRank.ClassyName,
                                          newWorld.BuildSecurity.MinRank.ClassyName);
                    }
                }
            }

            Server.RequestGC();
        }
        static void WorldLoadHandler(Player player, Command cmd)
        {
            string fileName = cmd.Next();
            string worldName = cmd.Next();

            if (worldName == null && player.World == null)
            {
                player.Message("When using /WLoad from console, you must specify the world name.");
                return;
            }

            if (fileName == null)
            {
                // No params given at all
                CdWorldLoad.PrintUsage(player);
                return;
            }

            string fullFileName = WorldManager.FindMapFile(player, fileName);
            if (fullFileName == null) return;

            // Loading map into current world
            if (worldName == null)
            {
                if (!cmd.IsConfirmed)
                {
                    player.Confirm(cmd, "Replace THIS MAP with \"{0}\"?", fileName);
                    return;
                }
                Map map;
                try
                {
                    map = MapUtility.Load(fullFileName);
                }
                catch (Exception ex)
                {
                    player.MessageNow("Could not load specified file: {0}: {1}", ex.GetType().Name, ex.Message);
                    return;
                }
                World world = player.World;

                // Loading to current world
                world.MapChangedBy = player.Name;
                world.ChangeMap(map);

                world.Players.Message(player, "{0}&S loaded a new map for this world.",
                                              player.ClassyName);
                player.MessageNow("New map loaded for the world {0}", world.ClassyName);

                Logger.Log(LogType.UserActivity,
                            "{0} loaded new map for world \"{1}\" from {2}",
                            player.Name, world.Name, fileName);

            }
            else
            {
                // Loading to some other (or new) world
                if (!World.IsValidName(worldName))
                {
                    player.MessageInvalidWorldName(worldName);
                    return;
                }

                string buildRankName = cmd.Next();
                string accessRankName = cmd.Next();
                Rank buildRank = RankManager.DefaultBuildRank;
                Rank accessRank = null;
                if (buildRankName != null)
                {
                    buildRank = RankManager.FindRank(buildRankName);
                    if (buildRank == null)
                    {
                        player.MessageNoRank(buildRankName);
                        return;
                    }
                    if (accessRankName != null)
                    {
                        accessRank = RankManager.FindRank(accessRankName);
                        if (accessRank == null)
                        {
                            player.MessageNoRank(accessRankName);
                            return;
                        }
                    }
                }

                // Retype world name, if needed
                if (worldName == "-")
                {
                    if (player.LastUsedWorldName != null)
                    {
                        worldName = player.LastUsedWorldName;
                    }
                    else
                    {
                        player.Message("Cannot repeat world name: you haven't used any names yet.");
                        return;
                    }
                }

                lock (WorldManager.SyncRoot)
                {
                    World world = WorldManager.FindWorldExact(worldName);
                    if (world != null)
                    {
                        player.LastUsedWorldName = world.Name;
                        // Replacing existing world's map
                        if (!cmd.IsConfirmed)
                        {
                            player.Confirm(cmd, "Replace map for {0}&S with \"{1}\"?",
                                            world.ClassyName, fileName);
                            return;
                        }

                        Map map;
                        try
                        {
                            map = MapUtility.Load(fullFileName);
                        }
                        catch (Exception ex)
                        {
                            player.MessageNow("Could not load specified file: {0}: {1}", ex.GetType().Name, ex.Message);
                            return;
                        }

                        try
                        {
                            world.MapChangedBy = player.Name;
                            world.ChangeMap(map);
                        }
                        catch (WorldOpException ex)
                        {
                            Logger.Log(LogType.Error,
                                        "Could not complete WorldLoad operation: {0}", ex.Message);
                            player.Message("&WWLoad: {0}", ex.Message);
                            return;
                        }

                        world.Players.Message(player, "{0}&S loaded a new map for the world {1}",
                                               player.ClassyName, world.ClassyName);
                        player.MessageNow("New map for the world {0}&S has been loaded.", world.ClassyName);
                        Logger.Log(LogType.UserActivity,
                                    "{0} loaded new map for world \"{1}\" from {2}",
                                    player.Name, world.Name, fullFileName);

                    }
                    else
                    {
                        // Adding a new world
                        string targetFullFileName = Path.Combine(Paths.MapPath, worldName + ".fcm");
                        if (!cmd.IsConfirmed &&
                            File.Exists(targetFullFileName) && // target file already exists
                            !Paths.Compare(targetFullFileName, fullFileName))
                        {
                            // and is different from sourceFile
                            player.Confirm(cmd,
                                            "A map named \"{0}\" already exists, and will be overwritten with \"{1}\".",
                                            Path.GetFileName(targetFullFileName), Path.GetFileName(fullFileName));
                            return;
                        }

                        Map map;
                        try
                        {
                            map = MapUtility.Load(fullFileName);
                        }
                        catch (Exception ex)
                        {
                            player.MessageNow("Could not load \"{0}\": {1}: {2}",
                                               fileName, ex.GetType().Name, ex.Message);
                            return;
                        }

                        World newWorld;
                        try
                        {
                            newWorld = WorldManager.AddWorld(player, worldName, map, false);
                        }
                        catch (WorldOpException ex)
                        {
                            player.Message("WLoad: {0}", ex.Message);
                            return;
                        }

                        if (newWorld == null)
                        {
                            player.MessageNow("Failed to create a new world.");
                            return;
                        }

                        player.LastUsedWorldName = worldName;
                        newWorld.BuildSecurity.MinRank = buildRank;
                        if (accessRank == null)
                        {
                            newWorld.AccessSecurity.ResetMinRank();
                        }
                        else
                        {
                            newWorld.AccessSecurity.MinRank = accessRank;
                        }
                        newWorld.BlockDB.AutoToggleIfNeeded();
                        if (BlockDB.IsEnabledGlobally && newWorld.BlockDB.IsEnabled)
                        {
                            player.Message("BlockDB is now auto-enabled on world {0}", newWorld.ClassyName);
                        }
                        newWorld.LoadedBy = player.Name;
                        newWorld.LoadedOn = DateTime.UtcNow;
                        Server.Message("{0}&S created a new world named {1}",
                                        player.ClassyName, newWorld.ClassyName);
                        Logger.Log(LogType.UserActivity,
                                    "{0} created a new world named \"{1}\" (loaded from \"{2}\")",
                                    player.Name, worldName, fileName);
                        WorldManager.SaveWorldList();
                        player.MessageNow("Access permission is {0}+&S, and build permission is {1}+",
                                           newWorld.AccessSecurity.MinRank.ClassyName,
                                           newWorld.BuildSecurity.MinRank.ClassyName);
                    }
                }
            }

            Server.RequestGC();
        }
        static void WorldRenameHandler(Player player, Command cmd)
        {
            string oldName = cmd.Next();
            string newName = cmd.Next();
            if (oldName == null || newName == null)
            {
                CdWorldRename.PrintUsage(player);
                return;
            }

            World oldWorld = WorldManager.FindWorldOrPrintMatches(player, oldName);
            if (oldWorld == null) return;
            oldName = oldWorld.Name;

            if (!World.IsValidName(newName))
            {
                player.MessageInvalidWorldName(newName);
                return;
            }

            World newWorld = WorldManager.FindWorldExact(newName);
            if (!cmd.IsConfirmed && newWorld != null && newWorld != oldWorld)
            {
                player.Confirm(cmd, "A world named {0}&S already exists. Replace it?", newWorld.ClassyName);
                return;
            }

            if (!cmd.IsConfirmed && File.Exists(Path.Combine(Paths.MapPath, newName + ".fcm")))
            {
                player.Confirm(cmd, "Renaming this world will overwrite an existing map file \"{0}.fcm\".", newName);
                return;
            }

            try
            {
                WorldManager.RenameWorld(oldWorld, newName, true, true);
            }
            catch (WorldOpException ex)
            {
                switch (ex.ErrorCode)
                {
                    case WorldOpExceptionCode.NoChangeNeeded:
                        player.MessageNow("WRename: World is already named \"{0}\"", oldName);
                        return;
                    case WorldOpExceptionCode.DuplicateWorldName:
                        player.MessageNow("WRename: Another world named \"{0}\" already exists.", newName);
                        return;
                    case WorldOpExceptionCode.InvalidWorldName:
                        player.MessageNow("WRename: Invalid world name: \"{0}\"", newName);
                        return;
                    case WorldOpExceptionCode.MapMoveError:
                        player.MessageNow("WRename: World \"{0}\" was renamed to \"{1}\", but the map file could not be moved due to an error: {2}",
                                            oldName, newName, ex.InnerException);
                        return;
                    default:
                        player.MessageNow("&WWRename: Unexpected error renaming world \"{0}\": {1}", oldName, ex.Message);
                        Logger.Log(LogType.Error,
                                    "WorldCommands.Rename: Unexpected error while renaming world {0} to {1}: {2}",
                                    oldWorld.Name, newName, ex);
                        return;
                }
            }

            player.LastUsedWorldName = newName;
            WorldManager.SaveWorldList();
            Logger.Log(LogType.UserActivity,
                        "{0} renamed the world \"{1}\" to \"{2}\".",
                        player.Name, oldName, newName);
            Server.Message("{0}&S renamed the world \"{1}\" to \"{2}\"",
                              player.ClassyName, oldName, newName);
        }
        static void PortalCreateCallback(Player player, Vector3I[] marks, object tag)
        {
            try
            {
                World world = WorldManager.FindWorldExact(player.PortalWorld);

                if (world != null)
                {
                    DrawOperation op = (DrawOperation)tag;
                    if (!op.Prepare(marks)) return;
                    if (!player.CanDraw(op.BlocksTotalEstimate))
                    {
                        player.MessageNow("You are only allowed to run draw commands that affect up to {0} blocks. This one would affect {1} blocks.",
                                           player.Info.Rank.DrawLimit,
                                           op.Bounds.Volume);
                        op.Cancel();
                        return;
                    }

                    int Xmin = Math.Min(marks[0].X, marks[1].X);
                    int Xmax = Math.Max(marks[0].X, marks[1].X);
                    int Ymin = Math.Min(marks[0].Y, marks[1].Y);
                    int Ymax = Math.Max(marks[0].Y, marks[1].Y);
                    int Zmin = Math.Min(marks[0].Z, marks[1].Z);
                    int Zmax = Math.Max(marks[0].Z, marks[1].Z);

                    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.World, new Vector3I(x, y, z)))
                                {
                                    player.Message("You can not build a portal near a spawnpoint.");
                                    return;
                                }

                                if (PortalHandler.GetInstance().GetPortal(player.World, new Vector3I(x, y, z)) != null)
                                {
                                    player.Message("You can not build a portal inside a portal, U MAD BRO?");
                                    return;
                                }
                            }
                        }
                    }

                    if (player.PortalName == null)
                    {
                        player.PortalName = Portal.GenerateName(player.World);
                    }

                    Portal portal = new Portal(player.PortalWorld, marks, player.PortalName, player.Name, player.World.Name);
                    PortalHandler.CreatePortal(portal, player.World);
                    op.AnnounceCompletion = false;
                    op.Context = BlockChangeContext.Portal;
                    op.Begin();

                    player.Message("Successfully created portal with name " + portal.Name + ".");
                }
                else
                {
                    player.MessageInvalidWorldName(player.PortalWorld);
                }
            }
            catch (Exception ex)
            {
                player.Message("Failed to create portal.");
                Logger.Log(LogType.Error, "WorldCommands.PortalCreateCallback: " + ex);
            }
        }