From df6214160c568f7eff792b9bad50ebce1bb3c1ed Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Thu, 7 Apr 2016 09:55:22 +1000 Subject: [PATCH 1/6] Make all 'you can only use on X rank or below' messages consistent. --- Blocks/Block.CoreProps.cs | 7 ++ Blocks/Block.cs | 108 ++++------------------------- Commands/Chat/CmdChatRoom.cs | 6 +- Commands/Chat/CmdHug.cs | 2 +- Commands/Command.cs | 11 +++ Commands/Fun/CmdCountdown.cs | 2 +- Commands/Fun/CmdSlap.cs | 2 +- Commands/Information/CmdOpRules.cs | 2 +- Commands/Moderation/CmdBan.cs | 6 +- Commands/Moderation/CmdFollow.cs | 2 +- Commands/Moderation/CmdFreeze.cs | 2 +- Commands/Moderation/CmdJail.cs | 2 +- Commands/Moderation/CmdJoker.cs | 4 +- Commands/Moderation/CmdMute.cs | 2 +- Commands/Moderation/CmdOhide.cs | 2 +- Commands/Moderation/CmdP2P.cs | 2 +- Commands/Moderation/CmdPossess.cs | 2 +- Commands/Moderation/CmdReveal.cs | 4 +- Commands/Moderation/CmdSetRank.cs | 5 +- Commands/Moderation/CmdTempBan.cs | 2 +- Commands/Moderation/CmdUndoArea.cs | 4 +- Commands/Moderation/CmdWarn.cs | 2 +- Commands/Moderation/CmdXmute.cs | 3 +- Commands/building/CmdUndo.cs | 2 +- Commands/other/CmdColor.cs | 2 +- Commands/other/CmdImpersonate.cs | 2 +- Commands/other/CmdInvincible.cs | 2 +- Commands/other/CmdKill.cs | 2 +- Commands/other/CmdMove.cs | 8 ++- Commands/other/CmdNick.cs | 2 +- Commands/other/CmdSendCmd.cs | 2 +- Commands/other/CmdSummon.cs | 2 +- Commands/other/CmdTitle.cs | 2 +- Commands/other/CmdTp.cs | 2 +- Commands/other/CmdXspawn.cs | 2 +- 35 files changed, 80 insertions(+), 134 deletions(-) diff --git a/Blocks/Block.CoreProps.cs b/Blocks/Block.CoreProps.cs index 296b63da7..6b78ddd29 100644 --- a/Blocks/Block.CoreProps.cs +++ b/Blocks/Block.CoreProps.cs @@ -56,7 +56,14 @@ namespace MCGalaxy { if (i >= red && i <= white) Properties[i].KilledByLava = true; + if (i == air || i == shrub || (i >= yellowflower && i <= redmushroom) { + Properties[i].KilledByLava = true; + Properties[i].KilledByWater = true; + } } + // Block specific properties + Properties[wood].KilledByLava = true; Properties[trunk].KilledByLava = true; + Properties[sponge].KilledByLava = true; Properties[bookcase].KilledByLava = true; SetupDefaultNames(); } diff --git a/Blocks/Block.cs b/Blocks/Block.cs index fe9bc7d30..6b454563f 100644 --- a/Blocks/Block.cs +++ b/Blocks/Block.cs @@ -21,23 +21,9 @@ namespace MCGalaxy { public sealed partial class Block { - public static bool Walkthrough(byte type) - { - switch (type) - { - case air: - case water: - case waterstill: - case lava: - case lavastill: - case yellowflower: - case redflower: - case mushroom: - case redmushroom: - case shrub: - return true; - } - return false; + public static bool Walkthrough(byte type) { + return type == air || type == shrub || (type >= water && type <= lavastill) + && (type >= yelllowflower && type <= redmushroom); } public static bool AllowBreak(byte type) @@ -98,35 +84,14 @@ namespace MCGalaxy return false; } - public static bool Placable(byte type) - { - switch (type) - { - // case Block.air: - // case Block.grass: - case Block.blackrock: - case Block.water: - case Block.waterstill: - case Block.lava: - case Block.lavastill: - return false; - } - return type < Block.CpeCount; + public static bool Placable(byte type) { + return !(type == blackrock || (type >= water && type <= lavstill) + && type < Block.CpeCount; } - public static bool RightClick(byte type, bool countAir = false) - { + public static bool RightClick(byte type, bool countAir = false) { if (countAir && type == Block.air) return true; - - switch (type) - { - case Block.water: - case Block.lava: - case Block.waterstill: - case Block.lavastill: - return true; - } - return false; + return type >= water && type <= lavastill; } public static bool OPBlocks(byte type) { return Properties[type].OPBlock; } @@ -169,60 +134,17 @@ namespace MCGalaxy public static bool BuildIn(byte type) { if (type == op_water || type == op_lava || Block.portal(type) || Block.mb(type)) return false; - - switch (Block.Convert(type)) - { - case water: - case lava: - case waterstill: - case lavastill: - return true; - } - return false; + type = Block.Convert(type); + return type >= water && type <= lavastill; } - public static bool Mover(byte type) { - return walkthroughHandlers[type] != null; - } + public static bool Mover(byte type) { return walkthroughHandlers[type] != null; } - public static bool FireKill(byte type) { - return type != Block.air && LavaKill(type); - } + public static bool FireKill(byte type) { return type != air && Properties[type].KilledByLava; } - public static bool LavaKill(byte type) - { - switch (type) - { - case Block.air: - case Block.wood: - case Block.shrub: - case Block.trunk: - case Block.leaf: - case Block.sponge: - case Block.yellowflower: - case Block.redflower: - case Block.mushroom: - case Block.redmushroom: - case Block.bookcase: - return true; - } - return Properties[type].KilledByLava; - } - public static bool WaterKill(byte type) - { - switch (type) - { - case Block.air: - case Block.shrub: - case Block.leaf: - case Block.yellowflower: - case Block.redflower: - case Block.mushroom: - case Block.redmushroom: - return true; - } - return Properties[type].KilledByWater; - } + public static bool LavaKill(byte type) { return Properties[type].KilledByLava; } + + public static bool WaterKill(byte type) { return Properties[type].KilledByWater; } public static bool LightPass(byte type, byte extType, BlockDefinition[] defs) { switch (Convert(type)) { diff --git a/Commands/Chat/CmdChatRoom.cs b/Commands/Chat/CmdChatRoom.cs index 8251bd652..593ade662 100644 --- a/Commands/Chat/CmdChatRoom.cs +++ b/Commands/Chat/CmdChatRoom.cs @@ -208,8 +208,7 @@ namespace MCGalaxy.Commands { return; } if (pl.group.Permission >= p.group.Permission) { - Player.SendMessage(p, "You can't force someone of a higher or equal rank to join a chatroom."); - return; + MessageTooHighRank(p, "force-join", false); return; } if (pl.spyChatRooms.Contains(room)) { @@ -234,8 +233,7 @@ namespace MCGalaxy.Commands { Player pl = PlayerInfo.FindOrShowMatches(p, name); if (pl == null) return; if (pl.group.Permission >= p.group.Permission) { - Player.SendMessage(p, "You can't kick someone of a higher or equal rank from a chatroom."); - return; + MessageTooHighRank(p, "kick from a chatroom", false); return; } Player.SendMessage(pl, "You were kicked from the chat room '" + pl.Chatroom + "'"); diff --git a/Commands/Chat/CmdHug.cs b/Commands/Chat/CmdHug.cs index 31aae3165..39a0d896b 100644 --- a/Commands/Chat/CmdHug.cs +++ b/Commands/Chat/CmdHug.cs @@ -51,7 +51,7 @@ namespace MCGalaxy.Commands { Player.SendMessage(p, "You cannot %cdeath-hug %Sat your current rank."); return; } if (p != null && who.group.Permission > p.group.Permission) { - Player.SendMessage(p, "You can't %cdeath-hug %Ssomeone of greater rank."); return; + MessageTooHighRank(p, "&cdeath-hug%S", true); return; } who.HandleDeath(Block.rock, " died from a %cdeadly hug."); } diff --git a/Commands/Command.cs b/Commands/Command.cs index 002da5913..19bf37b3e 100644 --- a/Commands/Command.cs +++ b/Commands/Command.cs @@ -70,6 +70,17 @@ namespace MCGalaxy else Player.SendMessage(p, "Only " + grp.color + grp.name + "%s+ can " + action); } + + protected void MessageTooHighRank(Player p, string action, bool canAffectOwnRank) { + MessageTooHighRank(p, action, p.group, canAffectOwnRank); + } + + protected void MessageTooHighRank(Player p, string action, Group grp, bool canAffectGroup) { + if (canAffectGroup) + Player.SendMessage(p, "Can only " + action + " players ranked " + grp.color + grp.name + " %Sor below"); + else + Player.SendMessage(p, "Can only " + action + " players ranked below " + grp.color + grp.name); + } } public struct CommandPerm { diff --git a/Commands/Fun/CmdCountdown.cs b/Commands/Fun/CmdCountdown.cs index be736e345..5885f01d5 100644 --- a/Commands/Fun/CmdCountdown.cs +++ b/Commands/Fun/CmdCountdown.cs @@ -204,7 +204,7 @@ namespace MCGalaxy.Commands { Player who = PlayerInfo.FindOrShowMatches(p, target); if (who == null) return; if (p.group.Permission < who.group.Permission) { - Player.SendMessage(p, "You can't send rules to someone of a higher rank than yourself!!"); return; + MessageTooHighRank(p, "send countdown rules", true); return; } else { Player.SendMessage(who, "Countdown rules sent to you by " + p.color + p.name); Player.SendMessage(who, "The aim of the game is to stay alive the longest."); diff --git a/Commands/Fun/CmdSlap.cs b/Commands/Fun/CmdSlap.cs index 3f3f11664..b55233a2f 100644 --- a/Commands/Fun/CmdSlap.cs +++ b/Commands/Fun/CmdSlap.cs @@ -45,7 +45,7 @@ namespace MCGalaxy.Commands { return; } if (p != null && who.group.Permission > p.group.Permission) { - Player.SendMessage(p, "You cannot slap someone ranked higher than you!"); return; + MessageTooHighRank(p, "slap", true); return; } ushort curX = (ushort)(who.pos[0] / 32), curY = (ushort)(who.pos[1] / 32), curZ = (ushort)(who.pos[2] / 32); diff --git a/Commands/Information/CmdOpRules.cs b/Commands/Information/CmdOpRules.cs index 09c89ba32..872a7d7b4 100644 --- a/Commands/Information/CmdOpRules.cs +++ b/Commands/Information/CmdOpRules.cs @@ -40,7 +40,7 @@ namespace MCGalaxy.Commands who = PlayerInfo.FindOrShowMatches(p, message); if (who == null) return; if (p != null && p.group.Permission < who.group.Permission) { - Player.SendMessage(p, "You cannot send /oprules to a higher or same ranked player."); return; + MessageTooHighRank(p, "send /oprules", false); return; } } diff --git a/Commands/Moderation/CmdBan.cs b/Commands/Moderation/CmdBan.cs index 389942080..9e7595a36 100644 --- a/Commands/Moderation/CmdBan.cs +++ b/Commands/Moderation/CmdBan.cs @@ -95,14 +95,14 @@ namespace MCGalaxy.Commands { bool CheckPerms(string name, Group group, Player p) { if ((int)group.Permission >= CommandOtherPerms.GetPerm(this)) { - string highest = Group.findPermInt(CommandOtherPerms.GetPerm(this)).name; - Player.SendMessage(p, "You can't ban players ranked " + highest + " or higher!"); return false; + Group grp = Group.findPermInt(CommandOtherPerms.GetPerm(this)); + MessageTooHighRank(p, "ban", grp, false); return false; } if (group.Permission == LevelPermission.Banned) { Player.SendMessage(p, name + " is already banned."); return false; } if (p != null && group.Permission >= p.group.Permission) { - Player.SendMessage(p, "You cannot ban a person ranked equal or higher than you."); return false; + MessageTooHighRank(p, "ban", false); return false; } group.playerList.Remove(name); diff --git a/Commands/Moderation/CmdFollow.cs b/Commands/Moderation/CmdFollow.cs index 71bb6763e..40b59ca02 100644 --- a/Commands/Moderation/CmdFollow.cs +++ b/Commands/Moderation/CmdFollow.cs @@ -95,7 +95,7 @@ namespace MCGalaxy.Commands } if (who == null) { Player.SendMessage(p, "Could not find player."); return; } else if (who == p) { Player.SendMessage(p, "Cannot follow yourself."); return; } - else if (who.group.Permission >= p.group.Permission) { Player.SendMessage(p, "Cannot follow someone of equal or greater rank."); return; } + else if (who.group.Permission >= p.group.Permission) { MessageTooHighRank(p, "follow", false); return;} else if (who.following != "") { Player.SendMessage(p, who.DisplayName + " is already following " + who.following); return; } if (!p.hidden) Command.all.Find("hide").Use(p, ""); diff --git a/Commands/Moderation/CmdFreeze.cs b/Commands/Moderation/CmdFreeze.cs index 502245a6b..5125de0f2 100644 --- a/Commands/Moderation/CmdFreeze.cs +++ b/Commands/Moderation/CmdFreeze.cs @@ -32,7 +32,7 @@ namespace MCGalaxy.Commands if (who == null) return; if (p == who) { Player.SendMessage(p, "Cannot freeze yourself."); return; } if (p != null && who.group.Permission >= p.group.Permission) { - Player.SendMessage(p, "Cannot freeze someone of equal or greater rank."); return; + MessageTooHighRank(p, "freeze", false); return; } string frozenby = (p == null) ? "(console)" : p.ColoredName; diff --git a/Commands/Moderation/CmdJail.cs b/Commands/Moderation/CmdJail.cs index 67300ed59..322b97081 100644 --- a/Commands/Moderation/CmdJail.cs +++ b/Commands/Moderation/CmdJail.cs @@ -40,7 +40,7 @@ namespace MCGalaxy.Commands if (!who.jailed) { if (p != null &&who.group.Permission >= p.group.Permission) { - Player.SendMessage(p, "Cannot jail someone of equal or greater rank."); return; + MessageTooHighRank(p, "jail", false); return; } Player.SendMessage(p, "You jailed " + who.DisplayName); Player.GlobalDespawn(who, false); diff --git a/Commands/Moderation/CmdJoker.cs b/Commands/Moderation/CmdJoker.cs index 7b15cf904..f2abd0c6b 100644 --- a/Commands/Moderation/CmdJoker.cs +++ b/Commands/Moderation/CmdJoker.cs @@ -37,7 +37,9 @@ namespace MCGalaxy.Commands { Player who = PlayerInfo.FindOrShowMatches(p, message); if (who == null) return; - if (p != null && who.group.Permission > p.group.Permission) { Player.SendMessage(p, "Cannot joker someone of equal or greater rank."); return; } + if (p != null && who.group.Permission > p.group.Permission) { + MessageTooHighRank(p, "joker", true); return; + } if (!who.joker) { if (stealth) { diff --git a/Commands/Moderation/CmdMute.cs b/Commands/Moderation/CmdMute.cs index dcc31fde4..fe13a78d9 100644 --- a/Commands/Moderation/CmdMute.cs +++ b/Commands/Moderation/CmdMute.cs @@ -48,7 +48,7 @@ namespace MCGalaxy.Commands Extensions.DeleteLineWord("ranks/muted.txt", who.name.ToLower()); } else { if (p != null && who.group.Permission >= p.group.Permission) { - Player.SendMessage(p, "Cannot mute someone of a higher or equal rank."); return; + MessageTooHighRank(p, "mute", false); return; } who.muted = true; Player.SendChatFrom(who, who.color + who.DisplayName + " %Shas been &8muted", false); diff --git a/Commands/Moderation/CmdOhide.cs b/Commands/Moderation/CmdOhide.cs index 9abc37141..ada36a7d1 100644 --- a/Commands/Moderation/CmdOhide.cs +++ b/Commands/Moderation/CmdOhide.cs @@ -39,7 +39,7 @@ namespace MCGalaxy.Commands } if (who.group.Permission >= p.group.Permission) { - Player.SendMessage(p, "Cannot use this on someone of equal or greater rank."); return; + MessageTooHighRank(p, "hide", false); return; } Command.all.Find("hide").Use(who, ""); Player.SendMessage(p, "Used /hide on " + who.color + who.name + "%S."); diff --git a/Commands/Moderation/CmdP2P.cs b/Commands/Moderation/CmdP2P.cs index 8af2dd2af..3591b8867 100644 --- a/Commands/Moderation/CmdP2P.cs +++ b/Commands/Moderation/CmdP2P.cs @@ -37,7 +37,7 @@ namespace MCGalaxy.Commands { if (source == null || target == null) return; if (p.group.Permission < source.group.Permission) { - Player.SendMessage(p, "You cannot force a player of higher rank to tp to another player."); return; + MessageTooHighRank(p, "teleport", true); return; } Player.SendMessage(p, "Attempting to teleport " + source.name + " to " + target.name + "."); Command.all.Find("tp").Use(source, target.name); diff --git a/Commands/Moderation/CmdPossess.cs b/Commands/Moderation/CmdPossess.cs index c1dac0d9c..27a4f3e76 100644 --- a/Commands/Moderation/CmdPossess.cs +++ b/Commands/Moderation/CmdPossess.cs @@ -57,7 +57,7 @@ namespace MCGalaxy.Commands { Player who = PlayerInfo.FindOrShowMatches(p, message); if (who == null) return; if (who.group.Permission >= p.group.Permission) { - Player.SendMessage(p, "Cannot possess someone of equal or greater rank."); return; + MessageTooHighRank(p, "possess", false); return; } if (who.possess != "") { diff --git a/Commands/Moderation/CmdReveal.cs b/Commands/Moderation/CmdReveal.cs index 2115170dc..2340a4975 100644 --- a/Commands/Moderation/CmdReveal.cs +++ b/Commands/Moderation/CmdReveal.cs @@ -55,8 +55,8 @@ namespace MCGalaxy.Commands { Player who = PlayerInfo.FindOrShowMatches(p, parts[0]); if (who == null) { return; - } else if (who.group.Permission > p.group.Permission && p != who) { - Player.SendMessage(p, "Cannot reload the map of someone higher than you."); return; + } else if (who.group.Permission > p.group.Permission && p != who) { + MessageTooHighRank(p, "reload the map for", true); return; } ReloadMap(p, who, true); } diff --git a/Commands/Moderation/CmdSetRank.cs b/Commands/Moderation/CmdSetRank.cs index e7fed7d3b..c85528e58 100644 --- a/Commands/Moderation/CmdSetRank.cs +++ b/Commands/Moderation/CmdSetRank.cs @@ -76,8 +76,11 @@ namespace MCGalaxy.Commands Player.SendMessage(p, "Cannot change the rank to or from \"" + banned.name + "\"."); return false; } if (p != null && (group.Permission >= p.group.Permission || newRank.Permission >= p.group.Permission)) { - Player.SendMessage(p, "Cannot change the rank of someone of or to a rank equal or higher to yours."); return false; + MessageTooHighRank(p, "change the rank of", false); return false; } + if (p != null && (newRank.Permission >= p.group.Permission)) { + Player.SendMessage(p, "Cannot change the rank of a player to a rank equal or higher to yours."); return false; + } if (who != null) { Group.because(who, newRank); diff --git a/Commands/Moderation/CmdTempBan.cs b/Commands/Moderation/CmdTempBan.cs index 1de7bcc2f..5381ca5c1 100644 --- a/Commands/Moderation/CmdTempBan.cs +++ b/Commands/Moderation/CmdTempBan.cs @@ -36,7 +36,7 @@ namespace MCGalaxy.Commands { if (!Player.ValidName(target)) { Player.SendMessage(p, "Invalid name \"" + target + "\"."); return; } Group grp = who == null ? PlayerInfo.GetGroup(target) : who.group; if (p != null && grp.Permission >= p.group.Permission) { - Player.SendMessage(p, "Cannot temp ban someone of the same or higher rank."); return; + MessageTooHighRank(p, "temp ban", false); return; } int minutes = 60; diff --git a/Commands/Moderation/CmdUndoArea.cs b/Commands/Moderation/CmdUndoArea.cs index 7ac086521..3e6f9bf34 100644 --- a/Commands/Moderation/CmdUndoArea.cs +++ b/Commands/Moderation/CmdUndoArea.cs @@ -66,7 +66,7 @@ namespace MCGalaxy.Commands { void UndoOnlinePlayer(Player p, Player who, CatchPos cpos, ushort x, ushort y, ushort z) { if (p != who && who.group.Permission >= p.group.Permission) { - Player.SendMessage(p, "Cannot undo a user of higher or equal rank"); return; + MessageTooHighRank(p, "undo", false); return; } UndoOnlineDrawOp op = new UndoOnlineDrawOp(); @@ -84,7 +84,7 @@ namespace MCGalaxy.Commands { void UndoOfflinePlayer(Player p, string whoName, CatchPos cpos, ushort x, ushort y, ushort z) { Group group = Group.findPlayerGroup(whoName); if (group.Permission >= p.group.Permission) { - Player.SendMessage(p, "Cannot undo a user of higher or equal rank"); return; + MessageTooHighRank(p, "undo", false); return; } UndoOfflineDrawOp op = new UndoOfflineDrawOp(); diff --git a/Commands/Moderation/CmdWarn.cs b/Commands/Moderation/CmdWarn.cs index 523966773..468f06435 100644 --- a/Commands/Moderation/CmdWarn.cs +++ b/Commands/Moderation/CmdWarn.cs @@ -34,7 +34,7 @@ namespace MCGalaxy.Commands { if (who == null) return; if (who == p) { Player.SendMessage(p, "you can't warn yourself"); return; } if (p != null && p.group.Permission <= who.group.Permission) { - Player.SendMessage(p, "Cannot warn a player of equal or higher rank."); return; + MessageTooHighRank(p, "warn", false); return; } string reason = args.Length == 1 ? "you know why." : args[1]; diff --git a/Commands/Moderation/CmdXmute.cs b/Commands/Moderation/CmdXmute.cs index 48d756e02..df859db95 100644 --- a/Commands/Moderation/CmdXmute.cs +++ b/Commands/Moderation/CmdXmute.cs @@ -40,8 +40,7 @@ namespace MCGalaxy.Commands if (p != null && muter.group.Permission > p.group.Permission) { - Player.SendMessage(p, "You cannot xmute someone ranked higher than you!"); - return; + MessageTooHighRank(p, "xmute", true); return; } if (p == muter) { diff --git a/Commands/building/CmdUndo.cs b/Commands/building/CmdUndo.cs index 945d2ec81..4d588b4ce 100644 --- a/Commands/building/CmdUndo.cs +++ b/Commands/building/CmdUndo.cs @@ -87,7 +87,7 @@ namespace MCGalaxy.Commands void UndoOnlinePlayer(Player p, long seconds, Player who) { if (p != null && p != who) { if (who.group.Permission > p.group.Permission) { - Player.SendMessage(p, "Cannot undo a user of higher or equal rank"); return; + MessageTooHighRank(p, "undo", true); return; } if (!CheckAdditionalPerm(p)) { MessageNeedPerms(p, "can undo other players."); return; } } diff --git a/Commands/other/CmdColor.cs b/Commands/other/CmdColor.cs index a65e24ecb..69c39c58e 100644 --- a/Commands/other/CmdColor.cs +++ b/Commands/other/CmdColor.cs @@ -34,7 +34,7 @@ namespace MCGalaxy.Commands { Player who = PlayerInfo.FindOrShowMatches(p, args[0]); if (who == null) return; if (p != null && who.group.Permission > p.group.Permission) { - Player.SendMessage(p, "Cannot change the color of someone of greater rank"); return; + MessageTooHighRank(p, "change the color of", true); return; } ParameterisedQuery query = ParameterisedQuery.Create(); diff --git a/Commands/other/CmdImpersonate.cs b/Commands/other/CmdImpersonate.cs index 7591e9114..2f8b430b5 100644 --- a/Commands/other/CmdImpersonate.cs +++ b/Commands/other/CmdImpersonate.cs @@ -35,7 +35,7 @@ namespace MCGalaxy.Commands { if (p == null || p == who || p.group.Permission > who.group.Permission) { Player.SendChatFrom(who, args[1]); } else { - Player.SendMessage(p, "You cannot impersonate a player of equal or greater rank."); + MessageTooHighRank(p, "impersonate", false); return; } } diff --git a/Commands/other/CmdInvincible.cs b/Commands/other/CmdInvincible.cs index ecb9e3157..9424cbe2c 100644 --- a/Commands/other/CmdInvincible.cs +++ b/Commands/other/CmdInvincible.cs @@ -33,7 +33,7 @@ namespace MCGalaxy.Commands if (who == null) return; if (p != null && who.group.Permission > p.group.Permission) { - Player.SendMessage(p, "Cannot toggle invincibility for someone of higher rank");return; + MessageTooHighRank(p, "toggle invinciblity", true); return; } if (who.invincible) diff --git a/Commands/other/CmdKill.cs b/Commands/other/CmdKill.cs index 59c189c33..a45a3adc2 100644 --- a/Commands/other/CmdKill.cs +++ b/Commands/other/CmdKill.cs @@ -54,7 +54,7 @@ namespace MCGalaxy.Commands if (p != null && who.group.Permission > p.group.Permission) { p.HandleDeath(Block.rock, " was killed by " + who.color + who.DisplayName); - Player.SendMessage(p, "Cannot kill someone of higher rank"); return; + MessageTooHighRank(p, "kill", true); return; } who.HandleDeath(Block.rock, deathMessage, explode); } diff --git a/Commands/other/CmdMove.cs b/Commands/other/CmdMove.cs index fada81097..b98a8fa66 100644 --- a/Commands/other/CmdMove.cs +++ b/Commands/other/CmdMove.cs @@ -51,7 +51,9 @@ namespace MCGalaxy.Commands Level where = LevelInfo.Find(param[1]); if (who == null) return; if (where == null) { Player.SendMessage(p, "Could not find level specified"); return; } - if (p != null && who.group.Permission > p.group.Permission) { Player.SendMessage(p, "Cannot move someone of greater rank"); return; } + if (p != null && who.group.Permission > p.group.Permission) { + MessageTooHighRank(p, "move", true); return; + } Command.all.Find("goto").Use(who, where.name); if (who.level == where) @@ -70,7 +72,9 @@ namespace MCGalaxy.Commands { who = PlayerInfo.FindOrShowMatches(p, param[0]); if (who == null) return; - if (p != null && who.group.Permission > p.group.Permission) { Player.SendMessage(p, "Cannot move someone of greater rank"); return; } + if (p != null && who.group.Permission > p.group.Permission) { + MessageTooHighRank(p, "move", true); return; + } message = message.Substring(message.IndexOf(' ') + 1); } else diff --git a/Commands/other/CmdNick.cs b/Commands/other/CmdNick.cs index 30a5b7e1c..8183f5a4b 100644 --- a/Commands/other/CmdNick.cs +++ b/Commands/other/CmdNick.cs @@ -37,7 +37,7 @@ namespace MCGalaxy.Commands { Player who = PlayerInfo.FindOrShowMatches(p, parts[0]); if (who == null) return; if (p != null && who.group.Permission > p.group.Permission) { - Player.SendMessage(p, "Cannot change the nick of someone of greater rank"); return; + MessageTooHighRank(p, "change the nick of", true); return; } string newName = parts.Length > 1 ? parts[1] : ""; diff --git a/Commands/other/CmdSendCmd.cs b/Commands/other/CmdSendCmd.cs index e426b2068..2e5b9fab3 100644 --- a/Commands/other/CmdSendCmd.cs +++ b/Commands/other/CmdSendCmd.cs @@ -33,7 +33,7 @@ namespace MCGalaxy.Commands { Player target = PlayerInfo.FindOrShowMatches(p, parts[0]); if (target == null) return; if (p != null && p.group.Permission < target.group.Permission) { - Player.SendMessage(p, "Cannot use this on someone of equal or greater rank."); return; + MessageTooHighRank(p, "send commands for", true); return; } if (parts.Length == 1) { Player.SendMessage(p, "No command name given."); return; diff --git a/Commands/other/CmdSummon.cs b/Commands/other/CmdSummon.cs index e1599b077..8c2a99a37 100644 --- a/Commands/other/CmdSummon.cs +++ b/Commands/other/CmdSummon.cs @@ -48,7 +48,7 @@ namespace MCGalaxy.Commands Player who = PlayerInfo.FindOrShowMatches(p, message); if (who == null) return; if (p.group.Permission < who.group.Permission) { - Player.SendMessage(p, "You cannot summon someone ranked higher than you!"); return; + MessageTooHighRank(p, "summon", true); return; } if (p.level != who.level) diff --git a/Commands/other/CmdTitle.cs b/Commands/other/CmdTitle.cs index 443be9a7c..054d5a8b3 100644 --- a/Commands/other/CmdTitle.cs +++ b/Commands/other/CmdTitle.cs @@ -35,7 +35,7 @@ namespace MCGalaxy.Commands { Player who = PlayerInfo.FindOrShowMatches(p, parts[0]); if (who == null) return; if (p != null && who.group.Permission > p.group.Permission) { - Player.SendMessage(p, "Cannot change the title of someone of greater rank"); return; + MessageTooHighRank(p, "change the title of", true); return; } string newTitle = parts.Length > 1 ? parts[1] : ""; diff --git a/Commands/other/CmdTp.cs b/Commands/other/CmdTp.cs index 5914bd534..78d7588e0 100644 --- a/Commands/other/CmdTp.cs +++ b/Commands/other/CmdTp.cs @@ -36,7 +36,7 @@ namespace MCGalaxy.Commands { } if (!Server.higherranktp && p.group.Permission < target.group.Permission) { - Player.SendMessage(p, "You cannot teleport to a player of higher rank!"); return; + MessageTooHighRank(p, "teleport to", true); return; } p.beforeTeleportMap = p.level.name; p.beforeTeleportPos = p.pos; diff --git a/Commands/other/CmdXspawn.cs b/Commands/other/CmdXspawn.cs index 910e29a24..eb60587b0 100644 --- a/Commands/other/CmdXspawn.cs +++ b/Commands/other/CmdXspawn.cs @@ -34,7 +34,7 @@ namespace MCGalaxy.Commands { Player.SendMessage(p, "Use /spawn to respawn yourself."); return; } if (p != null && pl.group.Permission >= p.group.Permission) { - Player.SendMessage(p, "Cannot respawn someone of greater or same rank"); return; + MessageTooHighRank(p, "respawn", false); return; } Command.all.Find("spawn").Use(pl, ""); From dfd65bbd77a2e57cf4353cbeafbe2c6806eeee4f Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Thu, 7 Apr 2016 12:13:50 +1000 Subject: [PATCH 2/6] Fix CPE messages not being reset for zombie survival, fixes #124. --- Blocks/Block.CoreProps.cs | 2 +- Blocks/Block.cs | 4 +-- Games/ZombieSurvival/ZombieGame.Game.cs | 38 ++++++++++++++----------- Games/ZombieSurvival/ZombieGame.cs | 11 +++++-- 4 files changed, 34 insertions(+), 21 deletions(-) diff --git a/Blocks/Block.CoreProps.cs b/Blocks/Block.CoreProps.cs index 93f43d81a..7c06daa71 100644 --- a/Blocks/Block.CoreProps.cs +++ b/Blocks/Block.CoreProps.cs @@ -56,7 +56,7 @@ namespace MCGalaxy { if (i >= red && i <= white) Properties[i].KilledByLava = true; - if (i == air || i == shrub || (i >= yellowflower && i <= redmushroom) { + if (i == air || i == shrub || (i >= yellowflower && i <= redmushroom)) { Properties[i].KilledByLava = true; Properties[i].KilledByWater = true; } diff --git a/Blocks/Block.cs b/Blocks/Block.cs index 6b454563f..bd57c6c61 100644 --- a/Blocks/Block.cs +++ b/Blocks/Block.cs @@ -23,7 +23,7 @@ namespace MCGalaxy { public static bool Walkthrough(byte type) { return type == air || type == shrub || (type >= water && type <= lavastill) - && (type >= yelllowflower && type <= redmushroom); + && (type >= yellowflower && type <= redmushroom); } public static bool AllowBreak(byte type) @@ -85,7 +85,7 @@ namespace MCGalaxy } public static bool Placable(byte type) { - return !(type == blackrock || (type >= water && type <= lavstill) + return !(type == blackrock || (type >= water && type <= lavastill)) && type < Block.CpeCount; } diff --git a/Games/ZombieSurvival/ZombieGame.Game.cs b/Games/ZombieSurvival/ZombieGame.Game.cs index ec489e79b..5042dd8ce 100644 --- a/Games/ZombieSurvival/ZombieGame.Game.cs +++ b/Games/ZombieSurvival/ZombieGame.Game.cs @@ -87,7 +87,7 @@ namespace MCGalaxy.Games { Player[] players = p.Game.Infected ? Infected.Items : Alive.Items; string type = p.Game.Infected ? " &cto zombies%S: " : " &ato humans%S: "; foreach (Player pl in players) - pl.SendMessage(p.color + p.DisplayName + type + message.Substring(1)); + pl.SendMessage(p.color + p.DisplayName + type + message.Substring(1)); return true; } else if (message[0] == '`' && message.Length > 1) { if (p.Game.Team == null) { @@ -122,7 +122,7 @@ namespace MCGalaxy.Games { } public override void PlayerJoinedLevel(Player p, Level lvl, Level oldLvl) { - p.SendCpeMessage(CpeMessageType.BottomRight2, ""); + p.SendCpeMessage(CpeMessageType.BottomRight2, ""); p.SendCpeMessage(CpeMessageType.BottomRight1, ""); if (RoundInProgress && lvl.name.CaselessEq(CurLevelName)) { if (Running && p != null) { @@ -152,30 +152,36 @@ namespace MCGalaxy.Games { return; } - p.SendCpeMessage(CpeMessageType.Status1, ""); - p.SendCpeMessage(CpeMessageType.Status2, ""); - p.SendCpeMessage(CpeMessageType.Status3, ""); + ResetCpeMessages(p); Alive.Remove(p); Infected.Remove(p); if (oldLvl != null && oldLvl.name.CaselessEq(CurLevelName)) UpdateAllPlayerStatus(); } - + + void ResetCpeMessages(Player p) { + p.SendCpeMessage(CpeMessageType.Status1, ""); + p.SendCpeMessage(CpeMessageType.Status2, ""); + p.SendCpeMessage(CpeMessageType.Status3, ""); + p.SendCpeMessage(CpeMessageType.BottomRight1, ""); + p.SendCpeMessage(CpeMessageType.BottomRight2, ""); + } + public override bool PlayerCanJoinLevel(Player p, Level lvl, Level oldLvl) { - if (!oldLvl.name.CaselessEq(CurLevelName)) return true; - if (lvl.name.CaselessEq(CurLevelName)) return true; - - if (RoundInProgress && !p.Game.Referee) { - p.SendMessage("Sorry, you cannot leave a zombie survival map until the current round has ended."); - return false; - } - return true; + if (!oldLvl.name.CaselessEq(CurLevelName)) return true; + if (lvl.name.CaselessEq(CurLevelName)) return true; + + if (RoundInProgress && !p.Game.Referee) { + p.SendMessage("Sorry, you cannot leave a zombie survival map until the current round has ended."); + return false; + } + return true; } public override void PlayerMoneyChanged(Player p) { if (!Running || !p.level.name.CaselessEq(CurLevelName)) return; - string moneyMsg = "&a" + p.money + " %S" + Server.moneys; - string stateMsg = " and you are " + (p.Game.Infected ? "&cdead" : "&aalive"); + string moneyMsg = "&a" + p.money + " %S" + Server.moneys; + string stateMsg = " and you are " + (p.Game.Infected ? "&cdead" : "&aalive"); p.SendCpeMessage(CpeMessageType.Status3, moneyMsg + stateMsg); } } diff --git a/Games/ZombieSurvival/ZombieGame.cs b/Games/ZombieSurvival/ZombieGame.cs index d7d570dab..fb9d43e91 100644 --- a/Games/ZombieSurvival/ZombieGame.cs +++ b/Games/ZombieSurvival/ZombieGame.cs @@ -203,11 +203,18 @@ namespace MCGalaxy.Games { RoundInProgress = false; RoundStart = DateTime.MinValue; RoundEnd = DateTime.MinValue; + Player[] online = PlayerInfo.Online.Items; + foreach (Player pl in online) { + pl.Game.RatedMap = false; + pl.Game.PledgeSurvive = false; + if (pl.level == null || !pl.level.name.CaselessEq(CurLevelName)) + continue; + ResetCpeMessages(pl); + } + LastLevelName = ""; CurLevelName = ""; CurLevel = null; - - Player[] online = PlayerInfo.Online.Items; foreach (Player pl in online) { pl.Game.RatedMap = false; pl.Game.PledgeSurvive = false; From c03e09b8535262aae02f0080b80eb1eedfb4d75e Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Thu, 7 Apr 2016 12:51:34 +1000 Subject: [PATCH 3/6] The /texture command should show you a warning if url is > 64 characters, also should show 'help' when an invalid scope is specified. --- Commands/World/CmdTexture.cs | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/Commands/World/CmdTexture.cs b/Commands/World/CmdTexture.cs index b18dd8612..ad7f38e1d 100644 --- a/Commands/World/CmdTexture.cs +++ b/Commands/World/CmdTexture.cs @@ -30,19 +30,21 @@ namespace MCGalaxy.Commands { if (message == "") { Help(p); return; } string[] args = message.Split(' '); string scope = args[0].ToLower(); + if (scope == "local") scope = "level"; + if (scope == "localzip") scope = "levelzip"; if (args.Length == 1) { - if (scope == "level") - Player.SendMessage(p, "Level terrain: " + GetPath(p.level.terrainUrl)); - else if (scope == "levelzip") + if (scope == "level") + Player.SendMessage(p, "Level terrain: " + GetPath(p.level.terrainUrl)); + else if (scope == "levelzip") Player.SendMessage(p, "Level tex pack: " + GetPath(p.level.texturePackUrl)); else if (scope == "global") - Player.SendMessage(p, "Global terrain: " + GetPath(Server.defaultTerrainUrl)); - else if (scope == "globalzip") - Player.SendMessage(p, "Global tex pack: " + GetPath(Server.defaultTexturePackUrl)); - else - Help(p); - return; + Player.SendMessage(p, "Global terrain: " + GetPath(Server.defaultTerrainUrl)); + else if (scope == "globalzip") + Player.SendMessage(p, "Global tex pack: " + GetPath(Server.defaultTexturePackUrl)); + else + Help(p); + return; } string url = args[1]; @@ -58,6 +60,7 @@ namespace MCGalaxy.Commands { if ((scope == "globalzip" || scope == "levelzip") && !(url == "" || url.EndsWith(".zip"))) { p.SendMessage("The texture pack URL must end in a .zip"); return; } + if (url.Length > 64) { p.SendMessage("The URL must be 64 characters or less."); return; } if (scope == "global") { Server.defaultTerrainUrl = url; @@ -75,15 +78,15 @@ namespace MCGalaxy.Commands { p.level.texturePackUrl = url; p.SendMessage("Set level's texture pack to " + args[1]); UpdateLevel(p, true); + } else { + Help(p); } } - static string GetPath(string url) { - return url == "" ? "(none)" : url; - } + static string GetPath(string url) { return url == "" ? "(none)" : url; } void UpdateGlobally(Player p, bool zip) { - Player[] players = PlayerInfo.Online.Items; + Player[] players = PlayerInfo.Online.Items; foreach (Player pl in players) { bool hasExt = pl.HasCpeExt(CpeExt.EnvMapAppearance) || pl.HasCpeExt(CpeExt.EnvMapAppearance, 2); string url = zip ? pl.level.texturePackUrl : pl.level.terrainUrl; @@ -94,7 +97,7 @@ namespace MCGalaxy.Commands { } void UpdateLevel(Player p, bool zip) { - Player[] players = PlayerInfo.Online.Items; + Player[] players = PlayerInfo.Online.Items; foreach (Player pl in players) { bool hasExt = pl.HasCpeExt(CpeExt.EnvMapAppearance) || pl.HasCpeExt(CpeExt.EnvMapAppearance, 2); if (hasExt && pl.level == p.level) From 95aeffe10ec6ef0bd5310bbf7de40d770c514db7 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Thu, 7 Apr 2016 16:12:18 +1000 Subject: [PATCH 4/6] Unhandled exceptions in zombie survival should not crash the entire server. (Thanks goodlyay) --- Games/ZombieSurvival/ZombieGame.Core.cs | 15 +++++++++++++++ Games/ZombieSurvival/ZombieGame.cs | 4 ---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/Games/ZombieSurvival/ZombieGame.Core.cs b/Games/ZombieSurvival/ZombieGame.Core.cs index c59ad1df2..bff8d6fa2 100644 --- a/Games/ZombieSurvival/ZombieGame.Core.cs +++ b/Games/ZombieSurvival/ZombieGame.Core.cs @@ -27,6 +27,21 @@ namespace MCGalaxy.Games { public sealed partial class ZombieGame { void MainLoop() { + // Make sure that in the worst case, we do not crash the entire server. + try { + MainLoopCore(); + } catch (Exception ex) { + Server.ErrorLog(ex); + Player.GlobalMessage("&cZombie survival disabled due to an error."); + try { + ResetState(); + } catch (Exception ex2) { + Server.ErrorLog(ex2); + } + } + } + + void MainLoopCore() { if (Status == ZombieGameStatus.NotStarted) return; if (!initialChangeLevel) { ChooseNextLevel(); diff --git a/Games/ZombieSurvival/ZombieGame.cs b/Games/ZombieSurvival/ZombieGame.cs index fb9d43e91..95a7901d6 100644 --- a/Games/ZombieSurvival/ZombieGame.cs +++ b/Games/ZombieSurvival/ZombieGame.cs @@ -215,10 +215,6 @@ namespace MCGalaxy.Games { LastLevelName = ""; CurLevelName = ""; CurLevel = null; - foreach (Player pl in online) { - pl.Game.RatedMap = false; - pl.Game.PledgeSurvive = false; - } } void UpdatePlayerStatus(Player p) { From 5fd7a87351b3e8c248e407bcba875965e3765011 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Thu, 7 Apr 2016 16:43:44 +1000 Subject: [PATCH 5/6] Fix /os map guns throwing a NullReferenceException. --- Commands/CmdOverseer.cs | 2 +- Games/ZombieSurvival/ZombieGame.Core.cs | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Commands/CmdOverseer.cs b/Commands/CmdOverseer.cs index 9232d1a26..9f069ec0c 100644 --- a/Commands/CmdOverseer.cs +++ b/Commands/CmdOverseer.cs @@ -206,7 +206,7 @@ namespace MCGalaxy.Commands Level.SaveSettings(p.level); } } else if (cmd == "GUNS") { - Command.all.Find("allowguns").Use(p, null); + Command.all.Find("allowguns").Use(p, ""); } else if (cmd == "PERVISIT") { string rank = value == "" ? Server.defaultRank : value; Command.all.Find("pervisit").Use(p, rank); diff --git a/Games/ZombieSurvival/ZombieGame.Core.cs b/Games/ZombieSurvival/ZombieGame.Core.cs index bff8d6fa2..0bddad2cf 100644 --- a/Games/ZombieSurvival/ZombieGame.Core.cs +++ b/Games/ZombieSurvival/ZombieGame.Core.cs @@ -125,19 +125,19 @@ namespace MCGalaxy.Games { List DoRoundCountdown() { while (true) { RoundStart = DateTime.UtcNow.AddSeconds(30); - CurLevel.ChatLevel("%4Round Start:%f 30..."); + CurLevel.ChatLevel("&4Round Start:&f 30..."); Thread.Sleep(20000); if (!Running) return null; - CurLevel.ChatLevel("%4Round Start:%f 10..."); + CurLevel.ChatLevel("&4Round Start:&f 10..."); Thread.Sleep(10000); if (!Running) return null; - CurLevel.ChatLevel("%4Round Start:%f 5..."); + CurLevel.ChatLevel("&4Round Start:&f 5..."); Thread.Sleep(1000); if (!Running) return null; - CurLevel.ChatLevel("%4Round Start:%f 4..."); + CurLevel.ChatLevel("&4Round Start:&f 4..."); Thread.Sleep(1000); if (!Running) return null; - CurLevel.ChatLevel("%4Round Start:%f 3..."); + CurLevel.ChatLevel("&4Round Start:&f 3..."); Thread.Sleep(1000); if (!Running) return null; - CurLevel.ChatLevel("%4Round Start:%f 2..."); + CurLevel.ChatLevel("&4Round Start:&f 2..."); Thread.Sleep(1000); if (!Running) return null; - CurLevel.ChatLevel("%4Round Start:%f 1..."); + CurLevel.ChatLevel("&4Round Start:&f 1..."); Thread.Sleep(1000); if (!Running) return null; int nonRefPlayers = 0; List players = new List(); @@ -151,7 +151,7 @@ namespace MCGalaxy.Games { } if (nonRefPlayers >= 2) return players; - CurLevel.ChatLevel(Colors.red + "ERROR: Need 2 or more players to play"); + CurLevel.ChatLevel("&cNeed 2 or more players to start a round."); } } From a0cd1885f0c171d64e2822842886541594b54621 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Thu, 7 Apr 2016 16:57:55 +1000 Subject: [PATCH 6/6] Store convert block ids locally instead of performing a Block.Convert call for each block, reduces map loading time. --- Network/Player.Networking.cs | 166 +++++++++++++++++++---------------- 1 file changed, 88 insertions(+), 78 deletions(-) diff --git a/Network/Player.Networking.cs b/Network/Player.Networking.cs index b5a0660f9..01c3af647 100644 --- a/Network/Player.Networking.cs +++ b/Network/Player.Networking.cs @@ -83,8 +83,8 @@ namespace MCGalaxy { AddExtension(enc.GetString(message, 0, 64).Trim(), NetUtils.ReadI32(message, 64)); extensionCount--; if (extensionCount <= 0 && !finishedCpeLogin) { - CompleteLoginProcess(); - finishedCpeLogin = true; + CompleteLoginProcess(); + finishedCpeLogin = true; } } @@ -105,13 +105,13 @@ namespace MCGalaxy { } public void SendRaw(int id) { - byte[] buffer = new [] { (byte)id }; - SendRaw(buffer); + byte[] buffer = new [] { (byte)id }; + SendRaw(buffer); } public void SendRaw(int id, byte data) { - byte[] buffer = new [] { (byte)id, data }; - SendRaw(buffer); + byte[] buffer = new [] { (byte)id, data }; + SendRaw(buffer); } [Obsolete("Include the opcode in the array to avoid an extra temp allocation.")] @@ -135,7 +135,7 @@ namespace MCGalaxy { socket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, delegate(IAsyncResult result) { }, null); buffer = null; } catch (SocketException e) { - buffer = null; + buffer = null; Disconnect(); #if DEBUG Server.ErrorLog(e); @@ -147,7 +147,7 @@ namespace MCGalaxy { } public void SendBlankMessage() { - byte[] buffer = new byte[66]; + byte[] buffer = new byte[66]; buffer[0] = Opcode.Message; NetUtils.WriteAscii("", buffer, 2); SendRaw(buffer); @@ -174,14 +174,14 @@ namespace MCGalaxy { } public void SendMessage(string message) { - SendMessage(0, Server.DefaultColor + message, true); + SendMessage(0, Server.DefaultColor + message, true); } public void SendMessage(string message, bool colorParse) { SendMessage(0, Server.DefaultColor + message, colorParse); } - public void SendMessage(byte id, string message, bool colorParse = true) { + public void SendMessage(byte id, string message, bool colorParse = true) { message = ConvertMessage(message, colorParse); int totalTries = 0; if ( MessageRecieve != null ) @@ -225,7 +225,7 @@ namespace MCGalaxy { else return; } message = ConvertMessage(message, colorParse); - + byte[] buffer = new byte[66]; buffer[0] = Opcode.Message; buffer[1] = (byte)id; @@ -240,7 +240,7 @@ namespace MCGalaxy { if (colorParse) message = Colors.EscapeColors(message); StringBuilder sb = new StringBuilder(message); - if (colorParse) + if (colorParse) ParseColors(sb); Chat.ApplyTokens(sb, this, colorParse); @@ -297,14 +297,14 @@ namespace MCGalaxy { if (level.motd == "ignore") { NetUtils.WriteAscii(Server.name, buffer, 2); - if (!String.IsNullOrEmpty(group.MOTD) ) - NetUtils.WriteAscii(group.MOTD, buffer, 66); - else - NetUtils.WriteAscii(Server.motd, buffer, 66); + if (!String.IsNullOrEmpty(group.MOTD) ) + NetUtils.WriteAscii(group.MOTD, buffer, 66); + else + NetUtils.WriteAscii(Server.motd, buffer, 66); } else { - NetUtils.WriteAscii(level.motd, buffer, 2); - if (level.motd.Length > 64) - NetUtils.WriteAscii(level.motd.Substring(64), buffer, 66); + NetUtils.WriteAscii(level.motd, buffer, 2); + if (level.motd.Length > 64) + NetUtils.WriteAscii(level.motd.Substring(64), buffer, 66); } bool canPlace = Block.canPlace(this, Block.blackrock); @@ -331,8 +331,8 @@ namespace MCGalaxy { } SendRaw(Opcode.LevelInitialise); - int totalRead = 0; - while (totalRead < usedLength) { + int totalRead = 0; + while (totalRead < usedLength) { byte[] packet = new byte[1028]; // need each packet separate for Mono packet[0] = Opcode.LevelDataChunk; short length = (short)Math.Min(buffer.Length - totalRead, 1024); @@ -340,7 +340,7 @@ namespace MCGalaxy { Buffer.BlockCopy(buffer, totalRead, packet, 3, length); packet[1027] = (byte)(100 * (float)totalRead / buffer.Length); - SendRaw(packet); + SendRaw(packet); if (ip != "127.0.0.1") Thread.Sleep(Server.updateTimer.Interval > 1000 ? 100 : 10); totalRead += length; @@ -380,12 +380,22 @@ namespace MCGalaxy { return success; } - byte[] CompressRawMap(out int usedLength) { + unsafe byte[] CompressRawMap(out int usedLength) { const int bufferSize = 64 * 1024; byte[] buffer = new byte[bufferSize]; MemoryStream temp = new MemoryStream(); int bIndex = 0; + // Store locally instead of performing func call for every block in map + byte* conv = stackalloc byte[256]; + byte* convCPE = stackalloc byte[256]; + for (int i = 0; i < 256; i++) + conv[i] = Block.Convert((byte)i); + if (!hasCustomBlocks) { + for (int i = 0; i < 256; i++) + convCPE[i] = Block.ConvertCPE((byte)i); + } + using (GZipStream compressor = new GZipStream(temp, CompressionMode.Compress, true)) { NetUtils.WriteI32(level.blocks.Length, buffer, 0); compressor.Write(buffer, 0, sizeof(int)); @@ -398,7 +408,7 @@ namespace MCGalaxy { if (hasBlockDefs) buffer[bIndex] = level.GetExtTile(i); else buffer[bIndex] = level.GetFallbackExtTile(i); } else { - buffer[bIndex] = Block.Convert(block); + buffer[bIndex] = conv[block]; } bIndex++; @@ -410,10 +420,10 @@ namespace MCGalaxy { for (int i = 0; i < level.blocks.Length; ++i) { byte block = level.blocks[i]; if (block == Block.custom_block) { - if (hasBlockDefs) buffer[bIndex] = Block.ConvertCPE(level.GetExtTile(i)); - else buffer[bIndex] = Block.ConvertCPE(level.GetFallbackExtTile(i)); + if (hasBlockDefs) buffer[bIndex] = convCPE[level.GetExtTile(i)]; + else buffer[bIndex] = convCPE[level.GetFallbackExtTile(i)]; } else { - buffer[bIndex] = Block.Convert(Block.ConvertCPE(level.blocks[i])); + buffer[bIndex] = convCPE[conv[block]]; } bIndex++; @@ -421,7 +431,7 @@ namespace MCGalaxy { compressor.Write(buffer, 0, bufferSize); bIndex = 0; } } - } + } if (bIndex > 0) compressor.Write(buffer, 0, bIndex); } usedLength = (int)temp.Length; @@ -429,12 +439,12 @@ namespace MCGalaxy { } void RemoveOldLevelCustomBlocks(Level oldLevel) { - BlockDefinition[] defs = oldLevel.CustomBlockDefs; - for (int i = Block.CpeCount; i < 256; i++) { - BlockDefinition def = defs[i]; - if (def == null || def == BlockDefinition.GlobalDefs[i]) continue; - SendRaw(Opcode.CpeRemoveBlockDefinition, (byte)i); - } + BlockDefinition[] defs = oldLevel.CustomBlockDefs; + for (int i = Block.CpeCount; i < 256; i++) { + BlockDefinition def = defs[i]; + if (def == null || def == BlockDefinition.GlobalDefs[i]) continue; + SendRaw(Opcode.CpeRemoveBlockDefinition, (byte)i); + } } public void SendSpawn(byte id, string name, ushort x, ushort y, ushort z, byte rotx, byte roty) { @@ -445,12 +455,12 @@ namespace MCGalaxy { NetUtils.WriteU16(x, buffer, 66); NetUtils.WriteU16(y, buffer, 68); NetUtils.WriteU16(z, buffer, 70); - buffer[72] = rotx; + buffer[72] = rotx; buffer[73] = roty; SendRaw(buffer); if (HasCpeExt(CpeExt.ChangeModel)) - UpdateModels(); + UpdateModels(); } public void SendPos(byte id, ushort x, ushort y, ushort z, byte rotx, byte roty) { @@ -466,13 +476,13 @@ namespace MCGalaxy { pos[0] = x; pos[1] = y; pos[2] = z; rot[0] = rotx; rot[1] = roty; - byte[] buffer = new byte[10]; + byte[] buffer = new byte[10]; buffer[0] = Opcode.EntityTeleport; buffer[1] = id; NetUtils.WriteU16(x, buffer, 2); NetUtils.WriteU16(y, buffer, 4); NetUtils.WriteU16(z, buffer, 6); - buffer[8] = rotx; + buffer[8] = rotx; buffer[9] = roty; SendRaw(buffer); } @@ -482,8 +492,8 @@ namespace MCGalaxy { } //TODO: Figure a way to SendPos without changing rotation - public void SendDespawn(byte id) { - SendRaw(Opcode.RemoveEntity, id); + public void SendDespawn(byte id) { + SendRaw(Opcode.RemoveEntity, id); } public void SendBlockchange(ushort x, ushort y, ushort z, byte type) { @@ -497,14 +507,14 @@ namespace MCGalaxy { NetUtils.WriteU16(z, buffer, 5); if (type == Block.custom_block) { - if (hasBlockDefs) - buffer[7] = level.GetExtTile(x, y, z); - else - buffer[7] = level.GetFallbackExtTile(x, y, z); + if (hasBlockDefs) + buffer[7] = level.GetExtTile(x, y, z); + else + buffer[7] = level.GetFallbackExtTile(x, y, z); } else if (hasCustomBlocks) { - buffer[7] = Block.Convert(type); + buffer[7] = Block.Convert(type); } else { - buffer[7] = Block.Convert(Block.ConvertCPE(type)); + buffer[7] = Block.Convert(Block.ConvertCPE(type)); } SendRaw(buffer); } @@ -521,27 +531,27 @@ namespace MCGalaxy { NetUtils.WriteU16(z, buffer, 5); if (type == Block.custom_block) { - if (hasBlockDefs) - buffer[7] = extType; - else - buffer[7] = level.GetFallback(extType); + if (hasBlockDefs) + buffer[7] = extType; + else + buffer[7] = level.GetFallback(extType); } else if (hasCustomBlocks) { - buffer[7] = Block.Convert(type); + buffer[7] = Block.Convert(type); } else { - buffer[7] = Block.Convert(Block.ConvertCPE(type)); + buffer[7] = Block.Convert(Block.ConvertCPE(type)); } SendRaw(buffer); } void SendKick(string message, bool sync) { - byte[] buffer = new byte[65]; - buffer[0] = Opcode.Kick; - NetUtils.WriteAscii(message, buffer, 1); - SendRaw(buffer, sync); + byte[] buffer = new byte[65]; + buffer[0] = Opcode.Kick; + NetUtils.WriteAscii(message, buffer, 1); + SendRaw(buffer, sync); } - void SendPing() { - SendRaw(Opcode.Ping); + void SendPing() { + SendRaw(Opcode.Ping); } void SendExtInfo( byte count ) { @@ -553,14 +563,14 @@ namespace MCGalaxy { } void SendExtEntry( string name, int version ) { - byte[] buffer = new byte[69]; - buffer[0] = Opcode.CpeExtEntry; + byte[] buffer = new byte[69]; + buffer[0] = Opcode.CpeExtEntry; NetUtils.WriteAscii(name, buffer, 1); NetUtils.WriteI32(version, buffer, 65); SendRaw(buffer, true); } - public void SendClickDistance( short distance ) { + public void SendClickDistance( short distance ) { byte[] buffer = new byte[3]; buffer[0] = Opcode.CpeSetClickDistance; NetUtils.WriteI16(distance, buffer, 1); @@ -668,8 +678,8 @@ namespace MCGalaxy { } public void SendSetMapAppearance( string url, byte sideblock, byte edgeblock, short sidelevel ) { - byte[] buffer = new byte[69]; - buffer[0] = Opcode.CpeEnvSetMapApperance; + byte[] buffer = new byte[69]; + buffer[0] = Opcode.CpeEnvSetMapApperance; NetUtils.WriteAscii(url, buffer, 1); buffer[65] = sideblock; buffer[66] = edgeblock; @@ -677,10 +687,10 @@ namespace MCGalaxy { SendRaw(buffer); } - public void SendSetMapAppearanceV2( string url, byte sideblock, byte edgeblock, short sidelevel, + public void SendSetMapAppearanceV2( string url, byte sideblock, byte edgeblock, short sidelevel, short cloudHeight, short maxFog ) { - byte[] buffer = new byte[73]; - buffer[0] = Opcode.CpeEnvSetMapApperance; + byte[] buffer = new byte[73]; + buffer[0] = Opcode.CpeEnvSetMapApperance; NetUtils.WriteAscii(url, buffer, 1); buffer[65] = sideblock; buffer[66] = edgeblock; @@ -694,7 +704,7 @@ namespace MCGalaxy { SendRaw(Opcode.CpeEnvWeatherType, weather); } - void SendHackControl( byte allowflying, byte allownoclip, byte allowspeeding, byte allowrespawning, + void SendHackControl( byte allowflying, byte allownoclip, byte allowspeeding, byte allowrespawning, byte allowthirdperson, short maxjumpheight ) { byte[] buffer = new byte[8]; buffer[0] = Opcode.CpeHackControl; @@ -708,11 +718,11 @@ namespace MCGalaxy { } void UpdatePosition() { - //pingDelayTimer.Stop(); - byte[] packet = NetUtils.GetPositionPacket(id, pos, oldpos, rot, oldrot, MakePitch(), false); - oldpos = pos; oldrot = rot; - if (packet == null) return; - + //pingDelayTimer.Stop(); + byte[] packet = NetUtils.GetPositionPacket(id, pos, oldpos, rot, oldrot, MakePitch(), false); + oldpos = pos; oldrot = rot; + if (packet == null) return; + Player[] players = PlayerInfo.Online.Items; foreach (Player p in players) { if (p != this && p.level == level) p.SendRaw(packet); @@ -720,12 +730,12 @@ namespace MCGalaxy { } byte MakePitch() { - if (Server.flipHead || flipHead) - if (rot[1] > 64 && rot[1] < 192) - return rot[1]; - else - return 128; - return rot[1]; + if (Server.flipHead || flipHead) + if (rot[1] > 64 && rot[1] < 192) + return rot[1]; + else + return 128; + return rot[1]; } internal void CloseSocket() {