diff --git a/ConfigPlugin/.gitignore b/ConfigPlugin/.gitignore new file mode 100644 index 0000000..cbbd0b5 --- /dev/null +++ b/ConfigPlugin/.gitignore @@ -0,0 +1,2 @@ +bin/ +obj/ \ No newline at end of file diff --git a/ConfigPlugin/Config.cs b/ConfigPlugin/Config.cs new file mode 100644 index 0000000..88e2068 --- /dev/null +++ b/ConfigPlugin/Config.cs @@ -0,0 +1,24 @@ +using Newtonsoft.Json; + +namespace ConfigPlugin; + +public class Config +{ + public string? CustomMessage; + public string? Player; + + public void Write() + { + File.WriteAllText(ConfigPlugin.ConfigPath, JsonConvert.SerializeObject(this, Formatting.Indented)); + } + + public static Config? Read() + { + if (!File.Exists(ConfigPlugin.ConfigPath)) + { + return new Config(); + } + + return JsonConvert.DeserializeObject(File.ReadAllText(ConfigPlugin.ConfigPath)); + } +} \ No newline at end of file diff --git a/ConfigPlugin/ConfigPlugin.cs b/ConfigPlugin/ConfigPlugin.cs new file mode 100644 index 0000000..a0fa26f --- /dev/null +++ b/ConfigPlugin/ConfigPlugin.cs @@ -0,0 +1,65 @@ +using Terraria; +using TShockAPI; +using TerrariaApi.Server; +using TShockAPI.Hooks; + +namespace ConfigPlugin +{ + [ApiVersion(2, 1)] + public class ConfigPlugin(Main game) : TerrariaPlugin(game) + { + public override string Author => "wymiller"; + public override string Description => "A plugin that takes some configuration"; + public override string Name => "Configuration Plugin"; + public override Version Version => new Version(1, 0, 0, 0); + + // instantiate our config class + public static string ConfigPath = Path.Combine(TShock.SavePath, "Config.json"); + private readonly Config _config = new Config(); + + public override void Initialize() + { + GeneralHooks.ReloadEvent += OnReload; + ServerApi.Hooks.NetGreetPlayer.Register(this, OnJoin); + + PerformConfigOperations(); + + Console.WriteLine("[ConfigPlugin] Plugin is loaded"); + } + + private void OnReload(ReloadEventArgs args) + { + PerformConfigOperations(); + } + + private void OnJoin(GreetPlayerEventArgs args) + { + TSPlayer player = TShock.Players[args.Who]; + player.SendInfoMessage($"{player} has arrived!"); + if (player.Name == _config.Player) + { + player.SendInfoMessage(_config.CustomMessage); + } + } + + private void PerformConfigOperations() + { + if (File.Exists(ConfigPath)) + { + Config.Read(); + } + else + { + _config.Write(); + } + } + + protected override void Dispose(bool disposing) + { + if (disposing) + { + base.Dispose(disposing); + } + } + } +} \ No newline at end of file diff --git a/ConfigPlugin/ConfigPlugin.csproj b/ConfigPlugin/ConfigPlugin.csproj new file mode 100644 index 0000000..a24ed3d --- /dev/null +++ b/ConfigPlugin/ConfigPlugin.csproj @@ -0,0 +1,15 @@ + + + + net9.0 + enable + enable + + + + + + + + + diff --git a/TerrariaPluginTemplate.sln b/TerrariaPluginTemplate.sln index d6ba76d..6cf04a6 100644 --- a/TerrariaPluginTemplate.sln +++ b/TerrariaPluginTemplate.sln @@ -2,6 +2,8 @@ Microsoft Visual Studio Solution File, Format Version 12.00 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HelloWorldPlugin", "HelloWorldPlugin\HelloWorldPlugin.csproj", "{F02910DB-41DA-4297-99BE-49B90A594C94}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfigPlugin", "ConfigPlugin\ConfigPlugin.csproj", "{06A284DA-1174-4793-84C4-25CCCA724ECB}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -12,5 +14,9 @@ Global {F02910DB-41DA-4297-99BE-49B90A594C94}.Debug|Any CPU.Build.0 = Debug|Any CPU {F02910DB-41DA-4297-99BE-49B90A594C94}.Release|Any CPU.ActiveCfg = Release|Any CPU {F02910DB-41DA-4297-99BE-49B90A594C94}.Release|Any CPU.Build.0 = Release|Any CPU + {06A284DA-1174-4793-84C4-25CCCA724ECB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {06A284DA-1174-4793-84C4-25CCCA724ECB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {06A284DA-1174-4793-84C4-25CCCA724ECB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {06A284DA-1174-4793-84C4-25CCCA724ECB}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal