diff --git a/package.json b/package.json index b26ebf3..adbfbc1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "asajs", - "version": "4.1.6", + "version": "4.1.7", "description": "Create your Minecraft JSON-UI resource packs using JavaScript", "keywords": [ "Minecraft", diff --git a/src/compilers/FormatProperties.ts b/src/compilers/FormatProperties.ts index bb7d556..2a1b2f0 100644 --- a/src/compilers/FormatProperties.ts +++ b/src/compilers/FormatProperties.ts @@ -15,6 +15,23 @@ export function FormatProperties(properties: any) { property_bag[key] = value delete properties[key] } + + if (key.startsWith("$")) { + const [varName, varType] = key.split("|") + + switch (varType) { + case "d": + properties[`${varName}|default`] = value + delete properties[key] + break + + case "default": + break + + default: + throw new Error("Invalid variable type") + } + } } if (config.compiler?.fixInventoryItemRenderer && property_bag[BagBinding.ITEM_ID_AUX]) { diff --git a/src/compilers/bindings/Checker.ts b/src/compilers/bindings/Checker.ts index aace94b..208a37b 100644 --- a/src/compilers/bindings/Checker.ts +++ b/src/compilers/bindings/Checker.ts @@ -34,6 +34,10 @@ export function isBinding(input: string) { return /^#\w+$/.test(input) } +export function isVariable(input: string) { + return /^\$\w+$/.test(input) +} + export function isNumber(input: string) { return /^[+-]?(?:\d+|\d+\.\d*|\.\d+)(?:[eE][+-]?\d+)?$/.test(input) } diff --git a/src/compilers/bindings/Function.ts b/src/compilers/bindings/Function.ts index 533c89b..0fa1fe3 100644 --- a/src/compilers/bindings/Function.ts +++ b/src/compilers/bindings/Function.ts @@ -1,7 +1,7 @@ -import { bs, RandomBindingString, RandomString, ResolveBinding } from "../../components/Utils.js" -import { BindingItem } from "../../types/properties/value.js" +import { bs, RandomBindingString, RandomString, ResolveBinding, vs } from "../../components/Utils.js" +import { Binding, BindingItem, Variable } from "../../types/properties/value.js" import { bindingFuntions } from "../Configuration.js" -import { isBinding, isNumber, isString } from "./Checker.js" +import { isBinding, isNumber, isString, isVariable } from "./Checker.js" import { Expression, GenBinding } from "./types.js" type CallbackRet = { @@ -244,7 +244,15 @@ export const defaultFunctions = { if (!isBinding(input_binding)) throw new Error("Invalid input binding") return { doNotAddParentesis: true, - value: bs(<`#${string}`>input_binding), + value: bs(input_binding), + } + }, + + vs: input_variable => { + if (!isVariable(input_variable)) throw new Error("Invalid input binding") + return { + doNotAddParentesis: true, + value: vs(input_variable), } }, diff --git a/src/components/Utils.ts b/src/components/Utils.ts index 463412a..9af6086 100644 --- a/src/components/Utils.ts +++ b/src/components/Utils.ts @@ -235,13 +235,18 @@ export function bs(input: Binding): Binding { } export function vs(input: Variable): Variable { - if (isNotObfuscate) return input + let [name, mode]: [Variable, string] = input.split("|") as [Variable, string] + input = name + if (mode) mode = "|" + mode + else mode = "" + + if (isNotObfuscate) return `${name}${mode}` else { - if (rndMap.has(input)) return rndMap.get(input) + if (rndMap.has(input)) return `${rndMap.get(input)}${mode}` else { const ret = RandomVariableBinding() rndMap.set(input, ret) - return ret + return `${ret}${mode}` } } }