github.com/df-mc/dragonfly@v0.9.13/server/block/decorated_pot.go (about)

     1  package block
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/df-mc/dragonfly/server/block/cube"
     6  	"github.com/df-mc/dragonfly/server/block/model"
     7  	"github.com/df-mc/dragonfly/server/internal/nbtconv"
     8  	"github.com/df-mc/dragonfly/server/item"
     9  	"github.com/df-mc/dragonfly/server/world"
    10  	"github.com/go-gl/mathgl/mgl64"
    11  )
    12  
    13  // PotDecoration represents an item that can be used as a decoration on a pot.
    14  type PotDecoration interface {
    15  	world.Item
    16  	PotDecoration() bool
    17  }
    18  
    19  // DecoratedPot is a decoration block that can be crafted from up to four pottery sherds, and bricks on the sides where
    20  // no pattern should be displayed.
    21  type DecoratedPot struct {
    22  	// Facing is the direction the pot is facing. The first decoration will be facing opposite of this direction.
    23  	Facing cube.Direction
    24  	// Decorations are the four decorations displayed on the sides of the pot. If a decoration is a brick or nil,
    25  	// the side will appear to be empty.
    26  	Decorations [4]PotDecoration
    27  }
    28  
    29  // BreakInfo ...
    30  func (p DecoratedPot) BreakInfo() BreakInfo {
    31  	return newBreakInfo(0, alwaysHarvestable, nothingEffective, oneOf(p))
    32  }
    33  
    34  // MaxCount ...
    35  func (DecoratedPot) MaxCount() int {
    36  	return 1
    37  }
    38  
    39  // EncodeItem ...
    40  func (p DecoratedPot) EncodeItem() (name string, meta int16) {
    41  	return "minecraft:decorated_pot", 0
    42  }
    43  
    44  // EncodeBlock ...
    45  func (p DecoratedPot) EncodeBlock() (name string, properties map[string]any) {
    46  	return "minecraft:decorated_pot", map[string]any{"direction": int32(horizontalDirection(p.Facing))}
    47  }
    48  
    49  // Model ...
    50  func (p DecoratedPot) Model() world.BlockModel {
    51  	return model.DecoratedPot{}
    52  }
    53  
    54  // UseOnBlock ...
    55  func (p DecoratedPot) UseOnBlock(pos cube.Pos, face cube.Face, _ mgl64.Vec3, w *world.World, user item.User, ctx *item.UseContext) (used bool) {
    56  	pos, _, used = firstReplaceable(w, pos, face, p)
    57  	if !used {
    58  		return
    59  	}
    60  	p.Facing = user.Rotation().Direction().Opposite()
    61  
    62  	place(w, pos, p, user, ctx)
    63  	return placed(ctx)
    64  }
    65  
    66  // EncodeNBT ...
    67  func (p DecoratedPot) EncodeNBT() map[string]any {
    68  	var sherds []string
    69  	for _, decoration := range p.Decorations {
    70  		if decoration == nil {
    71  			sherds = append(sherds, "minecraft:brick")
    72  		} else {
    73  			name, _ := decoration.EncodeItem()
    74  			sherds = append(sherds, name)
    75  		}
    76  	}
    77  	return map[string]any{
    78  		"sherds": sherds,
    79  		"id":     "DecoratedPot",
    80  	}
    81  }
    82  
    83  // DecodeNBT ...
    84  func (p DecoratedPot) DecodeNBT(data map[string]any) any {
    85  	p.Decorations = [4]PotDecoration{}
    86  	if sherds := nbtconv.Slice(data, "sherds"); sherds != nil {
    87  		for i, name := range sherds {
    88  			it, ok := world.ItemByName(name.(string), 0)
    89  			if !ok {
    90  				panic(fmt.Errorf("unknown item %s", name))
    91  			}
    92  			decoration, ok := it.(PotDecoration)
    93  			if !ok {
    94  				panic(fmt.Errorf("item %s is not a pot decoration", name))
    95  			}
    96  			p.Decorations[i] = decoration
    97  		}
    98  	}
    99  	return p
   100  }
   101  
   102  // allDecoratedPots ...
   103  func allDecoratedPots() (pots []world.Block) {
   104  	for _, f := range cube.Directions() {
   105  		pots = append(pots, DecoratedPot{Facing: f})
   106  	}
   107  	return
   108  }