Skip to content

Commit

Permalink
Made ExampleExplosive count for Demolitionist Spawn
Browse files Browse the repository at this point in the history
Added Hook so ExampleExplosive counts for demolitionist spawn
  • Loading branch information
chloenaut committed Apr 30, 2020
1 parent c3d5dd9 commit fb66c0e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
4 changes: 3 additions & 1 deletion ExampleMod/ExamplePlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class ExamplePlayer : ModPlayer
public bool exampleLightPet;
public bool exampleShield;
public bool infinity;
public bool exampleExplosive;
public bool strongBeesUpgrade;
public bool manaHeart;
public int manaHeartCounter;
Expand Down Expand Up @@ -78,7 +79,8 @@ public override void ResetEffects() {
exampleLightPet = false;
exampleShield = false;
infinity = false;
strongBeesUpgrade = false;
strongBeesUpgrade = false;
exampleExplosive = false;
if (!manaHeart) {
manaHeartCounter = 0;
}
Expand Down
43 changes: 42 additions & 1 deletion ExampleMod/Items/Weapons/ExampleExplosive.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,45 @@
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
using MonoMod.Cil;
using static Mono.Cecil.Cil.OpCodes;
using static Terraria.ModLoader.ModContent;

namespace ExampleMod.Items.Weapons
{
internal class ExampleExplosive : ModItem
{
// TODO, count as explosive for demolitionist spawn
public override bool Autoload(ref string name)
{
IL.Terraria.Main.UpdateTime_SpawnTownNPCs += HookUpdateTime_SpawnTownNPCs;
return base.Autoload(ref name);
}

private void HookUpdateTime_SpawnTownNPCs(ILContext il)
{
ILCursor cursor = new ILCursor(il);
//try to find where the item id for the Bomb is pushed onto stack
if (!cursor.TryGotoNext(MoveType.Before,
i => i.MatchLdcI4(166)))
return;
//move to the start of if statement
cursor.Index -= 7;
//We need to create another cursor to jump to when our check is true
ILCursor retCursor = cursor.Clone();

//Here we have to push the player field onto the eval stack
cursor.Emit(Ldsfld, typeof(Main).GetField(nameof(Main.player)));
cursor.Emit(Ldloc, 37);
cursor.Emit(Ldelem_Ref);
//Here we check if the player at the current index has ExampleExplosive
cursor.EmitDelegate<Func<Player, bool>>(player =>
{
return player.GetModPlayer<ExamplePlayer>().exampleExplosive;
});
//We have to find where the flag is set to true so we can jump to it
retCursor.TryGotoNext(i => i.Match(Ldc_I4_1));
cursor.Emit(Brtrue, retCursor.MarkLabel());
}

public override void SetStaticDefaults() {
base.SetStaticDefaults();
Expand Down Expand Up @@ -39,5 +71,14 @@ public override void AddRecipes() {
recipe.SetResult(this);
recipe.AddRecipe();
}

public override void UpdateInventory(Player player)
{
if (player.HasItem(ModContent.ItemType<ExampleExplosive>()))
{
player.GetModPlayer<ExamplePlayer>().exampleExplosive = true;
}
base.UpdateInventory(player);
}
}
}

0 comments on commit fb66c0e

Please sign in to comment.