From e72080d2ffada4fd6c4adc912b90e08a84569505 Mon Sep 17 00:00:00 2001 From: TheWii <67249660+TheWii@users.noreply.github.com> Date: Sun, 23 Jun 2024 15:09:13 -0300 Subject: [PATCH] fix: remove scientific notation formatting for small scale numbers --- bolt_expressions/ast_converter.py | 6 ++++-- .../src/data/test/functions/main.mcfunction | 15 ++++++++++++++- .../src/data/test/functions/main.mcfunction | 4 +--- .../pack.mcmeta | 2 +- .../test/functions/init_expressions.mcfunction | 3 +++ .../data/test/functions/main.mcfunction | 7 +++++++ .../pack.mcmeta | 2 +- 7 files changed, 31 insertions(+), 8 deletions(-) diff --git a/bolt_expressions/ast_converter.py b/bolt_expressions/ast_converter.py index fe8506b..50626fb 100644 --- a/bolt_expressions/ast_converter.py +++ b/bolt_expressions/ast_converter.py @@ -1,6 +1,7 @@ from dataclasses import dataclass, field from typing import Any, Generator, Iterable, cast from mecha import AstChildren, AstCommand, Mecha, Visitor, rule, AstRoot +from mecha.utils import number_to_string from nbtlib import Byte, Short, Int, Long, Float, Double # type: ignore from .typing import NBT_TYPE_STRING, NbtTypeString, NumericNbtValue @@ -108,7 +109,8 @@ def store(self, node: IrStore) -> Generator[IrNode, str, str]: case IrScore(): return f"execute store {type} score {value} run" case IrData(): - return f"execute store {type} {value} {nbt_type} {node.scale} run" + scale = number_to_string(node.scale) + return f"execute store {type} {value} {nbt_type} {scale} run" case _: raise ValueError(f"Invalid store source '{node.value}'.") @@ -476,7 +478,7 @@ def cast(self, node: IrCast) -> Generator[IrNode, str, None]: right = yield node.right nbt_type = self.serialize_nbt_type(node.cast_type) - scale = node.scale + scale = number_to_string(node.scale) match node.left, node.right: case IrData(), IrLiteral(): diff --git a/examples/operation_nbt_scale/src/data/test/functions/main.mcfunction b/examples/operation_nbt_scale/src/data/test/functions/main.mcfunction index ccd10b4..39b75f4 100644 --- a/examples/operation_nbt_scale/src/data/test/functions/main.mcfunction +++ b/examples/operation_nbt_scale/src/data/test/functions/main.mcfunction @@ -38,4 +38,17 @@ say # prepend scaled score temp.list.prepend(obj["$value"] * 100) say # insert scaled score -temp.list.insert(3, obj["$value"] * 100) \ No newline at end of file +temp.list.insert(3, obj["$value"] * 100) + + +TICKS_PER_SECOND = 20 +TICKS_PER_MINUTE = TICKS_PER_SECOND * 60 +TICKS_PER_HOUR = TICKS_PER_MINUTE * 60 + +temp.hours = (obj["ticks_since_death"] - temp.death_gametime) / TICKS_PER_HOUR + +obj["ticks_since_death"] = obj["ticks_since_death"] % TICKS_PER_MINUTE +temp.minutes = obj["ticks_since_death"] / TICKS_PER_MINUTE + +obj["ticks_since_death"] = obj["ticks_since_death"] % TICKS_PER_SECOND +temp.seconds = obj["ticks_since_death"] / TICKS_PER_SECOND \ No newline at end of file diff --git a/examples/source_lazy/src/data/test/functions/main.mcfunction b/examples/source_lazy/src/data/test/functions/main.mcfunction index 63779b8..d1cd1ff 100644 --- a/examples/source_lazy/src/data/test/functions/main.mcfunction +++ b/examples/source_lazy/src/data/test/functions/main.mcfunction @@ -73,9 +73,7 @@ def static_vars(f): return f"{f.__name__}{i}" def decorated(*args, **kwargs): - with - Expression.temp_score.override(format=score_format, reset=True), - Expression.temp_data.override(format=data_format, reset=True): + with Expression.temp_score.override(format=score_format, reset=True), Expression.temp_data.override(format=data_format, reset=True): return f(*args, **kwargs) return decorated diff --git a/tests/snapshots/examples__build_operation_condition__0.data_pack/pack.mcmeta b/tests/snapshots/examples__build_operation_condition__0.data_pack/pack.mcmeta index 12cf462..6e8dcf0 100644 --- a/tests/snapshots/examples__build_operation_condition__0.data_pack/pack.mcmeta +++ b/tests/snapshots/examples__build_operation_condition__0.data_pack/pack.mcmeta @@ -1,6 +1,6 @@ { "pack": { - "pack_format": 26, + "pack_format": 41, "description": "" } } diff --git a/tests/snapshots/examples__build_operation_nbt_scale__0.data_pack/data/test/functions/init_expressions.mcfunction b/tests/snapshots/examples__build_operation_nbt_scale__0.data_pack/data/test/functions/init_expressions.mcfunction index 816aad3..6578df3 100644 --- a/tests/snapshots/examples__build_operation_nbt_scale__0.data_pack/data/test/functions/init_expressions.mcfunction +++ b/tests/snapshots/examples__build_operation_nbt_scale__0.data_pack/data/test/functions/init_expressions.mcfunction @@ -1 +1,4 @@ +scoreboard objectives add bolt.expr.const dummy scoreboard objectives add bolt.expr.temp dummy +scoreboard players set $1200 bolt.expr.const 1200 +scoreboard players set $20 bolt.expr.const 20 diff --git a/tests/snapshots/examples__build_operation_nbt_scale__0.data_pack/data/test/functions/main.mcfunction b/tests/snapshots/examples__build_operation_nbt_scale__0.data_pack/data/test/functions/main.mcfunction index bfdf6ee..b9dce37 100644 --- a/tests/snapshots/examples__build_operation_nbt_scale__0.data_pack/data/test/functions/main.mcfunction +++ b/tests/snapshots/examples__build_operation_nbt_scale__0.data_pack/data/test/functions/main.mcfunction @@ -33,3 +33,10 @@ say # insert scaled score scoreboard players operation $i0 bolt.expr.temp = $value obj data modify storage demo list insert 3 value 0 execute store result storage demo list[3] int 100 run scoreboard players get $i0 bolt.expr.temp +scoreboard players operation $i0 bolt.expr.temp = ticks_since_death obj +execute store result score $i1 bolt.expr.temp run data get storage demo death_gametime 1 +execute store result storage demo hours double 0.00001388888888888889 run scoreboard players operation $i0 bolt.expr.temp -= $i1 bolt.expr.temp +scoreboard players operation ticks_since_death obj %= $1200 bolt.expr.const +execute store result storage demo minutes double 0.0008333333333333334 run scoreboard players get ticks_since_death obj +scoreboard players operation ticks_since_death obj %= $20 bolt.expr.const +execute store result storage demo seconds double 0.05 run scoreboard players get ticks_since_death obj diff --git a/tests/snapshots/examples__build_source_typing__0.data_pack/pack.mcmeta b/tests/snapshots/examples__build_source_typing__0.data_pack/pack.mcmeta index 12cf462..6e8dcf0 100644 --- a/tests/snapshots/examples__build_source_typing__0.data_pack/pack.mcmeta +++ b/tests/snapshots/examples__build_source_typing__0.data_pack/pack.mcmeta @@ -1,6 +1,6 @@ { "pack": { - "pack_format": 26, + "pack_format": 41, "description": "" } }