Stardew_Valley_Mods/BuildEndurance/Class1.cs

513 lines
21 KiB
C#
Raw Normal View History

2016-03-27 16:15:25 +08:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
2016-04-02 16:29:53 +08:00
using System.Timers;
2016-03-27 16:15:25 +08:00
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using StardewModdingAPI;
using Microsoft.Xna.Framework;
2016-04-02 16:29:53 +08:00
using System.Windows;
2016-03-27 16:15:25 +08:00
namespace BuildEndurance
{
public class BuildEndurance : Mod
{
2016-04-21 03:19:33 +08:00
static bool exhausted_check = false;
static bool collapse_check = false;
2016-04-11 13:39:16 +08:00
static double BuildEndurance_data_xp_nextlvl = 20;
static double BuildEndurance_data_xp_current = 0;
2016-03-27 16:15:25 +08:00
2016-04-11 13:39:16 +08:00
static int BuildEndurance_data_current_lvl = 0;
2016-03-27 16:15:25 +08:00
2016-04-11 13:39:16 +08:00
static int BuildEndurance_data_stam_bonus_acumulated = 0;
2016-03-27 16:15:25 +08:00
2016-04-11 13:39:16 +08:00
static int BuildEndurance_data_ini_stam_bonus = 0;
2016-03-27 16:15:25 +08:00
2016-04-11 13:39:16 +08:00
static bool BuildEndurance_data_clear_mod_effects = false;
2016-03-27 16:15:25 +08:00
2016-04-11 13:39:16 +08:00
static int BuildEndurance_data_old_stamina = 0;
2016-03-27 16:15:25 +08:00
2016-04-11 13:39:16 +08:00
static bool tool_cleaner = false;
2016-03-27 16:15:25 +08:00
2016-04-11 13:39:16 +08:00
static bool fed = false;
2016-03-27 16:15:25 +08:00
public Config ModConfig { get; set; }
2016-04-11 13:39:16 +08:00
static bool upon_loading = false;
2016-03-27 16:15:25 +08:00
2016-04-21 03:19:33 +08:00
static int nightly_stamina_value = 0;
2016-03-27 16:15:25 +08:00
//Credit goes to Zoryn for pieces of this config generation that I kinda repurposed.
public override void Entry(params object[] objects)
{
2016-04-02 16:29:53 +08:00
Log.Info("HEYO WORLD");
2016-04-11 13:39:16 +08:00
2016-03-27 16:15:25 +08:00
StardewModdingAPI.Events.GameEvents.UpdateTick += EatingCallBack; //sloppy again but it'll do.
StardewModdingAPI.Events.GameEvents.OneSecondTick += Tool_Cleanup;
2016-04-11 13:39:16 +08:00
StardewModdingAPI.Events.GameEvents.UpdateTick += ToolCallBack;
2016-03-27 16:15:25 +08:00
StardewModdingAPI.Events.PlayerEvents.LoadedGame += LoadingCallBack;
2016-04-02 16:29:53 +08:00
StardewModdingAPI.Events.TimeEvents.DayOfMonthChanged += SleepCallback;
2016-03-27 16:15:25 +08:00
2016-04-21 03:19:33 +08:00
StardewModdingAPI.Events.GameEvents.UpdateTick += Exhaustion_callback;
StardewModdingAPI.Events.GameEvents.UpdateTick += Collapse_Callback;
2016-03-27 16:15:25 +08:00
var configLocation = Path.Combine(PathOnDisk, "BuildEnduranceConfig.json");
if (!File.Exists(configLocation))
{
2016-04-02 16:29:53 +08:00
Console.WriteLine("Initial configuration file setup.");
2016-03-27 16:15:25 +08:00
ModConfig = new Config();
ModConfig.BuildEndurance_current_lvl = 0;
ModConfig.BuildEndurance_max_lvl = 100;
ModConfig.BuildEndurance_stam_increase_upon_lvl_up = 1;
ModConfig.BuildEndurance_xp_current = 0;
ModConfig.BuildEndurance_xp_nextlvl = 20;
ModConfig.BuildEndurance_xp_curve = 1.15;
ModConfig.BuildEndurance_xp_eating = 2;
ModConfig.BuildEndurance_xp_sleeping = 10;
ModConfig.BuildEndurance_xp_tooluse = 1;
ModConfig.BuildEndurance_ini_stam_boost = 0;
ModConfig.BuildEndurance_stam_accumulated = 0;
2016-04-21 03:19:33 +08:00
ModConfig.BuildEndurance_Exhaustion_XP = 25;
ModConfig.BuildEndurance_Pass_Out_XP = 50;
2016-03-27 16:15:25 +08:00
File.WriteAllBytes(configLocation, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(ModConfig)));
}
else
{
ModConfig = JsonConvert.DeserializeObject<Config>(Encoding.UTF8.GetString(File.ReadAllBytes(configLocation)));
Console.WriteLine("Found BuildEndurance config file.");
}
2016-04-11 13:39:16 +08:00
// DataLoader();
// MyWritter(); //hopefully loading these after the game is loaded will prevent wierd issues.
2016-03-27 16:15:25 +08:00
Console.WriteLine("BuildEndurance Initialization Completed");
}
2016-04-11 13:39:16 +08:00
2016-03-27 16:15:25 +08:00
public void ToolCallBack(object sender, EventArgs e) //ultra quick response for checking if a tool is used.
{
if (tool_cleaner == true) return;
2016-04-11 13:39:16 +08:00
if (StardewValley.Game1.player.usingTool == true)
2016-03-27 16:15:25 +08:00
{
2016-04-11 13:39:16 +08:00
// Console.WriteLine("Tool is being used");
2016-03-27 16:15:25 +08:00
BuildEndurance_data_xp_current += ModConfig.BuildEndurance_xp_tooluse;
2016-04-11 13:39:16 +08:00
//BuildEndurance_data_xp_current += 1000; For testing purposes
// Log.Info(BuildEndurance_data_xp_current);
2016-03-27 16:15:25 +08:00
tool_cleaner = true;
}
else return;
}
public void Tool_Cleanup(object sender, EventArgs e) //nerfs how quickly xp is actually gained. I hope.
{
if (tool_cleaner == true) tool_cleaner = false;
else return;
}
public void EatingCallBack(object sender, EventArgs e)
{
2016-04-11 13:39:16 +08:00
2016-03-27 16:15:25 +08:00
if (StardewValley.Game1.isEating == true)
2016-04-11 13:39:16 +08:00
{
// Console.WriteLine("NOM NOM NOM");
2016-03-27 16:15:25 +08:00
fed = true;
//this code will run when the player eats an object. I.E. increases their eating skills.
}
//I'm going to assume they ate the food.
if ((StardewValley.Game1.isEating == false) && fed == true)
{
2016-04-11 13:39:16 +08:00
//Console.WriteLine("NOM NOM NOM22222222222");
2016-03-27 16:15:25 +08:00
BuildEndurance_data_xp_current += ModConfig.BuildEndurance_xp_eating;
fed = false;
}
2016-04-11 13:39:16 +08:00
2016-03-27 16:15:25 +08:00
return;
}
2016-04-11 13:39:16 +08:00
2016-04-21 03:19:33 +08:00
public void Exhaustion_callback(object sender, EventArgs e) //if the player is exhausted add xp.
{
if (exhausted_check == false)
{
if (StardewValley.Game1.player.exhausted)
{
BuildEndurance_data_xp_current += ModConfig.BuildEndurance_Exhaustion_XP;
exhausted_check = true;
Log.Info("The player is exhausted");
}
}
}
public void Collapse_Callback(object sender, EventArgs e) //if the player stays up too late add some xp.
{
if (collapse_check == false)
{
if (StardewValley.Game1.timeOfDay==2600)
{
BuildEndurance_data_xp_current += ModConfig.BuildEndurance_Pass_Out_XP;
collapse_check = true;
Log.Info("The player has collapsed!");
}
}
}
2016-03-27 16:15:25 +08:00
2016-04-02 16:29:53 +08:00
public void LoadingCallBack(object sender, EventArgs e)
{
if (StardewModdingAPI.Inheritance.SGame.hasLoadedGame == true)
2016-03-27 16:15:25 +08:00
{
2016-04-02 16:29:53 +08:00
DataLoader();
MyWritter();
upon_loading = true;
//runs when the player is loaded.
2016-03-27 16:15:25 +08:00
2016-04-02 16:29:53 +08:00
var player = StardewValley.Game1.player;
2016-04-11 13:39:16 +08:00
2016-04-02 16:29:53 +08:00
if (BuildEndurance_data_old_stamina == 0)
2016-03-27 16:15:25 +08:00
{
2016-04-02 16:29:53 +08:00
BuildEndurance_data_old_stamina = player.MaxStamina; //grab the initial stamina value
2016-03-27 16:15:25 +08:00
}
2016-04-02 16:29:53 +08:00
2016-04-21 03:19:33 +08:00
player.MaxStamina = nightly_stamina_value;
if (nightly_stamina_value == 0)
{
player.MaxStamina = BuildEndurance_data_ini_stam_bonus + BuildEndurance_data_stam_bonus_acumulated + BuildEndurance_data_old_stamina; //incase the ini stam bonus is loaded in.
}
2016-04-02 16:29:53 +08:00
if (BuildEndurance_data_clear_mod_effects == true)
2016-03-27 16:15:25 +08:00
{
2016-04-02 16:29:53 +08:00
player.MaxStamina = BuildEndurance_data_old_stamina;
Console.WriteLine("BuildEndurance Reset!");
2016-03-27 16:15:25 +08:00
}
2016-04-02 16:29:53 +08:00
DataLoader();
MyWritter();
2016-03-27 16:15:25 +08:00
}
}
2016-04-02 16:29:53 +08:00
public void SleepCallback(object sender, EventArgs e)
2016-03-27 16:15:25 +08:00
{
2016-04-02 16:29:53 +08:00
//This will run when the character goes to sleep. It will increase their sleeping skill.
2016-04-21 03:19:33 +08:00
exhausted_check = false;
collapse_check = false;
2016-04-02 16:29:53 +08:00
if (upon_loading == true)
2016-03-27 16:15:25 +08:00
{
2016-04-11 13:39:16 +08:00
Log.Info("THIS IS MY NEW DAY CALL BACK XP version 1");
Log.Info(BuildEndurance_data_xp_current);
Clear_Checker();
Log.Info("CLEAR???");
Log.Info(BuildEndurance_data_clear_mod_effects);
2016-04-21 03:19:33 +08:00
2016-04-11 13:39:16 +08:00
//Clear_DataLoader();
2016-04-02 16:29:53 +08:00
//because this doesn't work propperly at first anyways.
//return;
2016-04-11 13:39:16 +08:00
2016-04-02 16:29:53 +08:00
2016-03-27 16:15:25 +08:00
var player = StardewValley.Game1.player;
2016-04-02 16:29:53 +08:00
BuildEndurance_data_xp_current += ModConfig.BuildEndurance_xp_sleeping;
2016-03-27 16:15:25 +08:00
if (BuildEndurance_data_old_stamina == 0)
{
BuildEndurance_data_old_stamina = player.MaxStamina; //grab the initial stamina value
}
if (BuildEndurance_data_clear_mod_effects == true)
{
2016-04-11 13:39:16 +08:00
Clear_DataLoader();
2016-03-27 16:15:25 +08:00
player.MaxStamina = BuildEndurance_data_old_stamina;
2016-04-02 16:29:53 +08:00
BuildEndurance_data_xp_nextlvl = ModConfig.BuildEndurance_xp_nextlvl;
BuildEndurance_data_xp_current = ModConfig.BuildEndurance_xp_current;
BuildEndurance_data_stam_bonus_acumulated = 0;
BuildEndurance_data_old_stamina = player.MaxStamina;
BuildEndurance_data_ini_stam_bonus = 0;
BuildEndurance_data_current_lvl = 0;
2016-04-11 13:39:16 +08:00
//because this doesn't work propperly at first anyways.
2016-03-27 16:15:25 +08:00
Console.WriteLine("BuildEndurance Reset!");
}
2016-04-02 16:29:53 +08:00
if (BuildEndurance_data_clear_mod_effects == false)
{
if (BuildEndurance_data_current_lvl < ModConfig.BuildEndurance_max_lvl)
{
while (BuildEndurance_data_xp_current >= BuildEndurance_data_xp_nextlvl)
{
BuildEndurance_data_current_lvl += 1;
BuildEndurance_data_xp_current = BuildEndurance_data_xp_current - BuildEndurance_data_xp_nextlvl;
BuildEndurance_data_xp_nextlvl = (ModConfig.BuildEndurance_xp_curve * BuildEndurance_data_xp_nextlvl);
player.MaxStamina += ModConfig.BuildEndurance_stam_increase_upon_lvl_up;
BuildEndurance_data_stam_bonus_acumulated += ModConfig.BuildEndurance_stam_increase_upon_lvl_up;
2016-04-11 13:39:16 +08:00
Log.Info("THIS IS MY NEW DAY CALL BACK XP version 2!");
Log.Info(BuildEndurance_data_xp_current);
2016-04-02 16:29:53 +08:00
Log.Info("IF YOU SEE THIS TOO MUCH THIS IS AN INFINITE LOOP. CRAP");
}
/*
if (player.MaxStamina != BuildEndurance_data_old_stamina + BuildEndurance_data_stam_bonus_acumulated + BuildEndurance_data_ini_stam_bonus)
{
player.MaxStamina = BuildEndurance_data_old_stamina + BuildEndurance_data_stam_bonus_acumulated + BuildEndurance_data_ini_stam_bonus;
}
*/
}
}
BuildEndurance_data_clear_mod_effects = false;
2016-04-21 03:19:33 +08:00
nightly_stamina_value = StardewValley.Game1.player.maxStamina;
2016-03-27 16:15:25 +08:00
MyWritter();
}
2016-04-02 16:45:38 +08:00
//else Log.Info("Safely Loading.");
2016-03-27 16:15:25 +08:00
}
2016-04-02 16:29:53 +08:00
2016-03-27 16:15:25 +08:00
//Mod config data.
public class Config
{
public double BuildEndurance_xp_nextlvl { get; set; }
public double BuildEndurance_xp_current { get; set; }
public double BuildEndurance_xp_curve { get; set; }
public int BuildEndurance_current_lvl { get; set; }
public int BuildEndurance_max_lvl { get; set; }
public int BuildEndurance_stam_increase_upon_lvl_up { get; set; }
public int BuildEndurance_xp_tooluse { get; set; }
public int BuildEndurance_xp_eating { get; set; }
public int BuildEndurance_xp_sleeping { get; set; }
public int BuildEndurance_ini_stam_boost { get; set; }
public int BuildEndurance_stam_accumulated { get; set; }
2016-04-11 13:39:16 +08:00
2016-04-21 03:19:33 +08:00
public int BuildEndurance_Exhaustion_XP { get; set; }
public int BuildEndurance_Pass_Out_XP { get; set; }
2016-03-27 16:15:25 +08:00
}
void Clear_DataLoader()
{
2016-04-02 16:29:53 +08:00
DataLoader();
MyWritter();
2016-03-27 16:15:25 +08:00
//loads the data to the variables upon loading the game.
2016-04-02 16:29:53 +08:00
string myname = StardewValley.Game1.player.name;
string mylocation = Path.Combine(PathOnDisk, "BuildEndurance_data_");
2016-04-11 13:39:16 +08:00
string mylocation2 = mylocation + myname;
string mylocation3 = mylocation2 + ".txt";
2016-04-02 16:29:53 +08:00
if (!File.Exists(mylocation3)) //if not data.json exists, initialize the data variables to the ModConfig data. I.E. starting out.
2016-03-27 16:15:25 +08:00
{
2016-04-02 16:29:53 +08:00
Console.WriteLine("Clear Data Loaded could not find the correct file.");
2016-03-27 16:15:25 +08:00
BuildEndurance_data_clear_mod_effects = false;
BuildEndurance_data_old_stamina = 0;
BuildEndurance_data_ini_stam_bonus = 0;
2016-04-02 16:29:53 +08:00
//return;
2016-03-27 16:15:25 +08:00
}
else
{
//loads the BuildEndurance_data upon loading the mod
2016-04-02 16:29:53 +08:00
string[] readtext = File.ReadAllLines(mylocation3);
2016-03-27 16:15:25 +08:00
BuildEndurance_data_ini_stam_bonus = Convert.ToInt32(readtext[9]);
BuildEndurance_data_clear_mod_effects = Convert.ToBoolean(readtext[14]);
BuildEndurance_data_old_stamina = Convert.ToInt32(readtext[16]);
}
}
2016-04-11 13:39:16 +08:00
void Clear_Checker()
{
//loads the data to the variables upon loading the game.
string myname = StardewValley.Game1.player.name;
string mylocation = Path.Combine(PathOnDisk, "BuildEndurance_data_");
string mylocation2 = mylocation + myname;
string mylocation3 = mylocation2 + ".txt";
if (!File.Exists(mylocation3)) //if not data.json exists, initialize the data variables to the ModConfig data. I.E. starting out.
{
Console.WriteLine("Clear Data Loaded could not find the correct file.");
BuildEndurance_data_clear_mod_effects = false;
BuildEndurance_data_old_stamina = 0;
BuildEndurance_data_ini_stam_bonus = 0;
//return;
}
else
{
//loads the BuildEndurance_data upon loading the mod
string[] readtext = File.ReadAllLines(mylocation3);
BuildEndurance_data_clear_mod_effects = Convert.ToBoolean(readtext[14]);
}
}
2016-03-27 16:15:25 +08:00
void DataLoader()
{
//loads the data to the variables upon loading the game.
2016-04-02 16:29:53 +08:00
string myname = StardewValley.Game1.player.name;
string mylocation = Path.Combine(PathOnDisk, "BuildEndurance_data_");
string mylocation2 = mylocation + myname;
string mylocation3 = mylocation2 + ".txt";
if (!File.Exists(mylocation3)) //if not data.json exists, initialize the data variables to the ModConfig data. I.E. starting out.
2016-03-27 16:15:25 +08:00
{
2016-04-02 16:29:53 +08:00
Console.WriteLine("DataLoading");
2016-03-27 16:15:25 +08:00
BuildEndurance_data_xp_nextlvl = ModConfig.BuildEndurance_xp_nextlvl;
BuildEndurance_data_xp_current = ModConfig.BuildEndurance_xp_current;
BuildEndurance_data_current_lvl = ModConfig.BuildEndurance_current_lvl;
2016-04-11 13:39:16 +08:00
BuildEndurance_data_ini_stam_bonus = ModConfig.BuildEndurance_ini_stam_boost;
2016-03-27 16:15:25 +08:00
BuildEndurance_data_stam_bonus_acumulated = ModConfig.BuildEndurance_stam_accumulated;
BuildEndurance_data_clear_mod_effects = false;
BuildEndurance_data_old_stamina = 0;
}
else
{
2016-04-11 13:39:16 +08:00
// Console.WriteLine("HEY THERE IM LOADING DATA");
2016-03-27 16:15:25 +08:00
//loads the BuildEndurance_data upon loading the mod
2016-04-02 16:29:53 +08:00
string[] readtext = File.ReadAllLines(mylocation3);
2016-03-27 16:15:25 +08:00
BuildEndurance_data_current_lvl = Convert.ToInt32(readtext[3]);
BuildEndurance_data_xp_nextlvl = Convert.ToDouble(readtext[7]); //these array locations refer to the lines in BuildEndurance_data.json
BuildEndurance_data_xp_current = Convert.ToDouble(readtext[5]);
BuildEndurance_data_ini_stam_bonus = Convert.ToInt32(readtext[9]);
2016-04-11 13:39:16 +08:00
BuildEndurance_data_stam_bonus_acumulated = Convert.ToInt32(readtext[11]);
2016-03-27 16:15:25 +08:00
BuildEndurance_data_clear_mod_effects = Convert.ToBoolean(readtext[14]);
BuildEndurance_data_old_stamina = Convert.ToInt32(readtext[16]);
2016-04-21 03:19:33 +08:00
nightly_stamina_value = Convert.ToInt32(readtext[18]); //this should grab the nightly stamina values
2016-03-27 16:15:25 +08:00
}
}
void MyWritter()
{
//saves the BuildEndurance_data at the end of a new day;
2016-04-02 16:29:53 +08:00
string myname = StardewValley.Game1.player.name;
string mylocation = Path.Combine(PathOnDisk, "BuildEndurance_data_");
string mylocation2 = mylocation + myname;
string mylocation3 = mylocation2 + ".txt";
2016-04-11 13:39:16 +08:00
string[] mystring3 = new string[20];
2016-04-02 16:29:53 +08:00
if (!File.Exists(mylocation3))
2016-03-27 16:15:25 +08:00
{
Console.WriteLine("The data file for BuildEndurance was not found, guess I'll create it when you sleep.");
//write out the info to a text file at the end of a day. This will run if it doesnt exist.
mystring3[0] = "Player: Build Endurance Data. Modification can cause errors. Edit at your own risk.";
mystring3[1] = "====================================================================================";
mystring3[2] = "Player Current Level:";
mystring3[3] = BuildEndurance_data_current_lvl.ToString();
mystring3[4] = "Player Current XP:";
mystring3[5] = BuildEndurance_data_xp_current.ToString();
mystring3[6] = "Xp to next Level:";
mystring3[7] = BuildEndurance_data_xp_nextlvl.ToString();
mystring3[8] = "Initial Stam Bonus:";
mystring3[9] = BuildEndurance_data_ini_stam_bonus.ToString();
mystring3[10] = "Additional Stam Bonus:";
mystring3[11] = BuildEndurance_data_stam_bonus_acumulated.ToString();
mystring3[12] = "=======================================================================================";
mystring3[13] = "RESET ALL MOD EFFECTS? This will effective start you back at square 1. Also good if you want to remove this mod.";
mystring3[14] = BuildEndurance_data_clear_mod_effects.ToString();
mystring3[15] = "OLD STAMINA AMOUNT: This is the initial value of the Player's Stamina before this mod took over.";
mystring3[16] = BuildEndurance_data_old_stamina.ToString();
2016-04-21 03:19:33 +08:00
mystring3[17] = "Nightly Stamina Value: This is the value of the player's stamina that was saved when the player slept.";
mystring3[18] = nightly_stamina_value.ToString(); //this should save the player's stamina upon sleeping.
2016-04-02 16:29:53 +08:00
File.WriteAllLines(mylocation3, mystring3);
2016-03-27 16:15:25 +08:00
}
2016-04-11 13:39:16 +08:00
2016-03-27 16:15:25 +08:00
else
{
2016-04-11 13:39:16 +08:00
// Console.WriteLine("HEY IM SAVING DATA");
2016-03-27 16:15:25 +08:00
//write out the info to a text file at the end of a day.
mystring3[0] = "Player: Build Endurance Data. Modification can cause errors. Edit at your own risk.";
mystring3[1] = "====================================================================================";
mystring3[2] = "Player Current Level:";
mystring3[3] = BuildEndurance_data_current_lvl.ToString();
mystring3[4] = "Player Current XP:";
mystring3[5] = BuildEndurance_data_xp_current.ToString();
mystring3[6] = "Xp to next Level:";
mystring3[7] = BuildEndurance_data_xp_nextlvl.ToString();
mystring3[8] = "Initial Stam Bonus:";
mystring3[9] = BuildEndurance_data_ini_stam_bonus.ToString();
mystring3[10] = "Additional Stam Bonus:";
mystring3[11] = BuildEndurance_data_stam_bonus_acumulated.ToString();
mystring3[12] = "=======================================================================================";
mystring3[13] = "RESET ALL MOD EFFECTS? This will effective start you back at square 1. Also good if you want to remove this mod.";
mystring3[14] = BuildEndurance_data_clear_mod_effects.ToString();
mystring3[15] = "OLD STAMINA AMOUNT: This is the initial value of the Player's Stamina before this mod took over.";
mystring3[16] = BuildEndurance_data_old_stamina.ToString();
2016-04-21 03:19:33 +08:00
mystring3[17] = "Nightly Stamina Value: This is the value of the player's stamina that was saved when the player slept.";
mystring3[18] = nightly_stamina_value.ToString();
2016-03-27 16:15:25 +08:00
2016-04-02 16:29:53 +08:00
File.WriteAllLines(mylocation3, mystring3);
2016-03-27 16:15:25 +08:00
}
}
} //end my function
}