initial commit
This commit is contained in:
71
packbuilder/builder.go
Normal file
71
packbuilder/builder.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package packbuilder
|
||||
|
||||
import (
|
||||
"github.com/df-mc/dragonfly/server/item/category"
|
||||
"golang.org/x/exp/maps"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ComponentBuilder represents a builder that can be used to construct an item components map to be sent to a client.
|
||||
type ComponentBuilder struct {
|
||||
name string
|
||||
identifier string
|
||||
category category.Category
|
||||
|
||||
properties map[string]any
|
||||
components map[string]any
|
||||
}
|
||||
|
||||
// NewComponentBuilder returns a new component builder with the provided item data.
|
||||
func NewComponentBuilder(name, identifier string, category category.Category) *ComponentBuilder {
|
||||
return &ComponentBuilder{
|
||||
name: name,
|
||||
identifier: identifier,
|
||||
category: category,
|
||||
|
||||
properties: make(map[string]any),
|
||||
components: make(map[string]any),
|
||||
}
|
||||
}
|
||||
|
||||
// AddProperty adds the provided property to the builder.
|
||||
func (builder *ComponentBuilder) AddProperty(name string, value any) {
|
||||
builder.properties[name] = value
|
||||
}
|
||||
|
||||
// AddComponent adds the provided component to the builder.
|
||||
func (builder *ComponentBuilder) AddComponent(name string, value any) {
|
||||
builder.components[name] = value
|
||||
}
|
||||
|
||||
// Construct constructs the final item components map and returns it. It also applies the default properties required
|
||||
// for the item to work without modifying the original maps in the builder.
|
||||
func (builder *ComponentBuilder) Construct() map[string]any {
|
||||
properties := maps.Clone(builder.properties)
|
||||
components := maps.Clone(builder.components)
|
||||
builder.applyDefaultProperties(properties)
|
||||
builder.applyDefaultComponents(components, properties)
|
||||
return map[string]any{"components": components}
|
||||
}
|
||||
|
||||
// applyDefaultProperties applies the default properties to the provided map. It is important that this method does
|
||||
// not modify the builder's properties map directly otherwise Empty() will return false in future use of the builder.
|
||||
func (builder *ComponentBuilder) applyDefaultProperties(x map[string]any) {
|
||||
x["minecraft:icon"] = map[string]any{
|
||||
"texture": strings.Split(builder.identifier, ":")[1],
|
||||
}
|
||||
x["creative_group"] = builder.category.Group()
|
||||
x["creative_category"] = int32(builder.category.Uint8())
|
||||
if _, ok := x["max_stack_size"]; !ok {
|
||||
x["max_stack_size"] = int32(64)
|
||||
}
|
||||
}
|
||||
|
||||
// applyDefaultComponents applies the default components to the provided map. It is important that this method does not
|
||||
// modify the builder's components map directly otherwise Empty() will return false in future use of the builder.
|
||||
func (builder *ComponentBuilder) applyDefaultComponents(x, properties map[string]any) {
|
||||
x["item_properties"] = properties
|
||||
x["minecraft:display_name"] = map[string]any{
|
||||
"value": builder.name,
|
||||
}
|
||||
}
|
||||
119
packbuilder/components.go
Normal file
119
packbuilder/components.go
Normal file
@@ -0,0 +1,119 @@
|
||||
package packbuilder
|
||||
|
||||
import (
|
||||
"github.com/df-mc/dragonfly/server/item"
|
||||
"github.com/df-mc/dragonfly/server/world"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Components returns all the components of the given custom item. If the item has no components, a nil map and false
|
||||
// are returned.
|
||||
func Components(it world.CustomItem) map[string]any {
|
||||
category := it.Category()
|
||||
identifier, _ := it.EncodeItem()
|
||||
name := strings.Split(identifier, ":")[1]
|
||||
|
||||
builder := NewComponentBuilder(it.Name(), identifier, category)
|
||||
|
||||
if x, ok := it.(item.Armour); ok {
|
||||
builder.AddComponent("minecraft:armor", map[string]any{
|
||||
"protection": int32(x.DefencePoints()),
|
||||
})
|
||||
builder.AddComponent("minecraft:knockback_resistance", map[string]any{
|
||||
"protection": float32(x.KnockBackResistance()),
|
||||
})
|
||||
|
||||
var slot string
|
||||
switch it.(type) {
|
||||
case item.HelmetType:
|
||||
slot = "slot.armor.head"
|
||||
case item.ChestplateType:
|
||||
slot = "slot.armor.chest"
|
||||
case item.LeggingsType:
|
||||
slot = "slot.armor.legs"
|
||||
case item.BootsType:
|
||||
slot = "slot.armor.feet"
|
||||
}
|
||||
builder.AddComponent("minecraft:wearable", map[string]any{
|
||||
"slot": slot,
|
||||
})
|
||||
}
|
||||
if x, ok := it.(item.Consumable); ok {
|
||||
builder.AddProperty("use_duration", int32(x.ConsumeDuration().Seconds()*20))
|
||||
builder.AddComponent("minecraft:food", map[string]any{
|
||||
"can_always_eat": x.AlwaysConsumable(),
|
||||
})
|
||||
|
||||
if y, ok := it.(item.Drinkable); ok && y.Drinkable() {
|
||||
builder.AddProperty("use_animation", int32(2))
|
||||
} else {
|
||||
builder.AddProperty("use_animation", int32(1))
|
||||
}
|
||||
}
|
||||
if x, ok := it.(item.Cooldown); ok {
|
||||
builder.AddComponent("minecraft:cooldown", map[string]any{
|
||||
"category": name,
|
||||
"duration": float32(x.Cooldown().Seconds()),
|
||||
})
|
||||
}
|
||||
if x, ok := it.(item.Durable); ok {
|
||||
builder.AddComponent("minecraft:durability", map[string]any{
|
||||
"max_durability": int32(x.DurabilityInfo().MaxDurability),
|
||||
})
|
||||
}
|
||||
if x, ok := it.(item.MaxCounter); ok {
|
||||
builder.AddProperty("max_stack_size", int32(x.MaxCount()))
|
||||
}
|
||||
if x, ok := it.(item.OffHand); ok {
|
||||
builder.AddProperty("allow_off_hand", x.OffHand())
|
||||
}
|
||||
if x, ok := it.(item.Throwable); ok {
|
||||
// The data in minecraft:projectile is only used by vanilla server-side, but we must send at least an empty map
|
||||
// so the client will play the throwing animation.
|
||||
builder.AddComponent("minecraft:projectile", map[string]any{})
|
||||
builder.AddComponent("minecraft:throwable", map[string]any{
|
||||
"do_swing_animation": x.SwingAnimation(),
|
||||
})
|
||||
}
|
||||
if x, ok := it.(item.Glinted); ok {
|
||||
builder.AddProperty("foil", x.Glinted())
|
||||
}
|
||||
if x, ok := it.(item.HandEquipped); ok {
|
||||
builder.AddProperty("hand_equipped", x.HandEquipped())
|
||||
}
|
||||
itemScale := calculateItemScale(it)
|
||||
builder.AddComponent("minecraft:render_offsets", map[string]any{
|
||||
"main_hand": map[string]any{
|
||||
"first_person": map[string]any{
|
||||
"scale": itemScale,
|
||||
},
|
||||
"third_person": map[string]any{
|
||||
"scale": itemScale,
|
||||
},
|
||||
},
|
||||
"off_hand": map[string]any{
|
||||
"first_person": map[string]any{
|
||||
"scale": itemScale,
|
||||
},
|
||||
"third_person": map[string]any{
|
||||
"scale": itemScale,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
return builder.Construct()
|
||||
}
|
||||
|
||||
// calculateItemScale calculates the scale of the item to be rendered to the player according to the given size.
|
||||
func calculateItemScale(it world.CustomItem) []float32 {
|
||||
width := float32(it.Texture().Bounds().Dx())
|
||||
height := float32(it.Texture().Bounds().Dy())
|
||||
var x, y, z float32 = 0.1, 0.1, 0.1
|
||||
if _, ok := it.(item.HandEquipped); ok {
|
||||
x, y, z = 0.075, 0.125, 0.075
|
||||
}
|
||||
newX := x / (width / 16)
|
||||
newY := y / (height / 16)
|
||||
newZ := z / (width / 16)
|
||||
return []float32{newX, newY, newZ}
|
||||
}
|
||||
81
packbuilder/items.go
Normal file
81
packbuilder/items.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package packbuilder
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/df-mc/dragonfly/server/world"
|
||||
"golang.org/x/image/colornames"
|
||||
"image"
|
||||
"image/png"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
_ "unsafe" // Imported for compiler directives.
|
||||
)
|
||||
|
||||
// buildItems builds all the item-related files for the resource pack. This includes textures, language
|
||||
// entries and item atlas.
|
||||
func buildItems(dir string, customItems []world.CustomItem) (count int, lang []string) {
|
||||
if err := os.Mkdir(filepath.Join(dir, "items"), os.ModePerm); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Join(dir, "textures/items"), os.ModePerm); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
textureData := make(map[string]any)
|
||||
for _, item := range customItems {
|
||||
identifier, _ := item.EncodeItem()
|
||||
lang = append(lang, fmt.Sprintf("item.%s.name=%s", identifier, item.Name()))
|
||||
|
||||
name := strings.Split(identifier, ":")[1]
|
||||
textureData[name] = map[string]string{"textures": fmt.Sprintf("textures/items/%s.png", name)}
|
||||
|
||||
buildItemTexture(dir, name, item.Texture())
|
||||
|
||||
count++
|
||||
}
|
||||
|
||||
buildItemAtlas(dir, map[string]any{
|
||||
"resource_pack_name": "vanilla",
|
||||
"texture_name": "atlas.items",
|
||||
"texture_data": textureData,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// buildItemTexture creates a PNG file for the item from the provided image and name and writes it to the pack.
|
||||
func buildItemTexture(dir, name string, img image.Image) {
|
||||
texture, err := os.Create(filepath.Join(dir, "textures/items", name+".png"))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if img == nil {
|
||||
im := image.NewAlpha(image.Rect(0, 0, 64, 64))
|
||||
for x := 0; x < im.Bounds().Dx(); x++ {
|
||||
for y := 0; y < im.Bounds().Dy(); y++ {
|
||||
im.Set(x, y, colornames.Goldenrod)
|
||||
}
|
||||
}
|
||||
img = im
|
||||
}
|
||||
|
||||
if err := png.Encode(texture, img); err != nil {
|
||||
_ = texture.Close()
|
||||
panic(err)
|
||||
}
|
||||
if err := texture.Close(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// buildItemAtlas creates the identifier to texture mapping and writes it to the pack.
|
||||
func buildItemAtlas(dir string, atlas map[string]any) {
|
||||
b, err := json.Marshal(atlas)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dir, "textures/item_texture.json"), b, 0666); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
17
packbuilder/lang.go
Normal file
17
packbuilder/lang.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package packbuilder
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// buildLanguageFile creates a lang file and writes all of the language entries to the pack.
|
||||
func buildLanguageFile(dir string, lang []string) {
|
||||
if err := os.Mkdir(filepath.Join(dir, "texts"), os.ModePerm); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dir, "texts/en_US.lang"), []byte(strings.Join(lang, "\n")), 0666); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
52
packbuilder/manifest.go
Normal file
52
packbuilder/manifest.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package packbuilder
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/google/uuid"
|
||||
"github.com/sandertv/gophertunnel/minecraft/resource"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// buildManifest creates a JSON manifest file for the client to be able to read the resource pack. It creates
|
||||
// basic information and writes it to the pack.
|
||||
func buildManifest(dir, version string, headerUUID, moduleUUID uuid.UUID) {
|
||||
m, err := json.Marshal(resource.Manifest{
|
||||
FormatVersion: 2,
|
||||
Header: resource.Header{
|
||||
Name: "dragonfly auto-generated resource pack",
|
||||
Description: "This resource pack contains auto-generated content from dragonfly",
|
||||
UUID: headerUUID,
|
||||
Version: [3]int{0, 0, 1},
|
||||
MinimumGameVersion: parseVersion(version),
|
||||
},
|
||||
Modules: []resource.Module{
|
||||
{
|
||||
UUID: moduleUUID.String(),
|
||||
Description: "This resource pack contains auto-generated content from dragonfly",
|
||||
Type: "resources",
|
||||
Version: [3]int{0, 0, 1},
|
||||
},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dir, "manifest.json"), m, 0666); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// parseVersion parses the version passed in the format of a.b.c as a [3]int.
|
||||
func parseVersion(ver string) [3]int {
|
||||
frag := strings.Split(ver, ".")
|
||||
if len(frag) != 3 {
|
||||
panic("invalid version number " + ver)
|
||||
}
|
||||
a, _ := strconv.ParseInt(frag[0], 10, 64)
|
||||
b, _ := strconv.ParseInt(frag[1], 10, 64)
|
||||
c, _ := strconv.ParseInt(frag[2], 10, 64)
|
||||
return [3]int{int(a), int(b), int(c)}
|
||||
}
|
||||
40
packbuilder/resource_pack.go
Normal file
40
packbuilder/resource_pack.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package packbuilder
|
||||
|
||||
import (
|
||||
"github.com/df-mc/dragonfly/server/world"
|
||||
"github.com/rogpeppe/go-internal/dirhash"
|
||||
"github.com/sandertv/gophertunnel/minecraft/resource"
|
||||
"os"
|
||||
)
|
||||
|
||||
// BuildResourcePack builds a resource pack based on custom features that have been registered to the server.
|
||||
// It creates a UUID based on the hash of the directory so the client will only be prompted to download it
|
||||
// once it is changed.
|
||||
func BuildResourcePack(customItems []world.CustomItem, version string) (*resource.Pack, bool) {
|
||||
dir, err := os.MkdirTemp("", "dragonfly_resource_pack-")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer os.RemoveAll(dir)
|
||||
|
||||
var assets int
|
||||
var lang []string
|
||||
|
||||
itemCount, itemLang := buildItems(dir, customItems)
|
||||
assets += itemCount
|
||||
lang = append(lang, itemLang...)
|
||||
|
||||
if assets > 0 {
|
||||
buildLanguageFile(dir, lang)
|
||||
hash, err := dirhash.HashDir(dir, "", dirhash.Hash1)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
var header, module [16]byte
|
||||
copy(header[:], hash)
|
||||
copy(module[:], hash[16:])
|
||||
buildManifest(dir, version, header, module)
|
||||
return resource.MustReadPath(dir), true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
Reference in New Issue
Block a user