Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement support for custom blocks #512

Merged
merged 88 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from 76 commits
Commits
Show all changes
88 commits
Select commit Hold shift + click to select a range
2fa6415
Start of custom blocks.. yeah
TwistedAsylumMC Apr 3, 2022
a0466b6
Merge branch 'master' into feature/custom-blocks
TwistedAsylumMC May 15, 2022
dfeca65
packbuilder/blocks.go: Fix package name
TwistedAsylumMC May 15, 2022
b0b0a4a
world/block_state.go: horrible horrible horrible sorting code.
JustTalDevelops May 21, 2022
fca2e6a
world/block_state.go: woops
JustTalDevelops May 21, 2022
a01a1ba
world/block_state.go: Properly register existing states.
JustTalDevelops May 21, 2022
4e8a57d
world/block_state.go: Possibly simplify code.
JustTalDevelops May 21, 2022
8a06a04
world/block_state.go: Don't sort unnecessarily.
JustTalDevelops May 21, 2022
8b7e40f
world/block_state.go: Various fixes.
JustTalDevelops May 21, 2022
8c74e52
block: Added a PHP custom block for testing.
JustTalDevelops May 21, 2022
5222de9
world/block_state.go: Update hashes.
JustTalDevelops May 21, 2022
051c5bb
block/hash.go: Added a NextHash function.
JustTalDevelops May 22, 2022
d90fb7e
customblock: Various material target changes.
JustTalDevelops May 22, 2022
2ee89b3
world/block.go: Improve doc.
JustTalDevelops May 22, 2022
2a4c4b6
customblock/server.go: Properly handle no geometry.
JustTalDevelops May 22, 2022
ca04af0
customblock/material_target.go: Added a Name function.
JustTalDevelops May 22, 2022
c4ea4be
Merge branch 'master' into feature/custom-blocks
JustTalDevelops May 22, 2022
4ea1ef8
block/hash.go: Various fixes.
JustTalDevelops May 22, 2022
5f82b33
server/server.go: Added some basic components. (WIP)
JustTalDevelops May 22, 2022
0dc36c5
customblock/render_method.go: Simplify AmbientOcclusion.
JustTalDevelops May 22, 2022
7d44493
customblock/render_method.go: Fix doc.
JustTalDevelops May 22, 2022
fed0155
customblock/model.go: Use MaterialTarget.
JustTalDevelops May 22, 2022
58d8b64
Merge remote-tracking branch 'origin/feature/custom-blocks' into feat…
JustTalDevelops May 22, 2022
7027e3f
customblock/render_method.go: consistency!!11
JustTalDevelops May 22, 2022
ed227b4
customblock/model.go: woops.
JustTalDevelops May 22, 2022
94f0edf
server/server.go: consistency v2
JustTalDevelops May 22, 2022
19f2562
world/block_state.go: Various improvements.
JustTalDevelops May 22, 2022
c541855
world/block_state.go: Remove debug.
JustTalDevelops May 22, 2022
9e82cab
world/block_state.go: Woops.
JustTalDevelops May 22, 2022
d2f9ebd
world/block_state.go: Update airRID.
JustTalDevelops May 22, 2022
7419a89
server/server.go: Actually use components.
JustTalDevelops May 22, 2022
502fac7
server/server.go: Component fixes.
JustTalDevelops May 22, 2022
cd61a67
world/block_state.go: Cleaned up a ton of code.
JustTalDevelops May 22, 2022
f158efe
block/php.go: Removed changes.
JustTalDevelops May 22, 2022
52ccfa5
main.go: Fix creative item count.
JustTalDevelops May 22, 2022
0cd2b2e
world/block_state.go: Various changes.
JustTalDevelops May 23, 2022
6847587
server/server.go: Fix the LightEmitter component.
JustTalDevelops May 23, 2022
fb56c62
server/server.go: Fix creative items not properly functioning + light…
JustTalDevelops May 23, 2022
9c2bc3f
server/server.go: Support properties for custom blocks.
JustTalDevelops May 23, 2022
f997312
world/block.go: Fixesss
JustTalDevelops May 23, 2022
5341d42
packbuilder/blocks.go: Use Sprintf for consistency.
JustTalDevelops May 23, 2022
14e2f57
blockinternal: Moved component building.
JustTalDevelops May 24, 2022
4fc2be4
fixed client crashes caused by empty block entry
DaPigGuy May 24, 2022
3436f2e
server/server.go: Made an itemComponentEntries function for consistency.
JustTalDevelops May 24, 2022
6452534
blockinternal/blocks.go: Permutations?
JustTalDevelops May 24, 2022
ed0d697
block/php.go: woops.
JustTalDevelops May 24, 2022
9025740
blockinternal/builder.go: Set Molang version.
JustTalDevelops May 24, 2022
b5f3994
blockinternal/builder.go: Update Empty check.
JustTalDevelops May 24, 2022
b71c60b
blockinternal/builder.go: Remove useless Empty.
JustTalDevelops May 24, 2022
c02ffe9
blockinternal/builder.go: Remove useless Values.
JustTalDevelops May 24, 2022
d0c2f68
world/block.go: Allow custom blocks to opt into custom geometry.
JustTalDevelops May 24, 2022
10efae1
blockinternal/components.go: More of permutations.
JustTalDevelops May 25, 2022
40a61a7
blockinternal/components.go: Docs.
JustTalDevelops May 25, 2022
77dad5f
block/pig.go: Changes.
JustTalDevelops May 25, 2022
7954a8a
block/pig.go: Made Pig flammable.
JustTalDevelops May 25, 2022
ae0f82d
block/pig.go: Changes to permutation API.
JustTalDevelops May 26, 2022
43b4f50
block/block.go: Removed Placeable.
JustTalDevelops May 26, 2022
bfef090
block/material_target.go: Added missing material targets.
JustTalDevelops May 26, 2022
fa457da
block/pig.go: Start implementing new permutation system.
JustTalDevelops May 26, 2022
5de1093
blockinternal/components.go: More changes.
JustTalDevelops May 26, 2022
4ceb8c4
blockinternal/components.go: Support for other permutable components.
JustTalDevelops May 27, 2022
5ef03d6
block/pig.go: Various changes.
JustTalDevelops May 27, 2022
11e9225
blockinternal/builder.go: Set aim and block collision boxes.
JustTalDevelops May 27, 2022
7e5eb82
blockinternal/builder.go: Various changes.
JustTalDevelops May 27, 2022
1c29cdc
Merge branch 'master' into feature/custom-blocks
JustTalDevelops Jul 22, 2022
112cf1c
dragonfly: Various fixes to custom blocks.
JustTalDevelops Jul 22, 2022
195a269
world/block_state.go: Use fast hash.
JustTalDevelops Jul 22, 2022
ba461ea
customblock: Some work on permutation specific textures + cleanup.
JustTalDevelops Jul 22, 2022
ceed0c5
customblock: Permutation-specific geometry.
JustTalDevelops Jul 22, 2022
62f9c8b
blockinternal/builder.go: Removed pointless nil checks.
JustTalDevelops Jul 22, 2022
7d12471
world/block_state.go: Remove pointless else if.
JustTalDevelops Jul 23, 2022
ac22916
customblock/geometry.go: Made UV `any` because the idiots at Mojang d…
JustTalDevelops Jul 25, 2022
b191b9e
customblock/geometry.go: Abs the cube sizes
TwistedAsylumMC Jul 25, 2022
f4b1855
server/server.go: Lazy init block and item components
TwistedAsylumMC Jul 26, 2022
efaf160
blockinternal/builder.go: Don't write multiple traits.
JustTalDevelops Jul 26, 2022
3494f10
blockinternal/builder.go: Maintain registration order.
JustTalDevelops Jul 26, 2022
786ba66
Update server/internal/blockinternal/components.go
JustTalDevelops Aug 11, 2022
e313001
Update server/world/block_state.go
JustTalDevelops Aug 11, 2022
d1e48b8
updated contributor list
JustTalDevelops Aug 11, 2022
cbdfa9b
dragonfly: Merge master into feature/custom-blocks
TwistedAsylumMC Nov 2, 2023
273b125
dragonfly: Finish custom block implementation
TwistedAsylumMC Nov 3, 2023
9ec048c
dragonfly: Cleanup custom block code
TwistedAsylumMC Nov 3, 2023
21e8df6
sliceutil/sliceutil.go: Remove Insert() in favour of slices package
TwistedAsylumMC Nov 5, 2023
884aaa5
dragonfly: Use Go 1.21 and use std slices package
TwistedAsylumMC Nov 5, 2023
7e8918c
.github/workflows: Use Go 1.21
TwistedAsylumMC Nov 5, 2023
945d56d
block/hash.go: Remove dropper hash
TwistedAsylumMC Nov 5, 2023
17073c1
leveldat/data.go: Add naturalgeneration field
TwistedAsylumMC Nov 17, 2023
79f1c57
Merge branch 'master' into feature/custom-blocks
TwistedAsylumMC Dec 9, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions cmd/blockhash/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,13 @@ func procPackage(pkg *ast.Package, fs *token.FileSet, w io.Writer) {

b.writePackage(w)
i := b.writeConstants(w)
b.writeNextHash(w)
b.writeMethods(w, i)
}

var (
packageFormat = "// Code generated by cmd/blockhash; DO NOT EDIT.\n\npackage %v\n\n"
methodFormat = "\nfunc (%v%v) Hash() uint64 {\n\treturn %v\n}\n"
methodFormat = "\n// Hash ...\nfunc (%v%v) Hash() uint64 {\n\treturn %v\n}\n"
constFormat = "\thash%v"
)

Expand Down Expand Up @@ -112,13 +113,28 @@ func (b *hashBuilder) writeConstants(w io.Writer) (bitSize int) {
i++
}

if _, err := fmt.Fprintln(w, ")"); err != nil {
if _, err := fmt.Fprintln(w, "\thashBase\n)"); err != nil {
log.Fatalln(err)
}

return bits.Len64(i)
}

func (b *hashBuilder) writeNextHash(w io.Writer) {
if _, err := fmt.Fprintln(w, "\n// base represents the base hash for all custom blocks."); err != nil {
log.Fatalln(err)
}
if _, err := fmt.Fprintln(w, "var base = uint64(hashBase - 1)"); err != nil {
log.Fatalln(err)
}
if _, err := fmt.Fprintln(w, "\n// NextHash returns the next free hash for custom blocks."); err != nil {
log.Fatalln(err)
}
if _, err := fmt.Fprintln(w, "func NextHash() uint64 {\n\tbase++\n\treturn base\n}"); err != nil {
log.Fatalln(err)
}
}

func (b *hashBuilder) writeMethods(w io.Writer, baseBits int) {
for _, name := range b.names {
fields := b.blockFields[name]
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ require (
github.com/df-mc/goleveldb v1.1.9
github.com/go-gl/mathgl v1.0.0
github.com/google/uuid v1.3.0
github.com/kr/pretty v0.1.0
github.com/pelletier/go-toml v1.9.4
github.com/rogpeppe/go-internal v1.3.0
github.com/sandertv/gophertunnel v1.22.3
github.com/segmentio/fasthash v1.0.3
github.com/sirupsen/logrus v1.8.1
go.uber.org/atomic v1.9.0
golang.org/x/exp v0.0.0-20220713135740-79cabaa25d75
Expand All @@ -22,6 +24,7 @@ require (
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/klauspost/compress v1.15.1 // indirect
github.com/kr/text v0.1.0 // indirect
github.com/muhammadmuzzammil1998/jsonc v1.0.0 // indirect
github.com/sandertv/go-raknet v1.11.1 // indirect
github.com/stretchr/testify v1.7.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,10 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/klauspost/compress v1.15.1 h1:y9FcTHGyrebwfP0ZZqFiaxTaiDnUrGkJkI+f583BL1A=
github.com/klauspost/compress v1.15.1/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/muhammadmuzzammil1998/jsonc v1.0.0 h1:8o5gBQn4ZA3NBA9DlTujCj2a4w0tqWrPVjDwhzkgTIs=
github.com/muhammadmuzzammil1998/jsonc v1.0.0/go.mod h1:saF2fIVw4banK0H4+/EuqfFLpRnoy5S+ECwTOCcRcSU=
Expand All @@ -149,6 +151,8 @@ github.com/sandertv/go-raknet v1.11.1 h1:0auvhHoZNyC/Z1l5xqniE3JE+w3MGO3n3JXEGHz
github.com/sandertv/go-raknet v1.11.1/go.mod h1:Gx+WgZBMQ0V2UoouGoJ8Wj6CDrMBQ4SB2F/ggpl5/+Y=
github.com/sandertv/gophertunnel v1.22.3 h1:xVTC348fU0gr62tbWnr8iuvZggOBgsKEm98sotZYhb8=
github.com/sandertv/gophertunnel v1.22.3/go.mod h1:dMOw79FHxr2azEqiGH20AwdljisAN1kqwu5SjPBnZ5k=
github.com/segmentio/fasthash v1.0.3 h1:EI9+KE1EwvMLBWwjpRDc+fEM+prwxDYbslddQGtrmhM=
github.com/segmentio/fasthash v1.0.3/go.mod h1:waKX8l2N8yckOgmSsXJi7x1ZfdKZ4x7KRMzBtS3oedY=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72 h1:qLC7fQah7D6K1B0ujays3HV9gkFtllcxhzImRR7ArPQ=
Expand Down
16 changes: 15 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ package main
import (
"fmt"
"github.com/df-mc/dragonfly/server"
"github.com/df-mc/dragonfly/server/block"
"github.com/df-mc/dragonfly/server/block/cube"
"github.com/df-mc/dragonfly/server/item"
"github.com/df-mc/dragonfly/server/item/creative"
"github.com/df-mc/dragonfly/server/player"
"github.com/df-mc/dragonfly/server/player/chat"
"github.com/df-mc/dragonfly/server/world"
"github.com/pelletier/go-toml"
"github.com/sirupsen/logrus"
"io/ioutil"
Expand All @@ -22,13 +28,21 @@ func main() {
log.Fatalln(err)
}

for _, direction := range cube.Directions() {
world.RegisterBlock(block.Pig{Facing: direction})
}
world.RegisterItem(block.Pig{})
creative.RegisterItem(item.NewStack(block.Pig{}, 1))

srv := server.New(&config, log)
srv.CloseOnProgramEnd()
if err := srv.Start(); err != nil {
log.Fatalln(err)
}

for srv.Accept(nil) {
for srv.Accept(func(p *player.Player) {
p.Inventory().AddItem(item.NewStack(block.Pig{}, 32))
}) {
}
}

Expand Down
Binary file added php.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pig.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 41 additions & 1 deletion server/block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/df-mc/dragonfly/server/item"
"github.com/df-mc/dragonfly/server/world"
"github.com/df-mc/dragonfly/server/world/sound"
"github.com/go-gl/mathgl/mgl64"
"time"
)

Expand Down Expand Up @@ -40,6 +41,13 @@ type LightEmitter interface {
LightEmissionLevel() uint8
}

// PermutableLightEmitter represents a permutable custom block that emits light when placed.
type PermutableLightEmitter interface {
// LightEmissionLevel returns the light emission level of the block, a number from 0-15 where 15 is the
// brightest and 0 means it doesn't emit light at all.
LightEmissionLevel() (uint8, bool, map[string]uint8)
}

// LightDiffuser represents a block that diffuses light. This means that a specific amount of light levels
// will be subtracted when light passes through the block.
// Blocks that do not implement LightDiffuser will be assumed to be solid: Light will not be able to pass
Expand All @@ -51,6 +59,14 @@ type LightDiffuser interface {
LightDiffusionLevel() uint8
}

// PermutableLightDiffuser represents a permutable custom block that diffuses light.
type PermutableLightDiffuser interface {
// LightDiffusionLevel returns the amount of light levels that is subtracted when light passes through
// this block. Some blocks, such as leaves, have this behaviour. A diffusion level of 15 means that all
// light will be completely blocked when it passes through the block.
LightDiffusionLevel() (uint8, bool, map[string]uint8)
}

// Replaceable represents a block that may be replaced by another block automatically. An example is grass,
// which may be replaced by clicking it with another block.
type Replaceable interface {
Expand All @@ -71,13 +87,31 @@ type EntityInsider interface {
EntityInside(pos cube.Pos, w *world.World, e world.Entity)
}

// Frictional represents a block that may have a custom friction value, friction is used for entity drag when the
// Frictional represents a block that may have a custom friction value. Friction is used for entity drag when the
// entity is on ground. If a block does not implement this interface, it should be assumed that its friction is 0.6.
type Frictional interface {
// Friction returns the block's friction value.
Friction() float64
}

// PermutableFrictional represents a permutable custom block that may have a custom friction value.
type PermutableFrictional interface {
// Friction returns the block's friction value.
Friction() (float64, bool, map[string]float64)
}

// Rotatable represents a custom block that may be rotated.
type Rotatable interface {
// Rotation returns the rotation of the block as an mgl64.Vec3.
Rotation() mgl64.Vec3
}

// PermutableRotatable represents a permutable custom block that may be rotated.
type PermutableRotatable interface {
// Rotation returns the rotation of the block as an mgl64.Vec3.
Rotation() (mgl64.Vec3, bool, map[string]mgl64.Vec3)
}

func calculateFace(user item.User, placePos cube.Pos) cube.Face {
userPos := user.Position()
pos := cube.PosFromVec3(userPos)
Expand Down Expand Up @@ -212,6 +246,12 @@ type Flammable interface {
FlammabilityInfo() FlammabilityInfo
}

// PermutableFlammable is an interface for permutable custom blocks that can catch on fire.
type PermutableFlammable interface {
// FlammabilityInfo returns information about a block's behavior involving fire.
FlammabilityInfo() (FlammabilityInfo, bool, map[string]FlammabilityInfo)
}

// FlammabilityInfo contains values related to block behaviors involving fire.
type FlammabilityInfo struct {
// Encouragement is the chance a block will catch on fire during attempted fire spread.
Expand Down
6 changes: 6 additions & 0 deletions server/block/break_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ type Breakable interface {
BreakInfo() BreakInfo
}

// PermutableBreakable represents a permutable custom block that may be broken by a player in survival mode.
type PermutableBreakable interface {
// BreakInfo returns information of the block related to the breaking of it.
BreakInfo() (BreakInfo, bool, map[string]BreakInfo)
}

// BreakDuration returns the base duration that breaking the block passed takes when being broken using the
// item passed.
func BreakDuration(b world.Block, i item.Stack) time.Duration {
Expand Down
2 changes: 1 addition & 1 deletion server/block/cube/axis.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (a Axis) RotateLeft() Axis {

// RotateRight rotates an Axis from X to Z or from Z to X.
func (a Axis) RotateRight() Axis {
// No difference in rotating left or right for an Axis.
// No difference in rotating left or right for an Facing.
TwistedAsylumMC marked this conversation as resolved.
Show resolved Hide resolved
return a.RotateLeft()
}

Expand Down
95 changes: 95 additions & 0 deletions server/block/customblock/geometry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package customblock

import (
"github.com/go-gl/mathgl/mgl32"
"github.com/go-gl/mathgl/mgl64"
"math"
)

// Geometries represents the JSON structure of a vanilla geometry file. It contains a format version and a slice of
// unique geometries.
type Geometries struct {
FormatVersion string `json:"format_version"`
Geometry []Geometry `json:"minecraft:geometry"`
}

// Geometry represents a single geometry that contains bones and other information.
type Geometry struct {
Description struct {
Identifier string `json:"identifier"`
TextureWidth int `json:"texture_width"`
TextureHeight int `json:"texture_height"`
VisibleBoundsWidth float64 `json:"visible_bounds_width"`
VisibleBoundsHeight float64 `json:"visible_bounds_height"`
VisibleBoundsOffset mgl64.Vec3 `json:"visible_bounds_offset"`
} `json:"description"`
Bones []struct {
Name string `json:"name"`
Pivot mgl64.Vec3 `json:"pivot,omitempty"`
Rotation mgl64.Vec3 `json:"rotation,omitempty"`
Cubes []struct {
Origin mgl64.Vec3 `json:"origin"`
Size mgl64.Vec3 `json:"size"`
UV any `json:"uv"`
Pivot mgl64.Vec3 `json:"pivot,omitempty"`
Rotation mgl64.Vec3 `json:"rotation,omitempty"`
Inflate float64 `json:"inflate,omitempty"`
} `json:"cubes"`
} `json:"bones"`
}

// Encode encodes the geometry into a JSON component.
func (g Geometry) Encode() map[string]any {
origin, size := vec64To32(g.Origin()), vec64To32(g.Size())
box := map[string]any{
"enabled": byte(0x1),
"origin": []float32{
origin.X(),
origin.Y(),
origin.Z(),
},
"size": []float32{
size.X(),
size.Y(),
size.Z(),
},
}
return map[string]any{
"minecraft:aim_collision": box,
"minecraft:collision_box": box,
"minecraft:pick_collision": map[string]any{
"enabled": uint8(1),
"origin": origin[:],
"size": size[:],
},
}
}

// Origin returns the origin of the geometry. It is calculated by using the smallest origin points of all cubes.
func (g Geometry) Origin() (x mgl64.Vec3) {
for _, bone := range g.Bones {
for _, cube := range bone.Cubes {
x[0] = math.Min(x[0], cube.Origin.X())
x[1] = math.Min(x[1], cube.Origin.Y())
x[2] = math.Min(x[2], cube.Origin.Z())
}
}
return
}

// Size returns the size of the geometry. It is calculated by using the largest size of all cubes.
func (g Geometry) Size() (x mgl64.Vec3) {
for _, bone := range g.Bones {
for _, cube := range bone.Cubes {
x[0] = math.Max(x[0], math.Abs(cube.Size.X()))
x[1] = math.Max(x[1], math.Abs(cube.Size.Y()))
x[2] = math.Max(x[2], math.Abs(cube.Size.Z()))
}
}
return
}

// vec64To32 converts a mgl64.Vec3 to a mgl32.Vec3.
func vec64To32(vec3 mgl64.Vec3) mgl32.Vec3 {
return mgl32.Vec3{float32(vec3[0]), float32(vec3[1]), float32(vec3[2])}
}
66 changes: 66 additions & 0 deletions server/block/customblock/material.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package customblock

// Material represents a single material used for rendering part of a custom block.
type Material struct {
// texture is the name of the texture for the material.
texture string
// renderMethod is the method to use when rendering the material.
renderMethod Method
// faceDimming is if the material should be dimmed by the direction it's facing.
faceDimming bool
// ambientOcclusion is if the material should have ambient occlusion applied when lighting.
ambientOcclusion bool
}

// NewMaterial returns a new Material with the provided information. It enables face dimming by default and ambient
// occlusion based on the render method given.
func NewMaterial(texture string, method Method) Material {
return Material{
faceDimming: true,
texture: texture,
renderMethod: method,
ambientOcclusion: method.AmbientOcclusion(),
}
}

// WithFaceDimming returns a copy of the Material with face dimming enabled.
func (m Material) WithFaceDimming() Material {
m.faceDimming = true
return m
}

// WithoutFaceDimming returns a copy of the Material with face dimming disabled.
func (m Material) WithoutFaceDimming() Material {
m.faceDimming = false
return m
}

// WithAmbientOcclusion returns a copy of the Material with ambient occlusion enabled.
func (m Material) WithAmbientOcclusion() Material {
m.ambientOcclusion = true
return m
}

// WithoutAmbientOcclusion returns a copy of the Material with ambient occlusion disabled.
func (m Material) WithoutAmbientOcclusion() Material {
m.ambientOcclusion = false
return m
}

// Encode returns the material encoded as a map[string]any.
func (m Material) Encode() map[string]any {
return map[string]any{
"texture": m.texture,
"render_method": m.renderMethod.String(),
"face_dimming": boolByte(m.faceDimming),
"ambient_occlusion": boolByte(m.ambientOcclusion),
}
}

// boolByte returns 1 if the bool passed is true, or 0 if it is false.
func boolByte(b bool) uint8 {
if b {
return 1
}
return 0
}
Loading