From 0d3a3affd75c72de49a02132adf8ef5bd13210b7 Mon Sep 17 00:00:00 2001 From: Vitaly Turovsky Date: Tue, 3 Sep 2024 01:00:54 +0300 Subject: [PATCH] fix recently introduced bug with crafting in singleplayer --- scripts/makeOptimizedMcData.mjs | 4 ++- scripts/testOptimizedMcdata.ts | 35 ++++++++++++++----- ...tation_console_controller_gamepad_icon.svg | 1 - src/inventoryWindows.ts | 6 ++-- src/optimizeJson.ts | 7 +++- src/{ => react}/GlobalSearchInput.tsx | 5 ++- src/react/Input.tsx | 12 +++++-- src/reactUi.tsx | 2 +- 8 files changed, 52 insertions(+), 20 deletions(-) delete mode 100644 src/cross_playstation_console_controller_gamepad_icon.svg rename src/{ => react}/GlobalSearchInput.tsx (87%) diff --git a/scripts/makeOptimizedMcData.mjs b/scripts/makeOptimizedMcData.mjs index d0adcae2..9794aeed 100644 --- a/scripts/makeOptimizedMcData.mjs +++ b/scripts/makeOptimizedMcData.mjs @@ -118,7 +118,9 @@ const dataTypeBundling = { blockLoot: { arrKey: 'block' }, - recipes: {}, // todo we can do better + recipes: { + raw: true + }, // todo we can do better blockCollisionShapes: {}, loginPacket: {}, protocol: { diff --git a/scripts/testOptimizedMcdata.ts b/scripts/testOptimizedMcdata.ts index 17b5f7ed..d6c74384 100644 --- a/scripts/testOptimizedMcdata.ts +++ b/scripts/testOptimizedMcdata.ts @@ -8,7 +8,7 @@ const json = JSON.parse(fs.readFileSync('./generated/minecraft-data-optimized.js const dataPaths = require('minecraft-data/minecraft-data/data/dataPaths.json') const validateData = (ver, type) => { - const target = JsonOptimizer.restoreData(json[type], ver) + const target = JsonOptimizer.restoreData(structuredClone(json[type]), ver) const arrKey = json[type].arrKey const originalPath = dataPaths.pc[ver][type] const original = require(`minecraft-data/minecraft-data/data/${originalPath}/${type}.json`) @@ -43,16 +43,33 @@ const validateData = (ver, type) => { } } -const checkObj = (source, diffing) => { - checkKeys(Object.keys(source), Object.keys(diffing)) - for (const [key, val] of Object.entries(source)) { - if (JSON.stringify(val) !== JSON.stringify(diffing[key])) { - throw new Error(`different value of ${key}: ${val} ${diffing[key]}`) - } +const sortObj = (obj) => { + const sorted = {} + for (const key of Object.keys(obj).sort()) { + sorted[key] = obj[key] } + return sorted } -const checkKeys = (source, diffing, isUniq = true, msg = '', redunantOk = false) => { +const checkObj = (source, diffing) => { + if (!Array.isArray(source)) { + source = sortObj(source) + } + if (!Array.isArray(diffing)) { + diffing = sortObj(diffing) + } + if (JSON.stringify(source) !== JSON.stringify(diffing)) { + throw new Error(`different value: ${JSON.stringify(source)} ${JSON.stringify(diffing)}`) + } + // checkKeys(Object.keys(source), Object.keys(diffing)) + // for (const [key, val] of Object.entries(source)) { + // if (JSON.stringify(val) !== JSON.stringify(diffing[key])) { + // throw new Error(`different value of ${key}: ${val} ${diffing[key]}`) + // } + // } +} + +const checkKeys = (source, diffing, isUniq = true, msg = '', redundantIsOk = false) => { if (isUniq) { for (const [i, item] of diffing.entries()) { if (diffing.indexOf(item) !== i) { @@ -65,7 +82,7 @@ const checkKeys = (source, diffing, isUniq = true, msg = '', redunantOk = false) throw new Error(`Diffing does not include "${key}" (${msg})`) } } - if (!redunantOk) { + if (!redundantIsOk) { for (const key of diffing) { if (!source.includes(key)) { throw new Error(`Source does not include "${key}" (${msg})`) diff --git a/src/cross_playstation_console_controller_gamepad_icon.svg b/src/cross_playstation_console_controller_gamepad_icon.svg deleted file mode 100644 index d7d176e2..00000000 --- a/src/cross_playstation_console_controller_gamepad_icon.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/src/inventoryWindows.ts b/src/inventoryWindows.ts index e5964646..b23c88fa 100644 --- a/src/inventoryWindows.ts +++ b/src/inventoryWindows.ts @@ -69,11 +69,12 @@ export const onGameLoad = (onLoad) => { } }) + // workaround: singleplayer player inventory crafting bot.inventory.on('updateSlot', ((_oldSlot, oldItem, newItem) => { - const oldSlot = _oldSlot as number + const currentSlot = _oldSlot as number if (!miscUiState.singleplayer) return const { craftingResultSlot } = bot.inventory - if (oldSlot === craftingResultSlot && oldItem && !newItem) { + if (currentSlot === craftingResultSlot && oldItem && !newItem) { for (let i = 1; i < 5; i++) { const count = bot.inventory.slots[i]?.count if (count && count > 1) { @@ -86,6 +87,7 @@ export const onGameLoad = (onLoad) => { } return } + if (currentSlot > 4) return const craftingSlots = bot.inventory.slots.slice(1, 5) try { const resultingItem = getResultingRecipe(craftingSlots, 2) diff --git a/src/optimizeJson.ts b/src/optimizeJson.ts index 442a72a1..00547ffe 100644 --- a/src/optimizeJson.ts +++ b/src/optimizeJson.ts @@ -192,7 +192,12 @@ export default class JsonOptimizer { for (const [key, removePropsId] of removedProps) { for (const removePropId of removePropsId) { const removeProp = propertiesById[removePropId] - delete dataByKeys[key][removeProp] + // todo: this is not correct! + if (Array.isArray(dataByKeys[key])) { + dataByKeys[key].splice(removeProp, 1) + } else { + delete dataByKeys[key][removeProp] + } } } if (versionToNumber(versionKey) <= versionToNumber(targetKey)) { diff --git a/src/GlobalSearchInput.tsx b/src/react/GlobalSearchInput.tsx similarity index 87% rename from src/GlobalSearchInput.tsx rename to src/react/GlobalSearchInput.tsx index d9266950..6f2d56d1 100644 --- a/src/GlobalSearchInput.tsx +++ b/src/react/GlobalSearchInput.tsx @@ -1,6 +1,6 @@ import { useSnapshot } from 'valtio' -import { miscUiState } from './globalState' -import Input from './react/Input' +import { miscUiState } from '../globalState' +import Input from './Input' function InnerSearch () { const { currentTouch } = useSnapshot(miscUiState) @@ -19,7 +19,6 @@ function InnerSearch () { autoFocus={currentTouch === false} width={50} placeholder='Search...' - defaultValue="" onChange={({ target: { value } }) => { customEvents.emit('search', value) }} diff --git a/src/react/Input.tsx b/src/react/Input.tsx index c3d40491..41dbc7ba 100644 --- a/src/react/Input.tsx +++ b/src/react/Input.tsx @@ -27,8 +27,16 @@ export default ({ autoFocus, rootStyles, inputRef, validateInput, ...inputProps return
{ setValidationStyle(validateInput?.(e.target.value) ?? {}) setValue(e.target.value) diff --git a/src/reactUi.tsx b/src/reactUi.tsx index b40c47a1..029b2493 100644 --- a/src/reactUi.tsx +++ b/src/reactUi.tsx @@ -28,7 +28,7 @@ import SoundMuffler from './react/SoundMuffler' import TouchControls from './react/TouchControls' import widgets from './react/widgets' import { useIsWidgetActive } from './react/utilsApp' -import GlobalSearchInput from './GlobalSearchInput' +import GlobalSearchInput from './react/GlobalSearchInput' import TouchAreasControlsProvider from './react/TouchAreasControlsProvider' import NotificationProvider, { showNotification } from './react/NotificationProvider' import HotbarRenderApp from './react/HotbarRenderApp'