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

Add world.BlockSource interface to allow external libraries to use DF block models #923

Merged
merged 3 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
4 changes: 2 additions & 2 deletions server/block/model/anvil.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ type Anvil struct {
}

// BBox ...
func (a Anvil) BBox(cube.Pos, *world.World) []cube.BBox {
func (a Anvil) BBox(cube.Pos, world.BlockSource) []cube.BBox {
return []cube.BBox{full.Stretch(a.Facing.RotateLeft().Face().Axis(), -0.125)}
}

// FaceSolid ...
func (Anvil) FaceSolid(cube.Pos, cube.Face, *world.World) bool {
func (Anvil) FaceSolid(cube.Pos, cube.Face, world.BlockSource) bool {
return false
}
4 changes: 2 additions & 2 deletions server/block/model/cactus.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
type Cactus struct{}

// BBox returns a physics.BBox that is slightly smaller than a full block.
func (Cactus) BBox(cube.Pos, *world.World) []cube.BBox {
func (Cactus) BBox(cube.Pos, world.BlockSource) []cube.BBox {
return []cube.BBox{cube.Box(0.025, 0, 0.025, 0.975, 1, 0.975)}
}

// FaceSolid always returns false.
func (Cactus) FaceSolid(cube.Pos, cube.Face, *world.World) bool {
func (Cactus) FaceSolid(cube.Pos, cube.Face, world.BlockSource) bool {
return false
}
4 changes: 2 additions & 2 deletions server/block/model/cake.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ type Cake struct {
}

// BBox returns an BBox with a size that depends on the amount of bites taken.
func (c Cake) BBox(cube.Pos, *world.World) []cube.BBox {
func (c Cake) BBox(cube.Pos, world.BlockSource) []cube.BBox {
return []cube.BBox{cube.Box(0.0625, 0, 0.0625, 0.9375, 0.5, 0.9375).
ExtendTowards(cube.FaceWest, -(float64(c.Bites) / 8))}
}

// FaceSolid always returns false.
func (c Cake) FaceSolid(cube.Pos, cube.Face, *world.World) bool {
func (c Cake) FaceSolid(cube.Pos, cube.Face, world.BlockSource) bool {
return false
}
4 changes: 2 additions & 2 deletions server/block/model/campfire.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
type Campfire struct{}

// BBox returns a flat BBox with a height of 0.4375.
func (Campfire) BBox(cube.Pos, *world.World) []cube.BBox {
func (Campfire) BBox(cube.Pos, world.BlockSource) []cube.BBox {
return []cube.BBox{cube.Box(0, 0, 0, 1, 0.4375, 1)}
}

// FaceSolid returns true if the face is down.
func (Campfire) FaceSolid(_ cube.Pos, face cube.Face, _ *world.World) bool {
func (Campfire) FaceSolid(_ cube.Pos, face cube.Face, _ world.BlockSource) bool {
return face == cube.FaceDown
}
4 changes: 2 additions & 2 deletions server/block/model/carpet.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
type Carpet struct{}

// BBox returns a flat BBox with a width of 0.0625.
func (Carpet) BBox(cube.Pos, *world.World) []cube.BBox {
func (Carpet) BBox(cube.Pos, world.BlockSource) []cube.BBox {
return []cube.BBox{cube.Box(0, 0, 0, 1, 0.0625, 1)}
}

// FaceSolid always returns false.
func (Carpet) FaceSolid(cube.Pos, cube.Face, *world.World) bool {
func (Carpet) FaceSolid(cube.Pos, cube.Face, world.BlockSource) bool {
return false
}
4 changes: 2 additions & 2 deletions server/block/model/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ type Chain struct {
}

// BBox ...
func (c Chain) BBox(cube.Pos, *world.World) []cube.BBox {
func (c Chain) BBox(cube.Pos, world.BlockSource) []cube.BBox {
return []cube.BBox{cube.Box(0.40625, 0.40625, 0.40625, 0.59375, 0.59375, 0.59375).Stretch(c.Axis, 0.40625)}
}

// FaceSolid ...
func (Chain) FaceSolid(cube.Pos, cube.Face, *world.World) bool {
func (Chain) FaceSolid(cube.Pos, cube.Face, world.BlockSource) bool {
return false
}
4 changes: 2 additions & 2 deletions server/block/model/chest.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
type Chest struct{}

// BBox returns a physics.BBox that is slightly smaller than a full block.
func (Chest) BBox(cube.Pos, *world.World) []cube.BBox {
func (Chest) BBox(cube.Pos, world.BlockSource) []cube.BBox {
return []cube.BBox{cube.Box(0.025, 0, 0.025, 0.975, 0.95, 0.975)}
}

// FaceSolid always returns false.
func (Chest) FaceSolid(cube.Pos, cube.Face, *world.World) bool {
func (Chest) FaceSolid(cube.Pos, cube.Face, world.BlockSource) bool {
return false
}
4 changes: 2 additions & 2 deletions server/block/model/cocoa_bean.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type CocoaBean struct {
}

// BBox returns a single physics.BBox whose size depends on the age of the CocoaBean.
func (c CocoaBean) BBox(cube.Pos, *world.World) []cube.BBox {
func (c CocoaBean) BBox(cube.Pos, world.BlockSource) []cube.BBox {
return []cube.BBox{full.
Stretch(c.Facing.RotateRight().Face().Axis(), -(6-float64(c.Age))/16).
ExtendTowards(cube.FaceDown, -0.25).
Expand All @@ -25,6 +25,6 @@ func (c CocoaBean) BBox(cube.Pos, *world.World) []cube.BBox {
}

// FaceSolid always returns false.
func (c CocoaBean) FaceSolid(cube.Pos, cube.Face, *world.World) bool {
func (c CocoaBean) FaceSolid(cube.Pos, cube.Face, world.BlockSource) bool {
return false
}
7 changes: 4 additions & 3 deletions server/block/model/composter.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package model

import (
"math"

"github.com/df-mc/dragonfly/server/block/cube"
"github.com/df-mc/dragonfly/server/world"
"math"
)

// Composter is a model used by composter blocks. It is solid on all sides apart from the top, and the height of the
Expand All @@ -14,7 +15,7 @@ type Composter struct {
}

// BBox ...
func (c Composter) BBox(_ cube.Pos, _ *world.World) []cube.BBox {
func (c Composter) BBox(_ cube.Pos, _ world.BlockSource) []cube.BBox {
compostHeight := math.Abs(math.Min(float64(c.Level), 7)*0.125 - 0.0625)
return []cube.BBox{
cube.Box(0, 0, 0, 1, 1, 0.125),
Expand All @@ -26,6 +27,6 @@ func (c Composter) BBox(_ cube.Pos, _ *world.World) []cube.BBox {
}

// FaceSolid returns true for all faces other than the top.
func (Composter) FaceSolid(_ cube.Pos, face cube.Face, _ *world.World) bool {
func (Composter) FaceSolid(_ cube.Pos, face cube.Face, _ world.BlockSource) bool {
return face != cube.FaceUp
}
4 changes: 2 additions & 2 deletions server/block/model/decorated_pot.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
type DecoratedPot struct{}

// BBox returns a physics.BBox that is slightly smaller than a full block.
func (DecoratedPot) BBox(cube.Pos, *world.World) []cube.BBox {
func (DecoratedPot) BBox(cube.Pos, world.BlockSource) []cube.BBox {
return []cube.BBox{cube.Box(0.025, 0, 0.025, 0.975, 1, 0.975)}
}

// FaceSolid always returns true for the top and bottom face, and false for all other faces.
func (DecoratedPot) FaceSolid(_ cube.Pos, face cube.Face, _ *world.World) bool {
func (DecoratedPot) FaceSolid(_ cube.Pos, face cube.Face, _ world.BlockSource) bool {
return face.Axis() == cube.Y
}
4 changes: 2 additions & 2 deletions server/block/model/door.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Door struct {

// BBox returns a physics.BBox that depends on if the Door is open, what direction it is facing and whether it is
// attached to the right/left side of a block.
func (d Door) BBox(cube.Pos, *world.World) []cube.BBox {
func (d Door) BBox(cube.Pos, world.BlockSource) []cube.BBox {
if d.Open {
if d.Right {
return []cube.BBox{full.ExtendTowards(d.Facing.RotateLeft().Face(), -0.8125)}
Expand All @@ -29,6 +29,6 @@ func (d Door) BBox(cube.Pos, *world.World) []cube.BBox {
}

// FaceSolid always returns false.
func (d Door) FaceSolid(cube.Pos, cube.Face, *world.World) bool {
func (d Door) FaceSolid(cube.Pos, cube.Face, world.BlockSource) bool {
return false
}
4 changes: 2 additions & 2 deletions server/block/model/empty.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
type Empty struct{}

// BBox returns an empty slice.
func (Empty) BBox(cube.Pos, *world.World) []cube.BBox {
func (Empty) BBox(cube.Pos, world.BlockSource) []cube.BBox {
return nil
}

// FaceSolid always returns false.
func (Empty) FaceSolid(cube.Pos, cube.Face, *world.World) bool {
func (Empty) FaceSolid(cube.Pos, cube.Face, world.BlockSource) bool {
return false
}
4 changes: 2 additions & 2 deletions server/block/model/enchanting_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
type EnchantingTable struct{}

// BBox ...
func (EnchantingTable) BBox(cube.Pos, *world.World) []cube.BBox {
func (EnchantingTable) BBox(cube.Pos, world.BlockSource) []cube.BBox {
return []cube.BBox{cube.Box(0, 0, 0, 1, 0.75, 1)}
}

// FaceSolid ...
func (EnchantingTable) FaceSolid(cube.Pos, cube.Face, *world.World) bool {
func (EnchantingTable) FaceSolid(cube.Pos, cube.Face, world.BlockSource) bool {
return false
}
8 changes: 4 additions & 4 deletions server/block/model/fence.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ type Fence struct {
}

// BBox returns multiple physics.BBox depending on how many connections it has with the surrounding blocks.
func (f Fence) BBox(pos cube.Pos, w *world.World) []cube.BBox {
func (f Fence) BBox(pos cube.Pos, s world.BlockSource) []cube.BBox {
const offset = 0.375

boxes := make([]cube.BBox, 0, 5)
mainBox := cube.Box(offset, 0, offset, 1-offset, 1.5, 1-offset)

for i := cube.Face(2); i < 6; i++ {
pos := pos.Side(i)
block := w.Block(pos)
block := s.Block(pos)

if fence, ok := block.Model().(Fence); (ok && fence.Wood == f.Wood) || block.Model().FaceSolid(pos, i, w) {
if fence, ok := block.Model().(Fence); (ok && fence.Wood == f.Wood) || block.Model().FaceSolid(pos, i, s) {
boxes = append(boxes, mainBox.ExtendTowards(i, offset))
} else if _, ok := block.Model().(FenceGate); ok {
boxes = append(boxes, mainBox.ExtendTowards(i, offset))
Expand All @@ -34,6 +34,6 @@ func (f Fence) BBox(pos cube.Pos, w *world.World) []cube.BBox {
}

// FaceSolid returns true if the face is cube.FaceDown or cube.FaceUp.
func (f Fence) FaceSolid(_ cube.Pos, face cube.Face, _ *world.World) bool {
func (f Fence) FaceSolid(_ cube.Pos, face cube.Face, _ world.BlockSource) bool {
return face == cube.FaceDown || face == cube.FaceUp
}
4 changes: 2 additions & 2 deletions server/block/model/fence_gate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ type FenceGate struct {
}

// BBox returns up to one physics.BBox depending on the facing direction of the FenceGate and whether it is open.
func (f FenceGate) BBox(cube.Pos, *world.World) []cube.BBox {
func (f FenceGate) BBox(cube.Pos, world.BlockSource) []cube.BBox {
if f.Open {
return nil
}
return []cube.BBox{cube.Box(0, 0, 0, 1, 1.5, 1).Stretch(f.Facing.Face().Axis(), -0.375)}
}

// FaceSolid always returns false.
func (f FenceGate) FaceSolid(cube.Pos, cube.Face, *world.World) bool {
func (f FenceGate) FaceSolid(cube.Pos, cube.Face, world.BlockSource) bool {
return false
}
4 changes: 2 additions & 2 deletions server/block/model/grindstone.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ type Grindstone struct {
}

// BBox ...
func (g Grindstone) BBox(cube.Pos, *world.World) []cube.BBox {
func (g Grindstone) BBox(cube.Pos, world.BlockSource) []cube.BBox {
return []cube.BBox{cube.Box(0.125, 0.125, 0.125, 0.825, 0.825, 0.825).Stretch(g.Axis, 0.125)}
}

// FaceSolid always returns false.
func (g Grindstone) FaceSolid(cube.Pos, cube.Face, *world.World) bool {
func (g Grindstone) FaceSolid(cube.Pos, cube.Face, world.BlockSource) bool {
return false
}
4 changes: 2 additions & 2 deletions server/block/model/hopper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
type Hopper struct{}

// BBox returns a physics.BBox that spans a full block.
func (h Hopper) BBox(cube.Pos, *world.World) []cube.BBox {
func (h Hopper) BBox(cube.Pos, world.BlockSource) []cube.BBox {
bbox := []cube.BBox{full.ExtendTowards(cube.FaceUp, -0.375)}
for _, f := range cube.HorizontalFaces() {
bbox = append(bbox, full.ExtendTowards(f, -0.875))
Expand All @@ -18,6 +18,6 @@ func (h Hopper) BBox(cube.Pos, *world.World) []cube.BBox {
}

// FaceSolid only returns true for the top face of the hopper.
func (Hopper) FaceSolid(_ cube.Pos, face cube.Face, _ *world.World) bool {
func (Hopper) FaceSolid(_ cube.Pos, face cube.Face, _ world.BlockSource) bool {
return face == cube.FaceUp
}
4 changes: 2 additions & 2 deletions server/block/model/ladder.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ type Ladder struct {
}

// BBox returns one physics.BBox that depends on the facing direction of the Ladder.
func (l Ladder) BBox(cube.Pos, *world.World) []cube.BBox {
func (l Ladder) BBox(cube.Pos, world.BlockSource) []cube.BBox {
return []cube.BBox{full.ExtendTowards(l.Facing, -0.8125)}
}

// FaceSolid always returns false.
func (l Ladder) FaceSolid(cube.Pos, cube.Face, *world.World) bool {
func (l Ladder) FaceSolid(cube.Pos, cube.Face, world.BlockSource) bool {
return false
}
4 changes: 2 additions & 2 deletions server/block/model/lantern.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ type Lantern struct {
}

// BBox returns a physics.BBox attached to either the ceiling or to the ground.
func (l Lantern) BBox(cube.Pos, *world.World) []cube.BBox {
func (l Lantern) BBox(cube.Pos, world.BlockSource) []cube.BBox {
if l.Hanging {
return []cube.BBox{cube.Box(0.3125, 0.125, 0.3125, 0.6875, 0.625, 0.6875)}
}
return []cube.BBox{cube.Box(0.3125, 0, 0.3125, 0.6875, 0.5, 0.6875)}
}

// FaceSolid always returns false.
func (l Lantern) FaceSolid(cube.Pos, cube.Face, *world.World) bool {
func (l Lantern) FaceSolid(cube.Pos, cube.Face, world.BlockSource) bool {
return false
}
4 changes: 2 additions & 2 deletions server/block/model/leaves.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
type Leaves struct{}

// BBox returns a physics.BBox that spans a full block.
func (Leaves) BBox(cube.Pos, *world.World) []cube.BBox {
func (Leaves) BBox(cube.Pos, world.BlockSource) []cube.BBox {
return []cube.BBox{full}
}

// FaceSolid always returns false.
func (Leaves) FaceSolid(cube.Pos, cube.Face, *world.World) bool {
func (Leaves) FaceSolid(cube.Pos, cube.Face, world.BlockSource) bool {
return false
}
4 changes: 2 additions & 2 deletions server/block/model/lectern.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
type Lectern struct{}

// BBox ...
func (Lectern) BBox(cube.Pos, *world.World) []cube.BBox {
func (Lectern) BBox(cube.Pos, world.BlockSource) []cube.BBox {
return []cube.BBox{cube.Box(0, 0, 0, 1, 0.9, 1)}
}

// FaceSolid ...
func (Lectern) FaceSolid(cube.Pos, cube.Face, *world.World) bool {
func (Lectern) FaceSolid(cube.Pos, cube.Face, world.BlockSource) bool {
return false
}
4 changes: 2 additions & 2 deletions server/block/model/skull.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Skull struct {
}

// BBox ...
func (s Skull) BBox(cube.Pos, *world.World) []cube.BBox {
func (s Skull) BBox(cube.Pos, world.BlockSource) []cube.BBox {
box := cube.Box(0.25, 0, 0.25, 0.75, 0.5, 0.75)
if !s.Hanging {
return []cube.BBox{box}
Expand All @@ -23,6 +23,6 @@ func (s Skull) BBox(cube.Pos, *world.World) []cube.BBox {
}

// FaceSolid ...
func (Skull) FaceSolid(cube.Pos, cube.Face, *world.World) bool {
func (Skull) FaceSolid(cube.Pos, cube.Face, world.BlockSource) bool {
return false
}
4 changes: 2 additions & 2 deletions server/block/model/slab.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Slab struct {

// BBox returns either a physics.BBox spanning a full block or a half block in the top/bottom part of the block,
// depending on the Double and Top fields.
func (s Slab) BBox(cube.Pos, *world.World) []cube.BBox {
func (s Slab) BBox(cube.Pos, world.BlockSource) []cube.BBox {
if s.Double {
return []cube.BBox{full}
}
Expand All @@ -27,7 +27,7 @@ func (s Slab) BBox(cube.Pos, *world.World) []cube.BBox {

// FaceSolid returns true if the Slab is double, or if the face is cube.FaceUp when the Top field is true, or if the
// face is cube.FaceDown when the Top field is false.
func (s Slab) FaceSolid(_ cube.Pos, face cube.Face, _ *world.World) bool {
func (s Slab) FaceSolid(_ cube.Pos, face cube.Face, _ world.BlockSource) bool {
if s.Double {
return true
} else if s.Top {
Expand Down
4 changes: 2 additions & 2 deletions server/block/model/solid.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ type Solid struct{}
var full = cube.Box(0, 0, 0, 1, 1, 1)

// BBox returns a physics.BBox spanning a full block.
func (Solid) BBox(cube.Pos, *world.World) []cube.BBox {
func (Solid) BBox(cube.Pos, world.BlockSource) []cube.BBox {
return []cube.BBox{full}
}

// FaceSolid always returns true.
func (Solid) FaceSolid(cube.Pos, cube.Face, *world.World) bool {
func (Solid) FaceSolid(cube.Pos, cube.Face, world.BlockSource) bool {
return true
}
Loading
Loading