-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Persist gamemode and add a minecraft-like command for it
- Loading branch information
1 parent
5f611e9
commit a1eb3ce
Showing
19 changed files
with
248 additions
and
97 deletions.
There are no files selected for viewing
31 changes: 27 additions & 4 deletions
31
NitroxClient/Communication/Packets/Processors/GameModeChangedProcessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,36 @@ | ||
using NitroxClient.Communication.Packets.Processors.Abstract; | ||
using NitroxClient.Communication.Packets.Processors.Abstract; | ||
using NitroxClient.GameLogic; | ||
using NitroxModel.Packets; | ||
|
||
namespace NitroxClient.Communication.Packets.Processors | ||
namespace NitroxClient.Communication.Packets.Processors; | ||
|
||
public class GameModeChangedProcessor : ClientPacketProcessor<GameModeChanged> | ||
{ | ||
public class GameModeChangedProcessor : ClientPacketProcessor<GameModeChanged> | ||
private readonly LocalPlayer localPlayer; | ||
private readonly PlayerManager playerManager; | ||
|
||
public GameModeChangedProcessor(LocalPlayer localPlayer, PlayerManager playerManager) | ||
{ | ||
this.localPlayer = localPlayer; | ||
this.playerManager = playerManager; | ||
} | ||
|
||
public override void Process(GameModeChanged packet) | ||
{ | ||
public override void Process(GameModeChanged packet) | ||
if (packet.AllPlayers || packet.PlayerId == localPlayer.PlayerId) | ||
{ | ||
GameModeUtils.SetGameMode((GameModeOption)(int)packet.GameMode, GameModeOption.None); | ||
} | ||
if (packet.AllPlayers) | ||
{ | ||
foreach (RemotePlayer remotePlayer in playerManager.GetAll()) | ||
{ | ||
remotePlayer.SetGameMode(packet.GameMode); | ||
} | ||
} | ||
else if (playerManager.TryFind(packet.PlayerId, out RemotePlayer remotePlayer)) | ||
{ | ||
remotePlayer.SetGameMode(packet.GameMode); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,33 @@ | ||
using System; | ||
using System; | ||
using NitroxModel.DataStructures; | ||
using NitroxModel.Server; | ||
|
||
namespace NitroxModel.MultiplayerSession | ||
namespace NitroxModel.MultiplayerSession; | ||
|
||
[Serializable] | ||
public class PlayerContext | ||
{ | ||
[Serializable] | ||
public class PlayerContext | ||
{ | ||
public string PlayerName { get; } | ||
public ushort PlayerId { get; } | ||
public NitroxId PlayerNitroxId { get; } | ||
public bool WasBrandNewPlayer { get; } | ||
public PlayerSettings PlayerSettings { get; } | ||
public bool IsMuted { get; set; } | ||
public string PlayerName { get; } | ||
public ushort PlayerId { get; } | ||
public NitroxId PlayerNitroxId { get; } | ||
public bool WasBrandNewPlayer { get; } | ||
public PlayerSettings PlayerSettings { get; } | ||
public bool IsMuted { get; set; } | ||
public NitroxGameMode GameMode { get; set; } | ||
|
||
public PlayerContext(string playerName, ushort playerId, NitroxId playerNitroxId, bool wasBrandNewPlayer, PlayerSettings playerSettings, bool isMuted) | ||
{ | ||
PlayerName = playerName; | ||
PlayerId = playerId; | ||
PlayerNitroxId = playerNitroxId; | ||
WasBrandNewPlayer = wasBrandNewPlayer; | ||
PlayerSettings = playerSettings; | ||
IsMuted = isMuted; | ||
} | ||
public PlayerContext(string playerName, ushort playerId, NitroxId playerNitroxId, bool wasBrandNewPlayer, PlayerSettings playerSettings, bool isMuted, NitroxGameMode gameMode) | ||
{ | ||
PlayerName = playerName; | ||
PlayerId = playerId; | ||
PlayerNitroxId = playerNitroxId; | ||
WasBrandNewPlayer = wasBrandNewPlayer; | ||
PlayerSettings = playerSettings; | ||
IsMuted = isMuted; | ||
GameMode = gameMode; | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return $"[PlayerContext - PlayerName: {PlayerName}, PlayerId: {PlayerId}, PlayerNitroxId: {PlayerNitroxId}, WasBrandNewPlayer: {WasBrandNewPlayer}, PlayerSettings: {PlayerSettings}]"; | ||
} | ||
public override string ToString() | ||
{ | ||
return $"[PlayerContext - PlayerName: {PlayerName}, PlayerId: {PlayerId}, PlayerNitroxId: {PlayerNitroxId}, WasBrandNewPlayer: {WasBrandNewPlayer}, PlayerSettings: {PlayerSettings}, GameMode: {GameMode}]"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,29 @@ | ||
using System; | ||
using System; | ||
using NitroxModel.Server; | ||
|
||
namespace NitroxModel.Packets | ||
namespace NitroxModel.Packets; | ||
|
||
[Serializable] | ||
public class GameModeChanged : Packet | ||
{ | ||
[Serializable] | ||
public class GameModeChanged : Packet | ||
public ushort PlayerId { get; } | ||
public bool AllPlayers { get; } | ||
public NitroxGameMode GameMode { get; } | ||
|
||
public GameModeChanged(ushort playerId, bool allPlayers, NitroxGameMode gameMode) | ||
{ | ||
public ServerGameMode GameMode { get; } | ||
PlayerId = playerId; | ||
AllPlayers = allPlayers; | ||
GameMode = gameMode; | ||
} | ||
|
||
public GameModeChanged(ServerGameMode gameMode) | ||
{ | ||
GameMode = gameMode; | ||
} | ||
public static GameModeChanged ForPlayer(ushort playerId, NitroxGameMode gameMode) | ||
{ | ||
return new(playerId, false, gameMode); | ||
} | ||
|
||
public static GameModeChanged ForAllPlayers(NitroxGameMode gameMode) | ||
{ | ||
return new(0, true, gameMode); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace NitroxModel.Server; | ||
|
||
/// <summary> | ||
/// GameModes according to Subnautica's enum GameModeOption | ||
/// </summary> | ||
public enum NitroxGameMode | ||
{ | ||
SURVIVAL = 0, | ||
FREEDOM = 2, | ||
HARDCORE = 257, | ||
CREATIVE = 1790, | ||
} |
This file was deleted.
Oops, something went wrong.
37 changes: 37 additions & 0 deletions
37
NitroxPatcher/Patches/Dynamic/GameModeConsoleCommands_OnConsoleCommand_Patch.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using System.Reflection; | ||
using HarmonyLib; | ||
using NitroxClient.Communication.Abstract; | ||
using NitroxModel.Helper; | ||
using NitroxModel.Packets; | ||
using NitroxModel.Server; | ||
|
||
namespace NitroxPatcher.Patches.Dynamic; | ||
|
||
/// <inheritdoc cref="CrashedShipExploder_OnConsoleCommand_Patch"/> | ||
public sealed partial class GameModeConsoleCommands_OnConsoleCommand_Patch : NitroxPatch, IDynamicPatch | ||
{ | ||
public static readonly MethodInfo TARGET_METHOD_SURVIVAL = Reflect.Method((GameModeConsoleCommands t) => t.OnConsoleCommand_survival()); | ||
public static readonly MethodInfo TARGET_METHOD_CREATIVE = Reflect.Method((GameModeConsoleCommands t) => t.OnConsoleCommand_creative()); | ||
public static readonly MethodInfo TARGET_METHOD_FREEDOM = Reflect.Method((GameModeConsoleCommands t) => t.OnConsoleCommand_freedom()); | ||
public static readonly MethodInfo TARGET_METHOD_HARDCORE = Reflect.Method((GameModeConsoleCommands t) => t.OnConsoleCommand_hardcore()); | ||
|
||
public static bool PrefixSurvival() => BroadcastGameModeChange(NitroxGameMode.SURVIVAL); | ||
public static bool PrefixCreative() => BroadcastGameModeChange(NitroxGameMode.CREATIVE); | ||
public static bool PrefixFreedom() => BroadcastGameModeChange(NitroxGameMode.FREEDOM); | ||
public static bool PrefixHardcore() => BroadcastGameModeChange(NitroxGameMode.HARDCORE); | ||
|
||
private static bool BroadcastGameModeChange(NitroxGameMode gameMode) | ||
{ | ||
Resolve<IPacketSender>().Send(new ServerCommand($"gamemode {gameMode}")); | ||
return false; | ||
} | ||
|
||
public override void Patch(Harmony harmony) | ||
{ | ||
PatchPrefix(harmony, TARGET_METHOD_SURVIVAL, ((Func<bool>)PrefixSurvival).Method); | ||
PatchPrefix(harmony, TARGET_METHOD_CREATIVE, ((Func<bool>)PrefixCreative).Method); | ||
PatchPrefix(harmony, TARGET_METHOD_FREEDOM, ((Func<bool>)PrefixFreedom).Method); | ||
PatchPrefix(harmony, TARGET_METHOD_HARDCORE, ((Func<bool>)PrefixHardcore).Method); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.