Skip to content

Commit

Permalink
🧑‍💻 lib:array/math/max, min を追加
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenCMD committed Oct 31, 2024
1 parent 23d556e commit d7aaad2
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 3 deletions.
11 changes: 8 additions & 3 deletions TheSkyBlessing/data/lib/functions/array/_index.d.mcfunction
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@
# @within function lib:array/**compare*
#declare score_holder $Temp

#> sum処理用
# @within function lib:array/**sum
#> 一部処理用
# @within function
# lib:array/**sum
# lib:array/**max
# lib:array/**min
#declare score_holder $isNumeric

#> 算術処理用
# @within function
# lib:array/**math/*
#declare score_holder $Sum
#declare score_holder $Max
#declare score_holder $Min
#declare score_holder $Mul
#declare score_holder $Temp
#declare score_holder $Temp2
#declare score_holder $Temp2
11 changes: 11 additions & 0 deletions TheSkyBlessing/data/lib/functions/array/core/math/max.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#> lib:array/core/math/max
# @within function lib:array/**max

# 取得/計算
execute if score $isNumeric Temporary matches 1 store result score $Temp Temporary run data get storage lib: Array[-1] 100
execute if score $isNumeric Temporary matches 0 store result score $Temp Temporary run data get storage lib: Array[-1]
scoreboard players operation $Max Temporary > $Temp Temporary
# 末尾削除
data remove storage lib: Array[-1]
# 要素がまだあるなら再帰
execute if data storage lib: Array[0] run function lib:array/core/math/max
11 changes: 11 additions & 0 deletions TheSkyBlessing/data/lib/functions/array/core/math/min.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#> lib:array/core/math/min
# @within function lib:array/**min

# 取得/計算
execute if score $isNumeric Temporary matches 1 store result score $Temp Temporary run data get storage lib: Array[-1] 100
execute if score $isNumeric Temporary matches 0 store result score $Temp Temporary run data get storage lib: Array[-1]
scoreboard players operation $Min Temporary < $Temp Temporary
# 末尾削除
data remove storage lib: Array[-1]
# 要素がまだあるなら再帰
execute if data storage lib: Array[0] run function lib:array/core/math/min
32 changes: 32 additions & 0 deletions TheSkyBlessing/data/lib/functions/array/math/max.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#> lib:array/math/max
#
# 配列の要素の最大値を計算します。
#
# 配列の値の型がNumeric型ではない場合は、`data get`で取得されるint値を値として扱います。
# 例えば、`List[String]`をArrayに渡した場合、各文字列の長さの最大値を返します。
#
# @input
# T extends any
# storage lib: Array: [T]
# 配列データ
# @output
# storage lib: MaxResult: T extends Numeric ? double : int
# 最大値
# @api

# セッションチェック
execute if data storage lib: {ArrayLibSessionOpened:false} run tellraw @a [{"storage":"global","nbt":"Prefix.ERROR"},{"text":"lib:array/のセッションが開かれずに利用されています。","color":"white"}]

# 初期化
scoreboard players set $Max Temporary 0
# 配列が Numeric 型か検証する
execute store success score $isNumeric Temporary run data get storage lib: Array[-1] 1
# 最大値を計算する
function lib:array/core/math/max
# 総和をSumResultに突っ込む
execute if score $isNumeric Temporary matches 0 store result storage lib: SumResult int 1.00 run scoreboard players get $Max Temporary
execute if score $isNumeric Temporary matches 1 store result storage lib: SumResult double 0.01 run scoreboard players get $Max Temporary
# リセット
scoreboard players reset $Max Temporary
scoreboard players reset $Temp Temporary
scoreboard players reset $isNumeric Temporary
32 changes: 32 additions & 0 deletions TheSkyBlessing/data/lib/functions/array/math/min.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#> lib:array/math/min
#
# 配列の要素の最小値を計算します。
#
# 配列の値の型がNumeric型ではない場合は、`data get`で取得されるint値を値として扱います。
# 例えば、`List[String]`をArrayに渡した場合、各文字列の長さの最小値を返します。
#
# @input
# T extends any
# storage lib: Array: [T]
# 配列データ
# @output
# storage lib: MaxResult: T extends Numeric ? double : int
# 最小値
# @api

# セッションチェック
execute if data storage lib: {ArrayLibSessionOpened:false} run tellraw @a [{"storage":"global","nbt":"Prefix.ERROR"},{"text":"lib:array/のセッションが開かれずに利用されています。","color":"white"}]

# 初期化
scoreboard players set $Min Temporary 0
# 配列が Numeric 型か検証する
execute store success score $isNumeric Temporary run data get storage lib: Array[-1] 1
# 最小値を計算する
function lib:array/core/math/min
# 総和をSumResultに突っ込む
execute if score $isNumeric Temporary matches 0 store result storage lib: SumResult int 1.00 run scoreboard players get $Min Temporary
execute if score $isNumeric Temporary matches 1 store result storage lib: SumResult double 0.01 run scoreboard players get $Min Temporary
# リセット
scoreboard players reset $Min Temporary
scoreboard players reset $Temp Temporary
scoreboard players reset $isNumeric Temporary

0 comments on commit d7aaad2

Please sign in to comment.