-
Notifications
You must be signed in to change notification settings - Fork 1
/
cape.go
60 lines (52 loc) · 1.39 KB
/
cape.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
package vails
import (
"github.com/df-mc/dragonfly/server/player/skin"
"image/png"
"os"
)
// Cape stores data about a cosmetic cape, such as it's name, image, etc.
type Cape struct {
name string
plus bool
cape skin.Cape
}
// NewCape creates a new cape with the given name and image. The plus parameter determines whether the cape can only
// be accessed by players with the Plus role.
func NewCape(name string, path string, plus bool) Cape {
return Cape{
name: name,
plus: plus,
cape: read("assets/capes/" + path),
}
}
// Name returns the name of the cape.
func (c Cape) Name() string {
return c.name
}
// Premium returns true if the cape can only be accessed by players with the Plus role.
func (c Cape) Premium() bool {
return c.plus
}
// Cape returns the image data of the cape.
func (c Cape) Cape() skin.Cape {
return c.cape
}
// read performs a read on the path provided and returns a dragonfly cape.
func read(path string) skin.Cape {
f, _ := os.OpenFile(path, os.O_RDONLY, os.ModePerm)
defer f.Close()
i, err := png.Decode(f)
if err != nil {
panic(err)
}
c := skin.NewCape(i.Bounds().Max.X, i.Bounds().Max.Y)
for y := 0; y < i.Bounds().Max.Y; y++ {
for x := 0; x < i.Bounds().Max.X; x++ {
color := i.At(x, y)
r, g, b, a := color.RGBA()
i := x*4 + i.Bounds().Max.X*y*4
c.Pix[i], c.Pix[i+1], c.Pix[i+2], c.Pix[i+3] = uint8(r), uint8(g), uint8(b), uint8(a)
}
}
return c
}