Skip to content

Commit

Permalink
Added proxy
Browse files Browse the repository at this point in the history
Added proxy in bot.config to avoid issue with types object
changed all code te reasign config using Object.assign
  • Loading branch information
sefirosweb committed Apr 1, 2024
1 parent 3578ecf commit 0257d84
Show file tree
Hide file tree
Showing 21 changed files with 75 additions and 58 deletions.
34 changes: 34 additions & 0 deletions core/src/createNewBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,6 +20,7 @@ export type Props = {
version?: string
}


export const createNewBot = (props: Props): Bot => {
const {
botName = 'Legion',
Expand All @@ -34,6 +38,36 @@ export const createNewBot = (props: Props): Bot => {
version
})

const botConfig = new Proxy(cloneDeep(defaultConfig), {
set: <K extends keyof Config>(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);
Expand Down
4 changes: 2 additions & 2 deletions core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down
19 changes: 1 addition & 18 deletions core/src/modules/botConfig.ts
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -40,16 +25,14 @@ export default (_bot: Bot, botName: string) => {
save = true
}

bot.config = botConfig
Object.assign(bot.config, botConfig)

if (save) {
saveBotConfig(botName)
}
}

export const saveBotConfig = async (botName?: string) => {
fixConfigTypes()

const dir = path.join(__dirname, '..', 'botConfig')

try {
Expand Down
4 changes: 2 additions & 2 deletions core/src/modules/botWebsocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<BotFriends> = []
Expand Down Expand Up @@ -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 });
})
Expand Down
16 changes: 8 additions & 8 deletions core/test/legionTests/01_makeHoleBasic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})

Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion core/test/legionTests/01_makeHoleWater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('01 Mining in water', function () {
minerCords
}

bot.config = config
Object.assign(bot.config, config)
bot.emit('reloadBotConfig')
})

Expand Down
2 changes: 1 addition & 1 deletion core/test/legionTests/01_makeHoleWaterAndLava.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})

Expand Down
8 changes: 4 additions & 4 deletions core/test/legionTests/02_makeTunel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})

Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion core/test/legionTests/02_makeTunelInLiquid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})

Expand Down
8 changes: 4 additions & 4 deletions core/test/legionTests/03_makeTunelAllDirections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -86,7 +86,7 @@ describe('03 Make tunnel in all directions', function () {
minerCords
}

bot.config = config
Object.assign(bot.config, config)
bot.emit('reloadBotConfig')


Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion core/test/legionTests/04_getReadyAndCombat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})

Expand Down
2 changes: 1 addition & 1 deletion core/test/legionTests/05_longRange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})

Expand Down
2 changes: 1 addition & 1 deletion core/test/legionTests/06_woodCutter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})

Expand Down
2 changes: 1 addition & 1 deletion core/test/legionTests/07_farming_bamboo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})

Expand Down
2 changes: 1 addition & 1 deletion core/test/legionTests/08_farming_cactus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})

Expand Down
2 changes: 1 addition & 1 deletion core/test/legionTests/09_farming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})

Expand Down
4 changes: 2 additions & 2 deletions core/test/legionTests/10_butcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions core/test/legionTests/11_breeder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}

Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion core/test/legionTests/12_sorting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})

Expand Down
Loading

0 comments on commit 0257d84

Please sign in to comment.