MP refresh after 24 hours

Hello,
Im looking for something to do the following;

-Player has set 100 MP, it does not regain the traditional ways only after 24 hours from cast.
-Player casts fireball at 9am for 10MP (Player MP now 90/100)
-Player casts heal at 930am for 20MP (Player MP now 70/100)
-The next day at 9am player recovers 10MP from fireball (Player MP now 80/100)
-30 minutes later player recovers the 20 from heal and MP is 100/100.
Here's the plugin.

Make sure to copy the code into a file named "MpDelayedAutorecovery.js"
You can change the recovery delay via the plugin manager.

Note:
This plugin uses the real clock, so people can save and quit the game, then load their savegame 24 hours later for their MP to be restored.
If your game implements its own clock, feel free to modify the plugin to suit your needs.

Spoiler
Code:
/*:
 * @plugindesc Restore MP some time after it's used.
 * @author Nolonar
 *
 * @param Delay
 * @desc How long to wait before recovering the MP used.
 * @type number
 * @min 1
 * @default 24
 *
 * @param Delay unit
 * @desc Whether the delay is in seconds, minutes, or hours
 * @type select
 * @option Seconds
 * @option Minutes
 * @option Hours
 * @default Hours
 */
(function() {
 let Parameters = PluginManager.parameters('MpDelayedAutorecovery');
 let Param = {
  Delay: Parameters['Delay'],
  DelayUnit: Parameters['Delay unit']
 };
 let Seconds = 1000;
 let Minutes = 60 * Seconds;
 let Hours = 60 * Minutes;
 
 let factor = {
  Seconds: Seconds,
  Minutes: Minutes,
  Hours: Hours
 };
 let recoveryQueue = [];
 let Game_Battler_gainMp = Game_Battler.prototype.gainMp;
 Game_Battler.prototype.gainMp = function(value) {
  Game_Battler_gainMp.call(this, value);
  if (value >= 0)
   return;
  recoveryQueue.push({
   time: Date.now() + Param.Delay * factor[Param.DelayUnit],
   battler: this,
   amount: -value
  });
 };
 let SceneManager_update = SceneManager.update;
 SceneManager.update = function() {
  SceneManager_update.call(this);
  if (recoveryQueue.length == 0)
   return;
  let recoveryData = recoveryQueue[0];
  if (Date.now() < recoveryData.time)
   return;
  recoveryData.battler.gainMp(recoveryData.amount);
  recoveryQueue.shift();
 };
})();

Terms of use are as usual for my plugins:

Use it in any game you wish, commercial or not.
No need for credits.
Feel free to modify it to suit your needs.
Feel free to redistribute it, but only for free.

Do not claim the plugin as your own.
Do not sell the plugin by itself or as part of a resource pack (selling a game that includes this plugin is ok)
If you modify the plugin, you may add your own terms of use, but you may not remove any restrictions (namely: claiming the plugin as your own, or selling the plugin)
I refuse any and all responsibility related to this plugin.

Essentially the terms of use are designed to give you as much freedom as possible, but disallow you from profiting directly from the plugin.
Oooh Thanks!
I forgot to mention I use a time system not the real clock. How would I change it so that it checks my in game clock?
I tried it and it didnt work, I changed the time so it would work only 10 seconds after but nothing happened.
I tried it and it didnt work, I changed the time so it would work only 10 seconds after but nothing happened.
Sorry, I forgot to test this in battle, so it only worked if the characters lost MP through an event.

Here's the fixed version:
Spoiler
Code:
/*:
 * @plugindesc Restore MP some time after it's used.
 * @author Nolonar
 *
 * @param Delay
 * @desc How long to wait before recovering the MP used.
 * @type number
 * @min 1
 *
 * @param Delay unit
 * @desc Whether the delay is in seconds, minutes, or hours
 * @type select
 * @option Seconds
 * @option Minutes
 * @option Hours
 */
(function() {
 let Parameters = PluginManager.parameters('MpDelayedAutorecovery');
 let Param = {
  Delay: Parameters['Delay'],
  DelayUnit: Parameters['Delay unit']
 };
 let Seconds = 1000;
 let Minutes = 60 * Seconds;
 let Hours = 60 * Minutes;
 
 let factor = {
  Seconds: Seconds,
  Minutes: Minutes,
  Hours: Hours
 };
 let recoveryQueue = [];
 Game_BattlerBase.prototype.trackMpLoss = function(mpLoss) {
  if (mpLoss <= 0)
   return;
  recoveryQueue.push({
   time: Date.now() + Param.Delay * factor[Param.DelayUnit],
   battler: this,
   amount: mpLoss
  });
 }
 let Game_Battler_gainMp = Game_Battler.prototype.gainMp;
 Game_Battler.prototype.gainMp = function(value) {
  Game_Battler_gainMp.call(this, value);
  
  this.trackMpLoss(-value);
 };
 let Game_BattlerBase_paySkillCost = Game_BattlerBase.prototype.paySkillCost;
 Game_BattlerBase.prototype.paySkillCost = function(skill) {
  Game_BattlerBase_paySkillCost.call(this, skill);
  this.trackMpLoss(this.skillMpCost(skill));
 };
 let SceneManager_update = SceneManager.update;
 SceneManager.update = function() {
  SceneManager_update.call(this);
  while (recoveryQueue.length) {
   let recoveryData = recoveryQueue[0];
   if (Date.now() < recoveryData.time)
    return;
   recoveryData.battler.gainMp(recoveryData.amount);
   recoveryQueue.shift();
  }
 };
})();

As for how to change the implementation from real clock to ingame clock:

1. Replace any instance of Date.now() with whatever your current ingame time is.
Note that Date.now() returns the number of milliseconds that have elapsed since 1970. It's a number that keeps growing.
I'm assuming that your ingame clock may be split into seconds, minutes, and hours, and each of these values are reset to 0 when they grow beyond 60 (24 for hours).
If that's the case, I can help you modify the plugin to work with your ingame clock, but then I need to know how your clock's implemented.

2. Adjust the values to the right of let Seconds, let Minutes, and let Hours.
For instance, if your clock is based on seconds instead of milliseconds, Seconds must be 1. If it takes 10 seconds for 1 minute, then Minutes must be 10 * Seconds, etc.
Note that this only applies if your clock has a unified value. If the clock is separated into seconds, minutes, and hours, then I need to know how the clock is implemented to make the necessary modifications.
Sorry, I forgot to test this in battle, so it only worked if the characters lost MP through an event.

Here's the fixed version:
Spoiler
Code:
/*:
 * @plugindesc Restore MP some time after it's used.
 * @author Nolonar
 *
 * @param Delay
 * @desc How long to wait before recovering the MP used.
 * @type number
 * @min 1
 *
 * @param Delay unit
 * @desc Whether the delay is in seconds, minutes, or hours
 * @type select
 * @option Seconds
 * @option Minutes
 * @option Hours
 */
(function() {
 let Parameters = PluginManager.parameters('MpDelayedAutorecovery');
 let Param = {
  Delay: Parameters['Delay'],
  DelayUnit: Parameters['Delay unit']
 };
 let Seconds = 1000;
 let Minutes = 60 * Seconds;
 let Hours = 60 * Minutes;
 
 let factor = {
  Seconds: Seconds,
  Minutes: Minutes,
  Hours: Hours
 };
 let recoveryQueue = [];
 Game_BattlerBase.prototype.trackMpLoss = function(mpLoss) {
  if (mpLoss <= 0)
   return;
  recoveryQueue.push({
   time: Date.now() + Param.Delay * factor[Param.DelayUnit],
   battler: this,
   amount: mpLoss
  });
 }
 let Game_Battler_gainMp = Game_Battler.prototype.gainMp;
 Game_Battler.prototype.gainMp = function(value) {
  Game_Battler_gainMp.call(this, value);
 
  this.trackMpLoss(-value);
 };
 let Game_BattlerBase_paySkillCost = Game_BattlerBase.prototype.paySkillCost;
 Game_BattlerBase.prototype.paySkillCost = function(skill) {
  Game_BattlerBase_paySkillCost.call(this, skill);
  this.trackMpLoss(this.skillMpCost(skill));
 };
 let SceneManager_update = SceneManager.update;
 SceneManager.update = function() {
  SceneManager_update.call(this);
  while (recoveryQueue.length) {
   let recoveryData = recoveryQueue[0];
   if (Date.now() < recoveryData.time)
    return;
   recoveryData.battler.gainMp(recoveryData.amount);
   recoveryQueue.shift();
  }
 };
})();

As for how to change the implementation from real clock to ingame clock:

1. Replace any instance of Date.now() with whatever your current ingame time is.
Note that Date.now() returns the number of milliseconds that have elapsed since 1970. It's a number that keeps growing.
I'm assuming that your ingame clock may be split into seconds, minutes, and hours, and each of these values are reset to 0 when they grow beyond 60 (24 for hours).
If that's the case, I can help you modify the plugin to work with your ingame clock, but then I need to know how your clock's implemented.

2. Adjust the values to the right of let Seconds, let Minutes, and let Hours.
For instance, if your clock is based on seconds instead of milliseconds, Seconds must be 1. If it takes 10 seconds for 1 minute, then Minutes must be 10 * Seconds, etc.
Note that this only applies if your clock has a unified value. If the clock is separated into seconds, minutes, and hours, then I need to know how the clock is implemented to make the necessary modifications.

This also didnt seem to work I tried using mana out of combat and set the delay to 15 seconds and nothing regained.

The time system im using is event based with variables for seconds minutes and hour, im transitioning into orange's time system although I find it a little complicated so I might stick with events if I cannot figure it out.
This also didnt seem to work I tried using mana out of combat and set the delay to 15 seconds and nothing regained.

That's because the plugin didn't refresh the menu, so as long as you didn't close and re-open the menu, you couldn't see that MP was restored.
Since your ingame clock probably won't be running while in the menu or during combat, I don't think it's relevant, but ​
this next version implements window refresh, so you can see whenever the MP refreshes, without closing and reopening the window.

Also, I have now implemented support for ingame clock (in return, real time clock is no longer supported).
You will need to reconfigure the plugin. "Delay" now only supports hours, and you will need to select the variable in which the clock's hour is saved.

Spoiler
Code:
/*:
 * @plugindesc Restore MP some time after it's used.
 * @author Nolonar
 *
 * @param Delay
 * @desc How long to wait before recovering the MP used (in ingame hours).
 * @type number
 * @min 1
 * @default 24
 *
 * @param Hour variable
 * @desc The variable, where the clock's hour is stored.
 * @type variable
 * @default 1
 */
(function() {
 let Parameters = PluginManager.parameters('MpDelayedAutorecovery');
 let Param = {
  Delay: Number(Parameters['Delay']),
  HourVarId: Number(Parameters['Hour variable'])
 };
 let recoveryQueue = [];
 let previousHour = null;
 let getCurrentHour = function() {
  if ($gameVariables === null) return null;
  return $gameVariables.value(Param.HourVarId);
 };
 Game_BattlerBase.prototype.trackMpLoss = function(mpLoss) {
  if (mpLoss <= 0)
   return;
  let now = getCurrentHour();
  recoveryQueue.push({
   timeLeft: Param.Delay,
   battler: this,
   amount: mpLoss
  });
 }
 let Game_Battler_gainMp = Game_Battler.prototype.gainMp;
 Game_Battler.prototype.gainMp = function(value) {
  Game_Battler_gainMp.call(this, value);
  
  this.trackMpLoss(-value);
 };
 let Game_BattlerBase_paySkillCost = Game_BattlerBase.prototype.paySkillCost;
 Game_BattlerBase.prototype.paySkillCost = function(skill) {
  Game_BattlerBase_paySkillCost.call(this, skill);
  this.trackMpLoss(this.skillMpCost(skill));
 };
 let SceneManager_update = SceneManager.update;
 SceneManager.update = function() {
  SceneManager_update.call(this);
  let now = getCurrentHour();
  if (previousHour === null)
   previousHour = now;
  if (now === previousHour)
   return;
  let index = 0;
  while (index < recoveryQueue.length) {
   let recoveryData = recoveryQueue[index];
   if (--recoveryData.timeLeft > 0)
    index++;
   else {
    recoveryData.battler.gainMp(recoveryData.amount);
    recoveryQueue.shift();
   }
  }
  previousHour = now;
  let scene = SceneManager._scene;
  [scene._statusWindow, scene._actorWindow].forEach(function(window) { if (window) window.refresh(); });
 };
 let DataManager_makeSaveContents = DataManager.makeSaveContents;
 DataManager.makeSaveContents = function() {
  let contents = DataManager_makeSaveContents.call(this);
  contents.N_MpDelayedAutorecovery_recoveryQueue = recoveryQueue;
  contents.N_MpDelayedAutorecovery_previousHour = previousHour;
  return contents;
 }
 let DataManager_extractSaveContents = DataManager.extractSaveContents;
 DataManager.extractSaveContents = function(contents) {
  DataManager_extractSaveContents.call(this, contents);
  recoveryQueue = contents.N_MpDelayedAutorecovery_recoveryQueue;
  previousHour = contents.N_MpDelayedAutorecovery_previousHour;
 }
})();
That's because the plugin didn't refresh the menu, so as long as you didn't close and re-open the menu, you couldn't see that MP was restored.
Since your ingame clock probably won't be running while in the menu or during combat, I don't think it's relevant, but ​
this next version implements window refresh, so you can see whenever the MP refreshes, without closing and reopening the window.

Also, I have now implemented support for ingame clock (in return, real time clock is no longer supported).
You will need to reconfigure the plugin. "Delay" now only supports hours, and you will need to select the variable in which the clock's hour is saved.

Spoiler
Code:
/*:
 * @plugindesc Restore MP some time after it's used.
 * @author Nolonar
 *
 * @param Delay
 * @desc How long to wait before recovering the MP used (in ingame hours).
 * @type number
 * @min 1
 * @default 24
 *
 * @param Hour variable
 * @desc The variable, where the clock's hour is stored.
 * @type variable
 * @default 1
 */
(function() {
 let Parameters = PluginManager.parameters('MpDelayedAutorecovery');
 let Param = {
  Delay: Number(Parameters['Delay']),
  HourVarId: Number(Parameters['Hour variable'])
 };
 let recoveryQueue = [];
 let previousHour = null;
 let getCurrentHour = function() {
  if ($gameVariables === null) return null;
  return $gameVariables.value(Param.HourVarId);
 };
 Game_BattlerBase.prototype.trackMpLoss = function(mpLoss) {
  if (mpLoss <= 0)
   return;
  let now = getCurrentHour();
  recoveryQueue.push({
   timeLeft: Param.Delay,
   battler: this,
   amount: mpLoss
  });
 }
 let Game_Battler_gainMp = Game_Battler.prototype.gainMp;
 Game_Battler.prototype.gainMp = function(value) {
  Game_Battler_gainMp.call(this, value);
 
  this.trackMpLoss(-value);
 };
 let Game_BattlerBase_paySkillCost = Game_BattlerBase.prototype.paySkillCost;
 Game_BattlerBase.prototype.paySkillCost = function(skill) {
  Game_BattlerBase_paySkillCost.call(this, skill);
  this.trackMpLoss(this.skillMpCost(skill));
 };
 let SceneManager_update = SceneManager.update;
 SceneManager.update = function() {
  SceneManager_update.call(this);
  let now = getCurrentHour();
  if (previousHour === null)
   previousHour = now;
  if (now === previousHour)
   return;
  let index = 0;
  while (index < recoveryQueue.length) {
   let recoveryData = recoveryQueue[index];
   if (--recoveryData.timeLeft > 0)
    index++;
   else {
    recoveryData.battler.gainMp(recoveryData.amount);
    recoveryQueue.shift();
   }
  }
  previousHour = now;
  let scene = SceneManager._scene;
  [scene._statusWindow, scene._actorWindow].forEach(function(window) { if (window) window.refresh(); });
 };
 let DataManager_makeSaveContents = DataManager.makeSaveContents;
 DataManager.makeSaveContents = function() {
  let contents = DataManager_makeSaveContents.call(this);
  contents.N_MpDelayedAutorecovery_recoveryQueue = recoveryQueue;
  contents.N_MpDelayedAutorecovery_previousHour = previousHour;
  return contents;
 }
 let DataManager_extractSaveContents = DataManager.extractSaveContents;
 DataManager.extractSaveContents = function(contents) {
  DataManager_extractSaveContents.call(this, contents);
  recoveryQueue = contents.N_MpDelayedAutorecovery_recoveryQueue;
  previousHour = contents.N_MpDelayedAutorecovery_previousHour;
 }
})();

Sorry I am really not getting this, nothing I have done worked so far.

Im using https://forums.rpgmakerweb.com/index.php?threads/orange-day-and-night.48951/ for the time system im testing the plug in with.
The ingame clock does not run in battle or in menu screen.

I first tried from the menu casting heal on my actor to use some MP. I closed the menu and waited 1 in game hour, reopened menu and no change in MP.
My settings for your plugin are
Delay 1
Hour variable 23 (which is the hour variable Im using for orange day and night plugin)

Can you provide me with a demo?
I need to clarify a few things.

1. The Orange Day and Night plugin only handles screen tinting and seems to require the OrangeTimeSystem plugin.
You are using the OrangeTimeSystem plugin, correct?

2. Do you intend to keep the OrangeTimeSystem plugin to handle your ingame time? If so, I'll have to adapt my plugin to it.

I'll provide a demo once I'm done adapting my plugin.
I need to clarify a few things.

1. The Orange Day and Night plugin only handles screen tinting and seems to require the OrangeTimeSystem plugin.
You are using the OrangeTimeSystem plugin, correct?

2. Do you intend to keep the OrangeTimeSystem plugin to handle your ingame time? If so, I'll have to adapt my plugin to it.

I'll provide a demo once I'm done adapting my plugin.

1. Yes sorry the OranageTimeSystem is what I am using.

2. Yes.

Will it be effected by advancing the game time. Ex: Sleeping at the inn?
This time, the plugin ended up being very small, since it can leave most of the heavy-lifting to OrangeTimeSystem instead.
You can once more define if your delay is in seconds, minutes, or hours (and it now additionally supports days, months and years).
I recommend you re-configure the plugin.

Note: The clock doesn't run during battles or while in the menu, so you have to escape battle or leave the menu for time to pass.

PS: The battle test has a bug. The bat is supposed to only have 100 HP, but it won't die even after receiving 100 damage multiple times. You can simply escape battle instead. There is no reward or secrets for defeating the bat.

Here it is:
Spoiler
Code:
/*:
 * @plugindesc Restore MP some time after it's used.
 * @author Nolonar
 *
 * @param Delay
 * @desc How long to wait before recovering the MP used (in ingame hours).
 * @type number
 * @min 1
 * @default 24
 *
 * @param Delay unit
 * @desc Whether the delay is in seconds, minutes, or hours
 * @type select
 * @option Seconds
 * @option Minutes
 * @option Hours
 * @option Days
 * @option Months
 * @option Years
 * @default Hours
 *
 * @help
 * Note: This plugin requires OrangeTimeSystem.js
 * https://github.com/Hudell/mv-plugins/blob/master/OrangeTimeSystem/OrangeTimeSystem.js
 */
(function() {
 let Parameters = PluginManager.parameters('MpDelayedAutorecovery');
 let Param = {
  Delay: Number(Parameters['Delay']),
  DelayUnit: Parameters['Delay unit']
 };
 let funcToCall = {
  Seconds: OrangeTimeSystem.runInSeconds,
  Minutes: OrangeTimeSystem.runInMinutes,
  Hours: OrangeTimeSystem.runInHours,
  Days: OrangeTimeSystem.runInDays,
  Months: OrangeTimeSystem.runInMonths,
  Years: OrangeTimeSystem.runInYears
 };
 Game_BattlerBase.prototype.trackMpLoss = function(mpLoss) {
  if (mpLoss <= 0)
   return;
  let that = this;
  funcToCall[Param.DelayUnit](function() {
   that.gainMp(mpLoss);
  }, Param.Delay);
 }
 let Game_Battler_gainMp = Game_Battler.prototype.gainMp;
 Game_Battler.prototype.gainMp = function(value) {
  Game_Battler_gainMp.call(this, value);
 
  this.trackMpLoss(-value);
 };
 let Game_BattlerBase_paySkillCost = Game_BattlerBase.prototype.paySkillCost;
 Game_BattlerBase.prototype.paySkillCost = function(skill) {
  Game_BattlerBase_paySkillCost.call(this, skill);
  this.trackMpLoss(this.skillMpCost(skill));
 };
})();

I also attached a demo.

Important note:
Due to size constraints (apparently 3 MB is too large for this site), I had to remove the img folder from the demo folder. Simply copy the img folder from your project before launching the demo. If that doesn't work, create a new project and copy the img folder from there to the demo.
I don't get it. I opened your demo (put the img folder), tested it using both battle and lose hp then healing. The MP never regains.
Your plugin is set to Delay 1, Unit Minutes. Waited 1 in game minute, and 1 minute on orange time system.. nothing...
Did you get this working?
Yes, I did get it working.

Use some MP either through battle or by using the heal spell after losing some HP.
Leave battle or menu. If you can move your character on the world map, the clock is running.

1 ingame minute should last 6 seconds. You can try and wait 10 to be safe.
Open the menu again, and the MP should be back to full.
Yes, I did get it working.

Use some MP either through battle or by using the heal spell after losing some HP.
Leave battle or menu. If you can move your character on the world map, the clock is running.

1 ingame minute should last 6 seconds. You can try and wait 10 to be safe.
Open the menu again, and the MP should be back to full.

Im using your demo, I used MP using Heal, closed menu did several laps around the map. Reopened menu and MP did not change.
I made a video where I use MP to heal, then wait for about 10 seconds for the MP to recover.
Are you doing anything differently?

I made a video where I use MP to heal, then wait for about 10 seconds for the MP to recover.
Are you doing anything differently?



No idea, I opened your demo, did the same thing waited and nothing regains. I dont understand im using your demo lol its not even my project.
Then the only reason I can think of, why it wouldn't work for you, is that we're not using the same version of MV. I'm using 1.6.1.
Are you using 1.5.2 or older?

If so, I'll have to downgrade to whatever version you're using.
But for now, I need to sleep.

EDIT:
Quick fix: Can you try replacing all instances of the let keyword with var?
I suspect that's your problem. 1.5.2 and older don't recognize let, which breaks the plugin.

EDIT 2:
For future reference, you could try pressing the F8 key on your keyboard while playtesting. This will open the developer console. Since the plugin obviously doesn't work, there should be red text somewhere, detailing what went wrong. I could use that text, if the above fix isn't enough.
Then the only reason I can think of, why it wouldn't work for you, is that we're not using the same version of MV. I'm using 1.6.1.
Are you using 1.5.2 or older?

If so, I'll have to downgrade to whatever version you're using.
But for now, I need to sleep.

EDIT:
Quick fix: Can you try replacing all instances of the let keyword with var?
I suspect that's your problem. 1.5.2 and older don't recognize let, which breaks the plugin.

EDIT 2:
For future reference, you could try pressing the F8 key on your keyboard while playtesting. This will open the developer console. Since the plugin obviously doesn't work, there should be red text somewhere, detailing what went wrong. I could use that text, if the above fix isn't enough.

Ahhh! Yes im using 1.5.1. I was so discouraged every modification you posted none worked and I was like im doing something wrong again!

I tried replacing let with var and broke the game. Whenever I try to use MP (in and out of battle) I get this error (see image)

Thanks I did not know about f8 will do.
It seems you renamed the plugin file.
When you do, look for the following code: PluginManager.parameters('N_MpDelayedAutorecovery') and make sure the inside of parameters (the part that I've highlighted in red) is the same as the file's name (without the .js part).
If they don't match, the plugin can't retrieve its settings and crashes.

In your case, that should be Restore_MP or Restore MP (I can't tell if there's an underscore or not).
The line of code should look like this:
Code:
var Parameters = PluginManager.parameters('Restore_MP');
It seems you renamed the plugin file.
When you do, look for the following code: PluginManager.parameters('N_MpDelayedAutorecovery') and make sure the inside of parameters (the part that I've highlighted in red) is the same as the file's name (without the .js part).
If they don't match, the plugin can't retrieve its settings and crashes.

In your case, that should be Restore_MP or Restore MP (I can't tell if there's an underscore or not).
The line of code should look like this:
Code:
var Parameters = PluginManager.parameters('Restore_MP');

IT WORKS!!!!!
Thank you so much! This is absolutely awesome! Wow much more complicated then I thought! Thanks a lot!

Original thread: https://forums.rpgmakerweb.com/threads/mp-refresh-after-24-hours.95788/