From 8bc821e51664a19459708f9478e83ef1278f1efc Mon Sep 17 00:00:00 2001 From: Misode Date: Fri, 25 Oct 2024 02:44:29 +0200 Subject: [PATCH] Make fixed lists and tuples consistent --- .../components/generator/McdocRenderer.tsx | 84 ++++++++++++++----- src/app/components/previews/Decorator.ts | 4 +- 2 files changed, 64 insertions(+), 24 deletions(-) diff --git a/src/app/components/generator/McdocRenderer.tsx b/src/app/components/generator/McdocRenderer.tsx index a02b9e04..320662d5 100644 --- a/src/app/components/generator/McdocRenderer.tsx +++ b/src/app/components/generator/McdocRenderer.tsx @@ -69,10 +69,13 @@ function Head({ type, optional, node, makeEdit, ctx }: HeadProps) { return } if (type.kind === 'list' || type.kind === 'byte_array' || type.kind === 'int_array' || type.kind === 'long_array') { + if (type.lengthRange?.min !== undefined && type.lengthRange.min === type.lengthRange.max) { + return getItemType(type)), attributes: type.attributes }} optional={optional} node={node} makeEdit={makeEdit} ctx={ctx} /> + } return } if (type.kind === 'tuple') { - return <> + return } if (type.kind === 'literal') { return @@ -351,11 +354,6 @@ interface ListHeadProps extends Props { function ListHead({ type, node, makeEdit, ctx }: ListHeadProps) { const { locale } = useLocale() - const fixedRange = type.lengthRange?.min !== undefined && type.lengthRange.min === type.lengthRange.max - if (fixedRange) { - return <> - } - const canAdd = (type.lengthRange?.max ?? Infinity) > (node?.children?.length ?? 0) const onAddTop = useCallback(() => { @@ -391,6 +389,42 @@ function ListHead({ type, node, makeEdit, ctx }: ListHeadProps) { } +interface TupleHeadProps extends HeadProps { + type: TupleType, +} +function TupleHead({ type, optional, node, makeEdit, ctx }: TupleHeadProps) { + const { locale } = useLocale() + + const onRemove = useCallback(() => { + makeEdit(() => { + return undefined + }) + }, [makeEdit]) + + const onSetDefault = useCallback(() => { + makeEdit((range) => { + return getDefault(type, range, ctx) + }) + }, [type, ctx]) + + if (optional) { + if (node && JsonArrayNode.is(node)) { + return + } else { + return + } + } else { + if (!node || !JsonArrayNode.is(node)) { + return + } + return <> + } +} + interface LiteralHeadProps extends HeadProps { type: LiteralType } @@ -415,8 +449,15 @@ function Body({ type, optional, node, makeEdit, ctx }: BodyProps) { } if (type.kind === 'list' || type.kind === 'byte_array' || type.kind === 'int_array' || type.kind === 'long_array') { - const fixedRange = type.lengthRange?.min !== undefined && type.lengthRange.min === type.lengthRange.max - if (!fixedRange && (!node || node.children?.length === 0)) { + if (!JsonArrayNode.is(node)) { + return <> + } + if (type.lengthRange?.min !== undefined && type.lengthRange.min === type.lengthRange.max) { + return
+ getItemType(type)), attributes: type.attributes }} node={node} makeEdit={makeEdit} ctx={ctx} /> +
+ } + if (node.children?.length === 0) { return <> } return
@@ -534,9 +575,7 @@ function ListBody({ type: outerType, node, makeEdit, ctx }: ListBodyProps) { return <> } const type = (node.typeDef?.kind === 'list' || node.typeDef?.kind === 'byte_array' || node.typeDef?.kind === 'int_array' || node.typeDef?.kind === 'long_array') ? node.typeDef : outerType - const fixedRange = type.lengthRange?.min !== undefined && type.lengthRange.min === type.lengthRange.max - - const canAdd = !fixedRange && (type.lengthRange?.max ?? Infinity) > (node?.children?.length ?? 0) + const canAdd = (type.lengthRange?.max ?? Infinity) > (node?.children?.length ?? 0) const onRemoveItem = useCallback((index: number) => { makeEdit(() => { @@ -616,19 +655,17 @@ function ListBody({ type: outerType, node, makeEdit, ctx }: ListBodyProps) { return
- {!fixedRange && <> - + {(canMoveUp || canMoveDown) &&
+ - {(canMoveUp || canMoveDown) &&
- - -
} - } + +
}
@@ -853,6 +890,7 @@ function getCategory(type: McdocType) { case '::java::data::loot::LootCondition': case '::java::data::worldgen::dimension::biome_source::BiomeSource': case '::java::data::worldgen::processor_list::ProcessorRule': + case '::java::data::worldgen::feature::placement::PlacementModifier': return 'predicate' case '::java::data::loot::LootFunction': case '::java::data::worldgen::density_function::CubicSpline': diff --git a/src/app/components/previews/Decorator.ts b/src/app/components/previews/Decorator.ts index f65b4c78..f3a37820 100644 --- a/src/app/components/previews/Decorator.ts +++ b/src/app/components/previews/Decorator.ts @@ -62,7 +62,9 @@ function decorateY(pos: BlockPos, y: number): BlockPos[] { } export function sampleInt(value: any, ctx: PlacementContext): number { - if (typeof value === 'number') { + if (value === undefined) { + return 0 + } else if (typeof value === 'number') { return value } else if (value.base) { return value.base ?? 1 + ctx.nextInt(1 + (value.spread ?? 0))