2016-12-09 08:34:28 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2017-08-06 03:51:44 +08:00
|
|
|
|
using System.Linq;
|
2017-07-31 11:07:07 +08:00
|
|
|
|
using Omegasis.BuildHealth.Framework;
|
2016-12-09 08:34:28 +08:00
|
|
|
|
using StardewModdingAPI;
|
2017-07-30 02:05:24 +08:00
|
|
|
|
using StardewModdingAPI.Events;
|
|
|
|
|
using StardewValley;
|
2016-12-09 08:34:28 +08:00
|
|
|
|
|
2017-07-28 08:28:39 +08:00
|
|
|
|
namespace Omegasis.BuildHealth
|
2016-12-09 08:34:28 +08:00
|
|
|
|
{
|
2017-07-30 02:05:24 +08:00
|
|
|
|
/// <summary>The mod entry point.</summary>
|
2016-12-09 08:34:28 +08:00
|
|
|
|
public class BuildHealth : Mod
|
|
|
|
|
{
|
2017-07-30 02:05:24 +08:00
|
|
|
|
/*********
|
|
|
|
|
** Properties
|
|
|
|
|
*********/
|
2017-08-06 03:51:44 +08:00
|
|
|
|
/// <summary>The relative path for the current player's data file.</summary>
|
|
|
|
|
private string DataFilePath => Path.Combine("data", $"{Constants.SaveFolderName}.json");
|
2016-12-09 08:34:28 +08:00
|
|
|
|
|
2017-08-06 03:51:44 +08:00
|
|
|
|
/// <summary>The absolute path for the current player's legacy data file.</summary>
|
2018-05-01 09:21:31 +08:00
|
|
|
|
private string LegacyDataFilePath => Path.Combine(this.Helper.DirectoryPath, "PlayerData", $"BuildHealth_data_{Game1.player.Name}.txt");
|
2016-12-09 08:34:28 +08:00
|
|
|
|
|
2017-08-06 03:51:44 +08:00
|
|
|
|
/// <summary>The mod settings and player data.</summary>
|
|
|
|
|
private ModConfig Config;
|
2016-12-09 08:34:28 +08:00
|
|
|
|
|
2017-08-06 03:51:44 +08:00
|
|
|
|
/// <summary>The data for the current player.</summary>
|
|
|
|
|
private PlayerData PlayerData;
|
2016-12-09 08:34:28 +08:00
|
|
|
|
|
2017-07-30 02:05:24 +08:00
|
|
|
|
/// <summary>Whether the player recently gained XP for tool use.</summary>
|
|
|
|
|
private bool HasRecentToolExp;
|
2016-12-09 08:34:28 +08:00
|
|
|
|
|
2017-07-30 02:05:24 +08:00
|
|
|
|
/// <summary>Whether the player was eating last time we checked.</summary>
|
|
|
|
|
private bool WasEating;
|
2016-12-09 08:34:28 +08:00
|
|
|
|
|
2017-07-30 02:05:24 +08:00
|
|
|
|
/// <summary>The player's health last time we checked it.</summary>
|
|
|
|
|
private int LastHealth;
|
2016-12-09 08:34:28 +08:00
|
|
|
|
|
2017-07-30 02:05:24 +08:00
|
|
|
|
/// <summary>Whether the player has collapsed today.</summary>
|
|
|
|
|
private bool WasCollapsed;
|
2016-12-09 08:34:28 +08:00
|
|
|
|
|
|
|
|
|
|
2017-07-30 02:05:24 +08:00
|
|
|
|
/*********
|
|
|
|
|
** Public methods
|
|
|
|
|
*********/
|
|
|
|
|
/// <summary>The mod entry point, called after the mod is first loaded.</summary>
|
|
|
|
|
/// <param name="helper">Provides simplified APIs for writing mods.</param>
|
2016-12-09 08:34:28 +08:00
|
|
|
|
public override void Entry(IModHelper helper)
|
|
|
|
|
{
|
2017-07-30 02:05:24 +08:00
|
|
|
|
GameEvents.UpdateTick += this.GameEvents_UpdateTick;
|
|
|
|
|
GameEvents.OneSecondTick += this.GameEvents_OneSecondTick;
|
2017-08-06 03:51:44 +08:00
|
|
|
|
TimeEvents.AfterDayStarted += this.SaveEvents_BeforeSave;
|
2017-07-30 02:05:24 +08:00
|
|
|
|
SaveEvents.AfterLoad += this.SaveEvents_AfterLoaded;
|
2016-12-09 08:34:28 +08:00
|
|
|
|
|
2017-08-06 03:49:27 +08:00
|
|
|
|
this.Config = helper.ReadConfig<ModConfig>();
|
2016-12-09 08:34:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-06 03:51:44 +08:00
|
|
|
|
|
|
|
|
|
/*********
|
|
|
|
|
** Private methods
|
|
|
|
|
*********/
|
2017-07-30 02:05:24 +08:00
|
|
|
|
/// <summary>The method invoked once per second during a game update.</summary>
|
|
|
|
|
/// <param name="sender">The event sender.</param>
|
|
|
|
|
/// <param name="e">The event data.</param>
|
2017-08-06 03:51:44 +08:00
|
|
|
|
private void GameEvents_OneSecondTick(object sender, EventArgs e)
|
2016-12-09 08:34:28 +08:00
|
|
|
|
{
|
2017-07-30 02:05:24 +08:00
|
|
|
|
// nerf how quickly tool xp is gained (I hope)
|
|
|
|
|
if (this.HasRecentToolExp)
|
|
|
|
|
this.HasRecentToolExp = false;
|
2016-12-09 08:34:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-30 02:05:24 +08:00
|
|
|
|
/// <summary>The method invoked when the game updates (roughly 60 times per second).</summary>
|
|
|
|
|
/// <param name="sender">The event sender.</param>
|
|
|
|
|
/// <param name="e">The event data.</param>
|
2017-08-06 03:51:44 +08:00
|
|
|
|
private void GameEvents_UpdateTick(object sender, EventArgs e)
|
2016-12-09 08:34:28 +08:00
|
|
|
|
{
|
2017-08-06 03:51:44 +08:00
|
|
|
|
if (!Context.IsWorldReady)
|
|
|
|
|
return;
|
|
|
|
|
|
2017-07-30 02:05:24 +08:00
|
|
|
|
// give XP when player finishes eating
|
2018-05-01 09:21:31 +08:00
|
|
|
|
if (Game1.player.isEating)
|
2017-07-30 02:05:24 +08:00
|
|
|
|
this.WasEating = true;
|
|
|
|
|
else if (this.WasEating)
|
2016-12-09 08:34:28 +08:00
|
|
|
|
{
|
2017-08-06 03:51:44 +08:00
|
|
|
|
this.PlayerData.CurrentExp += this.Config.ExpForEating;
|
2017-07-30 02:05:24 +08:00
|
|
|
|
this.WasEating = false;
|
2016-12-09 08:34:28 +08:00
|
|
|
|
}
|
2017-07-30 02:05:24 +08:00
|
|
|
|
|
|
|
|
|
// give XP when player uses tool
|
2018-05-01 09:21:31 +08:00
|
|
|
|
if (!this.HasRecentToolExp && Game1.player.UsingTool)
|
2016-12-09 08:34:28 +08:00
|
|
|
|
{
|
2017-08-06 03:51:44 +08:00
|
|
|
|
this.PlayerData.CurrentExp += this.Config.ExpForToolUse;
|
2017-07-30 02:05:24 +08:00
|
|
|
|
this.HasRecentToolExp = true;
|
2016-12-09 08:34:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-30 02:05:24 +08:00
|
|
|
|
// give XP for taking damage
|
|
|
|
|
var player = Game1.player;
|
|
|
|
|
if (this.LastHealth > player.health)
|
2016-12-09 08:34:28 +08:00
|
|
|
|
{
|
2017-08-06 03:51:44 +08:00
|
|
|
|
this.PlayerData.CurrentExp += this.LastHealth - player.health;
|
2017-07-30 02:05:24 +08:00
|
|
|
|
this.LastHealth = player.health;
|
2016-12-09 08:34:28 +08:00
|
|
|
|
}
|
2017-07-30 02:05:24 +08:00
|
|
|
|
else if (this.LastHealth < player.health)
|
|
|
|
|
this.LastHealth = player.health;
|
|
|
|
|
|
|
|
|
|
// give XP when player stays up too late or collapses
|
|
|
|
|
if (!this.WasCollapsed && Game1.farmerShouldPassOut)
|
2016-12-09 08:34:28 +08:00
|
|
|
|
{
|
2017-08-06 03:51:44 +08:00
|
|
|
|
this.PlayerData.CurrentExp += this.Config.ExpForCollapsing;
|
2017-07-30 02:05:24 +08:00
|
|
|
|
this.WasCollapsed = true;
|
|
|
|
|
this.Monitor.Log("The player has collapsed!");
|
2016-12-09 08:34:28 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-06 03:51:44 +08:00
|
|
|
|
/// <summary>The method invoked after the player loads a save.</summary>
|
2017-07-30 02:05:24 +08:00
|
|
|
|
/// <param name="sender">The event sender.</param>
|
|
|
|
|
/// <param name="e">The event data.</param>
|
2017-08-06 03:51:44 +08:00
|
|
|
|
private void SaveEvents_AfterLoaded(object sender, EventArgs e)
|
2016-12-09 08:34:28 +08:00
|
|
|
|
{
|
2017-08-06 11:00:37 +08:00
|
|
|
|
// reset state
|
|
|
|
|
this.HasRecentToolExp = false;
|
|
|
|
|
this.WasEating = false;
|
|
|
|
|
this.LastHealth = Game1.player.health;
|
|
|
|
|
this.WasCollapsed = false;
|
|
|
|
|
|
2017-08-06 03:51:44 +08:00
|
|
|
|
// load player data
|
|
|
|
|
this.MigrateLegacyData();
|
|
|
|
|
this.PlayerData = this.Helper.ReadJsonFile<PlayerData>(this.DataFilePath) ?? new PlayerData();
|
|
|
|
|
if (this.PlayerData.OriginalMaxHealth == 0)
|
|
|
|
|
this.PlayerData.OriginalMaxHealth = Game1.player.maxHealth;
|
2016-12-09 08:34:28 +08:00
|
|
|
|
|
2017-08-06 03:51:44 +08:00
|
|
|
|
// reset if needed
|
|
|
|
|
if (this.PlayerData.ClearModEffects)
|
2016-12-09 08:34:28 +08:00
|
|
|
|
{
|
2017-08-06 03:51:44 +08:00
|
|
|
|
Game1.player.maxHealth = this.PlayerData.OriginalMaxHealth;
|
|
|
|
|
this.PlayerData.ExpToNextLevel = this.Config.ExpToNextLevel;
|
|
|
|
|
this.PlayerData.CurrentExp = this.Config.CurrentExp;
|
|
|
|
|
this.PlayerData.CurrentLevelHealthBonus = 0;
|
|
|
|
|
this.PlayerData.OriginalMaxHealth = Game1.player.maxHealth;
|
|
|
|
|
this.PlayerData.BaseHealthBonus = 0;
|
|
|
|
|
this.PlayerData.CurrentLevel = 0;
|
|
|
|
|
this.PlayerData.ClearModEffects = false;
|
2016-12-09 08:34:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-06 11:00:37 +08:00
|
|
|
|
// else apply health bonus
|
2017-08-06 03:51:44 +08:00
|
|
|
|
else
|
|
|
|
|
Game1.player.maxHealth = this.PlayerData.BaseHealthBonus + this.PlayerData.CurrentLevelHealthBonus + this.PlayerData.OriginalMaxHealth;
|
2016-12-09 08:34:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-06 03:51:44 +08:00
|
|
|
|
/// <summary>The method invoked just before the game saves.</summary>
|
2017-07-30 02:05:24 +08:00
|
|
|
|
/// <param name="sender">The event sender.</param>
|
|
|
|
|
/// <param name="e">The event data.</param>
|
2017-08-06 03:51:44 +08:00
|
|
|
|
private void SaveEvents_BeforeSave(object sender, EventArgs e)
|
2016-12-09 08:34:28 +08:00
|
|
|
|
{
|
2017-08-06 03:51:44 +08:00
|
|
|
|
// reset data
|
2017-07-30 02:05:24 +08:00
|
|
|
|
this.LastHealth = Game1.player.maxHealth;
|
2017-08-06 03:51:44 +08:00
|
|
|
|
this.WasCollapsed = false;
|
2016-12-09 08:34:28 +08:00
|
|
|
|
|
2017-08-06 03:51:44 +08:00
|
|
|
|
// update settings
|
|
|
|
|
var player = Game1.player;
|
|
|
|
|
this.PlayerData.CurrentExp += this.Config.ExpForSleeping;
|
|
|
|
|
if (this.PlayerData.OriginalMaxHealth == 0)
|
|
|
|
|
this.PlayerData.OriginalMaxHealth = player.maxHealth; //grab the initial Health value
|
2016-12-09 08:34:28 +08:00
|
|
|
|
|
2017-08-06 03:51:44 +08:00
|
|
|
|
if (this.PlayerData.CurrentLevel < this.Config.MaxLevel)
|
2017-07-30 02:05:24 +08:00
|
|
|
|
{
|
2017-08-06 03:51:44 +08:00
|
|
|
|
while (this.PlayerData.CurrentExp >= this.PlayerData.ExpToNextLevel)
|
|
|
|
|
{
|
|
|
|
|
this.PlayerData.CurrentLevel += 1;
|
|
|
|
|
this.PlayerData.CurrentExp = this.PlayerData.CurrentExp - this.PlayerData.ExpToNextLevel;
|
|
|
|
|
this.PlayerData.ExpToNextLevel =
|
|
|
|
|
(this.Config.ExpCurve * this.PlayerData.ExpToNextLevel);
|
|
|
|
|
player.maxHealth += this.Config.HealthIncreasePerLevel;
|
|
|
|
|
this.PlayerData.CurrentLevelHealthBonus += this.Config.HealthIncreasePerLevel;
|
|
|
|
|
}
|
2016-12-09 08:34:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-06 03:51:44 +08:00
|
|
|
|
// save data
|
|
|
|
|
this.Helper.WriteJsonFile(this.DataFilePath, this.PlayerData);
|
2016-12-09 08:34:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-06 03:51:44 +08:00
|
|
|
|
/// <summary>Migrate the legacy settings for the current player.</summary>
|
|
|
|
|
private void MigrateLegacyData()
|
2016-12-09 08:34:28 +08:00
|
|
|
|
{
|
2017-08-06 03:51:44 +08:00
|
|
|
|
// skip if no legacy data or new data already exists
|
|
|
|
|
if (!File.Exists(this.LegacyDataFilePath) || File.Exists(this.DataFilePath))
|
|
|
|
|
return;
|
2016-12-09 08:34:28 +08:00
|
|
|
|
|
2017-08-06 03:51:44 +08:00
|
|
|
|
// migrate to new file
|
|
|
|
|
try
|
2017-07-30 02:05:24 +08:00
|
|
|
|
{
|
2017-08-06 03:51:44 +08:00
|
|
|
|
string[] text = File.ReadAllLines(this.LegacyDataFilePath);
|
|
|
|
|
this.Helper.WriteJsonFile(this.DataFilePath, new PlayerData
|
|
|
|
|
{
|
|
|
|
|
CurrentLevel = Convert.ToInt32(text[3]),
|
|
|
|
|
CurrentExp = Convert.ToDouble(text[5]),
|
|
|
|
|
ExpToNextLevel = Convert.ToDouble(text[7]),
|
|
|
|
|
BaseHealthBonus = Convert.ToInt32(text[9]),
|
|
|
|
|
CurrentLevelHealthBonus = Convert.ToInt32(text[11]),
|
|
|
|
|
ClearModEffects = Convert.ToBoolean(text[14]),
|
|
|
|
|
OriginalMaxHealth = Convert.ToInt32(text[16])
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
FileInfo file = new FileInfo(this.LegacyDataFilePath);
|
|
|
|
|
file.Delete();
|
|
|
|
|
if (!file.Directory.EnumerateFiles().Any())
|
|
|
|
|
file.Directory.Delete();
|
2016-12-09 08:34:28 +08:00
|
|
|
|
}
|
2017-08-06 03:51:44 +08:00
|
|
|
|
catch (Exception ex)
|
2016-12-09 08:34:28 +08:00
|
|
|
|
{
|
2017-08-06 03:51:44 +08:00
|
|
|
|
this.Monitor.Log($"Error migrating data from the legacy 'PlayerData' folder for the current player. Technical details:\n {ex}", LogLevel.Error);
|
2016-12-09 08:34:28 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-30 02:05:24 +08:00
|
|
|
|
}
|
|
|
|
|
}
|