Skip to content

Commit

Permalink
Fix convert string snbt to json
Browse files Browse the repository at this point in the history
  • Loading branch information
robinbraemer committed Dec 30, 2023
1 parent 36e1c60 commit 286ed28
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
23 changes: 16 additions & 7 deletions pkg/edition/java/proto/nbtconv/snbt_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package nbtconv

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"github.com/Tnze/go-mc/nbt"
"gopkg.in/yaml.v3"
"io"
"log/slog"
"strconv"
"strings"
)

Expand Down Expand Up @@ -45,14 +48,20 @@ func SnbtToJSON(snbt string) (json.RawMessage, error) {

// Ensure that input is not empty or trivially malformed
if len(snbt) < 2 || !strings.HasPrefix(snbt, "{") || !strings.HasSuffix(snbt, "}") {
// get first and last few characters of input and put ... in between
var truncated string
if len(snbt) > 10 {
truncated = snbt[:5] + "..." + snbt[len(snbt)-5:]
} else {
truncated = snbt
if slog.Default().Enabled(context.TODO(), slog.LevelDebug) {
// get first and last few characters of input and put ... in between
var truncated string
if len(snbt) > 10 {
truncated = snbt[:5] + "..." + snbt[len(snbt)-5:]
} else {
truncated = snbt
}
slog.Debug("got non-object snbt", "snbt", truncated)
}
return nil, fmt.Errorf("%w: but got %q", errSNBTInvalid, truncated)

// just a json string
return json.RawMessage(strconv.Quote(snbt)), nil

}

// Add spaces after colons that are not within quotes
Expand Down
4 changes: 2 additions & 2 deletions pkg/edition/java/proto/packet/chat/component_holder.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (c *ComponentHolder) AsComponentOrNil() component.Component {
}
comp, err := c.AsComponent()
if err != nil {
slog.Debug("error while converting component holder to component", "error", err)
slog.Error("error while converting component holder to component", "error", err)
return nil
}
return comp
Expand Down Expand Up @@ -137,7 +137,7 @@ func (c *ComponentHolder) AsJsonOrNil() json.RawMessage {
}
j, err := c.AsJson()
if err != nil {
slog.Debug("error while converting component holder to json", "error", err)
slog.Error("error while converting component holder to json", "error", err)
return nil
}
return j
Expand Down

0 comments on commit 286ed28

Please sign in to comment.