df-mc/dragonfly

Server crashes when using item.DisplayName()

Opened this issue · 3 comments

My give command shows up the item's display name based on the language the player is using. Right when I output the success of the command, the server crashes.

func (t Give) Run(source cmd.Source, output *cmd.Output) {
	targets := t.Player
	itemName := t.ItemName

	data, dataUsed := t.Data.Load()
	if !dataUsed {
		data = 0
	}

	name := string(itemName)
	if !strings.Contains(name, ":") {
		name = "minecraft:" + name
	}

	itemType, isItem := world.ItemByName(name, data)

	if !isItem || name == "air" {
		output.Errorf("Unknown item identifier provided %s", name)
		return
	}

	amount16, amountUsed := t.Amount.Load()
	amount := 0
	if !amountUsed {
		amount = 1
	} else {
		amount = int(math.Max(1, float64(amount16)))
	}

	langTag := language.English
	if p, ok := source.(*player.Player); ok {
		langTag = p.Locale()
	}

	var players = make([]*player.Player, 0)
	for _, target := range targets {
		plr, ok := target.(*player.Player)
		if !ok || plr == nil {
			continue
		}
		players = append(players, plr)
	}

	for _, p := range players {
		if p == nil {
			continue
		}
		given := giveItem(p, itemType, data, amount)
		output.Printf("Gave %s x%d to %s", item.DisplayName(itemType, langTag), given, p.Name())
	}
}
panic: should never happen [recovered]
        panic: should never happen

goroutine 33 [running]:
github.com/df-mc/dragonfly/server/session.(*Session).handlePackets.func1()
        C:/Users/Anthony/go/pkg/mod/github.com/df-mc/dragonfly@v0.9.9-0.20230930211047-d519f75b5eb0/server/session/session.go:303 +0x69
panic({0x94eb00?, 0xe32860?})
        C:/Program Files/Go/src/runtime/panic.go:914 +0x21f
github.com/df-mc/dragonfly/server/item.DisplayName({0xe36c60, 0xc000d3f8d1}, {0xf8d1?, 0xd3?, {0x0?, 0x0?}})
        C:/Users/Anthony/go/pkg/mod/github.com/df-mc/dragonfly@v0.9.9-0.20230930211047-d519f75b5eb0/server/item/item.go:220 +0xa6
github.com/imlighty/hardcore/server/cmd.Give.Run({{0xc000c1a0c0, 0x1, 0x1}, {0xc00037e7e0, 0xd}, {0x0, 0x0}, {0x0, 0x0}}, {0xe3e450, ...}, ...)
        D:/Projects/Hardcore/server/cmd/give.go:120 +0x5c9
github.com/df-mc/dragonfly/server/cmd.Command.executeRunnable({{0xc0010c85d0, 0x1, 0x1}, {0xa512d5, 0x4}, {0xa624b7, 0x19}, {0xc0017d8140, 0x3d}, {0x1652800, ...}}, ...)
        C:/Users/Anthony/go/pkg/mod/github.com/df-mc/dragonfly@v0.9.9-0.20230930211047-d519f75b5eb0/server/cmd/command.go:269 +0x88d
github.com/df-mc/dragonfly/server/cmd.Command.Execute({{0xc0010c85d0, 0x1, 0x1}, {0xa512d5, 0x4}, {0xa624b7, 0x19}, {0xc0017d8140, 0x3d}, {0x1652800, ...}}, ...)
        C:/Users/Anthony/go/pkg/mod/github.com/df-mc/dragonfly@v0.9.9-0.20230930211047-d519f75b5eb0/server/cmd/command.go:138 +0x298
github.com/df-mc/dragonfly/server/player.(*Player).ExecuteCommand(0xc000ada000, {0xc00020a120, 0x1e})
        C:/Users/Anthony/go/pkg/mod/github.com/df-mc/dragonfly@v0.9.9-0.20230930211047-d519f75b5eb0/server/player/player.go:387 +0x367
github.com/df-mc/dragonfly/server/session.(*CommandRequestHandler).Handle(0x972a80?, {0xe3bab8?, 0xc00394a140?}, 0xc0005fe000?)
        C:/Users/Anthony/go/pkg/mod/github.com/df-mc/dragonfly@v0.9.9-0.20230930211047-d519f75b5eb0/server/session/handler_command_request.go:22 +0xa2
github.com/df-mc/dragonfly/server/session.(*Session).handlePacket(0xc0005fe000, {0xe3bab8, 0xc00394a140})
        C:/Users/Anthony/go/pkg/mod/github.com/df-mc/dragonfly@v0.9.9-0.20230930211047-d519f75b5eb0/server/session/session.go:432 +0x8d
github.com/df-mc/dragonfly/server/session.(*Session).handlePackets(0xc0005fe000)
        C:/Users/Anthony/go/pkg/mod/github.com/df-mc/dragonfly@v0.9.9-0.20230930211047-d519f75b5eb0/server/session/session.go:312 +0xdc
created by github.com/df-mc/dragonfly/server/session.(*Session).Start in goroutine 1
        C:/Users/Anthony/go/pkg/mod/github.com/df-mc/dragonfly@v0.9.9-0.20230930211047-d519f75b5eb0/server/session/session.go:217 +0xa5

Process finished with the exit code 2

It happens specifically when giving myself blue_concrete

this also happens with a block.Log

turns out this only seems to work properly with british english, so use that instead for the time being