Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: more plugins use LazyAPI #587

Merged
merged 5 commits into from
Nov 30, 2024
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
6 changes: 0 additions & 6 deletions src/AnnouncementBoxPlus/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public AnnouncementBoxPlus(Main game) : base(game)
//插件加载时执行的代码
public override void Initialize()
{
GeneralHooks.ReloadEvent += this.GeneralHooks_ReloadEvent;
On.OTAPI.Hooks.Wiring.InvokeAnnouncementBox += this.OnAnnouncementBox;
GetDataHandlers.SignRead.Register(this.OnSignRead);
GetDataHandlers.Sign.Register(this.OnSign);
Expand All @@ -45,14 +44,9 @@ protected override void Dispose(bool disposing)
GetDataHandlers.SignRead.UnRegister(this.OnSignRead);
GetDataHandlers.Sign.UnRegister(this.OnSign);
On.OTAPI.Hooks.Wiring.InvokeAnnouncementBox -= this.OnAnnouncementBox;
GeneralHooks.ReloadEvent -= this.GeneralHooks_ReloadEvent;
}
base.Dispose(disposing);
}
private void GeneralHooks_ReloadEvent(ReloadEventArgs e)
{
e.Player.SendSuccessMessage("[AnnouncementBoxPlus]配置文件已重载!");
}

private void OnSign(object? sender, GetDataHandlers.SignEventArgs e)
{
Expand Down
77 changes: 27 additions & 50 deletions src/AutoFish/AutoFish.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,29 @@
using Terraria;
using Terraria.ID;
using TShockAPI;
using TShockAPI.Hooks;
using TerrariaApi.Server;
using System.Text;
using LazyAPI;

namespace AutoFish;

[ApiVersion(2, 1)]
public class AutoFish : TerrariaPlugin
public class AutoFish : LazyPlugin
{

#region 插件信息
public override string Name => "自动钓鱼";
public override string Author => "羽学 少司命";
public override Version Version => new Version(1, 3, 2);
public override Version Version => new Version(1, 3, 3);
public override string Description => "涡轮增压不蒸鸭";
#endregion

#region 注册与释放
public AutoFish(Main game) : base(game) { }
public override void Initialize()
{
LoadConfig();
GeneralHooks.ReloadEvent += ReloadConfig;
GetDataHandlers.NewProjectile += this.ProjectNew!;
GetDataHandlers.NewProjectile += this.BuffUpdate!;
GetDataHandlers.NewProjectile.Register(this.ProjectNew);
GetDataHandlers.NewProjectile.Register(this.BuffUpdate);
ServerApi.Hooks.ServerJoin.Register(this, this.OnJoin);
GetDataHandlers.PlayerUpdate.Register(this.OnPlayerUpdate);
ServerApi.Hooks.ProjectileAIUpdate.Register(this, this.ProjectAiUpdate);
Expand All @@ -37,9 +35,8 @@ protected override void Dispose(bool disposing)
{
if (disposing)
{
GeneralHooks.ReloadEvent -= ReloadConfig;
GetDataHandlers.NewProjectile -= this.ProjectNew!;
GetDataHandlers.NewProjectile -= this.BuffUpdate!;
GetDataHandlers.NewProjectile.UnRegister(this.ProjectNew);
GetDataHandlers.NewProjectile.UnRegister(this.BuffUpdate);
ServerApi.Hooks.ServerJoin.Deregister(this, this.OnJoin);
GetDataHandlers.PlayerUpdate.UnRegister(this.OnPlayerUpdate);
ServerApi.Hooks.ProjectileAIUpdate.Deregister(this, this.ProjectAiUpdate);
Expand All @@ -49,25 +46,11 @@ protected override void Dispose(bool disposing)
}
#endregion

#region 配置重载读取与写入方法
internal static Configuration Config = new();
private static void ReloadConfig(ReloadEventArgs args)
{
LoadConfig();
args.Player.SendInfoMessage(GetString("[自动钓鱼]重新加载配置完毕。"));
}
private static void LoadConfig()
{
Config = Configuration.Read();
Config.Write();
}
#endregion

#region 玩家更新配置方法(创建配置结构)
internal static MyData Data = new();
private void OnJoin(JoinEventArgs args)
{
if (args == null || !Config.Enabled)
if (args == null || !Configuration.Instance.Enabled)
{
return;
}
Expand Down Expand Up @@ -99,7 +82,7 @@ private void ProjectAiUpdate(ProjectileAiUpdateEventArgs args)
if (args.Projectile.owner is < 0 or > Main.maxPlayers ||
!args.Projectile.active ||
!args.Projectile.bobber ||
!Config.Enabled)
!Configuration.Instance.Enabled)
{
return;
}
Expand All @@ -119,7 +102,7 @@ private void ProjectAiUpdate(ProjectileAiUpdateEventArgs args)
}

//开启消耗模式
if (Config.ConMod)
if (Configuration.Instance.ConMod)
{
//多加一个 list.Mod 来判断玩家是否花费了【指定物品】来换取功能时长
if (list.Mod && list.Enabled)
Expand Down Expand Up @@ -194,7 +177,7 @@ private static void ControlFishing(ProjectileAiUpdateEventArgs args, TSPlayer pl
args.Projectile.FishingCheck();

//随机物品
if (Config.Random)
if (Configuration.Instance.Random)
{
var rm = new Random();
var id = rm.Next(1, ItemID.Count - 1);
Expand All @@ -205,13 +188,13 @@ private static void ControlFishing(ProjectileAiUpdateEventArgs args, TSPlayer pl
args.Projectile.ai[1] = args.Projectile.localAI[1];

//如果额外渔获 有任何1个物品ID 则参与AI[1]
if (Config.DoorItems.Any())
if (Configuration.Instance.DoorItems.Any())
{
//确保浮漂正在运动,没有鱼上钩(即ai[1]小于等于0)
if (args.Projectile.ai[1] < 0)
{
// 从DoorItems中随机选择一个物品,并将其ID赋值给ai[1],模拟有新鱼上钩的情况
args.Projectile.ai[1] = Convert.ToSingle(Config.DoorItems.OrderByDescending(x => Guid.NewGuid()).First());
args.Projectile.ai[1] = Convert.ToSingle(Configuration.Instance.DoorItems.OrderByDescending(x => Guid.NewGuid()).First());
}
}
}
Expand All @@ -229,7 +212,7 @@ private static void ControlFishing(ProjectileAiUpdateEventArgs args, TSPlayer pl
#endregion

#region 多线钓鱼
public void ProjectNew(object sender, GetDataHandlers.NewProjectileEventArgs e)
public void ProjectNew(object? sender, GetDataHandlers.NewProjectileEventArgs e)
{
var plr = e.Player;
var guid = Guid.NewGuid().ToString();
Expand All @@ -238,10 +221,10 @@ public void ProjectNew(object sender, GetDataHandlers.NewProjectileEventArgs e)
if (plr == null ||
!plr.Active ||
!plr.IsLoggedIn ||
!Config.Enabled ||
!Config.MoreHook ||
!Configuration.Instance.Enabled ||
!Configuration.Instance.MoreHook ||
!plr.HasPermission("autofish") ||
HookCount > Config.HookMax - 1)
HookCount > Configuration.Instance.HookMax - 1)
{
return;
}
Expand All @@ -255,7 +238,7 @@ public void ProjectNew(object sender, GetDataHandlers.NewProjectileEventArgs e)
}

//开启消耗模式
if (Config.ConMod)
if (Configuration.Instance.ConMod)
{
//玩家的自动钓鱼开关
if (list.Mod && list.Enabled)
Expand Down Expand Up @@ -294,11 +277,11 @@ public void ProjectNew(object sender, GetDataHandlers.NewProjectileEventArgs e)
#endregion

#region Buff更新方法
public void BuffUpdate(object sender, GetDataHandlers.NewProjectileEventArgs e)
public void BuffUpdate(object? sender, GetDataHandlers.NewProjectileEventArgs e)
{
var plr = e.Player;

if (plr == null || !plr.Active || !plr.IsLoggedIn || !Config.Enabled || !plr.HasPermission("autofish"))
if (plr == null || !plr.Active || !plr.IsLoggedIn || !Configuration.Instance.Enabled || !plr.HasPermission("autofish"))
{
return;
}
Expand All @@ -315,7 +298,7 @@ public void BuffUpdate(object sender, GetDataHandlers.NewProjectileEventArgs e)
{
if (Tools.BobbersActive(e.Owner))
{
foreach (var buff in Config.BuffID)
foreach (var buff in Configuration.Instance.BuffID)
{
plr.SetBuff(buff.Key, buff.Value);
}
Expand All @@ -328,7 +311,7 @@ public void BuffUpdate(object sender, GetDataHandlers.NewProjectileEventArgs e)
private void OnPlayerUpdate(object? sender, GetDataHandlers.PlayerUpdateEventArgs e)
{
var plr = e.Player;
if (!Config.Enabled || !Config.ConMod || e == null ||
if (!Configuration.Instance.Enabled || !Configuration.Instance.ConMod || e == null ||
plr == null || !plr.IsLoggedIn || !plr.Active ||
!plr.HasPermission("autofish"))
{
Expand All @@ -349,11 +332,11 @@ private void OnPlayerUpdate(object? sender, GetDataHandlers.PlayerUpdateEventArg
if (!data.Mod)
{
//初始化一个消耗值
var sun = Config.BaitStack;
var sun = Configuration.Instance.BaitStack;

// 统计背包中指定鱼饵的总数量(不包含手上物品)
var TotalBait = plr.TPlayer.inventory.Sum(inv =>
(Config.BaitType.Contains(inv.type) &&
(Configuration.Instance.BaitType.Contains(inv.type) &&
inv.type != plr.TPlayer.inventory[plr.TPlayer.selectedItem].type) ?
inv.stack : 0);

Expand All @@ -366,7 +349,7 @@ private void OnPlayerUpdate(object? sender, GetDataHandlers.PlayerUpdateEventArg
var inv = plr.TPlayer.inventory[i];

// 是Config里指定的鱼饵,不是手上的物品
if (Config.BaitType.Contains(inv.type))
if (Configuration.Instance.BaitType.Contains(inv.type))
{
var BaitStack = Math.Min(sun, inv.stack); // 计算需要消耗的鱼饵数量

Expand Down Expand Up @@ -411,13 +394,13 @@ private static void ExitMod(TSPlayer plr, MyData.ItemData data)
{
var mess2 = new StringBuilder();
mess2.AppendLine(GetString($"[i:3455][c/AD89D5:自][c/D68ACA:动][c/DF909A:钓][c/E5A894:鱼][i:3454]"));
mess2.AppendLine(GetString($"以下玩家超过 [c/E17D8C:{Config.timer}] 分钟 已关闭[c/76D5B4:自动钓鱼]权限:"));
mess2.AppendLine(GetString($"以下玩家超过 [c/E17D8C:{Configuration.Instance.timer}] 分钟 已关闭[c/76D5B4:自动钓鱼]权限:"));

// 只显示分钟
var Minutes = (DateTime.Now - data.LogTime).TotalMinutes;

// 时间过期 关闭自动钓鱼权限
if (Minutes >= Config.timer)
if (Minutes >= Configuration.Instance.timer)
{
ClearCount++;
data.Mod = false;
Expand All @@ -428,12 +411,6 @@ private static void ExitMod(TSPlayer plr, MyData.ItemData data)
// 确保有一个玩家计数,只播报一次
if (ClearCount > 0 && mess2.Length > 0)
{
//广告开关
if (Config.AdvertisementEnabled)
{
//自定义广告内容
mess2.AppendLine(Config.Advertisement);
}
plr.SendMessage(mess2.ToString(), 247, 244, 150);
ClearCount = 0;
}
Expand Down
4 changes: 4 additions & 0 deletions src/AutoFish/AutoFish.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@

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

<ItemGroup>
<ProjectReference Include="..\LazyAPI\LazyAPI.csproj" />
</ItemGroup>

</Project>
Loading