'use strict'; const conf = require('../conf'); const {sleep, nextTick} = require('../utils'); class Plugin{ plunginsLoaded = {}; constructor(options){ this.bot = options.bot; } async load(pluginName, options){ if(pluginName in this.plunginsLoaded) throw new Error(`Plugin ${pluginName} already loaded`); let plugin = new this.bot.constructor.plungins[pluginName]({...options, bot: this.bot}) this.plunginsLoaded[pluginName] = plugin; return await plugin.init(); } unload(pluginName){ console.log('Plugin.unload', pluginName); if(pluginName){ try{ return this.plunginsLoaded[pluginName].unload(); delete this.plunginsLoaded[pluginName]; }catch(error){ console.error('Plugin.unload error', pluginName); } } for(pluginName in this.plunginsLoaded){ console.log('Plugin.unload load', pluginName) try{ this.plunginsLoaded[pluginName].unload(); }catch(error){ console.error(`Plugin.unload ${pluginName} Error`, error); } delete this.plunginsLoaded[pluginName]; } } } class GuardianFarm extends Plugin{ constructor(options){ super(options); this.isDangerous = true; this.isAction = true; this.onTimeListen; } async init(){ console.log('GuardianFarm started'); this.onReadyListen = this.bot.on('onReady', async ()=>{ await sleep(3000); let lastTimeListen = this.bot.bot.time.timeOfDay; await this.load('Swing'); this.onTimeListen = this.bot.bot.on('time', async ()=>{ let isDay = lastTimeListen < this.bot.bot.time.timeOfDay; lastTimeListen = this.bot.bot.time.timeOfDay; if(isDay){ await this.onNewDay(); } }); }); return true; } unload(){ super.unload(); this.onReadyListen(); // this.onTimeListen(); return true; } async onNewDay(){ try{ console.log('GuardianFarm.onNewDay new day!'); await this.unload('Swing'); await this.load('Craft'); await this.unload('Craft'); await this.load('Swing'); }catch(error){ console.error('Error in GuardianFarm.onNewDay:', error); } } } module.exports = GuardianFarm;