Skip to content

Commit

Permalink
should fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
zardoy committed Sep 30, 2024
1 parent ab5f6ab commit 5b56518
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 133 deletions.
2 changes: 1 addition & 1 deletion prismarine-viewer/viewer/lib/worldDataEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { EventEmitter } from 'events'
import { generateSpiralMatrix, ViewRect } from 'flying-squid/dist/utils'
import { Vec3 } from 'vec3'
import { BotEvents } from 'mineflayer'
import { getItemFromBlock } from '../../../src/botUtils'
import { getItemFromBlock } from '../../../src/chatUtils'
import { chunkPos } from './simpleUtils'

export type ChunkPosKey = string
Expand Down
122 changes: 0 additions & 122 deletions src/botUtils.ts
Original file line number Diff line number Diff line change
@@ -1,127 +1,5 @@
// this should actually be moved to mineflayer / prismarine-viewer

import { fromFormattedString, TextComponent } from '@xmcl/text-component'
import type { IndexedData } from 'minecraft-data'
import { versionToNumber } from 'prismarine-viewer/viewer/prepare/utils'

export type MessageFormatPart = Pick<TextComponent, 'hoverEvent' | 'clickEvent'> & {
text: string
color?: string
bold?: boolean
italic?: boolean
underlined?: boolean
strikethrough?: boolean
obfuscated?: boolean
}

type MessageInput = {
text?: string
translate?: string
with?: Array<MessageInput | string>
color?: string
bold?: boolean
italic?: boolean
underlined?: boolean
strikethrough?: boolean
obfuscated?: boolean
extra?: MessageInput[]
json?: any
}

const global = globalThis as any

// todo move to sign-renderer, replace with prismarine-chat, fix mcData issue!
export const formatMessage = (message: MessageInput, mcData: IndexedData = global.loadedData) => {
let msglist: MessageFormatPart[] = []

const readMsg = (msg: MessageInput) => {
const styles = {
color: msg.color,
bold: !!msg.bold,
italic: !!msg.italic,
underlined: !!msg.underlined,
strikethrough: !!msg.strikethrough,
obfuscated: !!msg.obfuscated
}

if (msg.text) {
msglist.push({
...msg,
text: msg.text,
...styles
})
} else if (msg.translate) {
const tText = mcData?.language[msg.translate] ?? msg.translate

if (msg.with) {
const splitted = tText.split(/%s|%\d+\$s/g)

let i = 0
for (const [j, part] of splitted.entries()) {
msglist.push({ text: part, ...styles })

if (j + 1 < splitted.length) {
if (msg.with[i]) {
const msgWith = msg.with[i]
if (typeof msgWith === 'string') {
readMsg({
...styles,
text: msgWith
})
} else {
readMsg({
...styles,
...msgWith
})
}
}
i++
}
}
} else {
msglist.push({
...msg,
text: tText,
...styles
})
}
}

if (msg.extra) {
for (const ex of msg.extra) {
readMsg({ ...styles, ...ex })
}
}
}

readMsg(message)

const flat = (msg) => {
return [msg, msg.extra?.flatMap(flat) ?? []]
}

msglist = msglist.map(msg => {
// normalize §
if (!msg.text.includes?.('§')) return msg
const newMsg = fromFormattedString(msg.text)
return flat(newMsg)
}).flat(Infinity)

return msglist
}

const blockToItemRemaps = {
water: 'water_bucket',
lava: 'lava_bucket',
redstone_wire: 'redstone',
tripwire: 'tripwire_hook'
}

export const getItemFromBlock = (block: import('prismarine-block').Block) => {
const item = global.loadedData.itemsByName[blockToItemRemaps[block.name] ?? block.name]
return item
}

export const displayClientChat = (text: string) => {
const message = {
text
Expand Down
2 changes: 1 addition & 1 deletion src/botUtils.test.ts → src/chatUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test, expect } from 'vitest'
import mcData from 'minecraft-data'
import { formatMessage } from './botUtils'
import { formatMessage } from './chatUtils'

//@ts-expect-error
globalThis.loadedData ??= mcData('1.20.1')
Expand Down
123 changes: 123 additions & 0 deletions src/chatUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// this should actually be moved to mineflayer / prismarine-viewer

import { fromFormattedString, TextComponent } from '@xmcl/text-component'
import type { IndexedData } from 'minecraft-data'
import { versionToNumber } from 'prismarine-viewer/viewer/prepare/utils'

export type MessageFormatPart = Pick<TextComponent, 'hoverEvent' | 'clickEvent'> & {
text: string
color?: string
bold?: boolean
italic?: boolean
underlined?: boolean
strikethrough?: boolean
obfuscated?: boolean
}

type MessageInput = {
text?: string
translate?: string
with?: Array<MessageInput | string>
color?: string
bold?: boolean
italic?: boolean
underlined?: boolean
strikethrough?: boolean
obfuscated?: boolean
extra?: MessageInput[]
json?: any
}

const global = globalThis as any

// todo move to sign-renderer, replace with prismarine-chat, fix mcData issue!
export const formatMessage = (message: MessageInput, mcData: IndexedData = global.loadedData) => {
let msglist: MessageFormatPart[] = []

const readMsg = (msg: MessageInput) => {
const styles = {
color: msg.color,
bold: !!msg.bold,
italic: !!msg.italic,
underlined: !!msg.underlined,
strikethrough: !!msg.strikethrough,
obfuscated: !!msg.obfuscated
}

if (msg.text) {
msglist.push({
...msg,
text: msg.text,
...styles
})
} else if (msg.translate) {
const tText = mcData?.language[msg.translate] ?? msg.translate

if (msg.with) {
const splitted = tText.split(/%s|%\d+\$s/g)

let i = 0
for (const [j, part] of splitted.entries()) {
msglist.push({ text: part, ...styles })

if (j + 1 < splitted.length) {
if (msg.with[i]) {
const msgWith = msg.with[i]
if (typeof msgWith === 'string') {
readMsg({
...styles,
text: msgWith
})
} else {
readMsg({
...styles,
...msgWith
})
}
}
i++
}
}
} else {
msglist.push({
...msg,
text: tText,
...styles
})
}
}

if (msg.extra) {
for (const ex of msg.extra) {
readMsg({ ...styles, ...ex })
}
}
}

readMsg(message)

const flat = (msg) => {
return [msg, msg.extra?.flatMap(flat) ?? []]
}

msglist = msglist.map(msg => {
// normalize §
if (!msg.text.includes?.('§')) return msg
const newMsg = fromFormattedString(msg.text)
return flat(newMsg)
}).flat(Infinity)

return msglist
}

const blockToItemRemaps = {
water: 'water_bucket',
lava: 'lava_bucket',
redstone_wire: 'redstone',
tripwire: 'tripwire_hook'
}

export const getItemFromBlock = (block: import('prismarine-block').Block) => {
const item = global.loadedData.itemsByName[blockToItemRemaps[block.name] ?? block.name]
return item
}
2 changes: 1 addition & 1 deletion src/controls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { customCommandsConfig } from './customCommands'
import type { CustomCommand } from './react/KeybindingsCustom'
import { showOptionsModal } from './react/SelectOption'
import widgets from './react/widgets'
import { getItemFromBlock } from './botUtils'
import { getItemFromBlock } from './chatUtils'
import { gamepadUiCursorState, moveGamepadCursorByPx } from './react/GamepadUiCursor'
import { completeTexturePackInstall, resourcePackState } from './resourcePack'
import { showNotification } from './react/NotificationProvider'
Expand Down
3 changes: 2 additions & 1 deletion src/inventoryWindows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import { appReplacableResources } from './generated/resources'
import { activeModalStack, hideCurrentModal, hideModal, miscUiState, showModal } from './globalState'
import { options } from './optionsStorage'
import { assertDefined, inGameError } from './utils'
import { displayClientChat, MessageFormatPart } from './botUtils'
import { displayClientChat } from './botUtils'
import { currentScaling } from './scaleInterface'
import { getItemDescription } from './itemsDescriptions'
import { MessageFormatPart } from './chatUtils'

const loadedImagesCache = new Map<string, HTMLImageElement>()
const cleanLoadedImagesCache = () => {
Expand Down
2 changes: 1 addition & 1 deletion src/react/Chat.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Meta, StoryObj } from '@storybook/react'

import { useEffect, useState } from 'react'
import { formatMessage } from '../botUtils'
import { formatMessage } from '../chatUtils'
import Chat, { fadeMessage, chatInputValueGlobal } from './Chat'
import Button from './Button'

Expand Down
2 changes: 1 addition & 1 deletion src/react/Chat.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { proxy, subscribe } from 'valtio'
import { useEffect, useMemo, useRef, useState } from 'react'
import { MessageFormatPart } from '../botUtils'
import { MessageFormatPart } from '../chatUtils'
import { MessagePart } from './MessageFormatted'
import './Chat.css'
import { isIos, reactKeyForMessage } from './utils'
Expand Down
2 changes: 1 addition & 1 deletion src/react/ChatProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEffect, useMemo, useRef, useState } from 'react'
import { useSnapshot } from 'valtio'
import { formatMessage } from '../botUtils'
import { formatMessage } from '../chatUtils'
import { getBuiltinCommandsList, tryHandleBuiltinCommand } from '../builtinCommands'
import { hideCurrentModal, loadedGameState, miscUiState } from '../globalState'
import { options } from '../optionsStorage'
Expand Down
2 changes: 1 addition & 1 deletion src/react/DeathScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import './deathScreen.css'
import type { MessageFormatPart } from '../botUtils'
import type { MessageFormatPart } from '../chatUtils'
import MessageFormatted from './MessageFormatted'
import Button from './Button'

Expand Down
2 changes: 1 addition & 1 deletion src/react/DeathScreenProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useEffect } from 'react'
import { proxy, useSnapshot } from 'valtio'
import { disconnect } from '../flyingSquidUtils'
import { MessageFormatPart, formatMessage } from '../botUtils'
import { MessageFormatPart, formatMessage } from '../chatUtils'
import { showModal, hideModal } from '../globalState'
import { options } from '../optionsStorage'
import DeathScreen from './DeathScreen'
Expand Down
2 changes: 1 addition & 1 deletion src/react/MessageFormatted.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { render } from '@xmcl/text-component'
import { noCase } from 'change-case'
import mojangson from 'mojangson'
import { openURL } from 'prismarine-viewer/viewer/lib/simpleUtils'
import { MessageFormatPart } from '../botUtils'
import { MessageFormatPart } from '../chatUtils'
import { chatInputValueGlobal } from './Chat'
import './MessageFormatted.css'

Expand Down
2 changes: 1 addition & 1 deletion src/react/MessageFormattedString.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useMemo } from 'react'
import { fromFormattedString } from '@xmcl/text-component'
import { formatMessage } from '../botUtils'
import { formatMessage } from '../chatUtils'
import MessageFormatted from './MessageFormatted'

/** like MessageFormatted, but receives raw string or json instead, uses window.loadedData */
Expand Down

0 comments on commit 5b56518

Please sign in to comment.