From 4a3a1b82c55cf853e1016860eab7c4ff656fd050 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Thu, 28 Jul 2016 16:42:54 +1000 Subject: [PATCH] Now with less linq and less tabs. --- Commands/Information/CmdWhois.cs | 2 +- Levels/Physics/BirdPhysics.cs | 150 +++++++++++++++---------------- Player/Group/Group.cs | 104 ++++++++++----------- Player/Group/GroupCommands.cs | 17 ++-- Player/Player.Timers.cs | 3 +- 5 files changed, 135 insertions(+), 141 deletions(-) diff --git a/Commands/Information/CmdWhois.cs b/Commands/Information/CmdWhois.cs index 465920050..e3333d8c5 100644 --- a/Commands/Information/CmdWhois.cs +++ b/Commands/Information/CmdWhois.cs @@ -75,7 +75,7 @@ namespace MCGalaxy.Commands { } WhoInfo FromOffline(OfflinePlayer target, string message) { - Group group = Group.Find(Group.findPlayer(target.name)); + Group group = Group.findPlayerGroup(target.name); string color = target.color == "" ? group.color : target.color; string prefix = target.title == "" ? "" : color + "[" + target.titleColor + target.title + color + "] "; diff --git a/Levels/Physics/BirdPhysics.cs b/Levels/Physics/BirdPhysics.cs index f741d2eb1..95aab442f 100644 --- a/Levels/Physics/BirdPhysics.cs +++ b/Levels/Physics/BirdPhysics.cs @@ -1,82 +1,80 @@ /* - Copyright 2010 MCSharp team (Modified for use with MCZall/MCLawl/MCGalaxy) - - Dual-licensed under the Educational Community License, Version 2.0 and - the GNU General Public License, Version 3 (the "Licenses"); you may - not use this file except in compliance with the Licenses. You may - obtain a copy of the Licenses at - - http://www.opensource.org/licenses/ecl2.php - http://www.gnu.org/licenses/gpl-3.0.html - - Unless required by applicable law or agreed to in writing, - software distributed under the Licenses are distributed on an "AS IS" - BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - or implied. See the Licenses for the specific language governing - permissions and limitations under the Licenses. + Copyright 2010 MCSharp team (Modified for use with MCZall/MCLawl/MCGalaxy) + + Dual-licensed under the Educational Community License, Version 2.0 and + the GNU General Public License, Version 3 (the "Licenses"); you may + not use this file except in compliance with the Licenses. You may + obtain a copy of the Licenses at + + http://www.opensource.org/licenses/ecl2.php + http://www.gnu.org/licenses/gpl-3.0.html + + Unless required by applicable law or agreed to in writing, + software distributed under the Licenses are distributed on an "AS IS" + BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + or implied. See the Licenses for the specific language governing + permissions and limitations under the Licenses. */ using System; -namespace MCGalaxy.BlockPhysics { - - public static class BirdPhysics { - - public static void Do(Level lvl, ref Check C) { - Random rand = lvl.physRandom; - ushort x, y, z; - lvl.IntToPos(C.b, out x, out y, out z); +namespace MCGalaxy.BlockPhysics { + public static class BirdPhysics { + + public static void Do(Level lvl, ref Check C) { + Random rand = lvl.physRandom; + ushort x, y, z; + lvl.IntToPos(C.b, out x, out y, out z); - switch (rand.Next(1, 15)) { - case 1: - if (lvl.GetTile(x, (ushort)(y - 1), z) == Block.air) - lvl.AddUpdate(lvl.PosToInt(x, (ushort)(y - 1), z), lvl.blocks[C.b]); - else goto case 3; - break; - case 2: - if (lvl.GetTile(x, (ushort)(y + 1), z) == Block.air) - lvl.AddUpdate(lvl.PosToInt(x, (ushort)(y + 1), z), lvl.blocks[C.b]); - else goto case 6; - break; - case 3: - case 4: - case 5: - FlyTo(lvl, ref C, x - 1, y, z); - break; - case 6: - case 7: - case 8: - FlyTo(lvl, ref C, x + 1, y, z); - break; - case 9: - case 10: - case 11: - FlyTo(lvl, ref C, x, y, z - 1); - break; - default: - FlyTo(lvl, ref C, x, y, z + 1); - break; - } - lvl.AddUpdate(C.b, Block.air); - C.data.Data = 255; - } - - static void FlyTo(Level lvl, ref Check C, int x, int y, int z) { - int index = lvl.PosToInt((ushort)x, (ushort)y, (ushort)z); - if (index < 0) - return; - - switch (lvl.blocks[index]) { - case Block.air: - lvl.AddUpdate(index, lvl.blocks[C.b]); - break; - case Block.op_air: - break; - default: - PhysicsArgs args = default(PhysicsArgs); - args.Type1 = PhysicsArgs.Dissipate; args.Value1 = 25; - lvl.AddUpdate(C.b, Block.red, false, args); - break; - } - } - } + switch (rand.Next(1, 15)) { + case 1: + if (lvl.GetTile(x, (ushort)(y - 1), z) == Block.air) + lvl.AddUpdate(lvl.PosToInt(x, (ushort)(y - 1), z), lvl.blocks[C.b]); + else goto case 3; + break; + case 2: + if (lvl.GetTile(x, (ushort)(y + 1), z) == Block.air) + lvl.AddUpdate(lvl.PosToInt(x, (ushort)(y + 1), z), lvl.blocks[C.b]); + else goto case 6; + break; + case 3: + case 4: + case 5: + FlyTo(lvl, ref C, x - 1, y, z); + break; + case 6: + case 7: + case 8: + FlyTo(lvl, ref C, x + 1, y, z); + break; + case 9: + case 10: + case 11: + FlyTo(lvl, ref C, x, y, z - 1); + break; + default: + FlyTo(lvl, ref C, x, y, z + 1); + break; + } + lvl.AddUpdate(C.b, Block.air); + C.data.Data = 255; + } + + static void FlyTo(Level lvl, ref Check C, int x, int y, int z) { + int index = lvl.PosToInt((ushort)x, (ushort)y, (ushort)z); + if (index < 0) return; + + switch (lvl.blocks[index]) { + case Block.air: + lvl.AddUpdate(index, lvl.blocks[C.b]); + break; + case Block.op_air: + break; + default: + PhysicsArgs args = default(PhysicsArgs); + args.Type1 = PhysicsArgs.Dissipate; args.Value1 = 25; + lvl.AddUpdate(C.b, Block.red, false, args); + break; + } + } + } } diff --git a/Player/Group/Group.cs b/Player/Group/Group.cs index f786b84dc..ebc450b7b 100644 --- a/Player/Group/Group.cs +++ b/Player/Group/Group.cs @@ -19,7 +19,6 @@ using System; using System.Collections.Generic; using System.Globalization; using System.IO; -using System.Linq; namespace MCGalaxy { /// This is the group object, where ranks and their data are stored @@ -53,11 +52,8 @@ namespace MCGalaxy { public PlayerList playerList; public string MOTD = String.Empty; - /// - /// Create a new group object - /// - public Group() - { + /// Create a new group object + public Group() { Permission = LevelPermission.Null; } @@ -88,11 +84,9 @@ namespace MCGalaxy { OnGroupLoaded(this); OnGroupLoadedEvent.Call(this); } - /// - /// Fill the commands that this group can use - /// - public void fillCommands() - { + + /// Fill the commands that this group can use + public void fillCommands() { CommandList _commands = new CommandList(); GrpCommands.AddCommands(out _commands, Permission); commands = _commands; @@ -101,7 +95,7 @@ namespace MCGalaxy { public bool CanExecute(string cmdName) { return commands.Contains(Command.all.Find(cmdName)); } - + /// Check to see if this group can excute cmd /// The command object to check /// True if this group can use it, false if they cant @@ -114,25 +108,32 @@ namespace MCGalaxy { /// Load up all server groups public static void InitAll() { GroupList = new List(); - if (File.Exists("properties/ranks.properties")) GroupProperties.InitAll(); - if (findPerm(LevelPermission.Banned) == null) GroupList.Add(new Group(LevelPermission.Banned, 1, 1, "Banned", '8', String.Empty, "banned.txt")); - if (findPerm(LevelPermission.Guest) == null) GroupList.Add(new Group(LevelPermission.Guest, 1, 120, "Guest", '7', String.Empty, "guest.txt")); - if (findPerm(LevelPermission.Builder) == null) GroupList.Add(new Group(LevelPermission.Builder, 400, 300, "Builder", '2', String.Empty, "builders.txt")); - if (findPerm(LevelPermission.AdvBuilder) == null) GroupList.Add(new Group(LevelPermission.AdvBuilder, 1200, 900, "AdvBuilder", '3', String.Empty, "advbuilders.txt")); - if (findPerm(LevelPermission.Operator) == null) GroupList.Add(new Group(LevelPermission.Operator, 2500, 5400, "Operator", 'c', String.Empty, "operators.txt")); - if (findPerm(LevelPermission.Admin) == null) GroupList.Add(new Group(LevelPermission.Admin, 65536, int.MaxValue, "SuperOP", 'e', String.Empty, "uberOps.txt")); + if (findPerm(LevelPermission.Banned) == null) + GroupList.Add(new Group(LevelPermission.Banned, 1, 1, "Banned", '8', String.Empty, "banned.txt")); + if (findPerm(LevelPermission.Guest) == null) + GroupList.Add(new Group(LevelPermission.Guest, 1, 120, "Guest", '7', String.Empty, "guest.txt")); + if (findPerm(LevelPermission.Builder) == null) + GroupList.Add(new Group(LevelPermission.Builder, 400, 300, "Builder", '2', String.Empty, "builders.txt")); + if (findPerm(LevelPermission.AdvBuilder) == null) + GroupList.Add(new Group(LevelPermission.AdvBuilder, 1200, 900, "AdvBuilder", '3', String.Empty, "advbuilders.txt")); + if (findPerm(LevelPermission.Operator) == null) + GroupList.Add(new Group(LevelPermission.Operator, 2500, 5400, "Operator", 'c', String.Empty, "operators.txt")); + if (findPerm(LevelPermission.Admin) == null) + GroupList.Add(new Group(LevelPermission.Admin, 65536, int.MaxValue, "SuperOP", 'e', String.Empty, "uberOps.txt")); GroupList.Add(new Group(LevelPermission.Nobody, 65536, -1, "Nobody", '0', String.Empty, "nobody.txt")); GroupList.Sort((a, b) => a.Permission.CompareTo(b.Permission)); - if (Group.Find(Server.defaultRank) != null) standard = Group.Find(Server.defaultRank); - else standard = Group.findPerm(LevelPermission.Guest); + if (Group.Find(Server.defaultRank) != null) + standard = Group.Find(Server.defaultRank); + else + standard = Group.findPerm(LevelPermission.Guest); Player[] players = PlayerInfo.Online.Items; foreach (Player pl in players) - pl.group = GroupList.Find(g => g.name == pl.group.name); + pl.group = Find(pl.group.name); if (OnGroupLoad != null) OnGroupLoad(); OnGroupLoadEvent.Call(); @@ -142,9 +143,9 @@ namespace MCGalaxy { /// Save givenList group /// The list of groups to save public static void saveGroups(List givenList) { - lock (saveLock) - GroupProperties.SaveGroups(givenList); - + lock (saveLock) + GroupProperties.SaveGroups(givenList); + if (OnGroupSave != null) OnGroupSave(); OnGroupSaveEvent.Call(); } @@ -152,14 +153,21 @@ namespace MCGalaxy { /// Check whether a group with that name exists. public static bool Exists(string name) { name = name.ToLower(); - return GroupList.Any(gr => gr.name == name); + foreach (Group grp in GroupList) { + if (grp.name == name) return true; + } + return false; } /// Find the group which has the given name. public static Group Find(string name) { name = name.ToLower(); MapName(ref name); - return GroupList.Find(gr => gr.name == name); + + foreach (Group grp in GroupList) { + if (grp.name == name) return grp; + } + return null; } /// Find the group(s) which contain the given name. @@ -172,11 +180,11 @@ namespace MCGalaxy { /// Find the group(s) which contain the given name. public static Group FindMatches(Player p, string name) { - int matches = 0; return FindMatches(p, name, out matches); + int matches = 0; return FindMatches(p, name, out matches); } static void MapName(ref string name) { - if (name == "adv") name = "advbuilder"; + if (name == "adv") name = "advbuilder"; if (name == "op") name = "operator"; if (name == "super" || (name == "admin" && !Group.Exists("admin"))) name = "superop"; if (name == "noone") name = "nobody"; @@ -193,40 +201,32 @@ namespace MCGalaxy { } /// Get the group name that player /playerName/ is in - /// The player Name + /// The player Name /// The group name - public static string findPlayer(string playerName) { - foreach (Group grp in Group.GroupList.Where(grp => grp.playerList.Contains(playerName))) { - return grp.name; - } - return Group.standard.name; - } + public static string findPlayer(string name) { return findPlayerGroup(name).name; } /// Find the group object that the player /playerName/ is in - /// The player name + /// The player name /// The group object that the player is in - public static Group findPlayerGroup(string playerName) { - foreach (Group grp in Group.GroupList.Where(grp => grp.playerList.Contains(playerName))) { - return grp; + public static Group findPlayerGroup(string name) { + foreach (Group grp in Group.GroupList) { + if (grp.playerList.Contains(name)) return grp; } return Group.standard; } public static string concatList(bool includeColor = true, bool skipExtra = false, bool permissions = false) { - string returnString = ""; - foreach (Group grp in Group.GroupList.Where(grp => !skipExtra || (grp.Permission > LevelPermission.Guest && grp.Permission < LevelPermission.Nobody))) - { - if (includeColor) - returnString += ", " + grp.color + grp.name + Server.DefaultColor; - else if (permissions) - returnString += ", " + ((int)grp.Permission).ToString(CultureInfo.InvariantCulture); - else - returnString += ", " + grp.name; + string sum = ""; + foreach (Group grp in Group.GroupList) { + if (skipExtra && (grp.Permission < LevelPermission.Guest || grp.Permission >= LevelPermission.Nobody)) continue; + + if (includeColor) sum += ", " + grp.ColoredName + Server.DefaultColor; + else if (permissions) sum += ", " + ((int)grp.Permission); + else sum += ", " + grp.name; } - if (includeColor) returnString = returnString.Remove(returnString.Length - 2); - - return returnString.Remove(0, 2); + if (includeColor) sum = sum.Remove(sum.Length - 2); + return sum.Remove(0, 2); } /// Returns whether the given player is in the banned rank. diff --git a/Player/Group/GroupCommands.cs b/Player/Group/GroupCommands.cs index 760b4ce3c..f19eec32e 100644 --- a/Player/Group/GroupCommands.cs +++ b/Player/Group/GroupCommands.cs @@ -18,7 +18,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; namespace MCGalaxy { public class GrpCommands { @@ -163,20 +162,16 @@ namespace MCGalaxy { public static string getInts(List givenList) { if (givenList == null) return ""; - string returnString = ""; bool foundOne = false; - foreach (LevelPermission Perm in givenList) - { - foundOne = true; - returnString += "," + (int)Perm; - } - if (foundOne) returnString = returnString.Remove(0, 1); - return returnString; + return givenList.Join(p => ((int)p).ToString(), ","); } public static void AddCommands(out CommandList commands, LevelPermission perm) { commands = new CommandList(); - foreach (rankAllowance aV in allowedCommands.Where(aV => (aV.lowestRank <= perm && !aV.disallow.Contains(perm)) || aV.allow.Contains(perm))) - commands.Add(Command.all.Find(aV.commandName)); + foreach (rankAllowance perms in allowedCommands) { + bool canUse = perms.lowestRank <= perm && !perms.disallow.Contains(perm); + if (canUse || perms.allow.Contains(perm)) + commands.Add(Command.all.Find(perms.commandName)); + } } } } diff --git a/Player/Player.Timers.cs b/Player/Player.Timers.cs index ab490d1bf..786c09441 100644 --- a/Player/Player.Timers.cs +++ b/Player/Player.Timers.cs @@ -75,7 +75,8 @@ namespace MCGalaxy { SendMessage("You currently have &a" + money + " %S" + Server.moneys); try { - if ( !Group.Find("Nobody").commands.Contains("award") && !Group.Find("Nobody").commands.Contains("awards") && !Group.Find("Nobody").commands.Contains("awardmod") ) + Group nobody = Group.findPerm(LevelPermission.Nobody); + if (!nobody.commands.Contains("award") && !nobody.commands.Contains("awards") && !nobody.commands.Contains("awardmod") ) SendMessage("You have " + Awards.AwardAmount(name) + " awards."); } catch { }