Skip to content

Commit

Permalink
amf, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
aedenthorn committed Feb 17, 2021
1 parent bef0110 commit fe19343
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 93 deletions.
69 changes: 11 additions & 58 deletions AdvancedMeleeFramework/AMFPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,61 +19,12 @@ public static void Initialize(ModEntry modEntry)
}
public static void MeleeWeapon_Postfix(MeleeWeapon __instance)
{
//context.Monitor.Log($"created melee weapon {__instance.Name} {__instance.InitialParentTileIndex} {__instance.ParentSheetIndex}");
//context.Monitor.Log($"0 created melee weapon {__instance.Name} {__instance.InitialParentTileIndex} {__instance.ParentSheetIndex} {Environment.StackTrace}");

AdvancedMeleeWeapon amw = ModEntry.GetAdvancedWeapon(__instance, null);
if (amw != null)
{
int scount = 0;
foreach (AdvancedEnchantmentData aed in amw.enchantments)
{
switch (aed.type)
{
case "vampiric":
__instance.enchantments.Add(new VampiricEnchantment());
break;
case "jade":
__instance.enchantments.Add(new JadeEnchantment());
break;
case "aquamarine":
__instance.enchantments.Add(new AquamarineEnchantment());
break;
case "topaz":
__instance.enchantments.Add(new TopazEnchantment());
break;
case "amethyst":
__instance.enchantments.Add(new AmethystEnchantment());
break;
case "ruby":
__instance.enchantments.Add(new RubyEnchantment());
break;
case "emerald":
__instance.enchantments.Add(new EmeraldEnchantment());
break;
case "haymaker":
__instance.enchantments.Add(new HaymakerEnchantment());
break;
case "bugkiller":
__instance.enchantments.Add(new BugKillerEnchantment());
break;
case "crusader":
__instance.enchantments.Add(new CrusaderEnchantment());
break;
case "magic":
__instance.enchantments.Add(new MagicEnchantment());
break;
default:
BaseWeaponEnchantment we = new BaseWeaponEnchantment();
string key = aed.name;
context.Helper.Reflection.GetField<string>(we, "_displayName").SetValue(key);
__instance.enchantments.Add(we);
break;
}
scount++;
context.Monitor.Log($"added enchantment {aed.type} to {__instance.Name} {__instance.enchantments.Count}");
}
}
ModEntry.AddEnchantments(__instance);
}


public static bool doAnimateSpecialMove_Prefix(MeleeWeapon __instance, Farmer ___lastUser)
{
context.Monitor.Log($"Special move for {__instance.Name}, id {__instance.InitialParentTileIndex}");
Expand Down Expand Up @@ -154,13 +105,14 @@ public static void drawInMenu_Postfix(MeleeWeapon __instance, int __state)

public static bool _OnDealDamage_Prefix(BaseEnchantment __instance, string ____displayName, Monster monster, GameLocation location, Farmer who, ref int amount)
{
if (!(__instance is BaseWeaponEnchantment) || ____displayName == null || !ModEntry.advancedEnchantments.ContainsKey(____displayName))
if (!(__instance is BaseWeaponEnchantment) || ____displayName == null || ____displayName == "" || !ModEntry.advancedEnchantments.ContainsKey(____displayName) || (ModEntry.EnchantmentTriggers.ContainsKey(who.uniqueMultiplayerID + ____displayName) && ModEntry.EnchantmentTriggers[who.uniqueMultiplayerID + ____displayName] == Game1.ticks))
return true;
AdvancedEnchantmentData enchantment = ModEntry.advancedEnchantments[____displayName];

if (enchantment.parameters["trigger"] == "damage" || (enchantment.parameters["trigger"] == "crit" && amount > (who.CurrentTool as MeleeWeapon).maxDamage))
if (enchantment.parameters["trigger"] == "damage" || (enchantment.parameters["trigger"] == "crit" && amount > (who.CurrentTool as MeleeWeapon).maxDamage) && !Environment.StackTrace.Contains("OnCalculateDamage"))
{
context.Monitor.Log($"Triggered enchantment {enchantment.name} on {enchantment.parameters["trigger"]}");
context.Monitor.Log($"Triggered enchantment {enchantment.name} on {enchantment.parameters["trigger"]} {amount} {(who.CurrentTool as MeleeWeapon).enchantments.Count}");
ModEntry.EnchantmentTriggers[who.uniqueMultiplayerID + ____displayName] = Game1.ticks;
if (enchantment.type == "heal")
{
if (Game1.random.NextDouble() < float.Parse(enchantment.parameters["chance"]) / 100f)
Expand All @@ -182,18 +134,19 @@ public static bool _OnDealDamage_Prefix(BaseEnchantment __instance, string ____d
if (enchantment.parameters.ContainsKey("sound"))
Game1.playSound(enchantment.parameters["sound"]);
}
}
}
}
return false;
}
public static bool _OnMonsterSlay_Prefix(BaseEnchantment __instance, string ____displayName, Monster m, GameLocation location, Farmer who)
{
if (!(__instance is BaseWeaponEnchantment) || ____displayName == null || !ModEntry.advancedEnchantments.ContainsKey(____displayName))
if (!(__instance is BaseWeaponEnchantment) || ____displayName == null || !ModEntry.advancedEnchantments.ContainsKey(____displayName) || (ModEntry.EnchantmentTriggers.ContainsKey(who.uniqueMultiplayerID + ____displayName) && ModEntry.EnchantmentTriggers[who.uniqueMultiplayerID + ____displayName] == Game1.ticks))
return true;
AdvancedEnchantmentData enchantment = ModEntry.advancedEnchantments[____displayName];
if (enchantment.parameters["trigger"] == "slay")
{
context.Monitor.Log($"Triggered enchantment {enchantment.name} on slay");
ModEntry.EnchantmentTriggers[who.uniqueMultiplayerID + ____displayName] = Game1.ticks;
if (enchantment.type == "heal")
{
if (Game1.random.NextDouble() < float.Parse(enchantment.parameters["chance"]) / 100f)
Expand Down
90 changes: 87 additions & 3 deletions AdvancedMeleeFramework/ModEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace AdvancedMeleeFramework
{
public class ModEntry : Mod
{
public static ModEntry context;
public ModConfig Config;
public static Random myRand;
public static Dictionary<int, List<AdvancedMeleeWeapon>> advancedMeleeWeapons = new Dictionary<int, List<AdvancedMeleeWeapon>>();
Expand All @@ -31,9 +32,11 @@ public class ModEntry : Mod
public static AdvancedMeleeWeapon advancedWeaponAnimating = null;
public static IJsonAssetsApi mJsonAssets;
public static Dictionary<string, AdvancedEnchantmentData> advancedEnchantments = new Dictionary<string, AdvancedEnchantmentData>();
public static Dictionary<string, int> EnchantmentTriggers = new Dictionary<string, int>();

public override void Entry(IModHelper helper)
{
context = this;
Config = Helper.ReadConfig<ModConfig>();
if (!Config.EnableMod)
return;
Expand All @@ -44,6 +47,8 @@ public override void Entry(IModHelper helper)
Helper.Events.GameLoop.SaveLoaded += GameLoop_SaveLoaded;
Helper.Events.GameLoop.UpdateTicking += GameLoop_UpdateTicking;
Helper.Events.Input.ButtonPressed += Input_ButtonPressed;
Helper.Events.Player.InventoryChanged += Player_InventoryChanged;


myRand = new Random();

Expand All @@ -53,7 +58,6 @@ public override void Entry(IModHelper helper)
original: AccessTools.Method(typeof(MeleeWeapon), "doAnimateSpecialMove"),
prefix: new HarmonyMethod(typeof(AMFPatches), nameof(AMFPatches.doAnimateSpecialMove_Prefix))
);

ConstructorInfo ci = typeof(MeleeWeapon).GetConstructor(new Type[] { typeof(int) });
harmony.Patch(
original: ci,
Expand All @@ -64,7 +68,11 @@ public override void Entry(IModHelper helper)
original: ci,
postfix: new HarmonyMethod(typeof(AMFPatches), nameof(AMFPatches.MeleeWeapon_Postfix))
);

ci = typeof(MeleeWeapon).GetConstructor(new Type[] { typeof(int), typeof(int) });
harmony.Patch(
original: ci,
postfix: new HarmonyMethod(typeof(AMFPatches), nameof(AMFPatches.MeleeWeapon_Postfix))
);
harmony.Patch(
original: AccessTools.Method(typeof(MeleeWeapon), nameof(MeleeWeapon.drawInMenu), new Type[] { typeof(SpriteBatch), typeof(Vector2), typeof(float), typeof(float), typeof(float), typeof(StackDrawType), typeof(Color), typeof(bool) }),
prefix: new HarmonyMethod(typeof(AMFPatches), nameof(AMFPatches.drawInMenu_Prefix)),
Expand All @@ -82,17 +90,93 @@ public override void Entry(IModHelper helper)

}

private void Player_InventoryChanged(object sender, StardewModdingAPI.Events.InventoryChangedEventArgs e)
{
foreach(Item item in e.Player.items)
{
if(item is MeleeWeapon)
{
AddEnchantments(item as MeleeWeapon);
}
}
}


public void GameLoop_GameLaunched(object sender, StardewModdingAPI.Events.GameLaunchedEventArgs e)
{
mJsonAssets = Helper.ModRegistry.GetApi<IJsonAssetsApi>("spacechase0.JsonAssets");

}

public void GameLoop_SaveLoaded(object sender, StardewModdingAPI.Events.SaveLoadedEventArgs e)
{
LoadAdvancedMeleeWeapons();
foreach (Item item in Game1.player.items)
{
if (item is MeleeWeapon)
{
AddEnchantments(item as MeleeWeapon);
}
}
}

public static void AddEnchantments(MeleeWeapon weapon)
{
AdvancedMeleeWeapon amw = GetAdvancedWeapon(weapon, null);
if (amw != null && amw.enchantments.Any())
{
weapon.enchantments.Clear();
foreach (AdvancedEnchantmentData aed in amw.enchantments)
{
BaseWeaponEnchantment bwe = null;
switch (aed.type)
{
case "vampiric":
bwe = new VampiricEnchantment();
break;
case "jade":
bwe = new JadeEnchantment();
break;
case "aquamarine":
bwe = new AquamarineEnchantment();
break;
case "topaz":
bwe = new TopazEnchantment();
break;
case "amethyst":
bwe = new AmethystEnchantment();
break;
case "ruby":
bwe = new RubyEnchantment();
break;
case "emerald":
bwe = new EmeraldEnchantment();
break;
case "haymaker":
bwe = new HaymakerEnchantment();
break;
case "bugkiller":
bwe = new BugKillerEnchantment();
break;
case "crusader":
bwe = new CrusaderEnchantment();
break;
case "magic":
bwe = new MagicEnchantment();
break;
default:
bwe = new BaseWeaponEnchantment();
string key = aed.name;
context.Helper.Reflection.GetField<string>(bwe, "_displayName").SetValue(key);
break;
}
if(bwe != null)
{
weapon.enchantments.Add(bwe);
//context.Monitor.Log($"added enchantment {aed.type} to {weapon.Name} {weapon.enchantments.Count}");
}
}
}
}
public void LoadAdvancedMeleeWeapons()
{
advancedMeleeWeapons.Clear();
Expand Down
2 changes: 1 addition & 1 deletion AdvancedMeleeFramework/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"Name": "Advanced Melee Framework",
"Author": "aedenthorn",
"Version": "0.6.0",
"Version": "0.7.0",
"Description": "Advanced melee framework.",
"UniqueID": "aedenthorn.AdvancedMeleeFramework",
"EntryDll": "AdvancedMeleeFramework.dll",
Expand Down
2 changes: 2 additions & 0 deletions MultiStoryFarmhouse/ModConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ public class ModConfig
"ManyRooms",
"EmptyHall"
};
public int MainFloorStairsX { get; set; } = 7;
public int MainFloorStairsY { get; set; } = 22;
}
}
Loading

0 comments on commit fe19343

Please sign in to comment.