From 0257d84493a92848cba4424b33ec0b4942a90c6e Mon Sep 17 00:00:00 2001 From: Sefirosweb Date: Mon, 1 Apr 2024 12:59:07 +0200 Subject: [PATCH] Added proxy Added proxy in bot.config to avoid issue with types object changed all code te reasign config using Object.assign --- core/src/createNewBot.ts | 34 +++++++++++++++++++ core/src/index.ts | 4 +-- core/src/modules/botConfig.ts | 19 +---------- core/src/modules/botWebsocket.ts | 4 +-- core/test/legionTests/01_makeHoleBasic.ts | 16 ++++----- core/test/legionTests/01_makeHoleWater.ts | 2 +- .../legionTests/01_makeHoleWaterAndLava.ts | 2 +- core/test/legionTests/02_makeTunel.ts | 8 ++--- core/test/legionTests/02_makeTunelInLiquid.ts | 2 +- .../legionTests/03_makeTunelAllDirections.ts | 8 ++--- core/test/legionTests/04_getReadyAndCombat.ts | 2 +- core/test/legionTests/05_longRange.ts | 2 +- core/test/legionTests/06_woodCutter.ts | 2 +- core/test/legionTests/07_farming_bamboo.ts | 2 +- core/test/legionTests/08_farming_cactus.ts | 2 +- core/test/legionTests/09_farming.ts | 2 +- core/test/legionTests/10_butcher.ts | 4 +-- core/test/legionTests/11_breeder.ts | 4 +-- core/test/legionTests/12_sorting.ts | 2 +- core/test/legionTests/13_cross_portals.ts | 10 +++--- core/test/legionTests/plugins/testCommon.ts | 2 +- 21 files changed, 75 insertions(+), 58 deletions(-) diff --git a/core/src/createNewBot.ts b/core/src/createNewBot.ts index b795151..5435022 100644 --- a/core/src/createNewBot.ts +++ b/core/src/createNewBot.ts @@ -7,6 +7,9 @@ import mineflayerPathfinder from 'mineflayer-pathfinder' import hawkEye from 'minecrafthawkeye' import StartStateMachine from '@/NestedStateModules/startStateMachine' import injectBotConfig from "./modules/botConfig"; +import { Vec3 } from "vec3"; +import { Config, defaultConfig } from "base-types"; +import { cloneDeep } from 'lodash' export type Props = { botName?: string; @@ -17,6 +20,7 @@ export type Props = { version?: string } + export const createNewBot = (props: Props): Bot => { const { botName = 'Legion', @@ -34,6 +38,36 @@ export const createNewBot = (props: Props): Bot => { version }) + const botConfig = new Proxy(cloneDeep(defaultConfig), { + set: (target: Config, property: K, value: Config[K]) => { + if (typeof property === 'string') { + + if (property === 'sleepArea') { + // @ts-ignore + value = !value || isNaN(parseFloat(value.x)) || isNaN(parseFloat(value.y)) || isNaN(parseFloat(value.z)) ? undefined : new Vec3(parseFloat(value.x), parseFloat(value.y), parseFloat(value.z)) + } + + if (property === 'patrol') { + // @ts-ignore + value = value.map(p => new Vec3(p.x, p.y, p.z)); + } + + if (property === 'chests') { + // @ts-ignore + value = value.map(c => ({ + ...c, + position: new Vec3(c.position.x, c.position.y, c.position.z) + })) + } + + target[property] = value; + } + return true; + } + }); + + bot.config = botConfig + botWebsocket.loadBot(bot); bot.setMaxListeners(0); diff --git a/core/src/index.ts b/core/src/index.ts index ff8aa17..1acf611 100644 --- a/core/src/index.ts +++ b/core/src/index.ts @@ -5,8 +5,8 @@ import { connectCore } from "@/modules/connectSocket"; const index = () => { const botsToStart: botType[] = [ - // { username: 'Sephi' }, - // { username: 'Types' }, + { username: 'MinerTest' }, + { username: 'MinerTestB' }, // { username: 'Type' }, // { username: "MinerY1" }, diff --git a/core/src/modules/botConfig.ts b/core/src/modules/botConfig.ts index 445dade..88901f9 100644 --- a/core/src/modules/botConfig.ts +++ b/core/src/modules/botConfig.ts @@ -1,26 +1,11 @@ import fs from 'fs' import { Config, defaultConfig } from 'base-types' import path from 'path' -import { Vec3 } from 'vec3' import { Bot } from 'mineflayer' import _ from 'lodash' let bot: Bot -export const fixConfigTypes = () => { - if (bot.config.sleepArea) { - bot.config.sleepArea = new Vec3(bot.config.sleepArea.x, bot.config.sleepArea.y, bot.config.sleepArea.z) - } - - // Transform JSON raw data into Object types - bot.config.patrol = bot.config.patrol.map(p => new Vec3(p.x, p.y, p.z)) - bot.config.chests = bot.config.chests.map(c => ({ - ...c, - position: new Vec3(c.position.x, c.position.y, c.position.z) - })) - -} - export default (_bot: Bot, botName: string) => { bot = _bot @@ -40,7 +25,7 @@ export default (_bot: Bot, botName: string) => { save = true } - bot.config = botConfig + Object.assign(bot.config, botConfig) if (save) { saveBotConfig(botName) @@ -48,8 +33,6 @@ export default (_bot: Bot, botName: string) => { } export const saveBotConfig = async (botName?: string) => { - fixConfigTypes() - const dir = path.join(__dirname, '..', 'botConfig') try { diff --git a/core/src/modules/botWebsocket.ts b/core/src/modules/botWebsocket.ts index 6c98a35..596284f 100644 --- a/core/src/modules/botWebsocket.ts +++ b/core/src/modules/botWebsocket.ts @@ -8,7 +8,7 @@ import { Entity } from 'prismarine-entity' import { Bot } from 'mineflayer' import { connectBotToServer } from '@/modules/connectSocket' import { webSocketQueue } from './queues' -import { fixConfigTypes, saveBotConfig } from './botConfig' +import { saveBotConfig } from './botConfig' let socket: Socket let friends: Array = [] @@ -73,7 +73,7 @@ const connect = async () => { }) socket.on('saveConfig', ({ botConfig }: { botConfig: Config }, response) => { - bot.config = botConfig + Object.assign(bot.config, botConfig) saveBotConfig() response({ success: true }); }) diff --git a/core/test/legionTests/01_makeHoleBasic.ts b/core/test/legionTests/01_makeHoleBasic.ts index 6d61c4a..d8b40a7 100644 --- a/core/test/legionTests/01_makeHoleBasic.ts +++ b/core/test/legionTests/01_makeHoleBasic.ts @@ -47,7 +47,7 @@ describe('01 Basic Mining', function () { bot.creative.stopFlying() bot.test.becomeSurvival() - bot.config = config + Object.assign(bot.config, config) bot.emit('reloadBotConfig') }) @@ -73,7 +73,7 @@ describe('01 Basic Mining', function () { minerCords: newMinerCords } - bot.config = newConfig + Object.assign(bot.config, newConfig) bot.emit('reloadBotConfig') return new Promise((resolve) => { bot.once('finishedJob', () => resolve()) @@ -96,7 +96,7 @@ describe('01 Basic Mining', function () { minerCords: newMinerCords } - bot.config = newConfig + Object.assign(bot.config, newConfig) bot.emit('reloadBotConfig') return new Promise((resolve) => { bot.once('finishedJob', () => resolve()) @@ -119,7 +119,7 @@ describe('01 Basic Mining', function () { minerCords: newMinerCords } - bot.config = newConfig + Object.assign(bot.config, newConfig) bot.emit('reloadBotConfig') return new Promise((resolve) => { bot.once('finishedJob', () => resolve()) @@ -142,7 +142,7 @@ describe('01 Basic Mining', function () { minerCords: newMinerCords } - bot.config = newConfig + Object.assign(bot.config, newConfig) bot.emit('reloadBotConfig') return new Promise((resolve) => { bot.once('finishedJob', () => resolve()) @@ -165,7 +165,7 @@ describe('01 Basic Mining', function () { minerCords: newMinerCords } - bot.config = newConfig + Object.assign(bot.config, newConfig) bot.emit('reloadBotConfig') return new Promise((resolve) => { bot.once('finishedJob', () => resolve()) @@ -188,7 +188,7 @@ describe('01 Basic Mining', function () { minerCords: newMinerCords } - bot.config = newConfig + Object.assign(bot.config, newConfig) bot.emit('reloadBotConfig') return new Promise((resolve) => { bot.once('finishedJob', () => resolve()) @@ -211,7 +211,7 @@ describe('01 Basic Mining', function () { minerCords: newMinerCords } - bot.config = newConfig + Object.assign(bot.config, newConfig) bot.emit('reloadBotConfig') return new Promise((resolve) => { bot.once('finishedJob', () => resolve()) diff --git a/core/test/legionTests/01_makeHoleWater.ts b/core/test/legionTests/01_makeHoleWater.ts index 6676ef4..9689791 100644 --- a/core/test/legionTests/01_makeHoleWater.ts +++ b/core/test/legionTests/01_makeHoleWater.ts @@ -41,7 +41,7 @@ describe('01 Mining in water', function () { minerCords } - bot.config = config + Object.assign(bot.config, config) bot.emit('reloadBotConfig') }) diff --git a/core/test/legionTests/01_makeHoleWaterAndLava.ts b/core/test/legionTests/01_makeHoleWaterAndLava.ts index 44cf995..a264612 100644 --- a/core/test/legionTests/01_makeHoleWaterAndLava.ts +++ b/core/test/legionTests/01_makeHoleWaterAndLava.ts @@ -47,7 +47,7 @@ describe('01 Make hole in liquid & lava', function () { bot.creative.stopFlying() bot.test.becomeSurvival() - bot.config = config + Object.assign(bot.config, config) bot.emit('reloadBotConfig') }) diff --git a/core/test/legionTests/02_makeTunel.ts b/core/test/legionTests/02_makeTunel.ts index 59f856e..ed93d92 100644 --- a/core/test/legionTests/02_makeTunel.ts +++ b/core/test/legionTests/02_makeTunel.ts @@ -47,7 +47,7 @@ describe('02 Make basic tunnel', function () { bot.creative.stopFlying() bot.test.becomeSurvival() - bot.config = config + Object.assign(bot.config, config) bot.emit('reloadBotConfig') }) @@ -71,7 +71,7 @@ describe('02 Make basic tunnel', function () { minerCords: newMinerCords } - bot.config = newConfig + Object.assign(bot.config, newConfig) bot.emit('reloadBotConfig') return new Promise((resolve) => { bot.once('finishedJob', () => resolve()) @@ -94,7 +94,7 @@ describe('02 Make basic tunnel', function () { minerCords: newMinerCords } - bot.config = newConfig + Object.assign(bot.config, newConfig) bot.emit('reloadBotConfig') return new Promise((resolve) => { bot.once('finishedJob', () => resolve()) @@ -117,7 +117,7 @@ describe('02 Make basic tunnel', function () { minerCords: newMinerCords } - bot.config = newConfig + Object.assign(bot.config, newConfig) bot.emit('reloadBotConfig') return new Promise((resolve) => { bot.once('finishedJob', () => resolve()) diff --git a/core/test/legionTests/02_makeTunelInLiquid.ts b/core/test/legionTests/02_makeTunelInLiquid.ts index cbc659d..0a02321 100644 --- a/core/test/legionTests/02_makeTunelInLiquid.ts +++ b/core/test/legionTests/02_makeTunelInLiquid.ts @@ -59,7 +59,7 @@ describe('02 Make basic tunnel in water & lava', function () { bot.creative.stopFlying() bot.test.becomeSurvival() - bot.config = config + Object.assign(bot.config, config) bot.emit('reloadBotConfig') }) diff --git a/core/test/legionTests/03_makeTunelAllDirections.ts b/core/test/legionTests/03_makeTunelAllDirections.ts index 236ba5b..964922d 100644 --- a/core/test/legionTests/03_makeTunelAllDirections.ts +++ b/core/test/legionTests/03_makeTunelAllDirections.ts @@ -51,7 +51,7 @@ describe('03 Make tunnel in all directions', function () { minerCords } - bot.config = config + Object.assign(bot.config, config) bot.emit('reloadBotConfig') return new Promise((resolve) => { @@ -86,7 +86,7 @@ describe('03 Make tunnel in all directions', function () { minerCords } - bot.config = config + Object.assign(bot.config, config) bot.emit('reloadBotConfig') @@ -122,7 +122,7 @@ describe('03 Make tunnel in all directions', function () { minerCords } - bot.config = config + Object.assign(bot.config, config) bot.emit('reloadBotConfig') return new Promise((resolve) => { @@ -157,7 +157,7 @@ describe('03 Make tunnel in all directions', function () { minerCords } - bot.config = config + Object.assign(bot.config, config) bot.emit('reloadBotConfig') return new Promise((resolve) => { diff --git a/core/test/legionTests/04_getReadyAndCombat.ts b/core/test/legionTests/04_getReadyAndCombat.ts index 6c5944b..38688c2 100644 --- a/core/test/legionTests/04_getReadyAndCombat.ts +++ b/core/test/legionTests/04_getReadyAndCombat.ts @@ -153,7 +153,7 @@ describe('04 Get ready for combat', function () { bot.creative.stopFlying() bot.test.becomeSurvival() - bot.config = config + Object.assign(bot.config, config) bot.emit('reloadBotConfig') }) diff --git a/core/test/legionTests/05_longRange.ts b/core/test/legionTests/05_longRange.ts index e917218..5e52dc2 100644 --- a/core/test/legionTests/05_longRange.ts +++ b/core/test/legionTests/05_longRange.ts @@ -28,7 +28,7 @@ describe('05 Test bow long range', function () { bot.creative.stopFlying() bot.test.becomeSurvival() - bot.config = config + Object.assign(bot.config, config) bot.emit('reloadBotConfig') }) diff --git a/core/test/legionTests/06_woodCutter.ts b/core/test/legionTests/06_woodCutter.ts index d2ca812..87780a2 100644 --- a/core/test/legionTests/06_woodCutter.ts +++ b/core/test/legionTests/06_woodCutter.ts @@ -44,7 +44,7 @@ describe('06 Wood cutter', function () { bot.creative.stopFlying() bot.test.becomeSurvival() - bot.config = config + Object.assign(bot.config, config) bot.emit('reloadBotConfig') }) diff --git a/core/test/legionTests/07_farming_bamboo.ts b/core/test/legionTests/07_farming_bamboo.ts index e9c5d77..6ba41d8 100644 --- a/core/test/legionTests/07_farming_bamboo.ts +++ b/core/test/legionTests/07_farming_bamboo.ts @@ -40,7 +40,7 @@ describe('07 Farming bamboo', function () { bot.creative.stopFlying() bot.test.becomeSurvival() - bot.config = config + Object.assign(bot.config, config) bot.emit('reloadBotConfig') }) diff --git a/core/test/legionTests/08_farming_cactus.ts b/core/test/legionTests/08_farming_cactus.ts index 2c213c4..62e17f1 100644 --- a/core/test/legionTests/08_farming_cactus.ts +++ b/core/test/legionTests/08_farming_cactus.ts @@ -43,7 +43,7 @@ describe('08 Farming cactus', function () { bot.creative.stopFlying() bot.test.becomeSurvival() - bot.config = config + Object.assign(bot.config, config) bot.emit('reloadBotConfig') }) diff --git a/core/test/legionTests/09_farming.ts b/core/test/legionTests/09_farming.ts index d767ea8..8cdaef4 100644 --- a/core/test/legionTests/09_farming.ts +++ b/core/test/legionTests/09_farming.ts @@ -139,7 +139,7 @@ describe('09 General farming', function () { bot.creative.stopFlying() bot.test.becomeSurvival() - bot.config = config + Object.assign(bot.config, config) bot.emit('reloadBotConfig') }) diff --git a/core/test/legionTests/10_butcher.ts b/core/test/legionTests/10_butcher.ts index ccc1a75..2c67766 100644 --- a/core/test/legionTests/10_butcher.ts +++ b/core/test/legionTests/10_butcher.ts @@ -75,7 +75,7 @@ describe('10 Butcher animals', function () { bot.creative.stopFlying() bot.test.becomeSurvival() - bot.config = config + Object.assign(bot.config, config) bot.emit('reloadBotConfig') } @@ -125,7 +125,7 @@ describe('10 Butcher animals', function () { }) if (foundAll) { - bot.config = defaultConfig + Object.assign(bot.config, defaultConfig) bot.emit('reloadBotConfig') clearInterval(intervalFeedCows) clearInterval(interval) diff --git a/core/test/legionTests/11_breeder.ts b/core/test/legionTests/11_breeder.ts index 4e180af..b325d73 100644 --- a/core/test/legionTests/11_breeder.ts +++ b/core/test/legionTests/11_breeder.ts @@ -93,7 +93,7 @@ describe('11 Breeder animals', function () { bot.creative.stopFlying() bot.test.becomeSurvival() - bot.config = config + Object.assign(bot.config, config) bot.emit('reloadBotConfig') } @@ -135,7 +135,7 @@ describe('11 Breeder animals', function () { } if (finished) { - bot.config = defaultConfig + Object.assign(bot.config, defaultConfig) bot.emit('reloadBotConfig') clearInterval(interval) resolve() diff --git a/core/test/legionTests/12_sorting.ts b/core/test/legionTests/12_sorting.ts index c349251..adb31ff 100644 --- a/core/test/legionTests/12_sorting.ts +++ b/core/test/legionTests/12_sorting.ts @@ -21,7 +21,7 @@ describe('11 Sorting items', function () { bot.creative.stopFlying() bot.test.becomeSurvival() - bot.config = config + Object.assign(bot.config, config) bot.emit('reloadBotConfig') }) diff --git a/core/test/legionTests/13_cross_portals.ts b/core/test/legionTests/13_cross_portals.ts index c00000d..df3a517 100644 --- a/core/test/legionTests/13_cross_portals.ts +++ b/core/test/legionTests/13_cross_portals.ts @@ -20,7 +20,7 @@ describe('12 Cross the portals', function () { bot.creative.stopFlying() bot.test.becomeSurvival() - bot.config = defaultConfig + Object.assign(bot.config, defaultConfig) bot.emit('reloadBotConfig') }) @@ -67,7 +67,7 @@ describe('12 Cross the portals', function () { } bot.once('checkPortalsOnSpawn', async () => { - bot.config = config + Object.assign(bot.config, config) bot.emit('reloadBotConfig') }) @@ -76,7 +76,7 @@ describe('12 Cross the portals', function () { bot.once('spawn', async () => { bot.once('checkPortalsOnSpawn', async () => { - bot.config = defaultConfig + Object.assign(bot.config, defaultConfig) bot.emit('reloadBotConfig') setTimeout(() => { @@ -134,14 +134,14 @@ describe('12 Cross the portals', function () { bot.once('checkPortalsOnSpawn', async () => { - bot.config = config + Object.assign(bot.config, config) bot.emit('reloadBotConfig') }) return new Promise((resolve) => { bot.once('spawn', () => { bot.once('checkPortalsOnSpawn', async () => { - bot.config = defaultConfig + Object.assign(bot.config, defaultConfig) bot.emit('reloadBotConfig') setTimeout(() => { diff --git a/core/test/legionTests/plugins/testCommon.ts b/core/test/legionTests/plugins/testCommon.ts index feba148..5928fda 100644 --- a/core/test/legionTests/plugins/testCommon.ts +++ b/core/test/legionTests/plugins/testCommon.ts @@ -67,7 +67,7 @@ export default (bot: Bot) => { bot.chat('/kill @e[type=!player]') bot.chat('/gamerule doMobLoot true') bot.chat('/gamerule randomTickSpeed 20') - bot.config = defaultConfig + Object.assign(bot.config, defaultConfig) bot.emit('reloadBotConfig') await becomeCreative() await clearInventory()