From fda2e569e4447cbaa66a8157e26013ca681b198e Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Thu, 13 Jul 2017 13:46:38 +1000 Subject: [PATCH] Move ZS stuff into a plugin. --- .../Commands/Fun/ZombieSurvival/CmdReferee.cs | 24 +--- .../Fun/ZombieSurvival/CmdZombieGame.cs | 2 +- MCGalaxy/Commands/Maintenance/CmdServer.cs | 2 +- .../Database/BlockDB/BlockDBTableDumper.cs | 2 +- MCGalaxy/Events/PlayerEvents.cs | 5 +- MCGalaxy/Games/IGame.cs | 16 --- MCGalaxy/Games/ZombieSurvival/ZSPlugin.cs | 119 ++++++++++++++++++ .../Games/ZombieSurvival/ZombieGame.Core.cs | 10 +- .../Games/ZombieSurvival/ZombieGame.Game.cs | 46 +------ .../Games/ZombieSurvival/ZombieGame.Props.cs | 1 + MCGalaxy/Games/ZombieSurvival/ZombieGame.cs | 8 +- MCGalaxy/MCGalaxy_.csproj | 1 + MCGalaxy/Player/Player.Handlers.cs | 3 - MCGalaxy/Player/Player.Login.cs | 1 - MCGalaxy/Player/Player.cs | 4 - 15 files changed, 143 insertions(+), 101 deletions(-) create mode 100644 MCGalaxy/Games/ZombieSurvival/ZSPlugin.cs diff --git a/MCGalaxy/Commands/Fun/ZombieSurvival/CmdReferee.cs b/MCGalaxy/Commands/Fun/ZombieSurvival/CmdReferee.cs index 996e1e8e5..dc29c30df 100644 --- a/MCGalaxy/Commands/Fun/ZombieSurvival/CmdReferee.cs +++ b/MCGalaxy/Commands/Fun/ZombieSurvival/CmdReferee.cs @@ -16,6 +16,7 @@ permissions and limitations under the Licenses. */ using MCGalaxy.Games; +using MCGalaxy.Events.PlayerEvents; using MCGalaxy.Network; namespace MCGalaxy.Commands.Fun { @@ -30,26 +31,13 @@ namespace MCGalaxy.Commands.Fun { public override void Use(Player p, string message) { if (p.Game.Referee) { Chat.MessageGlobal(p, p.ColoredName + " %Sis no longer a referee", false); - p.Game.Referee = !p.Game.Referee; - if (p.level == Server.zombie.CurLevel) - Server.zombie.PlayerJoinedLevel(p, Server.zombie.CurLevel, Server.zombie.CurLevel); - - if (p.HasCpeExt(CpeExt.HackControl)) - p.Send(Hacks.MakeHackControl(p)); - Command.all.Find("spawn").Use(p, ""); + OnPlayerActionEvent.Call(p, PlayerAction.UnReferee); + p.Game.Referee = false; } else { - Chat.MessageGlobal(p, p.ColoredName + " %Sis now a referee", false); - Server.zombie.PlayerLeftServer(p); - Entities.GlobalDespawn(p, false, true); - p.Game.Referee = !p.Game.Referee; - - if (p.HasCpeExt(CpeExt.HackControl)) - p.Send(Packet.HackControl(true, true, true, true, true, -1)); + Chat.MessageGlobal(p, p.ColoredName + " %Sis now a referee", false); + OnPlayerActionEvent.Call(p, PlayerAction.Referee); + p.Game.Referee = true; } - - Entities.GlobalSpawn(p, false, ""); - TabList.Add(p, p, Entities.SelfID); - p.SetPrefix(); } public override void Help(Player p) { diff --git a/MCGalaxy/Commands/Fun/ZombieSurvival/CmdZombieGame.cs b/MCGalaxy/Commands/Fun/ZombieSurvival/CmdZombieGame.cs index f78264750..4d8099cf8 100644 --- a/MCGalaxy/Commands/Fun/ZombieSurvival/CmdZombieGame.cs +++ b/MCGalaxy/Commands/Fun/ZombieSurvival/CmdZombieGame.cs @@ -108,7 +108,7 @@ namespace MCGalaxy.Commands.Fun { src = p == null ? "(console)" : p.name; Logger.Log(LogType.GameActivity, "Zombie Survival stopped by " + src); - Server.zombie.ResetState(); + Server.zombie.End(); } void HandleSet(Player p, string[] args) { diff --git a/MCGalaxy/Commands/Maintenance/CmdServer.cs b/MCGalaxy/Commands/Maintenance/CmdServer.cs index 0e2e3e9fd..cb70c32e5 100644 --- a/MCGalaxy/Commands/Maintenance/CmdServer.cs +++ b/MCGalaxy/Commands/Maintenance/CmdServer.cs @@ -169,7 +169,7 @@ namespace MCGalaxy.Commands.Maintenance { Server.voteKickInProgress = false; Server.voteKickVotesNeeded = 0; - Server.zombie.ResetState(); + Server.zombie.End(); SrvProperties.GenerateSalt(); ServerConfig.RestartTime = DateTime.Now; diff --git a/MCGalaxy/Database/BlockDB/BlockDBTableDumper.cs b/MCGalaxy/Database/BlockDB/BlockDBTableDumper.cs index c135c63a1..329a2b4d1 100644 --- a/MCGalaxy/Database/BlockDB/BlockDBTableDumper.cs +++ b/MCGalaxy/Database/BlockDB/BlockDBTableDumper.cs @@ -20,9 +20,9 @@ using System.Collections.Generic; using System.Data; using System.IO; using MCGalaxy.Levels.IO; +using MCGalaxy.Maths; using MCGalaxy.SQL; using MCGalaxy.Util; -using MCGalaxy.Maths; namespace MCGalaxy.DB { diff --git a/MCGalaxy/Events/PlayerEvents.cs b/MCGalaxy/Events/PlayerEvents.cs index 94b6cc7ab..f6e115a35 100644 --- a/MCGalaxy/Events/PlayerEvents.cs +++ b/MCGalaxy/Events/PlayerEvents.cs @@ -15,11 +15,10 @@ or implied. See the Licenses for the specific language governing permissions and limitations under the Licenses. */ -using System; -using System.Collections.Generic; +using System; namespace MCGalaxy.Events.PlayerEvents { - public enum PlayerAction { Joker, Unjoker, AFK, UnAFK, JoinWorld, Me, Review }; + public enum PlayerAction { Joker, Unjoker, AFK, UnAFK, JoinWorld, Me, Review, Referee, UnReferee }; public delegate void OnPlayerChat(Player p, string message); /// Called whenever a player chats on the server. diff --git a/MCGalaxy/Games/IGame.cs b/MCGalaxy/Games/IGame.cs index c0c9498a1..e609cad66 100644 --- a/MCGalaxy/Games/IGame.cs +++ b/MCGalaxy/Games/IGame.cs @@ -30,11 +30,6 @@ namespace MCGalaxy.Games { return false; } - /// Returns whether this game handled the player moving to a new position. - public virtual bool HandlesMovement(Player p, Position pos, byte rotX, byte rotY) { - return false; - } - /// Returns whether this game handled the player sending a chat message. public virtual bool HandlesChatMessage(Player p, string message) { return false; @@ -43,9 +38,6 @@ namespace MCGalaxy.Games { /// Raised when a player joins the server. public virtual void PlayerJoinedServer(Player p) { } - /// Raised when a player leaves the server. - public virtual void PlayerLeftServer(Player p) { } - /// Raised when a player joins this game. public virtual void PlayerJoinedGame(Player p) { } @@ -59,18 +51,10 @@ namespace MCGalaxy.Games { return true; } - /// Raised when a player's money amount changes. - public virtual void PlayerMoneyChanged(Player p) { } - /// Raised when the server is about to send a heartbeat. public virtual void OnHeartbeat(ref string name) { } /// Adjusts the prefix (e.g. title) shown before the player's name in chat. public virtual void AdjustPrefix(Player p, ref string prefix) { } - - /// Gets the player's name shown in the tab list - /// (for clients that support a separate tab list) that are in this game. - public virtual void GetTabName(Player p, Player dst, - ref string name, ref string group) { } } } diff --git a/MCGalaxy/Games/ZombieSurvival/ZSPlugin.cs b/MCGalaxy/Games/ZombieSurvival/ZSPlugin.cs new file mode 100644 index 000000000..e70ecc699 --- /dev/null +++ b/MCGalaxy/Games/ZombieSurvival/ZSPlugin.cs @@ -0,0 +1,119 @@ +/* + Copyright 2010 MCLawl Team - + Created by Snowl (David D.) and Cazzar (Cayde D.) + + 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.osedu.org/licenses/ECL-2.0 + 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; +using MCGalaxy.Events.EconomyEvents; +using MCGalaxy.Events.EntityEvents; +using MCGalaxy.Events.PlayerEvents; +using MCGalaxy.Network; + +namespace MCGalaxy.Games.ZS { + public sealed class ZSPlugin : Plugin_Simple { + public override string creator { get { return Server.SoftwareName + " team"; } } + public override string MCGalaxy_Version { get { return Server.VersionString; } } + public override string name { get { return "Core_ZSPlugin"; } } + public ZombieGame Game; + + public override void Load(bool startup) { + OnTabListEntryAddedEvent.Register(HandleTabListEntryAdded, Priority.High); + OnMoneyChangedEvent.Register(HandleMoneyChanged, Priority.High); + OnPlayerConnectEvent.Register(HandlePlayerConnect, Priority.High); + OnPlayerDisconnectEvent.Register(HandlePlayerDisconnect, Priority.High); + OnPlayerMoveEvent.Register(HandlePlayerMove, Priority.High); + OnPlayerActionEvent.Register(HandlePlayerAction, Priority.High); + } + + public override void Unload(bool shutdown) { + OnTabListEntryAddedEvent.Unregister(HandleTabListEntryAdded); + OnMoneyChangedEvent.Unregister(HandleMoneyChanged); + OnPlayerConnectEvent.Unregister(HandlePlayerConnect); + OnPlayerDisconnectEvent.Unregister(HandlePlayerDisconnect); + OnPlayerMoveEvent.Unregister(HandlePlayerMove); + OnPlayerActionEvent.Unregister(HandlePlayerAction); + } + + void HandleTabListEntryAdded(Entity entity, ref string tabName, ref string tabGroup, Player dst) { + Player p = entity as Player; + if (p == null || p.level != Game.CurLevel) return; + + if (p.Game.Referee) { + tabGroup = "&2Referees"; + } else if (p.Game.Infected) { + tabGroup = "&cZombies"; + if (ZSConfig.ZombieName != "" && !dst.Game.Aka) { + tabName = "&c" + ZSConfig.ZombieName; + } else { + tabName = "&c" + p.truename; + } + } else { + tabGroup = "&fHumans"; + } + } + + void HandleMoneyChanged(Player p) { + if (p.level != Game.CurLevel) return; + HUD.UpdateTertiary(p); + } + + void HandlePlayerConnect(Player p) { + if (!ZSConfig.SetMainLevel) return; + Player.Message(p, "Zombie Survival is running! Type %T/zs go %Sto join."); + } + + void HandlePlayerDisconnect(Player p, string reason) { + Game.Alive.Remove(p); + Game.Infected.Remove(p); + p.Game.Infected = false; + Game.RemoveBounties(p); + + Game.AssignFirstZombie(); + HUD.UpdateAllPrimary(Game); + } + + void HandlePlayerMove(Player p, Position next, byte rotX, byte rotY) { + if (!Game.RoundInProgress || p.level != Game.CurLevel) return; + + bool reverted = MovementCheck.DetectNoclip(p, next) + || MovementCheck.DetectSpeedhack(p, next, ZSConfig.MaxMoveDistance); + if (reverted) p.cancelmove = true; + } + + void HandlePlayerAction(Player p, PlayerAction action, string message, bool stealth) { + if (!(action == PlayerAction.Referee || action == PlayerAction.UnReferee)) return; + if (p.level != Game.CurLevel) return; + + if (action == PlayerAction.UnReferee) { + Game.PlayerJoinedLevel(p, Game.CurLevel, Game.CurLevel); + Command.all.Find("spawn").Use(p, ""); + + if (p.HasCpeExt(CpeExt.HackControl)) + p.Send(Hacks.MakeHackControl(p)); + } else { + HandlePlayerDisconnect(p, null); + Entities.GlobalDespawn(p, false, true); + + if (p.HasCpeExt(CpeExt.HackControl)) + p.Send(Packet.HackControl(true, true, true, true, true, -1)); + } + + Entities.GlobalSpawn(p, false, ""); + TabList.Add(p, p, Entities.SelfID); + p.SetPrefix(); + } + } +} diff --git a/MCGalaxy/Games/ZombieSurvival/ZombieGame.Core.cs b/MCGalaxy/Games/ZombieSurvival/ZombieGame.Core.cs index 8f8afa26a..99291fb2b 100644 --- a/MCGalaxy/Games/ZombieSurvival/ZombieGame.Core.cs +++ b/MCGalaxy/Games/ZombieSurvival/ZombieGame.Core.cs @@ -18,9 +18,7 @@ */ using System; using System.Collections.Generic; -using System.IO; using System.Threading; -using System.Timers; using MCGalaxy.Games.ZS; using MCGalaxy.Network; @@ -36,7 +34,7 @@ namespace MCGalaxy.Games { Logger.LogError(ex); Chat.MessageGlobal("&cZombie survival disabled due to an error."); try { - ResetState(); + End(); } catch (Exception ex2) { Logger.LogError(ex2); } @@ -57,17 +55,17 @@ namespace MCGalaxy.Games { LevelPicker.ChooseNextLevel(this); } else if (Status == ZombieGameStatus.SingleRound) { DoRound(); - ResetState(); return; + End(); return; } else if (Status == ZombieGameStatus.VariableRounds) { if (RoundsDone == MaxRounds) { - ResetState(); return; + End(); return; } else { DoRound(); if (ZSConfig.ChangeLevels) LevelPicker.ChooseNextLevel(this); } } else if (Status == ZombieGameStatus.LastRound) { - ResetState(); return; + End(); return; } } } diff --git a/MCGalaxy/Games/ZombieSurvival/ZombieGame.Game.cs b/MCGalaxy/Games/ZombieSurvival/ZombieGame.Game.cs index 8e27283eb..626e952b2 100644 --- a/MCGalaxy/Games/ZombieSurvival/ZombieGame.Game.cs +++ b/MCGalaxy/Games/ZombieSurvival/ZombieGame.Game.cs @@ -53,14 +53,6 @@ namespace MCGalaxy.Games { return false; } - public override bool HandlesMovement(Player p, Position next, byte rotX, byte rotY) { - if (!Running || !RoundInProgress) return false; - if (p.level == null || !p.level.name.CaselessEq(CurLevelName)) return false; - - return MovementCheck.DetectNoclip(p, next) - || MovementCheck.DetectSpeedhack(p, next, ZSConfig.MaxMoveDistance); - } - public override bool HandlesChatMessage(Player p, string message) { if (!Running || (p.level == null || !p.level.name.CaselessEq(CurLevelName))) return false; if (Server.votingforlevel && HandleVote(p, message)) return true; @@ -94,17 +86,7 @@ namespace MCGalaxy.Games { return false; } - public override void PlayerLeftServer(Player p) { - Alive.Remove(p); - Infected.Remove(p); - p.Game.Infected = false; - RemoveBounties(p); - - AssignFirstZombie(); - HUD.UpdateAllPrimary(this); - } - - void RemoveBounties(Player p) { + internal void RemoveBounties(Player p) { BountyData[] bounties = Bounties.Items; foreach (BountyData b in bounties) { if (!(b.Origin.CaselessEq(p.name) || b.Target.CaselessEq(p.name))) continue; @@ -118,11 +100,6 @@ namespace MCGalaxy.Games { } } - public override void PlayerJoinedServer(Player p) { - if (!Running || ZSConfig.SetMainLevel) return; - Player.Message(p, "Zombie Survival is running! Type %T/zs go %Sto join."); - } - public override void PlayerJoinedLevel(Player p, Level lvl, Level oldLvl) { p.SendCpeMessage(CpeMessageType.BottomRight3, ""); p.SendCpeMessage(CpeMessageType.BottomRight2, ""); @@ -177,11 +154,6 @@ namespace MCGalaxy.Games { return true; } - public override void PlayerMoneyChanged(Player p) { - if (!Running || !p.level.name.CaselessEq(CurLevelName)) return; - HUD.UpdateTertiary(p); - } - public override void OnHeartbeat(ref string name) { if (!Running || !ZSConfig.IncludeMapInHeartbeat || CurLevelName == null) return; name += " (map: " + CurLevelName + ")"; @@ -195,21 +167,5 @@ namespace MCGalaxy.Games { else if (winStreak == 3) prefix += "&6*"+ p.color; else if (winStreak > 0) prefix += "&6" + winStreak + p.color; } - - public override void GetTabName(Player p, Player dst, - ref string name, ref string group) { - if (p.Game.Referee) { - group = "&2Referees"; - } else if (p.Game.Infected) { - group = "&cZombies"; - if (ZSConfig.ZombieName != "" && !dst.Game.Aka) { - name = "&c" + ZSConfig.ZombieName; - } else { - name = "&c" + p.truename; - } - } else { - group = "&fHumans"; - } - } } } diff --git a/MCGalaxy/Games/ZombieSurvival/ZombieGame.Props.cs b/MCGalaxy/Games/ZombieSurvival/ZombieGame.Props.cs index 057fb5267..765b4268e 100644 --- a/MCGalaxy/Games/ZombieSurvival/ZombieGame.Props.cs +++ b/MCGalaxy/Games/ZombieSurvival/ZombieGame.Props.cs @@ -91,6 +91,7 @@ namespace MCGalaxy.Games { string lastPlayerToInfect = ""; int infectCombo = 0; + /// List of players who have a bounty on them. public VolatileArray Bounties = new VolatileArray(); diff --git a/MCGalaxy/Games/ZombieSurvival/ZombieGame.cs b/MCGalaxy/Games/ZombieSurvival/ZombieGame.cs index d30ecf5ef..6c3858f5b 100644 --- a/MCGalaxy/Games/ZombieSurvival/ZombieGame.cs +++ b/MCGalaxy/Games/ZombieSurvival/ZombieGame.cs @@ -33,6 +33,7 @@ namespace MCGalaxy.Games { } public sealed partial class ZombieGame { + ZSPlugin plugin = new ZSPlugin(); public void Start(ZombieGameStatus status, Level level, int rounds) { Status = status; @@ -42,6 +43,8 @@ namespace MCGalaxy.Games { if (!SetStartLevel(level)) return; HookStats(); + if (plugin.Game == null) { plugin.Game = this; plugin.Load(false); } + Thread t = new Thread(MainLoop); t.Name = "MCG_ZombieGame"; t.Start(); @@ -179,14 +182,15 @@ namespace MCGalaxy.Games { } } - public void ResetState() { + public void End() { Status = ZombieGameStatus.NotStarted; MaxRounds = 0; RoundInProgress = false; RoundStart = DateTime.MinValue; RoundEnd = DateTime.MinValue; - Player[] online = PlayerInfo.Online.Items; + if (plugin.Game != null) { plugin.Game = null; plugin.Unload(false); } + Player[] online = PlayerInfo.Online.Items; Alive.Clear(); Infected.Clear(); diff --git a/MCGalaxy/MCGalaxy_.csproj b/MCGalaxy/MCGalaxy_.csproj index 2fefd4c6a..205c0d926 100644 --- a/MCGalaxy/MCGalaxy_.csproj +++ b/MCGalaxy/MCGalaxy_.csproj @@ -511,6 +511,7 @@ + diff --git a/MCGalaxy/Player/Player.Handlers.cs b/MCGalaxy/Player/Player.Handlers.cs index 5b8e7918b..fd9e74596 100644 --- a/MCGalaxy/Player/Player.Handlers.cs +++ b/MCGalaxy/Player/Player.Handlers.cs @@ -328,9 +328,6 @@ namespace MCGalaxy { Position next = new Position(x, y, z); CheckBlocks(next); - if (Server.zombie.Running && Server.zombie.HandlesMovement(this, next, yaw, pitch)) - return; - OnPlayerMoveEvent.Call(this, next, yaw, pitch); if (cancelmove) { cancelmove = false; return; } diff --git a/MCGalaxy/Player/Player.Login.cs b/MCGalaxy/Player/Player.Login.cs index 175cc0a66..5266a9ce6 100644 --- a/MCGalaxy/Player/Player.Login.cs +++ b/MCGalaxy/Player/Player.Login.cs @@ -184,7 +184,6 @@ namespace MCGalaxy { Logger.Log(LogType.UserActivity, "{0} [{1}] connected using {2}.", name, ip, appName); } Game.InfectMessages = PlayerDB.GetInfectMessages(this); - Server.zombie.PlayerJoinedServer(this); Server.lava.PlayerJoinedServer(this); Pos = level.SpawnPos; diff --git a/MCGalaxy/Player/Player.cs b/MCGalaxy/Player/Player.cs index 6a29c2cc0..619e97000 100644 --- a/MCGalaxy/Player/Player.cs +++ b/MCGalaxy/Player/Player.cs @@ -309,7 +309,6 @@ namespace MCGalaxy { return; } - Server.zombie.PlayerLeftServer(this); if ( Game.team != null ) Game.team.RemoveMember(this); TntWarsGame tntwarsgame = TntWarsGame.GetTntWarsGame(this); if ( tntwarsgame != null ) { @@ -452,9 +451,6 @@ namespace MCGalaxy { public void SetMoney(int amount) { money = amount; - - if (Server.zombie.Running) Server.zombie.PlayerMoneyChanged(this); - if (Server.lava.active) Server.lava.PlayerMoneyChanged(this); OnMoneyChangedEvent.Call(this); }