Skip to content

Commit

Permalink
Persist gamemode and add a minecraft-like command for it
Browse files Browse the repository at this point in the history
  • Loading branch information
tornac1234 committed Oct 26, 2023
1 parent 5f611e9 commit a1eb3ce
Show file tree
Hide file tree
Showing 19 changed files with 248 additions and 97 deletions.
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private void SetPlayerStats(PlayerStatsData statsData)
Player.main.GetPDA().Close();
}

private void SetPlayerGameMode(ServerGameMode gameMode)
private void SetPlayerGameMode(NitroxGameMode gameMode)
{
Log.Info($"Received initial sync packet with gamemode {gameMode}");
GameModeUtils.SetGameMode((GameModeOption)(int)gameMode, GameModeOption.None);
Expand Down
16 changes: 16 additions & 0 deletions NitroxClient/GameLogic/RemotePlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using NitroxClient.MonoBehaviours.Gui.HUD;
using NitroxClient.Unity.Helper;
using NitroxModel.MultiplayerSession;
using NitroxModel.Server;
using UnityEngine;
using UWE;
using Object = UnityEngine.Object;
Expand Down Expand Up @@ -88,6 +89,7 @@ public void InitializeGameObject(GameObject playerBody)
SetupSkyAppliers();

vitals = playerVitalsManager.CreateOrFindForPlayer(this);
RefreshVitalsVisibility();
}

public void Attach(Transform transform, bool keepWorldTransform = false)
Expand Down Expand Up @@ -338,5 +340,19 @@ private void SetupSkyAppliers()
skyApplier.dynamic = true;
skyApplier.renderers = Body.GetComponentsInChildren<SkinnedMeshRenderer>(true);
}

public void SetGameMode(NitroxGameMode gameMode)
{
PlayerContext.GameMode = gameMode;
RefreshVitalsVisibility();
}

private void RefreshVitalsVisibility()
{
if (vitals)
{
vitals.gameObject.SetActive(PlayerContext.GameMode != NitroxGameMode.CREATIVE);
}
}
}
}
16 changes: 8 additions & 8 deletions NitroxLauncher/Pages/ServerPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ public void SaveConfigSettings()
{
Config.SaveName = TBWorldName.Text;
if (IsNewWorld) { Config.Seed = TBWorldSeed.Text; }
if (RBFreedom.IsChecked == true) { Config.GameMode = ServerGameMode.FREEDOM; }
else if (RBSurvival.IsChecked == true) { Config.GameMode = ServerGameMode.SURVIVAL; }
else if (RBCreative.IsChecked == true) { Config.GameMode = ServerGameMode.CREATIVE; }
else if (RBHardcore.IsChecked == true) { Config.GameMode = ServerGameMode.HARDCORE; }
if (RBFreedom.IsChecked == true) { Config.GameMode = NitroxGameMode.FREEDOM; }
else if (RBSurvival.IsChecked == true) { Config.GameMode = NitroxGameMode.SURVIVAL; }
else if (RBCreative.IsChecked == true) { Config.GameMode = NitroxGameMode.CREATIVE; }
else if (RBHardcore.IsChecked == true) { Config.GameMode = NitroxGameMode.HARDCORE; }

Config.DisableConsole = !CBCheats.IsChecked ?? Config.DisableConsole;
Config.MaxConnections = Convert.ToInt32(TBMaxPlayerCap.Text);
Expand Down Expand Up @@ -109,16 +109,16 @@ public void UpdateVisualWorldSettings()
TBWorldSeed.Text = Config.Seed;
switch (Config.GameMode)
{
case ServerGameMode.FREEDOM:
case NitroxGameMode.FREEDOM:
RBFreedom.IsChecked = true;
break;
case ServerGameMode.SURVIVAL:
case NitroxGameMode.SURVIVAL:
RBSurvival.IsChecked = true;
break;
case ServerGameMode.CREATIVE:
case NitroxGameMode.CREATIVE:
RBCreative.IsChecked = true;
break;
case ServerGameMode.HARDCORE:
case NitroxGameMode.HARDCORE:
RBHardcore.IsChecked = true;
break;
}
Expand Down
50 changes: 26 additions & 24 deletions NitroxModel/MultiplayerSession/PlayerContext.cs
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}]";
}
}
31 changes: 22 additions & 9 deletions NitroxModel/Packets/GameModeChanged.cs
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);
}
}
6 changes: 3 additions & 3 deletions NitroxModel/Packets/InitialPlayerSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class InitialPlayerSync : Packet
public List<PlayerContext> OtherPlayers { get; }
public List<Entity> GlobalRootEntities { get; }
public List<NitroxId> InitialSimulationOwnerships { get; }
public ServerGameMode GameMode { get; }
public NitroxGameMode GameMode { get; }
public Perms Permissions { get; }
public SubnauticaPlayerPreferences Preferences { get; }
public TimeData TimeData { get; }
Expand All @@ -49,7 +49,7 @@ public class InitialPlayerSync : Packet
IEnumerable<PlayerContext> otherPlayers,
IEnumerable<Entity> globalRootEntities,
IEnumerable<NitroxId> initialSimulationOwnerships,
ServerGameMode gameMode,
NitroxGameMode gameMode,
Perms perms,
SubnauticaPlayerPreferences preferences,
TimeData timeData,
Expand Down Expand Up @@ -94,7 +94,7 @@ public class InitialPlayerSync : Packet
List<PlayerContext> otherPlayers,
List<Entity> globalRootEntities,
List<NitroxId> initialSimulationOwnerships,
ServerGameMode gameMode,
NitroxGameMode gameMode,
Perms permissions,
SubnauticaPlayerPreferences preferences,
TimeData timeData,
Expand Down
12 changes: 12 additions & 0 deletions NitroxModel/Server/NitroxGameMode.cs
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,
}
10 changes: 0 additions & 10 deletions NitroxModel/Server/ServerGameMode.cs

This file was deleted.

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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public override void Process(PlayerJoiningMultiplayerSession packet, NitroxConne
GetOtherPlayers(player),
globalRootEntities,
simulations,
world.GameMode,
player.GameMode,
player.Permissions,
new(new(player.PingInstancePreferences), player.PinnedRecipePreferences.ToList()),
storyManager.GetTimeData(),
Expand Down
49 changes: 24 additions & 25 deletions NitroxServer/ConsoleCommands/ChangeServerGamemodeCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.IO;
using System.IO;
using NitroxModel.DataStructures.GameLogic;
using NitroxModel.Packets;
using NitroxModel.Server;
Expand All @@ -8,38 +8,37 @@
using NitroxServer.Serialization;
using NitroxServer.Serialization.World;

namespace NitroxServer.ConsoleCommands
namespace NitroxServer.ConsoleCommands;

internal class ChangeServerGamemodeCommand : Command
{
internal class ChangeServerGamemodeCommand : Command
private readonly PlayerManager playerManager;
private readonly ServerConfig serverConfig;

public ChangeServerGamemodeCommand(PlayerManager playerManager, ServerConfig serverConfig) : base("changeservergamemode", Perms.ADMIN, "Changes server gamemode")
{
private readonly PlayerManager playerManager;
private readonly ServerConfig serverConfig;
AddParameter(new TypeEnum<NitroxGameMode>("gamemode", true, "Gamemode to change to"));

public ChangeServerGamemodeCommand(PlayerManager playerManager, ServerConfig serverConfig) : base("changeservergamemode", Perms.ADMIN, "Changes server gamemode")
{
AddParameter(new TypeEnum<ServerGameMode>("gamemode", true, "Gamemode to change to"));
this.playerManager = playerManager;
this.serverConfig = serverConfig;
}

this.playerManager = playerManager;
this.serverConfig = serverConfig;
}
protected override void Execute(CallArgs args)
{
NitroxGameMode sgm = args.Get<NitroxGameMode>(0);

protected override void Execute(CallArgs args)
using (serverConfig.Update(Path.Combine(WorldManager.SavesFolderDir, serverConfig.SaveName)))
{
ServerGameMode sgm = args.Get<ServerGameMode>(0);

using (serverConfig.Update(Path.Combine(WorldManager.SavesFolderDir, serverConfig.SaveName)))
if (serverConfig.GameMode != sgm)
{
if (serverConfig.GameMode != sgm)
{
serverConfig.GameMode = sgm;
serverConfig.GameMode = sgm;

playerManager.SendPacketToAllPlayers(new GameModeChanged(sgm));
SendMessageToAllPlayers($"Server gamemode changed to \"{sgm}\" by {args.SenderName}");
}
else
{
SendMessage(args.Sender, "Server is already using this gamemode");
}
playerManager.SendPacketToAllPlayers(GameModeChanged.ForAllPlayers(sgm));
SendMessageToAllPlayers($"Server gamemode changed to \"{sgm}\" by {args.SenderName}");
}
else
{
SendMessage(args.Sender, "Server is already using this gamemode");
}
}
}
Expand Down
Loading

0 comments on commit a1eb3ce

Please sign in to comment.