added config plugin

This commit is contained in:
2025-08-13 23:32:35 -04:00
parent 2682c80120
commit 336af4e31a
5 changed files with 112 additions and 0 deletions

2
ConfigPlugin/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
bin/
obj/

24
ConfigPlugin/Config.cs Normal file
View File

@@ -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<Config>(File.ReadAllText(ConfigPlugin.ConfigPath));
}
}

View File

@@ -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);
}
}
}
}

View File

@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="OTAPI.Upcoming" Version="3.2.6" />
<PackageReference Include="TSAPI" Version="5.2.1" />
<PackageReference Include="TShock" Version="5.2.4" />
</ItemGroup>
</Project>

View File

@@ -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