Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various fixes to the creative inventory #581

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions server/block/banner.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ func (b Banner) EncodeBlock() (name string, properties map[string]any) {
return "minecraft:standing_banner", map[string]any{"ground_sign_direction": int32(b.Attach.o)}
}

// EncodeNBT ...
func (b Banner) EncodeNBT() map[string]any {
// EncodeBlockNBT ...
func (b Banner) EncodeBlockNBT(cube.Pos, *world.World) map[string]any {
patterns := make([]any, 0, len(b.Patterns))
for _, p := range b.Patterns {
patterns = append(patterns, p.EncodeNBT())
Expand All @@ -100,8 +100,8 @@ func (b Banner) EncodeNBT() map[string]any {
}
}

// DecodeNBT ...
func (b Banner) DecodeNBT(data map[string]any) any {
// DecodeBlockNBT ...
func (b Banner) DecodeBlockNBT(_ cube.Pos, _ *world.World, data map[string]any) any {
b.Colour = invertColourID(int16(nbtconv.Map[int32](data, "Base")))
b.Illager = nbtconv.Map[int32](data, "Type") == 1
if patterns, ok := data["Patterns"].([]any); ok {
Expand All @@ -113,6 +113,34 @@ func (b Banner) DecodeNBT(data map[string]any) any {
return b
}
Sandertv marked this conversation as resolved.
Show resolved Hide resolved

// EncodeItemNBT ...
func (b Banner) EncodeItemNBT() map[string]any {
data := map[string]any{}
if b.Illager {
data["Type"] = int32(1)
}
if len(b.Patterns) > 0 {
patterns := make([]any, 0, len(b.Patterns))
for _, p := range b.Patterns {
patterns = append(patterns, p.EncodeNBT())
}
data["Patterns"] = patterns
}
return data
}

// DecodeItemNBT ...
func (b Banner) DecodeItemNBT(data map[string]any) any {
b.Illager = nbtconv.Map[int32](data, "Type") == 1
if patterns, ok := data["Patterns"].([]any); ok {
b.Patterns = make([]BannerPatternLayer, len(patterns))
for i, p := range b.Patterns {
b.Patterns[i] = p.DecodeNBT(patterns[i].(map[string]any)).(BannerPatternLayer)
}
}
return b
}

// invertColour converts the item.Colour passed and returns the colour ID inverted.
func invertColour(c item.Colour) int16 {
return ^int16(c.Uint8()) & 0xf
Expand Down
8 changes: 4 additions & 4 deletions server/block/barrel.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ func (Barrel) FuelInfo() item.FuelInfo {
return newFuelInfo(time.Second * 15)
}

// DecodeNBT ...
func (b Barrel) DecodeNBT(data map[string]any) any {
// DecodeBlockNBT ...
func (b Barrel) DecodeBlockNBT(_ cube.Pos, _ *world.World, data map[string]any) any {
facing := b.Facing
//noinspection GoAssignmentToReceiver
b = NewBarrel()
Expand All @@ -148,8 +148,8 @@ func (b Barrel) DecodeNBT(data map[string]any) any {
return b
}

// EncodeNBT ...
func (b Barrel) EncodeNBT() map[string]any {
// EncodeBlockNBT ...
func (b Barrel) EncodeBlockNBT(cube.Pos, *world.World) map[string]any {
if b.inventory == nil {
facing, customName := b.Facing, b.CustomName
//noinspection GoAssignmentToReceiver
Expand Down
8 changes: 4 additions & 4 deletions server/block/beacon.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func (b Beacon) Activate(pos cube.Pos, _ cube.Face, _ *world.World, u item.User,
return true
}

// DecodeNBT ...
func (b Beacon) DecodeNBT(data map[string]any) any {
// DecodeBlockNBT ...
func (b Beacon) DecodeBlockNBT(_ cube.Pos, _ *world.World, data map[string]any) any {
b.level = int(nbtconv.Map[int32](data, "Levels"))
if primary, ok := effect.ByID(int(nbtconv.Map[int32](data, "Primary"))); ok {
b.Primary = primary.(effect.LastingType)
Expand All @@ -59,8 +59,8 @@ func (b Beacon) DecodeNBT(data map[string]any) any {
return b
}

// EncodeNBT ...
func (b Beacon) EncodeNBT() map[string]any {
// EncodeBlockNBT ...
func (b Beacon) EncodeBlockNBT(cube.Pos, *world.World) map[string]any {
m := map[string]any{
"id": "Beacon",
"Levels": int32(b.level),
Expand Down
16 changes: 8 additions & 8 deletions server/block/blast_furnace.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ func (b BlastFurnace) EncodeItem() (name string, meta int16) {
}

// EncodeBlock ...
func (b BlastFurnace) EncodeBlock() (name string, properties map[string]interface{}) {
func (b BlastFurnace) EncodeBlock() (name string, properties map[string]any) {
if b.Lit {
return "minecraft:lit_blast_furnace", map[string]interface{}{"facing_direction": int32(b.Facing)}
return "minecraft:lit_blast_furnace", map[string]any{"facing_direction": int32(b.Facing)}
}
return "minecraft:blast_furnace", map[string]interface{}{"facing_direction": int32(b.Facing)}
return "minecraft:blast_furnace", map[string]any{"facing_direction": int32(b.Facing)}
}

// UseOnBlock ...
Expand Down Expand Up @@ -85,14 +85,14 @@ func (b BlastFurnace) Activate(pos cube.Pos, _ cube.Face, _ *world.World, u item
return false
}

// EncodeNBT ...
func (b BlastFurnace) EncodeNBT() map[string]interface{} {
// EncodeBlockNBT ...
func (b BlastFurnace) EncodeBlockNBT(cube.Pos, *world.World) map[string]any {
if b.smelter == nil {
//noinspection GoAssignmentToReceiver
b = NewBlastFurnace(b.Facing)
}
remaining, maximum, cook := b.Durations()
return map[string]interface{}{
return map[string]any{
"BurnTime": int16(remaining.Milliseconds() / 50),
"CookTime": int16(cook.Milliseconds() / 50),
"BurnDuration": int16(maximum.Milliseconds() / 50),
Expand All @@ -102,8 +102,8 @@ func (b BlastFurnace) EncodeNBT() map[string]interface{} {
}
}

// DecodeNBT ...
func (b BlastFurnace) DecodeNBT(data map[string]interface{}) interface{} {
// DecodeBlockNBT ...
func (b BlastFurnace) DecodeBlockNBT(_ cube.Pos, _ *world.World, data map[string]any) any {
remaining := time.Duration(nbtconv.Map[int16](data, "BurnTime")) * time.Millisecond * 50
maximum := time.Duration(nbtconv.Map[int16](data, "BurnDuration")) * time.Millisecond * 50
cook := time.Duration(nbtconv.Map[int16](data, "CookTime")) * time.Millisecond * 50
Expand Down
7 changes: 7 additions & 0 deletions server/block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ type Frictional interface {
Friction() float64
}

var (
// unknownDirection is a direction that is used for certain block items. This should not be exposed in the API.
unknownDirection = cube.Direction(len(cube.Directions()))
// unknownFace is a face that is used for certain block items. This should not be exposed in the API.
unknownFace = cube.Face(len(cube.Faces()))
)

func calculateFace(user item.User, placePos cube.Pos) cube.Face {
userPos := user.Position()
pos := cube.PosFromVec3(userPos)
Expand Down
13 changes: 8 additions & 5 deletions server/block/chest.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ func (c Chest) FlammabilityInfo() FlammabilityInfo {
return newFlammabilityInfo(0, 0, true)
}

// DecodeNBT ...
func (c Chest) DecodeNBT(data map[string]any) any {
// DecodeBlockNBT ...
func (c Chest) DecodeBlockNBT(_ cube.Pos, _ *world.World, data map[string]any) any {
facing := c.Facing
//noinspection GoAssignmentToReceiver
c = NewChest()
Expand All @@ -159,8 +159,8 @@ func (c Chest) DecodeNBT(data map[string]any) any {
return c
}

// EncodeNBT ...
func (c Chest) EncodeNBT() map[string]any {
// EncodeBlockNBT ...
func (c Chest) EncodeBlockNBT(cube.Pos, *world.World) map[string]any {
if c.inventory == nil {
facing, customName := c.Facing, c.CustomName
//noinspection GoAssignmentToReceiver
Expand All @@ -184,12 +184,15 @@ func (Chest) EncodeItem() (name string, meta int16) {

// EncodeBlock ...
func (c Chest) EncodeBlock() (name string, properties map[string]any) {
if c.Facing == unknownDirection {
return "minecraft:chest", map[string]any{"facing_direction": int32(0)}
}
return "minecraft:chest", map[string]any{"facing_direction": 2 + int32(c.Facing)}
}

// allChests ...
func allChests() (chests []world.Block) {
for _, direction := range cube.Directions() {
for _, direction := range append(cube.Directions(), unknownDirection) {
chests = append(chests, Chest{Facing: direction})
}
return
Expand Down
2 changes: 1 addition & 1 deletion server/block/crafting_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (c CraftingTable) EncodeItem() (name string, meta int16) {
}

// EncodeBlock ...
func (c CraftingTable) EncodeBlock() (name string, properties map[string]interface{}) {
func (c CraftingTable) EncodeBlock() (name string, properties map[string]any) {
return "minecraft:crafting_table", nil
}

Expand Down
6 changes: 4 additions & 2 deletions server/block/cube/face.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (f Face) Direction() Direction {
// and vice versa.
func (f Face) Opposite() Face {
switch f {
default:
case FaceDown:
return FaceUp
case FaceUp:
return FaceDown
Expand All @@ -41,19 +41,21 @@ func (f Face) Opposite() Face {
case FaceEast:
return FaceWest
}
panic("invalid face")
}

// Axis returns the axis the face is facing. FaceEast and west correspond to the x-axis, north and south to the z
// axis and up and down to the y-axis.
func (f Face) Axis() Axis {
switch f {
default:
case FaceDown, FaceUp:
return Y
case FaceEast, FaceWest:
return X
case FaceNorth, FaceSouth:
return Z
}
panic("invalid face")
}

// RotateRight rotates the face 90 degrees to the right horizontally and returns the new face.
Expand Down
11 changes: 6 additions & 5 deletions server/block/enchanting_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,14 @@ func (EnchantingTable) EncodeBlock() (string, map[string]any) {
return "minecraft:enchanting_table", nil
}

// EncodeNBT is used to encode the block to NBT, so that the enchanting table book will be rendered properly client-side.
// The actual rotation value doesn't need to be set in the NBT, we just need to write the default NBT for the block.
func (e EnchantingTable) EncodeNBT() map[string]any {
// EncodeBlockNBT is used to encode the block to NBT, so that the enchanting table book will be rendered properly
// client-side. The actual rotation value doesn't need to be set in the NBT, we just need to write the default NBT for
// the block.
func (e EnchantingTable) EncodeBlockNBT(cube.Pos, *world.World) map[string]any {
return map[string]any{"id": "EnchantTable"}
}

// DecodeNBT is used to implement world.NBTer.
func (e EnchantingTable) DecodeNBT(map[string]any) any {
// DecodeBlockNBT is used to implement world.NBTer.
func (e EnchantingTable) DecodeBlockNBT(cube.Pos, *world.World, map[string]any) any {
return e
}
19 changes: 11 additions & 8 deletions server/block/ender_chest.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ func (c EnderChest) close(w *world.World, pos cube.Pos) {
w.PlaySound(pos.Vec3Centre(), sound.ChestClose{})
}

// EncodeNBT ...
func (c EnderChest) EncodeNBT() map[string]interface{} {
return map[string]interface{}{"id": "EnderChest"}
// EncodeBlockNBT ...
func (c EnderChest) EncodeBlockNBT(cube.Pos, *world.World) map[string]any {
return map[string]any{"id": "EnderChest"}
}

// DecodeNBT ...
func (c EnderChest) DecodeNBT(map[string]interface{}) interface{} {
// DecodeBlockNBT ...
func (c EnderChest) DecodeBlockNBT(cube.Pos, *world.World, map[string]any) any {
return NewEnderChest()
}

Expand All @@ -122,13 +122,16 @@ func (EnderChest) EncodeItem() (name string, meta int16) {
}

// EncodeBlock ...
func (c EnderChest) EncodeBlock() (name string, properties map[string]interface{}) {
return "minecraft:ender_chest", map[string]interface{}{"facing_direction": 2 + int32(c.Facing)}
func (c EnderChest) EncodeBlock() (name string, properties map[string]any) {
if c.Facing == unknownDirection {
return "minecraft:ender_chest", map[string]any{"facing_direction": int32(0)}
}
return "minecraft:ender_chest", map[string]any{"facing_direction": 2 + int32(c.Facing)}
}

// allEnderChests ...
func allEnderChests() (chests []world.Block) {
for _, direction := range cube.Directions() {
for _, direction := range append(cube.Directions(), unknownDirection) {
chests = append(chests, EnderChest{Facing: direction})
}
return
Expand Down
16 changes: 8 additions & 8 deletions server/block/furnace.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ func (f Furnace) EncodeItem() (name string, meta int16) {
}

// EncodeBlock ...
func (f Furnace) EncodeBlock() (name string, properties map[string]interface{}) {
func (f Furnace) EncodeBlock() (name string, properties map[string]any) {
if f.Lit {
return "minecraft:lit_furnace", map[string]interface{}{"facing_direction": int32(f.Facing)}
return "minecraft:lit_furnace", map[string]any{"facing_direction": int32(f.Facing)}
}
return "minecraft:furnace", map[string]interface{}{"facing_direction": int32(f.Facing)}
return "minecraft:furnace", map[string]any{"facing_direction": int32(f.Facing)}
}

// UseOnBlock ...
Expand Down Expand Up @@ -84,14 +84,14 @@ func (f Furnace) Activate(pos cube.Pos, _ cube.Face, _ *world.World, u item.User
return false
}

// EncodeNBT ...
func (f Furnace) EncodeNBT() map[string]interface{} {
// EncodeBlockNBT ...
func (f Furnace) EncodeBlockNBT(cube.Pos, *world.World) map[string]any {
if f.smelter == nil {
//noinspection GoAssignmentToReceiver
f = NewFurnace(f.Facing)
}
remaining, maximum, cook := f.Durations()
return map[string]interface{}{
return map[string]any{
"BurnTime": int16(remaining.Milliseconds() / 50),
"CookTime": int16(cook.Milliseconds() / 50),
"BurnDuration": int16(maximum.Milliseconds() / 50),
Expand All @@ -101,8 +101,8 @@ func (f Furnace) EncodeNBT() map[string]interface{} {
}
}

// DecodeNBT ...
func (f Furnace) DecodeNBT(data map[string]interface{}) interface{} {
// DecodeBlockNBT ...
func (f Furnace) DecodeBlockNBT(_ cube.Pos, _ *world.World, data map[string]any) any {
remaining := time.Duration(nbtconv.Map[int16](data, "BurnTime")) * time.Millisecond * 50
maximum := time.Duration(nbtconv.Map[int16](data, "BurnDuration")) * time.Millisecond * 50
cook := time.Duration(nbtconv.Map[int16](data, "CookTime")) * time.Millisecond * 50
Expand Down
5 changes: 4 additions & 1 deletion server/block/glazed_terracotta.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ func (t GlazedTerracotta) EncodeItem() (name string, meta int16) {

// EncodeBlock ...
func (t GlazedTerracotta) EncodeBlock() (name string, properties map[string]any) {
if t.Facing == unknownDirection {
return "minecraft:" + t.Colour.String() + "_glazed_terracotta", map[string]any{"facing_direction": int32(0)}
}
return "minecraft:" + t.Colour.String() + "_glazed_terracotta", map[string]any{"facing_direction": int32(2 + t.Facing)}
}

Expand All @@ -47,7 +50,7 @@ func (t GlazedTerracotta) UseOnBlock(pos cube.Pos, face cube.Face, _ mgl64.Vec3,

// allGlazedTerracotta returns glazed terracotta blocks with all possible colours.
func allGlazedTerracotta() (b []world.Block) {
for dir := cube.Direction(0); dir < 4; dir++ {
for _, dir := range append(cube.Directions(), unknownDirection) {
for _, c := range item.Colours() {
b = append(b, GlazedTerracotta{Colour: c, Facing: dir})
}
Expand Down
4 changes: 2 additions & 2 deletions server/block/hay_bale.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func (HayBale) EncodeItem() (name string, meta int16) {
}

// EncodeBlock ...
func (h HayBale) EncodeBlock() (name string, properties map[string]interface{}) {
return "minecraft:hay_block", map[string]interface{}{"pillar_axis": h.Axis.String(), "deprecated": int32(0)}
func (h HayBale) EncodeBlock() (name string, properties map[string]any) {
return "minecraft:hay_block", map[string]any{"pillar_axis": h.Axis.String(), "deprecated": int32(0)}
}

// allHayBales ...
Expand Down
8 changes: 4 additions & 4 deletions server/block/item_frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,16 @@ func (i ItemFrame) EncodeBlock() (name string, properties map[string]any) {
}
}

// DecodeNBT ...
func (i ItemFrame) DecodeNBT(data map[string]any) any {
// DecodeBlockNBT ...
func (i ItemFrame) DecodeBlockNBT(_ cube.Pos, _ *world.World, data map[string]any) any {
i.DropChance = float64(nbtconv.Map[float32](data, "ItemDropChance"))
i.Rotations = int(nbtconv.Map[byte](data, "ItemRotation"))
i.Item = nbtconv.MapItem(data, "Item")
return i
}

// EncodeNBT ...
func (i ItemFrame) EncodeNBT() map[string]any {
// EncodeBlockNBT ...
func (i ItemFrame) EncodeBlockNBT(cube.Pos, *world.World) map[string]any {
m := map[string]any{
"ItemDropChance": float32(i.DropChance),
"ItemRotation": uint8(i.Rotations),
Expand Down
Loading