alias default variable format

This commit is contained in:
Asaki Yuki 2026-02-23 17:54:12 +07:00
parent 58a1893c1d
commit 0b1f80da43
5 changed files with 42 additions and 8 deletions

View file

@ -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",

View file

@ -15,6 +15,23 @@ export function FormatProperties(properties: any) {
property_bag[<Binding>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]) {

View file

@ -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)
}

View file

@ -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(<Binding>input_binding),
}
},
vs: input_variable => {
if (!isVariable(input_variable)) throw new Error("Invalid input binding")
return {
doNotAddParentesis: true,
value: vs(<Variable>input_variable),
}
},

View file

@ -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 <Variable>`${name}${mode}`
else {
if (rndMap.has(input)) return <Variable>rndMap.get(input)
if (rndMap.has(input)) return <Variable>`${rndMap.get(input)}${mode}`
else {
const ret = RandomVariableBinding()
rndMap.set(input, ret)
return ret
return <Variable>`${ret}${mode}`
}
}
}