Skip to content

Commit

Permalink
snbt support float point
Browse files Browse the repository at this point in the history
  • Loading branch information
Tnze committed Jun 19, 2024
1 parent 2f003c5 commit 300304e
Show file tree
Hide file tree
Showing 7 changed files with 560 additions and 6 deletions.
10 changes: 8 additions & 2 deletions nbt/snbt_decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,8 @@ func parseLiteral(literal []byte) (byte, any, error) {
strlen := len(literal)
integer := true
number := true
hasExp := false
afterExp := false
unqstr := true
var numberType byte

Expand All @@ -484,14 +486,18 @@ func parseLiteral(literal []byte) (byte, any, error) {
if i == strlen-1 && i != 0 && isIntegerType(c) {
numberType = c
strlen--
} else if i > 0 || i == 0 && c != '-' {
} else if i > 0 || i == 0 && c != '-' && c != '+' {
integer = false
if i == 0 || c != '.' {
number = false
}
}
} else if number {
if i == strlen-1 && isFloatType(c) {
if hasExp && !afterExp && c == '-' || c == '+' {
afterExp = true
} else if c == 'E' || c == 'e' {
hasExp = true
} else if i == strlen-1 && isFloatType(c) {
numberType = c
} else {
number = false
Expand Down
30 changes: 28 additions & 2 deletions nbt/snbt_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func stateBeginValue(s *scanner, c byte) int {
case '"', '\'': // beginning of TAG_String
return stateBeginString(s, c)
default:
if isNumber(c) {
if isNumber(c) || c == '-' || c == '+' {
stateNum0(s, c)
return scanBeginLiteral
}
Expand Down Expand Up @@ -240,7 +240,7 @@ func stateArrayT(s *scanner, c byte) int {
}

func stateNum0(s *scanner, c byte) int {
if isNumber(c) {
if isNumber(c) || c == '-' || c == '+' {
s.step = stateNum1
return scanContinue
}
Expand All @@ -266,6 +266,11 @@ func stateNumDot(s *scanner, c byte) int {
s.step = stateNumDot0
return scanContinue
}
switch c {
case 'e', 'E':
s.step = stateNumExp
return scanContinue
}
if isAllowedInUnquotedString(c) {
s.step = stateInUnquotedString
return scanContinue
Expand All @@ -280,6 +285,27 @@ func stateNumDot0(s *scanner, c byte) int {
s.step = stateNumDot0
return scanContinue
}
switch c {
case 'e', 'E':
s.step = stateNumExp
return scanContinue
}
return stateEndNumDotValue(s, c)
}

func stateNumExp(s *scanner, c byte) int {
if isNumber(c) || c == '-' || c == '+' {
s.step = stateNumExp0
return scanContinue
}
return stateEndNumDotValue(s, c)
}

func stateNumExp0(s *scanner, c byte) int {
if isNumber(c) {
s.step = stateNumExp0
return scanContinue
}
return stateEndNumDotValue(s, c)
}

Expand Down
13 changes: 11 additions & 2 deletions nbt/snbt_scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,28 @@ func TestSNBT_number(t *testing.T) {
}
}

//go:embed bigTest_test.snbt
//go:embed testdata/bigTest_test.snbt
var bigTestSNBT string

//go:embed 1-dimension_codec.snbt
//go:embed testdata/1-dimension_codec.snbt
var dimensionCodecSNBT string

//go:embed testdata/58f6356e-b30c-4811-8bfc-d72a9ee99e73.dat.snbt
var tnzePlayerDat string

//go:embed testdata/level.dat.snbt
var tnzeLevelDat string

func TestSNBT_compound(t *testing.T) {
goods := []string{
`{}`, `{name:3.14f}`, `{ "name" : 12345 }`,
`{ abc: { }}`, `{ "a b\"c": {}, def: 12345}`,
`{ ghi: [], klm: 1}`,
`{T: 1.2E3d, U: 1.2e-3D, V: 12e3d, W: -1.2E3F }`,
bigTestSNBT,
dimensionCodecSNBT,
tnzePlayerDat,
tnzeLevelDat,
}
var s scanner
for _, str := range goods {
Expand Down
File renamed without changes.
169 changes: 169 additions & 0 deletions nbt/testdata/58f6356e-b30c-4811-8bfc-d72a9ee99e73.dat.snbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
{
DataVersion: 3218,
data: [],
palette: [],
AbsorptionAmount: 0.0f,
Air: 300s,
Attributes: [
{
Base: 0.10000000149011612d,
Name: "minecraft:generic.movement_speed"
}
],
Brain: {
memories: {}
},
DeathTime: 0s,
Dimension: "minecraft:overworld",
EnderItems: [],
FallDistance: 0.0f,
FallFlying: 0b,
Fire: -20s,
Health: 20.0f,
HurtByTimestamp: 0,
HurtTime: 0s,
Inventory: [
{
Count: 1b,
Slot: 0b,
id: "minecraft:obsidian"
},
{
Count: 1b,
Slot: 1b,
id: "minecraft:end_portal_frame"
},
{
Count: 2b,
Slot: 2b,
id: "minecraft:redstone"
},
{
Count: 1b,
Slot: 3b,
id: "minecraft:piston"
},
{
Count: 1b,
Slot: 4b,
id: "minecraft:sticky_piston"
},
{
Count: 1b,
Slot: 5b,
id: "minecraft:redstone_block"
},
{
Count: 1b,
Slot: 6b,
id: "minecraft:white_concrete"
},
{
Count: 1b,
Slot: 7b,
id: "minecraft:diorite_wall"
},
{
Count: 1b,
Slot: 8b,
id: "minecraft:birch_fence"
},
{
Count: 1b,
Slot: 9b,
id: "minecraft:lever"
}
],
Invulnerable: 0b,
Motion: [
0.0d,
0.0d,
0.0d
],
OnGround: 0b,
PortalCooldown: 0,
Pos: [
-241.52665309975936d,
92.96455951628484d,
-226.6873418918888d
],
Rotation: [
-179.86888f,
35.09954f
],
Score: 0,
SelectedItemSlot: 6,
SleepTimer: 0s,
UUID: [I; 1492530542, -1291040751, -1946364118, -1628856717],
XpLevel: 0,
XpP: 0.0f,
XpSeed: 1529348704,
XpTotal: 0,
abilities: {
flySpeed: 0.05f,
flying: 1b,
instabuild: 1b,
invulnerable: 1b,
mayBuild: 1b,
mayfly: 1b,
walkSpeed: 0.1f
},
foodExhaustionLevel: 0.0f,
foodLevel: 20,
foodSaturationLevel: 5.0f,
foodTickTimer: 0,
playerGameType: 1,
previousPlayerGameType: 0,
recipeBook: {
isBlastingFurnaceFilteringCraftable: 0b,
isBlastingFurnaceGuiOpen: 0b,
isFilteringCraftable: 0b,
isFurnaceFilteringCraftable: 0b,
isFurnaceGuiOpen: 0b,
isGuiOpen: 0b,
isSmokerFilteringCraftable: 0b,
isSmokerGuiOpen: 0b,
recipes: [
"minecraft:redstone_torch",
"minecraft:piston",
"minecraft:chest",
"minecraft:pumpkin_seeds",
"minecraft:flint_and_steel",
"minecraft:enchanting_table",
"minecraft:target",
"minecraft:ender_chest",
"minecraft:note_block",
"minecraft:end_crystal",
"minecraft:redstone_block",
"minecraft:pumpkin_pie",
"minecraft:clock",
"minecraft:dropper",
"minecraft:redstone",
"minecraft:compass"
],
toBeDisplayed: [
"minecraft:redstone_torch",
"minecraft:piston",
"minecraft:chest",
"minecraft:pumpkin_seeds",
"minecraft:flint_and_steel",
"minecraft:enchanting_table",
"minecraft:target",
"minecraft:ender_chest",
"minecraft:note_block",
"minecraft:end_crystal",
"minecraft:redstone_block",
"minecraft:pumpkin_pie",
"minecraft:clock",
"minecraft:dropper",
"minecraft:redstone",
"minecraft:compass"
]
},
seenCredits: 1b,
warden_spawn_tracker: {
cooldown_ticks: 0,
ticks_since_last_warning: 6931,
warning_level: 0
}
}
File renamed without changes.
Loading

0 comments on commit 300304e

Please sign in to comment.