From 9515325b01e0d9c94b269a5b902e7989f18fcabe Mon Sep 17 00:00:00 2001 From: direwolf420 Date: Mon, 27 Jun 2022 22:48:23 +0200 Subject: [PATCH] Add ModPlayer.ModifyFishingAttempt hook (#2623) * give ExampleBuffPotion a DrinkParticleColors entry * ModPlayer.ModifyFishingAttempt hook * ExampleMod: new buff/potion for ModifyFishingAttempt --- .../Common/Players/ExampleFishingPlayer.cs | 20 ++++++- ExampleMod/Content/Buffs/ExampleCrateBuff.cs | 19 +++++++ ExampleMod/Content/Buffs/ExampleCrateBuff.png | Bin 0 -> 505 bytes .../Items/Consumables/ExampleBuffPotion.cs | 8 +++ .../Items/Consumables/ExampleCratePotion.cs | 49 ++++++++++++++++++ .../Items/Consumables/ExampleCratePotion.png | Bin 0 -> 366 bytes .../Terraria/ModLoader/ModPlayer.cs | 11 +++- .../Terraria/ModLoader/PlayerLoader.cs | 11 ++++ .../tModLoader/Terraria/Projectile.cs.patch | 7 ++- 9 files changed, 122 insertions(+), 3 deletions(-) create mode 100644 ExampleMod/Content/Buffs/ExampleCrateBuff.cs create mode 100644 ExampleMod/Content/Buffs/ExampleCrateBuff.png create mode 100644 ExampleMod/Content/Items/Consumables/ExampleCratePotion.cs create mode 100644 ExampleMod/Content/Items/Consumables/ExampleCratePotion.png diff --git a/ExampleMod/Common/Players/ExampleFishingPlayer.cs b/ExampleMod/Common/Players/ExampleFishingPlayer.cs index cc37b368857..9e07a15b165 100644 --- a/ExampleMod/Common/Players/ExampleFishingPlayer.cs +++ b/ExampleMod/Common/Players/ExampleFishingPlayer.cs @@ -9,8 +9,25 @@ namespace ExampleMod.Common.Players { + // This class showcases things you can do with fishing public class ExampleFishingPlayer : ModPlayer { + public bool hasExampleCrateBuff; + + public override void ResetEffects() { + hasExampleCrateBuff = false; + } + + public override void ModifyFishingAttempt(ref FishingAttempt attempt) { + // If the player has the Example Crate buff (given by Example Crate Potion), 10% additional chance that the catch will be a crate + // The "tier" of the crate depends on the rarity, which we don't modify here, see the comments in CatchFish for details + if (hasExampleCrateBuff && !attempt.crate) { + if (Main.rand.Next(100) < 10) { + attempt.crate = true; + } + } + } + public override void CatchFish(FishingAttempt attempt, ref int itemDrop, ref int npcSpawn, ref AdvancedPopupRequest sonar, ref Vector2 sonarPosition) { bool inWater = !attempt.inLava && !attempt.inHoney; bool inExampleSurfaceBiome = Player.InModBiome(); @@ -43,7 +60,8 @@ public override void CatchFish(FishingAttempt attempt, ref int itemDrop, ref int // We don't want to replace golden/titanium crates (the highest tier crates), as they take highest priority in crate catches // Their drop conditions are "veryrare" or "legendary" // (After that come biome crates ("rare"), then iron/mythril ("uncommon"), then wood/pearl (none of the previous)) - if (!attempt.veryrare && !attempt.legendary && attempt.rare) { + // Let's replace biome crates 50% of the time (player could be in multiple (modded) biomes, we should respect that) + if (!attempt.veryrare && !attempt.legendary && attempt.rare && Main.rand.NextBool()) { itemDrop = ModContent.ItemType(); } } diff --git a/ExampleMod/Content/Buffs/ExampleCrateBuff.cs b/ExampleMod/Content/Buffs/ExampleCrateBuff.cs new file mode 100644 index 00000000000..b5391c9905f --- /dev/null +++ b/ExampleMod/Content/Buffs/ExampleCrateBuff.cs @@ -0,0 +1,19 @@ +using ExampleMod.Common.Players; +using Terraria; +using Terraria.ModLoader; + +namespace ExampleMod.Content.Buffs +{ + public class ExampleCrateBuff : ModBuff + { + public override void SetStaticDefaults() { + DisplayName.SetDefault("Example Crate"); + Description.SetDefault("Greater chance of fishing up a crate"); + } + + public override void Update(Player player, ref int buffIndex) { + // Use a ModPlayer to keep track of the buff being active + player.GetModPlayer().hasExampleCrateBuff = true; + } + } +} diff --git a/ExampleMod/Content/Buffs/ExampleCrateBuff.png b/ExampleMod/Content/Buffs/ExampleCrateBuff.png new file mode 100644 index 0000000000000000000000000000000000000000..48dc028604036fb950e31eb3627ef350eaf16221 GIT binary patch literal 505 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE1|*BCs=fdzwj^(N7l!{JxM1({$v_eI5>H=O z_GfI80z%qp-==0VFfh*cba4!cIGmi~(D0w<3u_M#5cu#g^GGDx+5i3~&)m}Hk&?5g zIs7qCy{L%Y##g4O^1n=2HJd-jfky(* zKC^JMvVnjiqa2vFlk2$TAOCNGM1vmRcZ4%m(uhUt-ZY zcd?c)xSc`1&BL-G&U@i=CT`x0yB}DEzN_ciI0UxO^S*GIN$)dzd~Qda+<_WC?Z=6V zO>);?*uAT$kUR9CH6Zh3lYsp1xpFaz4Ee%FrS})Duw`Iow7$OhLoI)bkpU3ol+EbN z`uwopd5KKJ)SvI|9!yF<5cU6Xjh}`Jo4?eI(%14?tHp20DTu~A*lWM#62sXlxBq4{ oldhJR9sYgrs&SIc$Gyo6FaNFe4YzQ*4~#hmPgg&ebxsLQ00@o3f&c&j literal 0 HcmV?d00001 diff --git a/ExampleMod/Content/Items/Consumables/ExampleBuffPotion.cs b/ExampleMod/Content/Items/Consumables/ExampleBuffPotion.cs index fd68fc920fd..23df9f07abd 100644 --- a/ExampleMod/Content/Items/Consumables/ExampleBuffPotion.cs +++ b/ExampleMod/Content/Items/Consumables/ExampleBuffPotion.cs @@ -2,6 +2,7 @@ using Terraria.ID; using Terraria.GameContent.Creative; using Terraria.ModLoader; +using Microsoft.Xna.Framework; namespace ExampleMod.Content.Items.Consumables { @@ -11,6 +12,13 @@ public override void SetStaticDefaults() { Tooltip.SetDefault("Gives a light defense buff."); CreativeItemSacrificesCatalog.Instance.SacrificeCountNeededByItemId[Type] = 20; + + // Dust that will appear in these colors when the item with ItemUseStyleID.DrinkLiquid is used + ItemID.Sets.DrinkParticleColors[Type] = new Color[3] { + new Color(240, 240, 240), + new Color(200, 200, 200), + new Color(140, 140, 140) + }; } public override void SetDefaults() { diff --git a/ExampleMod/Content/Items/Consumables/ExampleCratePotion.cs b/ExampleMod/Content/Items/Consumables/ExampleCratePotion.cs new file mode 100644 index 00000000000..a7965baa10a --- /dev/null +++ b/ExampleMod/Content/Items/Consumables/ExampleCratePotion.cs @@ -0,0 +1,49 @@ +using Terraria; +using Terraria.ID; +using Terraria.GameContent.Creative; +using Terraria.ModLoader; +using Microsoft.Xna.Framework; + +namespace ExampleMod.Content.Items.Consumables +{ + public class ExampleCratePotion : ModItem + { + public override void SetStaticDefaults() { + Tooltip.SetDefault("Increases chance to get a crate" + + "\nStacks with {$ItemName.CratePotion}"); + + CreativeItemSacrificesCatalog.Instance.SacrificeCountNeededByItemId[Type] = 20; + + // Dust that will appear in these colors when the item with ItemUseStyleID.DrinkLiquid is used + ItemID.Sets.DrinkParticleColors[Type] = new Color[3] { + new Color(240, 240, 240), + new Color(200, 200, 200), + new Color(140, 140, 140) + }; + } + + public override void SetDefaults() { + Item.width = 20; + Item.height = 26; + Item.useStyle = ItemUseStyleID.DrinkLiquid; + Item.useAnimation = 15; + Item.useTime = 15; + Item.useTurn = true; + Item.UseSound = SoundID.Item3; + Item.maxStack = 30; + Item.consumable = true; + Item.rare = ItemRarityID.Green; + Item.value = Item.buyPrice(silver: 8); + Item.buffType = ModContent.BuffType(); // Specify an existing buff to be applied when used. + Item.buffTime = 3 * 60 * 60; // The amount of time the buff declared in Item.buffType will last in ticks. Set to 3 minutes, as 60 ticks = 1 second. + } + + // Please see Content/ExampleRecipes.cs for a detailed explanation of recipe creation. + public override void AddRecipes() { + CreateRecipe() + .AddIngredient(ItemID.CratePotion, 4) + .AddTile(TileID.CrystalBall) + .Register(); + } + } +} diff --git a/ExampleMod/Content/Items/Consumables/ExampleCratePotion.png b/ExampleMod/Content/Items/Consumables/ExampleCratePotion.png new file mode 100644 index 0000000000000000000000000000000000000000..816b3468de5415f3e4b66cb631cd8ca88fcf1af2 GIT binary patch literal 366 zcmV-!0g?WRP)PIz+_s$-#bL_v@|v~* ztR9aNtZ1AJXdhszzPAF=y+JO;?Naq0gEazC_0d2_bF1y8TwdLz9DH3c<77ZP0y`pA z=NqjWfv9>2I+!1&@8b0QBGth=o6S=l>VqWG8kTdu8kTeQ0=!<{rW|jZ_fK)ZUZ<~u zpW5K>fTn0CY>&)G!wDIF4RrlpDsVfQjnsE01ET*1G>OTmxnxA7A;WSYTv`lusYuAZfL{0Ob;Rez*)|Pyhe` M07*qoM6N<$g6JikasU7T literal 0 HcmV?d00001 diff --git a/patches/tModLoader/Terraria/ModLoader/ModPlayer.cs b/patches/tModLoader/Terraria/ModLoader/ModPlayer.cs index ea6ae2e4401..16f0f4d80f1 100644 --- a/patches/tModLoader/Terraria/ModLoader/ModPlayer.cs +++ b/patches/tModLoader/Terraria/ModLoader/ModPlayer.cs @@ -714,7 +714,16 @@ public virtual void OnHitByProjectile(Projectile proj, int damage, bool crit) { } /// - /// Allows you to change the item or enemy the player gets when sucessfully catching a "fish". The Fishing Attempt structure contains most information about the vanilla event, including the Item Rod and Bait used by the player, the liquid it is being fished on, and so on. + /// Allows you to change information about the ongoing fishing attempt before cought items/NPCs are decided, after all vanilla information has been gathered. + ///
Will not be called if various conditions for getting a catch aren't met, meaning you can't modify those. + ///
Setting or is not allowed and will be reset, use for that. + ///
+ /// The structure containing most data from the vanilla fishing attempt + public virtual void ModifyFishingAttempt(ref FishingAttempt attempt) { + } + + /// + /// Allows you to change the item or enemy the player gets when sucessfully catching an item or NPC. The Fishing Attempt structure contains most information about the vanilla event, including the Item Rod and Bait used by the player, the liquid it is being fished on, and so on. /// The Sonar and Sonar position fields allow you to change the text, color, velocity and position of the catch's name (be it item or NPC) freely /// /// The structure containing most data from the vanilla fishing attempt diff --git a/patches/tModLoader/Terraria/ModLoader/PlayerLoader.cs b/patches/tModLoader/Terraria/ModLoader/PlayerLoader.cs index 81abfdd5923..67f1ffecf2a 100644 --- a/patches/tModLoader/Terraria/ModLoader/PlayerLoader.cs +++ b/patches/tModLoader/Terraria/ModLoader/PlayerLoader.cs @@ -824,6 +824,17 @@ public static void OnHitByProjectile(Player player, Projectile proj, int damage, } } + private delegate void DelegateModifyFishingAttempt(ref FishingAttempt attempt); + private static HookList HookModifyFishingAttempt = AddHook(p => p.ModifyFishingAttempt); + + public static void ModifyFishingAttempt(Player player, ref FishingAttempt attempt) { + foreach (int index in HookModifyFishingAttempt.arr) { + player.modPlayers[index].ModifyFishingAttempt(ref attempt); + } + + attempt.rolledItemDrop = attempt.rolledEnemySpawn = 0; // Reset, modders need to use CatchFish for this + } + private delegate void DelegateCatchFish(FishingAttempt attempt, ref int itemDrop, ref int enemySpawn, ref AdvancedPopupRequest sonar, ref Vector2 sonarPosition); private static HookList HookCatchFish = AddHook(p => p.CatchFish); diff --git a/patches/tModLoader/Terraria/Projectile.cs.patch b/patches/tModLoader/Terraria/Projectile.cs.patch index 29ad2cb702c..ac7c993a17b 100644 --- a/patches/tModLoader/Terraria/Projectile.cs.patch +++ b/patches/tModLoader/Terraria/Projectile.cs.patch @@ -797,7 +797,12 @@ if (wet) position += wetVelocity; else -@@ -13443,8 +_,17 @@ +@@ -13439,12 +_,22 @@ + fisher.heightLevel = 4; + + FishingCheck_RollDropLevels(fisher.fishingLevel, out fisher.common, out fisher.uncommon, out fisher.rare, out fisher.veryrare, out fisher.legendary, out fisher.crate); ++ PlayerLoader.ModifyFishingAttempt(Main.player[owner], ref fisher); + FishingCheck_ProbeForQuestFish(ref fisher); FishingCheck_RollEnemySpawns(ref fisher); FishingCheck_RollItemDrop(ref fisher); bool flag = false;