diff --git a/config.d.ts b/config.d.ts index ac61f82..6c3ff0f 100644 --- a/config.d.ts +++ b/config.d.ts @@ -7,6 +7,7 @@ export interface Config { importToPreview?: boolean autoEnable?: boolean gdkUserId?: string + fixInventoryItemRenderer?: boolean } packinfo?: { name?: string diff --git a/src/compilers/FormatProperties.ts b/src/compilers/FormatProperties.ts index 7ab723c..bb7d556 100644 --- a/src/compilers/FormatProperties.ts +++ b/src/compilers/FormatProperties.ts @@ -1,42 +1,46 @@ -import { AnimationKeyframe } from "../components/AnimationKeyframe.js"; -import { AnimType } from "../types/enums/AnimType.js"; -import { KeyframeAnimationProperties } from "../types/properties/element/Animation.js"; -import { Binding } from "../types/properties/value.js"; +import { AnimationKeyframe } from "../components/AnimationKeyframe.js" +import { AnimType } from "../types/enums/AnimType.js" +import { BagBinding } from "../types/enums/BagBinding.js" +import { KeyframeAnimationProperties } from "../types/properties/element/Animation.js" +import { Binding } from "../types/properties/value.js" +import { config } from "./Configuration.js" export function FormatProperties(properties: any) { - const property_bag: Record = {}; + const property_bag: Record = {} - for (const key in properties) { - const value = properties[key]; + for (const key in properties) { + const value = properties[key] - if (key.startsWith("#")) { - property_bag[key] = value; - delete properties[key]; - } - } + if (key.startsWith("#")) { + property_bag[key] = value + delete properties[key] + } + } - if (properties.anchor) { - properties.anchor_from = properties.anchor_to = properties.anchor; - delete properties.anchor; - } + if (config.compiler?.fixInventoryItemRenderer && property_bag[BagBinding.ITEM_ID_AUX]) { + property_bag[BagBinding.ITEM_ID_AUX] = `(${property_bag[BagBinding.ITEM_ID_AUX]} / 1)` + } - if (Object.keys(property_bag).length) { - if (properties.property_bag) { - properties.property_bag = { ...property_bag, ...properties.property_bag }; - } else { - properties.property_bag = property_bag; - } - } + if (properties.anchor) { + properties.anchor_from = properties.anchor_to = properties.anchor + delete properties.anchor + } - return properties; + if (Object.keys(property_bag).length) { + if (properties.property_bag) { + properties.property_bag = { ...property_bag, ...properties.property_bag } + } else { + properties.property_bag = property_bag + } + } + + return properties } -export function FormatAnimationProperties( - properties: KeyframeAnimationProperties, -) { - if (properties.next instanceof AnimationKeyframe) { - properties.next = `${properties.next}`; - } +export function FormatAnimationProperties(properties: KeyframeAnimationProperties) { + if (properties.next instanceof AnimationKeyframe) { + properties.next = `${properties.next}` + } - return properties; + return properties } diff --git a/src/compilers/bindings/Function.ts b/src/compilers/bindings/Function.ts index 2d70931..987ea62 100644 --- a/src/compilers/bindings/Function.ts +++ b/src/compilers/bindings/Function.ts @@ -49,20 +49,43 @@ export const defaultFunctions = { } }, - sqrt: n => { - const notAllow = RandomBindingString() - const g = RandomBindingString() - const h = RandomBindingString() + sqrt: input => { const ret = RandomBindingString() + const isNegative = RandomBindingString() + const isLowerThanTwo = RandomBindingString() + const next = RandomBindingString() + const nextEqualOrGreaterThan = RandomBindingString() + + const isNextEqualOrGreaterThanRet = `(${nextEqualOrGreaterThan} * ${ret})` + const isNotNextEqualOrGreaterThanRet = `((not ${nextEqualOrGreaterThan}) * ${next})` + + const lowerThanTwoPart = `(${isLowerThanTwo} * ${input})` + const notLowerThanTwoPart = `((not ${isLowerThanTwo}) * (${isNextEqualOrGreaterThanRet} + ${isNotNextEqualOrGreaterThanRet}))` + + const negativePart = `(${isNegative} * -1)` + const notNegativePart = `((not ${isNegative}) * (${lowerThanTwoPart} + ${notLowerThanTwoPart}))` + return { genBindings: [ { - source: `${n} / 2`, - target: g, + source: `(${input} < 2)`, + target: isLowerThanTwo, }, { - source: `(${notAllow} * -1)`, + source: input, + target: ret, + }, + { + source: `(${ret} + ${input} / ${ret}) / 2`, + target: next, + }, + { + source: `(${next} = ${ret}) or (${next} > ${ret})`, + target: nextEqualOrGreaterThan, + }, + { + source: `${negativePart} + ${notNegativePart}`, target: ret, }, ], @@ -86,6 +109,10 @@ export const defaultFunctions = { } }, + // str_length: str => { + // if (!/\#\w+/.test(str)) throw new Error("Invalid string") + // } + /** * Return a translatable string * @param key