Skip to content

Commit

Permalink
fix: remove scientific notation formatting for small scale numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
TheWii committed Jun 23, 2024
1 parent 8bf0eac commit e72080d
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 8 deletions.
6 changes: 4 additions & 2 deletions bolt_expressions/ast_converter.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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}'.")

Expand Down Expand Up @@ -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():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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
4 changes: 1 addition & 3 deletions examples/source_lazy/src/data/test/functions/main.mcfunction
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"pack": {
"pack_format": 26,
"pack_format": 41,
"description": ""
}
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"pack": {
"pack_format": 26,
"pack_format": 41,
"description": ""
}
}

0 comments on commit e72080d

Please sign in to comment.