Skip to content

Commit

Permalink
改为GetDataHander.PlayerUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
THEXN committed Apr 10, 2024
1 parent 2b21313 commit 2cb79ba
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 52 deletions.
16 changes: 8 additions & 8 deletions LifemaxExtra/LifemaxExtra.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="TShock" Version="5.2.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="TShock" Version="5.2.0" />
</ItemGroup>

</Project>
86 changes: 42 additions & 44 deletions LifemaxExtra/PluginContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using TerrariaApi.Server;
using TShockAPI;
using TShockAPI.Hooks;
using static TShockAPI.GetDataHandlers;

namespace LifemaxExtra
{
Expand All @@ -11,7 +12,7 @@ public class LifemaxExtra : TerrariaPlugin
public override string Author => "佚名,肝帝熙恩添加自定义";
public override string Description => "提升生命值上限";
public override string Name => "LifemaxExtra";
public override Version Version => new Version(1, 0, 0, 5);
public override Version Version => new Version(1, 0, 0, 6);
public static Configuration Config;

Check warning on line 16 in LifemaxExtra/PluginContainer.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'Config' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 16 in LifemaxExtra/PluginContainer.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'Config' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
private bool[] controlUseItemOld;
private int[] itemUseTime;
Expand All @@ -38,7 +39,7 @@ private static void ReloadConfig(ReloadEventArgs args)
public override void Initialize()
{
GeneralHooks.ReloadEvent += ReloadConfig;
ServerApi.Hooks.GameUpdate.Register(this, new HookHandler<EventArgs>(this.OnUpdate));
GetDataHandlers.PlayerUpdate += OnPlayerUpdate;

Check warning on line 42 in LifemaxExtra/PluginContainer.cs

View workflow job for this annotation

GitHub Actions / build

Nullability of reference types in type of parameter 'sender' of 'void LifemaxExtra.OnPlayerUpdate(object sender, PlayerUpdateEventArgs args)' doesn't match the target delegate 'EventHandler<GetDataHandlers.PlayerUpdateEventArgs>' (possibly because of nullability attributes).

Check warning on line 42 in LifemaxExtra/PluginContainer.cs

View workflow job for this annotation

GitHub Actions / build

Nullability of reference types in type of parameter 'sender' of 'void LifemaxExtra.OnPlayerUpdate(object sender, PlayerUpdateEventArgs args)' doesn't match the target delegate 'EventHandler<GetDataHandlers.PlayerUpdateEventArgs>' (possibly because of nullability attributes).
PlayerHooks.PlayerPostLogin += OnPlayerPostLogin;
}

Expand All @@ -48,7 +49,7 @@ protected override void Dispose(bool disposing)
{
GeneralHooks.ReloadEvent -= ReloadConfig;
PlayerHooks.PlayerPostLogin -= OnPlayerPostLogin;
ServerApi.Hooks.GameUpdate.Deregister(this, new HookHandler<EventArgs>(this.OnUpdate));
GetDataHandlers.PlayerUpdate -= OnPlayerUpdate;

Check warning on line 52 in LifemaxExtra/PluginContainer.cs

View workflow job for this annotation

GitHub Actions / build

Nullability of reference types in type of parameter 'sender' of 'void LifemaxExtra.OnPlayerUpdate(object sender, PlayerUpdateEventArgs args)' doesn't match the target delegate 'EventHandler<GetDataHandlers.PlayerUpdateEventArgs>' (possibly because of nullability attributes).

Check warning on line 52 in LifemaxExtra/PluginContainer.cs

View workflow job for this annotation

GitHub Actions / build

Nullability of reference types in type of parameter 'sender' of 'void LifemaxExtra.OnPlayerUpdate(object sender, PlayerUpdateEventArgs args)' doesn't match the target delegate 'EventHandler<GetDataHandlers.PlayerUpdateEventArgs>' (possibly because of nullability attributes).
}
base.Dispose(disposing);
}
Expand Down Expand Up @@ -79,64 +80,61 @@ private static void CheckAndSetPlayerHealth(TSPlayer tsplayer)
}
}

private void OnUpdate(EventArgs args)
private void OnPlayerUpdate(object sender, PlayerUpdateEventArgs args)
{
foreach (TSPlayer tsplayer in TShock.Players)
TSPlayer tsplayer = TShock.Players[args.PlayerId];
if (tsplayer != null)
{
int index = tsplayer.Index;
Player tplayer = tsplayer.TPlayer;
Item heldItem = tplayer.HeldItem;

if (!(tsplayer == null))
if (!this.controlUseItemOld[index] && tsplayer.TPlayer.controlUseItem && this.itemUseTime[index] <= 0)
{
int index = tsplayer.Index;
Player tplayer = tsplayer.TPlayer;
Item heldItem = tplayer.HeldItem;
int useTime = heldItem.useTime; // 获取物品使用时间
int type = heldItem.type; // 获取物品类型

if (!this.controlUseItemOld[index] && tsplayer.TPlayer.controlUseItem && this.itemUseTime[index] <= 0)
if (type != 29) // 如果物品不是 ID 为 29 的物品
{
int useTime = heldItem.useTime; // 获取物品使用时间
int type = heldItem.type; // 获取物品类型

if (type != 29) // 如果物品不是 ID 为 29 的物品
if (type == 1291) // 如果物品是 ID 为 1291 的物品(Life Fruit)
{
if (type == 1291) // 如果物品是 ID 为 1291 的物品(Life Fruit)
if (tplayer.statLifeMax >= Config.LifeCrystalMaxLife) // 如果玩家的生命上限大于等于配置的最大水晶生命值
{
if (tplayer.statLifeMax >= Config.LifeCrystalMaxLife) // 如果玩家的生命上限大于等于配置的最大水晶生命值
if (tsplayer.TPlayer.statLifeMax < Config.LifeFruitMaxLife) // 如果玩家当前生命上限小于配置的最大果实生命值
{
tsplayer.TPlayer.inventory[tsplayer.TPlayer.selectedItem].stack--; // 减少玩家背包中选定物品的堆叠数量
tsplayer.SendData(PacketTypes.PlayerSlot, "", index, (float)tplayer.selectedItem); // 更新客户端的选定物品槽位
tplayer.statLifeMax += 5; // 增加玩家的生命上限
tsplayer.SendData(PacketTypes.PlayerHp, "", index); // 更新客户端的生命值显示
}
else if (tsplayer.TPlayer.statLifeMax > Config.LifeFruitMaxLife) // 如果玩家当前生命上限大于配置的最大果实生命值
{
if (tsplayer.TPlayer.statLifeMax < Config.LifeFruitMaxLife) // 如果玩家当前生命上限小于配置的最大果实生命值
{
tsplayer.TPlayer.inventory[tsplayer.TPlayer.selectedItem].stack--; // 减少玩家背包中选定物品的堆叠数量
tsplayer.SendData(PacketTypes.PlayerSlot, "", index, (float)tplayer.selectedItem); // 更新客户端的选定物品槽位
tplayer.statLifeMax += 5; // 增加玩家的生命上限
tsplayer.SendData(PacketTypes.PlayerHp, "", index); // 更新客户端的生命值显示
}
else if (tsplayer.TPlayer.statLifeMax > Config.LifeFruitMaxLife) // 如果玩家当前生命上限大于配置的最大果实生命值
{
tplayer.statLifeMax = Config.LifeFruitMaxLife; // 将玩家的生命上限设置为配置的最大果实生命值
tsplayer.SendData(PacketTypes.PlayerHp, "", index); // 更新客户端的生命值显示
}
tplayer.statLifeMax = Config.LifeFruitMaxLife; // 将玩家的生命上限设置为配置的最大果实生命值
tsplayer.SendData(PacketTypes.PlayerHp, "", index); // 更新客户端的生命值显示
}
}
}
else // 如果物品是 ID 为 29 的物品(Life Crystal)
}
else // 如果物品是 ID 为 29 的物品(Life Crystal)
{
if (tplayer.statLifeMax <= Config.LifeCrystalMaxLife) // 如果玩家的生命上限小于等于最大水晶生命值
{
if (tplayer.statLifeMax <= Config.LifeCrystalMaxLife) // 如果玩家的生命上限小于等于最大水晶生命值
{
if (tsplayer.TPlayer.statLifeMax < Config.LifeCrystalMaxLife) // 如果玩家当前生命上限小于配置的最大水晶生命值
{
tsplayer.TPlayer.inventory[tplayer.selectedItem].stack--; // 减少玩家背包中选定物品的堆叠数量
tsplayer.SendData(PacketTypes.PlayerSlot, "", index, (float)tplayer.selectedItem); // 更新客户端的选定物品槽位
tplayer.statLifeMax += 20; // 增加玩家的生命上限
tsplayer.SendData(PacketTypes.PlayerHp, "", index); // 更新客户端的生命值显示
}
else if (tsplayer.TPlayer.statLifeMax > Config.LifeFruitMaxLife) // 如果玩家当前生命上限大于配置的最大果生命值
{
tplayer.statLifeMax = Config.LifeFruitMaxLife; // 将玩家的生命上限设置为配置的最大生命果生命值
tsplayer.SendData(PacketTypes.PlayerHp, "", index); // 更新客户端的生命值显示
}
if (tsplayer.TPlayer.statLifeMax < Config.LifeCrystalMaxLife) // 如果玩家当前生命上限小于配置的最大水晶生命值
{
tsplayer.TPlayer.inventory[tplayer.selectedItem].stack--; // 减少玩家背包中选定物品的堆叠数量
tsplayer.SendData(PacketTypes.PlayerSlot, "", index, (float)tplayer.selectedItem); // 更新客户端的选定物品槽位
tplayer.statLifeMax += 20; // 增加玩家的生命上限
tsplayer.SendData(PacketTypes.PlayerHp, "", index); // 更新客户端的生命值显示
}
else if (tsplayer.TPlayer.statLifeMax > Config.LifeFruitMaxLife) // 如果玩家当前生命上限大于配置的最大果生命值
{
tplayer.statLifeMax = Config.LifeFruitMaxLife; // 将玩家的生命上限设置为配置的最大生命果生命值
tsplayer.SendData(PacketTypes.PlayerHp, "", index); // 更新客户端的生命值显示
}
}
}
this.controlUseItemOld[index] = tsplayer.TPlayer.controlUseItem;
}
this.controlUseItemOld[index] = tsplayer.TPlayer.controlUseItem;
}
}
}
Expand Down

0 comments on commit 2cb79ba

Please sign in to comment.