Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Validate the size of the random range #1670

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 47 additions & 3 deletions packages/java-edition/src/mcfunction/parser/argument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,15 @@ export const argument: mcf.ArgumentParserGetter = (
case 'minecraft:entity_summon':
return wrap(core.resourceLocation({ category: 'entity_type' }))
case 'minecraft:float_range':
return wrap(range('float'))
return wrap(
range(
'float',
treeNode.properties?.min,
treeNode.properties?.max,
treeNode.properties?.minSpan,
treeNode.properties?.maxSpan,
),
)
case 'minecraft:function':
return wrap(core.resourceLocation({ category: 'function', allowTag: true }))
case 'minecraft:gamemode':
Expand All @@ -181,7 +189,15 @@ export const argument: mcf.ArgumentParserGetter = (
case 'minecraft:heightmap':
return wrap(core.literal(...HeightmapValues))
case 'minecraft:int_range':
return wrap(range('integer'))
return wrap(
range(
'integer',
treeNode.properties?.min,
treeNode.properties?.max,
treeNode.properties?.minSpan,
treeNode.properties?.maxSpan,
),
)
case 'minecraft:item_enchantment':
return wrap(core.resourceLocation({ category: 'enchantment' }))
case 'minecraft:item_predicate':
Expand Down Expand Up @@ -650,18 +666,24 @@ function range(
type: 'float',
min?: number,
max?: number,
minSpan?: number,
maxSpan?: number,
cycleable?: boolean,
): core.Parser<FloatRangeNode>
function range(
type: 'integer',
min?: number,
max?: number,
minSpan?: number,
maxSpan?: number,
cycleable?: boolean,
): core.Parser<IntRangeNode>
function range(
type: 'float' | 'integer',
min?: number,
max?: number,
minSpan?: number,
maxSpan?: number,
cycleable?: boolean,
): core.Parser<FloatRangeNode | IntRangeNode> {
const number: core.Parser<core.FloatNode | core.IntegerNode> = type === 'float'
Expand Down Expand Up @@ -707,6 +729,21 @@ function range(
localize('mcfunction.parser.range.min>max', ans.value[0], ans.value[1]),
res,
)
} else if (minSpan !== undefined || maxSpan !== undefined) {
const span = ans.value[0] !== undefined && ans.value[1] !== undefined
? Math.abs(ans.value[0] - ans.value[1])
: (ans.value[0] ?? ans.value[1] ?? Infinity)
if (minSpan !== undefined && span < minSpan) {
ctx.err.report(
localize('mcfunction.parser.range.span-too-small', span, minSpan),
res,
)
} else if (maxSpan !== undefined && span > maxSpan) {
ctx.err.report(
localize('mcfunction.parser.range.span-too-large', span, maxSpan),
res,
)
}
}
return ans
},
Expand Down Expand Up @@ -1234,7 +1271,14 @@ export function selector(ignoreInvalidPrefix = false): core.Parser<EntitySelecto
case 'x_rotation':
case 'y_rotation':
return core.map<FloatRangeNode>(
range('float', undefined, undefined, true),
range(
'float',
undefined,
undefined,
undefined,
undefined,
true,
),
(res, _, ctx) => {
if (hasKey(key.value)) {
ctx.err.report(
Expand Down
8 changes: 8 additions & 0 deletions packages/java-edition/src/mcfunction/tree/argument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,15 @@ export interface MinecraftEntityAnchorArgumentTreeNode extends mcf.ArgumentTreeN
export interface MinecraftEntitySummonArgumentTreeNode extends mcf.ArgumentTreeNode {
parser: 'minecraft:entity_summon'
}
export interface RangeProperties extends Record<string, unknown> {
min: number
max: number
minSpan: number
maxSpan: number
}
export interface MinecraftFloatRangeArgumentTreeNode extends mcf.ArgumentTreeNode {
parser: 'minecraft:float_range'
properties?: RangeProperties
}
export interface MinecraftFunctionArgumentTreeNode extends mcf.ArgumentTreeNode {
parser: 'minecraft:function'
Expand All @@ -76,6 +83,7 @@ export interface MinecraftHeightmapArgumentTreeNode extends mcf.ArgumentTreeNode
}
export interface MinecraftIntRangeArgumentTreeNode extends mcf.ArgumentTreeNode {
parser: 'minecraft:int_range'
properties?: RangeProperties
}
export interface MinecraftItemEnchantmentArgumentTreeNode extends mcf.ArgumentTreeNode {
parser: 'minecraft:item_enchantment'
Expand Down
8 changes: 8 additions & 0 deletions packages/java-edition/src/mcfunction/tree/patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,10 @@ export function getPatch(release: ReleaseVersion): PartialRootTreeNode {
roll: {
children: {
range: {
properties: {
minSpan: 1,
maxSpan: 2147483646,
},
children: {
sequence: {
properties: {
Expand All @@ -538,6 +542,10 @@ export function getPatch(release: ReleaseVersion): PartialRootTreeNode {
value: {
children: {
range: {
properties: {
minSpan: 1,
maxSpan: 2147483646,
},
children: {
sequence: {
properties: {
Expand Down
2 changes: 2 additions & 0 deletions packages/locales/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@
"mcfunction.parser.no-permission": "Permission level %0% is required, which is higher than %1% defined in config",
"mcfunction.parser.objective.too-long": "Objective names cannot be longer than %0% characters",
"mcfunction.parser.range.min>max": "The minimum value %0% is larger than the maximum value %1%",
"mcfunction.parser.range.span-too-large": "The range size %0% is larger than the maximum %1%",
"mcfunction.parser.range.span-too-small": "The range size %0% is smaller than the minimum %1%",
"mcfunction.parser.score_holder.fake-name.too-long": "Fake names cannot be longer than %0% characters",
"mcfunction.parser.sep": "a space (%0%)",
"mcfunction.parser.trailing": "Trailing data encountered: %0%",
Expand Down
Loading