-
Notifications
You must be signed in to change notification settings - Fork 150
/
handler_smithing.go
57 lines (52 loc) · 2.19 KB
/
handler_smithing.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
package session
import (
"fmt"
"github.com/df-mc/dragonfly/server/item/recipe"
"github.com/sandertv/gophertunnel/minecraft/protocol"
)
const (
// smithingInputSlot is the slot index of the input item in the smithing table.
smithingInputSlot = 0x33
// smithingMaterialSlot is the slot index of the material in the smithing table.
smithingMaterialSlot = 0x34
)
// handleSmithing handles a CraftRecipe stack request action made using a smithing table.
func (h *ItemStackRequestHandler) handleSmithing(a *protocol.CraftRecipeStackRequestAction, s *Session) error {
// First, check the recipe and ensure it is valid for the smithing table.
craft, ok := s.recipes[a.RecipeNetworkID]
if !ok {
return fmt.Errorf("recipe with network id %v does not exist", a.RecipeNetworkID)
}
if _, shapeless := craft.(recipe.Smithing); !shapeless {
return fmt.Errorf("recipe with network id %v is not a smithing recipe", a.RecipeNetworkID)
}
if craft.Block() != "smithing_table" {
return fmt.Errorf("recipe with network id %v is not a smithing table recipe", a.RecipeNetworkID)
}
// Check if the input item and material item match what the recipe requires.
expectedInputs := craft.Input()
input, _ := h.itemInSlot(protocol.StackRequestSlotInfo{
ContainerID: protocol.ContainerSmithingTableInput,
Slot: smithingInputSlot,
}, s)
if !matchingStacks(input, expectedInputs[0]) {
return fmt.Errorf("input item is not the same as expected input")
}
material, _ := h.itemInSlot(protocol.StackRequestSlotInfo{
ContainerID: protocol.ContainerSmithingTableMaterial,
Slot: smithingMaterialSlot,
}, s)
if !matchingStacks(material, expectedInputs[1]) {
return fmt.Errorf("material item is not the same as expected material")
}
// Create the output using the input stack as reference and the recipe's output item type.
h.setItemInSlot(protocol.StackRequestSlotInfo{
ContainerID: protocol.ContainerSmithingTableInput,
Slot: smithingInputSlot,
}, input.Grow(-1), s)
h.setItemInSlot(protocol.StackRequestSlotInfo{
ContainerID: protocol.ContainerSmithingTableMaterial,
Slot: smithingMaterialSlot,
}, material.Grow(-1), s)
return h.createResults(s, duplicateStack(input, craft.Output()[0].Item()))
}