-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConvert-JSON2Command.ps1
87 lines (65 loc) · 2.91 KB
/
Convert-JSON2Command.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# input .json file
$filePath = (Get-ChildItem -LiteralPath $PSScriptRoot -Filter "*.json" | Sort-Object -Property LastWriteTime | Select-Object -Last 1).FullName
#$filePath = Join-Path $PSScriptRoot "<fileName>.json"
$nbtJson = Get-Content -LiteralPath $filePath -Encoding UTF8 | ConvertFrom-Json
$resultText = ""
foreach($workBlock in $nbtJson.blocks)
{
$resultText += "/setblock "
$resultText += [string]$workBlock.pos[0] + " "
$resultText += [string]$workBlock.pos[1] + " "
$resultText += [string]$workBlock.pos[2] + " "
$resultText += $nbtJson.palette[$workBlock.state].Name
if($nbtJson.palette[$workBlock.state].Properties -ne $null)
{
$propertiesWork = $nbtJson.palette[$workBlock.state].Properties | ConvertTo-Json -Compress -Depth 64
$propertiesWork = $propertiesWork -replace "^{","[" -replace "}$","]" -replace ":","=" -replace """",""
$resultText += $propertiesWork
}
if($workBlock.nbt -ne $null)
{
$nbtWork = $workBlock.nbt | ConvertTo-Json -Compress -Depth 64
if($nbtWork -notmatch "Text")
{
$nbtWork = $nbtWork -replace """minecraft:(.*?)""","#00#minecraft:`$1#00#"
$nbtWork = $nbtWork -replace """",""
$nbtWork = $nbtWork -replace "#00#",""""
}
$resultText += $nbtWork
}
$resultText += "`r`n"
}
foreach($workEntity in $nbtJson.entities)
{
if($workEntity.nbt.id -ne $null)
{
$resultText += "/summon "
$resultText += $workEntity.nbt.id + " "
# int value
$resultText += [string]$workEntity.blockPos[0] + " "
$resultText += [string]$workEntity.blockPos[1] + " "
$resultText += [string]$workEntity.blockPos[2] + " "
# update nbt.Pos
#$workEntity.nbt.Pos = $workEntity.blockPos | ForEach-Object {[string]$_ + "d"}
# delete nbt.Pos
$workEntity.nbt.psobject.properties.remove("Pos")
<# double value
$resultText += ($workEntity.pos[0] -replace "d","") + " "
$resultText += ($workEntity.pos[1] -replace "d","") + " "
$resultText += ($workEntity.pos[2] -replace "d","") + " "
# update nbt.Pos
$workEntity.nbt.Pos = $workEntity.pos
#>
$resultText += $workEntity.nbt | ConvertTo-Json -Compress -Depth 64
$resultText += "`r`n"
}
}
$resultText = $resultText -replace "`r`n$",""
# delete /setblock x y z minecraft:air
$resultText = $resultText -split "`r`n" | ?{$_ -notmatch "minecraft:air"}
# output .command.txt file
$filePath = $filePath.Substring(0, $filePath.Length - 5) + ".command.txt"
Set-Content -Value $resultText -LiteralPath $filePath -Encoding UTF8
# utf8 no bom
$utf8 = New-Object System.Text.UTF8Encoding $false
Set-Content -Value $utf8.GetBytes((Get-Content -LiteralPath $filePath -Raw)) -LiteralPath $filePath -Encoding Byte