using Newtonsoft.Json;
using Newtonsoft.Json;
namespace TestPlugin; //这里要改成和你插件命名空间一样
public class Config
{
/// <summary>
/// 配置文件路径
/// </summary>
private const string ConfigPath = "tshock/TestPlugin.json";
/// <summary>
/// 配置文件实例
/// </summary>
public static Config Instance = new();
private static readonly JsonSerializerSettings JsonSettings = new() //配置文件格式化相关,不熟悉Newtonsoft.Json不要更改
{
Formatting = Formatting.Indented,
ObjectCreationHandling = ObjectCreationHandling.Replace
};
/*
*从这开始,就算是正式的配置项部分, 这里以 `在服务器发送表情踢出玩家为例`
*/
/// <summary>
/// 允许玩家发表情
/// </summary>
[JsonProperty]
public bool AllowSendEmoji;
/// <summary>
/// 玩家发表情后踢出的消息
/// </summary>
[JsonProperty]
public string SendEmojiKickMessage = "此服务器不允许发表情!!!";
/// <summary>
/// 将配置文件写入硬盘
/// </summary>
public void Write()
{
using FileStream fileStream = new(ConfigPath, FileMode.Create, FileAccess.Write, FileShare.Write);
using StreamWriter streamWriter = new(fileStream);
streamWriter.Write(JsonConvert.SerializeObject(this, JsonSettings));
}
/// <summary>
/// 从硬盘读取配置文件
/// </summary>
public void Read()
{
Config result;
if (!File.Exists(ConfigPath))
{
result = new Config();
result.Write();
}
else
{
using FileStream fileStream = new(ConfigPath, FileMode.Open, FileAccess.Read, FileShare.Read);
using StreamReader streamReader = new(fileStream);
result = JsonConvert.DeserializeObject<Config>(streamReader.ReadToEnd(), JsonSettings)!;
}
Instance = result;
}
}
namespace TestPlugin;
private const string ConfigPath = "tshock/TestPlugin.json";
private const string ConfigPath = "tshock/TestPlugin/TestPlugin.json"; //套文件夹的配置文件
public void Read()
{
Config result;
if (!File.Exists(ConfigPath))
{
Terraria.Utils.TryCreatingDirectory("tshock/TestPlugin"); //创建文件夹,返回值是bool(文件夹是否可用)
result = new Config();
result.Write();
}
else
{
using FileStream fileStream = new(ConfigPath, FileMode.Open, FileAccess.Read, FileShare.Read);
using StreamReader streamReader = new(fileStream);
result = JsonConvert.DeserializeObject<Config>(streamReader.ReadToEnd(), JsonSettings)!;
}
Instance = result;
}
public override void Initialize()
{
Config.Instance.Read(); //读取配置文件
}
{
"AllowSendEmoji": false,
"SendEmojiKickMessage": "此服务器不允许发表情!!!"
}
if (!Config.Instance.AllowSendEmoji)
{
TSPlayer.All.Kick(Config.Instance.SendEmojiKickMessage);
}
Config.Instance.SendEmojiKickMessage = "5555,服主大人不让你发表情捏~"; //修改配置
Config.Instance.Write(); //写入配置文件
public override void Initialize()
{
Config.Instance.Read(); //读取配置文件
GeneralHooks.ReloadEvent += OnReloadEvent; //注册Reload事件
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
GeneralHooks.ReloadEvent -=OnReloadEvent; //卸载Reload事件
}
base.Dispose(disposing);
}
private void OnReloadEvent(ReloadEventArgs e)
{
Config.Instance.Read(); //读取配置文件
e.Player.SendSuccessMessage("[TestPlugin]插件配置已重载喵~"); //向玩家发送提示~
}
[JsonProperty("允许玩家发表情")]
public bool AllowSendEmoji;
[JsonProperty("发表情踢出消息")]
public string SendEmojiKickMessage = "此服务器不允许发表情!!!";
{
"允许玩家发表情": false,
"玩家发表情后踢出的消息": "此服务器不允许发表情!!!"
}
[JsonProperty("玩家发表情后踢出的消息",Order = 2)] //Oder 2更大排在后面
public string SendEmojiKickMessage = "此服务器不允许发表情!!!";
[JsonProperty("允许玩家发表情",Order = 1)] //Oder 1更小排在前面
public bool AllowSendEmoji;
{
"允许玩家发表情": false,
"玩家发表情后踢出的消息": "此服务器不允许发表情!!!"
}
using Terraria;
using TerrariaApi.Server;
using TShockAPI;
using TShockAPI.Hooks;
namespace KickEmoji;
[ApiVersion(2, 1)]
public class KickEmoji : TerrariaPlugin
{
public KickEmoji(Main game) : base(game)
{
}
public override string Author => "Cai";
public override string Description => "踢出哪些乱发表情的坏蛋~";
public override string Name => "KickEmoji";
public override Version Version => new(2024, 12, 21, 0); //版本号可以用日期哦~~~
public override void Initialize()
{
Config.Instance.Read(); //读取配置文件
GeneralHooks.ReloadEvent += OnReloadEvent; //注册Reload事件
GetDataHandlers.Emoji.Register(OnPlayerSendEmoji); //注册发表情事件
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
GeneralHooks.ReloadEvent -=OnReloadEvent; //卸载Reload事件
GetDataHandlers.Emoji.UnRegister(OnPlayerSendEmoji); //卸载发表情事件
}
base.Dispose(disposing);
}
private void OnPlayerSendEmoji(object? sender, GetDataHandlers.EmojiEventArgs e)
{
if (Config.Instance.DisallowEmoji.Contains(e.EmojiID)) //判断是否为`禁用表情`
{
e.Handled = true; //处理掉这个`表情`
if (Config.Instance.KickPlayer) //判断是否需要踢出玩家
{
e.Player.Kick(Config.Instance.SendEmojiKickMessage, true); //踢出玩家,Kick的第二个形参`force`是`强制踢出`,可踢管理员
}
else
{
e.Player.SendWarningMessage(Config.Instance.SendEmojiKickMessage); //警告玩家
}
}
}
private void OnReloadEvent(ReloadEventArgs e)
{
Config.Instance.Read(); //读取配置文件
e.Player.SendSuccessMessage("[TestPlugin]插件配置已重载喵~"); //向玩家发送提示~
}
}