Skip to content

Commit

Permalink
fix nbtconv test
Browse files Browse the repository at this point in the history
  • Loading branch information
robinbraemer committed Dec 30, 2023
1 parent 286ed28 commit ec01f33
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 36 deletions.
35 changes: 16 additions & 19 deletions pkg/edition/java/proto/nbtconv/snbt_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,26 +159,9 @@ func BinaryTagToJSON(tag *nbt.RawMessage) (json.RawMessage, error) {

// SnbtToBinaryTag converts a stringified NBT to binary tag.
func SnbtToBinaryTag(snbt string) (nbt.RawMessage, error) {
// Convert SNBT to JSON
j, err := SnbtToJSON(snbt)
if err != nil {
return nbt.RawMessage{}, err
}
// Then convert JSON to binary tag
return JsonToBinaryTag(j)
}

// JsonToBinaryTag converts a JSON to binary tag.
func JsonToBinaryTag(j json.RawMessage) (nbt.RawMessage, error) {
// Convert JSON to snbt
snbt, err := JsonToSNBT(j)
if err != nil {
return nbt.RawMessage{}, err
}

// Then convert snbt to bytes
buf := new(bytes.Buffer)
err = nbt.StringifiedMessage(snbt).MarshalNBT(buf)
err := nbt.StringifiedMessage(snbt).MarshalNBT(buf)
if err != nil {
return nbt.RawMessage{}, fmt.Errorf("error marshalling snbt to binary: %w", err)
}
Expand Down Expand Up @@ -232,6 +215,20 @@ func JsonToBinaryTag(j json.RawMessage) (nbt.RawMessage, error) {
if _, err = dec.Decode(&m); err != nil {
return m, fmt.Errorf("error decoding binary tag: %w", err)
}

return m, nil
}

// JsonToBinaryTag converts a JSON to binary tag.
//
// Note that type information such as boolean is lost in the conversion, since
// SNBT uses 1 and 0 byte values for booleans which are not distinguishable from
// JSON numbers.
//
// Example: {"a":1,"b":"hello","c":"world","d":true} -> {a:1,b:hello,c:"world",d:1}
func JsonToBinaryTag(j json.RawMessage) (nbt.RawMessage, error) {
snbt, err := JsonToSNBT(j)
if err != nil {
return nbt.RawMessage{}, err
}
return SnbtToBinaryTag(snbt)
}
2 changes: 1 addition & 1 deletion pkg/edition/java/proto/nbtconv/snbt_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestSnbtToJSON(t *testing.T) {
{
name: "without spaces",
snbt: `{a:1,b:hello,c:"world",d:1}`,
want: json.RawMessage(`{"a":1,"b":"hello","c":"world","d":true}`),
want: json.RawMessage(`{"a":1,"b":"hello","c":"world","d":1}`),
wantErr: false,
},
{
Expand Down
33 changes: 18 additions & 15 deletions pkg/edition/java/proto/packet/packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"encoding/json"
"fmt"
"go.minekube.com/common/minecraft/key"
"go.minekube.com/gate/pkg/edition/java/proto/nbtconv"
"go.minekube.com/gate/pkg/edition/java/proto/packet/config"
"go.minekube.com/gate/pkg/edition/java/proto/util"
"io"
Expand Down Expand Up @@ -38,7 +39,7 @@ import (
"go.minekube.com/gate/pkg/util/uuid"
)

// TODO write utility that records all known packets into gob for use in testing.
// TODO write utility that while running the proxy records all known packets into .bin (per protocol version) for use in testing.
var (
//go:embed testdata/PlayerChat-1.19.gob
playerChatPacketGob []byte
Expand All @@ -55,7 +56,7 @@ func init() {
panic(err)
}

dimensionBinaryTag, err = chat.SnbtToBinaryTag(dimensionCodecSnbt)
dimensionBinaryTag, err = nbtconv.SnbtToBinaryTag(dimensionCodecSnbt)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -163,24 +164,26 @@ var packets = []proto.Packet{
},
},
&JoinGame{
EntityID: 4,
Gamemode: 1,
Dimension: 4,
PartialHashedSeed: 1,
Difficulty: 3,
EntityID: 1,
Gamemode: 2,
Dimension: 3,
PartialHashedSeed: 4,
Difficulty: 5,
Hardcore: true,
MaxPlayers: 3,
LevelType: ptr("test"),
ViewDistance: 3,
MaxPlayers: 6,
LevelType: ptr("myLevelType"),
ViewDistance: 7,
ReducedDebugInfo: true,
ShowRespawnScreen: true,
DoLimitedCrafting: true,
LevelNames: []string{"level1", "level2"},
Registry: dimensionBinaryTag, // still use dimension codec for now
DimensionInfo: mustFake(&DimensionInfo{}),
LevelNames: []string{"test", "test2"},
PreviousGamemode: 2,
SimulationDistance: 3,
LastDeathPosition: mustFake(&DeathPosition{}),
CurrentDimensionData: dimensionBinaryTag,
Registry: dimensionBinaryTag, // still use dimension codec for now
PreviousGamemode: 8,
SimulationDistance: 9,
LastDeathPosition: mustFake(&DeathPosition{}),
PortalCooldown: 10,
},
&Respawn{
Dimension: 1,
Expand Down
2 changes: 1 addition & 1 deletion pkg/edition/java/proto/util/nbt.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func WriteBinaryTag(w io.Writer, protocol proto.Protocol, bt BinaryTag) error {
}
if protocol.Lower(version.Minecraft_1_20_2) {
// Empty name
if err := WriteInt16(w, 0); err != nil {
if err := WriteUint16(w, 0); err != nil {
return fmt.Errorf("error writing binary tag name: %w", err)
}
}
Expand Down

0 comments on commit ec01f33

Please sign in to comment.