Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions GoodNight/Configuration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using Newtonsoft.Json;
using System.Text;
using TShockAPI;

public struct TimeRange
{
public TimeSpan Start { get; set; }
public TimeSpan End { get; set; }
}

namespace Goodnight
{
internal class Configuration
{
public static readonly string FilePath = Path.Combine(TShock.SavePath, "宵禁.json");

[JsonProperty("启用状态")]
public bool Enabled { get; set; } = true;

[JsonProperty("是否断开连接")]
public bool DiscPlayers { get; set; } = false;

[JsonProperty("消息")]
public string Message { get; set; } = "别肝了,休息一会吧!大半夜推进度这河里吗?\n当前为宵禁时间(0点至5点),禁止游戏";

[JsonProperty("时间设置")]
public TimeRange Time { get; set; } = new TimeRange { Start = TimeSpan.FromHours(0), End = TimeSpan.FromHours(5) };

[JsonProperty("豁免玩家")]
public List<string> ExemptPlayers { get; set; } = new List<string>();

[JsonProperty("阻止怪物生成表")]
public HashSet<int> Npcs { get; set; } = new() { 1, 2, 4, 13, 14, 15, 37, 50, 68, 73, 81, 87, 88, 89, 90, 91, 92, 98, 99, 100, 113, 114, 120, 125, 126, 127, 128, 129, 130, 131, 134, 135, 136, 180, 182, 183, 184, 186, 187, 188, 189, 190, 191, 192, 194, 200, 210, 211, 222, 223, 224, 225, 244, 245, 250, 251, 253, 262, 266, 302, 303, 304, 325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 338, 339, 340, 341, 342, 344, 345, 346, 347, 348, 349, 350, 351, 352, 370, 381, 382, 383, 384, 385, 386, 387, 388, 389, 390, 391, 392, 393, 394, 395, 396, 397, 398, 399, 422, 430, 431, 432, 433, 435, 436, 438, 439, 440, 460, 461, 462, 463, 464, 465, 466, 467, 468, 469, 473, 474, 475, 476, 477, 478, 479, 482, 489, 490, 491, 492, 493, 507, 517, 548, 549, 551, 564, 565, 571, 576, 577, 586, 587, 618, 619, 620, 621, 622, 623, 624, 636, 657, 661, 664, 668 };

#region 读取与创建配置文件方法
public void Write(string path)
{
using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Write))
using (var sw = new StreamWriter(fs, new UTF8Encoding(false)))
{
var str = JsonConvert.SerializeObject(this, Formatting.Indented);
sw.Write(str);
}
}

public static Configuration Read(string path)
{
if (!File.Exists(path))
{
var c = new Configuration();
c.Write(path);
return c;
}
else
{
using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
using (var sr = new StreamReader(fs))
{
var json = sr.ReadToEnd();
var cf = JsonConvert.DeserializeObject<Configuration>(json);
return cf!;
}
}
}
#endregion
}
}
132 changes: 132 additions & 0 deletions GoodNight/Goodnight.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
using Terraria;
using Terraria.Localization;
using TerrariaApi.Server;
using TShockAPI;
using TShockAPI.Hooks;
using static TShockAPI.GetDataHandlers;

namespace Goodnight
{
[ApiVersion(2, 1)]
public class Goodnight : TerrariaPlugin
{
#region 变量与插件信息
public override string Name => "宵禁 Goodnight";
public override string Author => "Jonesn 羽学";
public override Version Version => new Version(2, 0, 0);
public override string Description => "宵禁插件,设置服务器无法进入的时段";
private static Configuration Config;
#endregion


#region 构造注册卸载
public Goodnight(Main game) : base(game) { }

public override void Initialize()
{
LoadConfig();
GeneralHooks.ReloadEvent += LoadConfig;
ServerApi.Hooks.NpcSpawn.Register(this, OnSpawn);
ServerApi.Hooks.NpcTransform.Register(this, OnTransform);
NewProjectile += HandleEvent;
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
GeneralHooks.ReloadEvent -= LoadConfig;
ServerApi.Hooks.NpcSpawn.Deregister(this, OnSpawn);
ServerApi.Hooks.NpcTransform.Deregister(this, OnTransform);
NewProjectile -= HandleEvent;
}
base.Dispose(disposing);
}
#endregion


#region 配置文件创建与重读加载方法
private static void LoadConfig(ReloadEventArgs args = null!)
{
Config = Configuration.Read(Configuration.FilePath);
Config.Write(Configuration.FilePath);
if (args != null && args.Player != null)
{
args.Player.SendSuccessMessage("[宵禁]重新加载配置完毕。");
}
}
#endregion


#region 宵禁
private void HandleEvent(object sender, EventArgs e)
{
if (!Config.Enabled) return;
if (DateTime.Now.TimeOfDay >= Config.Time.Start && DateTime.Now.TimeOfDay < Config.Time.End)
{
if (e is JoinEventArgs joinArgs)
{
var plr = TShock.Players[joinArgs.Who];
if (plr != null && !IsExempt(plr.Name))
{
if (Config.DiscPlayers)
plr.Disconnect("当前为宵禁时间,无法加入游戏。");
}
}

else if (e is NewProjectileEventArgs)
{
var Disconnect = TShock.Players.Where(p => p != null && !IsExempt(p.Name)).ToList();
foreach (var plr in Disconnect)
{
if (Config.DiscPlayers)
plr.Disconnect("到点了,晚安");
}
}

else
{
foreach (var plr in TShock.Players.Where(p => p != null))
{
if (Config.DiscPlayers)
NetMessage.SendData(2, plr.TPlayer.whoAmI, -1, NetworkText.FromLiteral(Config.Message), 0, 0f, 0f, 0f, 0, 0, 0);
}
}
}
}

private bool IsExempt(string playerName)
{
return Config.ExemptPlayers.Contains(playerName);
}

#endregion

#region 禁止召唤怪物
private void OnSpawn(NpcSpawnEventArgs args)
{
if (args.Handled || !Config.Enabled) return;
else if (DateTime.Now.TimeOfDay >= Config.Time.Start && DateTime.Now.TimeOfDay < Config.Time.End)
{
if (Config.Npcs.Contains(Main.npc[args.NpcId].netID))
{
args.Handled = true;
Main.npc[args.NpcId].active = false;
}
}
}

private void OnTransform(NpcTransformationEventArgs args)
{
if (args.Handled || !Config.Enabled) return;
else if (DateTime.Now.TimeOfDay >= Config.Time.Start && DateTime.Now.TimeOfDay < Config.Time.End)
{
if (Config.Npcs.Contains(Main.npc[args.NpcId].netID))
{
Main.npc[args.NpcId].active = false;
}
}
}
#endregion
}
}
5 changes: 5 additions & 0 deletions GoodNight/Goodnight.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">

<Import Project="..\template.targets" />

</Project>
Loading