forked from df-mc/dragonfly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_loom.go
95 lines (87 loc) · 3.16 KB
/
handler_loom.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package session
import (
"fmt"
"github.com/Adrian8115/dragonfly-Amethyst-Protocol/server/block"
"github.com/Adrian8115/dragonfly-Amethyst-Protocol/server/item"
"github.com/Adrian8115/gophertunnel-Amethyst-Protocol/minecraft/protocol"
)
const (
// loomInputSlot is the slot index of the input item in the loom table.
loomInputSlot = 0x09
// loomDyeSlot is the slot index of the dye item in the loom table.
loomDyeSlot = 0x0a
// loomPatternSlot is the slot index of the pattern item in the loom table.
loomPatternSlot = 0x0b
)
// handleLoomCraft handles a CraftLoomRecipe stack request action made using a loom table.
func (h *ItemStackRequestHandler) handleLoomCraft(a *protocol.CraftLoomRecipeStackRequestAction, s *Session) error {
// First check if there actually is a loom opened.
if !s.containerOpened.Load() {
return fmt.Errorf("no loom container opened")
}
if _, ok := s.c.World().Block(s.openedPos.Load()).(block.Loom); !ok {
return fmt.Errorf("no loom container opened")
}
// Next, check if the input slot has a valid banner item.
input, _ := h.itemInSlot(protocol.StackRequestSlotInfo{
ContainerID: protocol.ContainerLoomInput,
Slot: loomInputSlot,
}, s)
if input.Empty() {
return fmt.Errorf("input item is empty")
}
b, ok := input.Item().(block.Banner)
if !ok {
return fmt.Errorf("input item is not a banner")
}
if b.Illager {
return fmt.Errorf("input item is an illager banner")
}
// Do the same with the input dye.
dye, _ := h.itemInSlot(protocol.StackRequestSlotInfo{
ContainerID: protocol.ContainerLoomDye,
Slot: loomDyeSlot,
}, s)
if dye.Empty() {
return fmt.Errorf("dye item is empty")
}
d, ok := dye.Item().(item.Dye)
if !ok {
return fmt.Errorf("dye item is not a dye")
}
// The action contains the pattern that the client wanted to apply, so parse the ID and check if it is a valid
// pattern.
expectedPattern := block.BannerPatternByID(a.Pattern)
// Some banner patterns have equivalent banner pattern items that are required to craft the pattern. If the expected
// pattern has a pattern item, check if the player input the correct pattern item.
pattern, _ := h.itemInSlot(protocol.StackRequestSlotInfo{
ContainerID: protocol.ContainerLoomMaterial,
Slot: loomPatternSlot,
}, s)
if expectedPatternItem, hasPatternItem := expectedPattern.Item(); hasPatternItem {
if pattern.Empty() {
return fmt.Errorf("pattern item is empty but the pattern is required")
}
p, ok := pattern.Item().(item.BannerPattern)
if !ok {
return fmt.Errorf("pattern item is not a banner pattern")
}
if expectedPatternItem != p.Type {
return fmt.Errorf("pattern item does not match the expected pattern")
}
}
// Add a new pattern layer onto the banner, and create the result.
b.Patterns = append(b.Patterns, block.BannerPatternLayer{
Type: expectedPattern,
Colour: d.Colour,
})
h.setItemInSlot(protocol.StackRequestSlotInfo{
ContainerID: protocol.ContainerLoomInput,
Slot: loomInputSlot,
}, input.Grow(-1), s)
h.setItemInSlot(protocol.StackRequestSlotInfo{
ContainerID: protocol.ContainerLoomDye,
Slot: loomDyeSlot,
}, dye.Grow(-1), s)
return h.createResults(s, duplicateStack(input, b))
}