Skip to content

Commit

Permalink
Merge branch '1.4_useitemfix' into 1.4_mergedtesting
Browse files Browse the repository at this point in the history
# Conflicts:
#	patches/tModLoader/Terraria/ModLoader/ItemLoader.cs
  • Loading branch information
Mirsario committed Jan 15, 2021
2 parents bf9a8a5 + 84250ff commit 15f9974
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 22 deletions.
2 changes: 1 addition & 1 deletion ExampleMod/Content/Items/Consumables/ExampleLifeFruit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public override bool CanUseItem(Player player) {
return player.statLifeMax == 500 && player.GetModPlayer<ExampleLifeFruitPlayer>().exampleLifeFruits < MaxExampleLifeFruits;
}

public override bool UseItem(Player player) {
public override bool? UseItem(Player player) {
// Do not do this: player.statLifeMax += 2;
player.statLifeMax2 += LifePerFruit;
player.statLife += LifePerFruit;
Expand Down
2 changes: 1 addition & 1 deletion ExampleMod/Content/Items/Consumables/PlanteraItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public override bool CanUseItem(Player player) {
return Main.hardMode && NPC.downedMechBoss1 && NPC.downedMechBoss2 && NPC.downedMechBoss3 && !NPC.AnyNPCs(NPCID.Plantera);
}

public override bool UseItem(Player player) {
public override bool? UseItem(Player player) {
if (player.whoAmI == Main.myPlayer) {
//If the player using the item is the client
//(explicitely excluded serverside here)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public override void SetDefaults() {
Item.rare = 10;
}

public override bool UseItem(Player player) {
public override bool? UseItem(Player player) {
const int time = 3600 * 60;
player.AddBuff(BuffID.Wet, time);
player.AddBuff(BuffID.Lovestruck, time);
Expand Down
6 changes: 3 additions & 3 deletions patches/tModLoader/Terraria/ModLoader/GlobalItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,10 @@ public virtual void OnHitPvp(Item item, Player player, Player target, int damage
}

/// <summary>
/// Allows you to make things happen when an item is used. Return true if using the item actually does stuff. Returns false by default.
/// Allows you to make things happen when an item is used. Return true if using the item actually does stuff. Returns null by default.
/// </summary>
public virtual bool UseItem(Item item, Player player) {
return false;
public virtual bool? UseItem(Item item, Player player) {
return null;
}

/// <summary>
Expand Down
25 changes: 16 additions & 9 deletions patches/tModLoader/Terraria/ModLoader/ItemLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -728,23 +728,30 @@ public static void OnHitPvp(Item item, Player player, Player target, int damage,
g.OnHitPvp(item, player, target, damage, crit);
}

private static HookList HookUseItem = AddHook<Func<Item, Player, bool>>(g => g.UseItem);
private static HookList HookUseItem = AddHook<Func<Item, Player, bool?>>(g => g.UseItem);
/// <summary>
/// Returns true if any of ModItem.UseItem or GlobalItem.UseItem return true
/// Returns false if any of ModItem.UseItem or GlobalItem.UseItem return false.
/// Returns true if anything returns true without returning false.
/// Returns null by default.
/// Does not fail fast (calls every hook)
/// </summary>
public static bool UseItem(Item item, Player player) {
public static bool? UseItem(Item item, Player player) {
if (item.IsAir)
return false;

bool flag = false;
if (item.ModItem != null)
flag |= item.ModItem.UseItem(player);
bool? result = null;

foreach (var g in HookUseItem.Enumerate(item))
flag |= g.UseItem(item, player);
foreach (var g in HookUseItem.Enumerate(item)) {
bool? useItem = g.UseItem(item, player);

return flag;
if (useItem.HasValue && result != false) {
result = useItem.Value;
}
}

bool? modItemResult = item.ModItem?.UseItem(player);

return result ?? modItemResult;
}

private static HookList HookConsumeItem = AddHook<Func<Item, Player, bool>>(g => g.ConsumeItem);
Expand Down
4 changes: 2 additions & 2 deletions patches/tModLoader/Terraria/ModLoader/ModItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,8 @@ public virtual void OnHitPvp(Player player, Player target, int damage, bool crit
/// </summary>
/// <param name="player">The player.</param>
/// <returns></returns>
public virtual bool UseItem(Player player) {
return false;
public virtual bool? UseItem(Player player) {
return null;
}

/// <summary>
Expand Down
19 changes: 14 additions & 5 deletions patches/tModLoader/Terraria/Player.cs.patch
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,26 @@
public static int defaultItemGrabRange = 42;
private static float itemGrabSpeed = 0.45f;
private static float itemGrabSpeedMax = 4f;
@@ -2051,11 +_,13 @@
@@ -2050,12 +_,20 @@
itemTimeMax = frames;
}

public void ApplyItemTime(Item sItem) {
- public void ApplyItemTime(Item sItem) {
- SetItemTime(sItem.useTime);
+ public void ApplyItemTime(Item sItem, bool? callUseItem = null) {
+ if ((callUseItem ?? ItemTimeIsZero) && ItemLoader.UseItem(sItem, this) == false)
+ return;
+
+ int totalUseTime = PlayerHooks.TotalUseTime(sItem.useTime, this, sItem);
+ SetItemTime(totalUseTime);
}

public void ApplyItemTime(Item sItem, float multiplier) {
- public void ApplyItemTime(Item sItem, float multiplier) {
- SetItemTime((int)((float)sItem.useTime * multiplier));
+ public void ApplyItemTime(Item sItem, float multiplier, bool? callUseItem = null) {
+ if ((callUseItem ?? ItemTimeIsZero) && ItemLoader.UseItem(sItem, this) == false)
+ return;
+
+ int totalUseTime = PlayerHooks.TotalUseTime((int)(sItem.useTime * multiplier), this, sItem);
+ SetItemTime(totalUseTime);
}
Expand Down Expand Up @@ -2896,8 +2905,8 @@
}

if (ItemTimeIsZero && itemAnimation > 0) {
+ if (ItemLoader.UseItem(item, this))
+ ApplyItemTime(item);
+ if (ItemLoader.UseItem(item, this) == true)
+ ApplyItemTime(item, false);
+
if (item.hairDye >= 0) {
ApplyItemTime(item);
Expand Down

0 comments on commit 15f9974

Please sign in to comment.