|
| 1 | +package recipe |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/df-mc/dragonfly/server/item" |
| 5 | + "github.com/df-mc/dragonfly/server/world" |
| 6 | +) |
| 7 | + |
| 8 | +// DecoratedPotRecipe is a dynamic recipe for crafting decorated pots. The output depends on which |
| 9 | +// pottery sherds or bricks are used in the crafting grid. |
| 10 | +type DecoratedPotRecipe struct { |
| 11 | + block string |
| 12 | +} |
| 13 | + |
| 14 | +// NewDecoratedPotRecipe creates a new decorated pot recipe. |
| 15 | +func NewDecoratedPotRecipe() DecoratedPotRecipe { |
| 16 | + return DecoratedPotRecipe{block: "crafting_table"} |
| 17 | +} |
| 18 | + |
| 19 | +// potDecoration is a local interface to check if an item can be used as a pot decoration |
| 20 | +// without importing the block package (which would create an import cycle). |
| 21 | +type potDecoration interface { |
| 22 | + world.Item |
| 23 | + PotDecoration() bool |
| 24 | +} |
| 25 | + |
| 26 | +// Match checks if the given input items match the decorated pot recipe pattern. |
| 27 | +// The pattern requires exactly 4 PotDecoration items (bricks or pottery sherds) in a diamond/plus shape: |
| 28 | +// - Slot 1 (top centre) |
| 29 | +// - Slot 3 (middle left) |
| 30 | +// - Slot 5 (middle right) |
| 31 | +// - Slot 7 (bottom centre) |
| 32 | +// All other slots must be empty. |
| 33 | +func (r DecoratedPotRecipe) Match(input []Item) (output []item.Stack, ok bool) { |
| 34 | + // For a 3x3 crafting grid, we need exactly 9 slots |
| 35 | + if len(input) != 9 { |
| 36 | + return nil, false |
| 37 | + } |
| 38 | + |
| 39 | + // Define the slots for the diamond pattern (0-indexed) |
| 40 | + // Layout: 0 1 2 |
| 41 | + // 3 4 5 |
| 42 | + // 6 7 8 |
| 43 | + // We need items at: 1 (top), 3 (left), 5 (right), 7 (bottom) |
| 44 | + // Odd indices should have items, even indices should be empty |
| 45 | + |
| 46 | + decorations := [4]world.Item{} |
| 47 | + decorationIndex := 0 |
| 48 | + for i := range input { |
| 49 | + it := input[i] |
| 50 | + if i%2 == 0 { |
| 51 | + // Even slots (0, 2, 4, 6, 8) should be empty |
| 52 | + if !it.Empty() { |
| 53 | + return nil, false |
| 54 | + } |
| 55 | + } else { |
| 56 | + // Odd slots (1, 3, 5, 7) should have items |
| 57 | + if it.Empty() { |
| 58 | + return nil, false |
| 59 | + } |
| 60 | + |
| 61 | + // Extract the actual item from the Item interface |
| 62 | + var actualItem item.Stack |
| 63 | + if v, ok := it.(item.Stack); ok { |
| 64 | + actualItem = v |
| 65 | + } else { |
| 66 | + // ItemTag or other types are not valid for decorated pots |
| 67 | + return nil, false |
| 68 | + } |
| 69 | + |
| 70 | + // Check if the item implements PotDecoration |
| 71 | + decoration, ok := actualItem.Item().(potDecoration) |
| 72 | + if !ok { |
| 73 | + return nil, false |
| 74 | + } |
| 75 | + decorations[decorationIndex] = decoration |
| 76 | + decorationIndex++ |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // Create the decorated pot by encoding the decorations into NBT |
| 81 | + // We'll use world.BlockByName to get the DecoratedPot block and set its decorations |
| 82 | + // The decorations are ordered: [top, left, right, bottom] in the crafting grid |
| 83 | + // For the pot NBT: [back, left, front, right] based on facing direction |
| 84 | + |
| 85 | + // Get a decorated pot block instance |
| 86 | + pot, ok := world.BlockByName("minecraft:decorated_pot", map[string]any{"direction": int32(2)}) |
| 87 | + if !ok { |
| 88 | + return nil, false |
| 89 | + } |
| 90 | + |
| 91 | + // The pot will be decoded with the decorations through NBT when placed |
| 92 | + // For now, we'll create a pot with the decorations in the correct order |
| 93 | + // DecoratedPot.DecodeNBT expects sherds in order: [back, left, front, right] |
| 94 | + sherds := []any{} |
| 95 | + // Order: top -> back, left -> left, bottom -> front, right -> right |
| 96 | + for _, idx := range []int{0, 1, 3, 2} { // top, left, bottom, right |
| 97 | + name, _ := decorations[idx].EncodeItem() |
| 98 | + sherds = append(sherds, name) |
| 99 | + } |
| 100 | + |
| 101 | + // Decode the pot with the sherds NBT data using type assertion |
| 102 | + if nbtDecoder, ok := pot.(interface { |
| 103 | + DecodeNBT(map[string]any) any |
| 104 | + }); ok { |
| 105 | + decodedPot := nbtDecoder.DecodeNBT(map[string]any{ |
| 106 | + "id": "DecoratedPot", |
| 107 | + "sherds": sherds, |
| 108 | + }) |
| 109 | + if potItem, ok := decodedPot.(world.Item); ok { |
| 110 | + return []item.Stack{item.NewStack(potItem, 1)}, true |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return nil, false |
| 115 | +} |
| 116 | + |
| 117 | +// Block returns the block used to craft this recipe. |
| 118 | +func (r DecoratedPotRecipe) Block() string { |
| 119 | + return r.block |
| 120 | +} |
0 commit comments