github.com/df-mc/dragonfly@v0.9.13/server/block/wood_fence_gate.go (about)

     1  package block
     2  
     3  import (
     4  	"github.com/df-mc/dragonfly/server/block/cube"
     5  	"github.com/df-mc/dragonfly/server/block/model"
     6  	"github.com/df-mc/dragonfly/server/item"
     7  	"github.com/df-mc/dragonfly/server/world"
     8  	"github.com/df-mc/dragonfly/server/world/sound"
     9  	"github.com/go-gl/mathgl/mgl64"
    10  	"time"
    11  )
    12  
    13  // WoodFenceGate is a block that can be used as an openable 1x1 barrier.
    14  type WoodFenceGate struct {
    15  	transparent
    16  	bass
    17  	sourceWaterDisplacer
    18  
    19  	// Wood is the type of wood of the fence gate. This field must have one of the values found in the material
    20  	// package.
    21  	Wood WoodType
    22  	// Facing is the direction the fence gate swings open.
    23  	Facing cube.Direction
    24  	// Open is whether the fence gate is open.
    25  	Open bool
    26  	// Lowered lowers the fence gate by 3 pixels and is set when placed next to wall blocks.
    27  	Lowered bool
    28  }
    29  
    30  // BreakInfo ...
    31  func (f WoodFenceGate) BreakInfo() BreakInfo {
    32  	return newBreakInfo(2, alwaysHarvestable, axeEffective, oneOf(f)).withBlastResistance(15)
    33  }
    34  
    35  // FlammabilityInfo ...
    36  func (f WoodFenceGate) FlammabilityInfo() FlammabilityInfo {
    37  	if !f.Wood.Flammable() {
    38  		return newFlammabilityInfo(0, 0, false)
    39  	}
    40  	return newFlammabilityInfo(5, 20, true)
    41  }
    42  
    43  // FuelInfo ...
    44  func (WoodFenceGate) FuelInfo() item.FuelInfo {
    45  	return newFuelInfo(time.Second * 15)
    46  }
    47  
    48  // UseOnBlock ...
    49  func (f WoodFenceGate) UseOnBlock(pos cube.Pos, face cube.Face, _ mgl64.Vec3, w *world.World, user item.User, ctx *item.UseContext) bool {
    50  	pos, _, used := firstReplaceable(w, pos, face, f)
    51  	if !used {
    52  		return false
    53  	}
    54  	f.Facing = user.Rotation().Direction()
    55  	f.Lowered = f.shouldBeLowered(pos, w)
    56  
    57  	place(w, pos, f, user, ctx)
    58  	return placed(ctx)
    59  }
    60  
    61  // NeighbourUpdateTick ...
    62  func (f WoodFenceGate) NeighbourUpdateTick(pos, _ cube.Pos, w *world.World) {
    63  	if f.shouldBeLowered(pos, w) != f.Lowered {
    64  		f.Lowered = !f.Lowered
    65  		w.SetBlock(pos, f, nil)
    66  	}
    67  }
    68  
    69  // shouldBeLowered returns if the fence gate should be lowered or not, based on the neighbouring walls.
    70  func (f WoodFenceGate) shouldBeLowered(pos cube.Pos, w *world.World) bool {
    71  	leftSide := f.Facing.RotateLeft().Face()
    72  	_, left := w.Block(pos.Side(leftSide)).(Wall)
    73  	_, right := w.Block(pos.Side(leftSide.Opposite())).(Wall)
    74  	return left || right
    75  }
    76  
    77  // Activate ...
    78  func (f WoodFenceGate) Activate(pos cube.Pos, _ cube.Face, w *world.World, u item.User, _ *item.UseContext) bool {
    79  	f.Open = !f.Open
    80  	if f.Open && f.Facing.Opposite() == u.Rotation().Direction() {
    81  		f.Facing = f.Facing.Opposite()
    82  	}
    83  	w.SetBlock(pos, f, nil)
    84  	if f.Open {
    85  		w.PlaySound(pos.Vec3Centre(), sound.FenceGateOpen{Block: f})
    86  		return true
    87  	}
    88  	w.PlaySound(pos.Vec3Centre(), sound.FenceGateClose{Block: f})
    89  	return true
    90  }
    91  
    92  // SideClosed ...
    93  func (f WoodFenceGate) SideClosed(cube.Pos, cube.Pos, *world.World) bool {
    94  	return false
    95  }
    96  
    97  // EncodeItem ...
    98  func (f WoodFenceGate) EncodeItem() (name string, meta int16) {
    99  	if f.Wood == OakWood() {
   100  		return "minecraft:fence_gate", 0
   101  	}
   102  	return "minecraft:" + f.Wood.String() + "_fence_gate", 0
   103  }
   104  
   105  // EncodeBlock ...
   106  func (f WoodFenceGate) EncodeBlock() (name string, properties map[string]any) {
   107  	if f.Wood == OakWood() {
   108  		return "minecraft:fence_gate", map[string]any{"direction": int32(horizontalDirection(f.Facing)), "open_bit": f.Open, "in_wall_bit": f.Lowered}
   109  	}
   110  	return "minecraft:" + f.Wood.String() + "_fence_gate", map[string]any{"direction": int32(horizontalDirection(f.Facing)), "open_bit": f.Open, "in_wall_bit": f.Lowered}
   111  }
   112  
   113  // Model ...
   114  func (f WoodFenceGate) Model() world.BlockModel {
   115  	return model.FenceGate{Facing: f.Facing, Open: f.Open}
   116  }
   117  
   118  // allFenceGates returns a list of all trapdoor types.
   119  func allFenceGates() (fenceGates []world.Block) {
   120  	for _, w := range WoodTypes() {
   121  		for i := cube.Direction(0); i <= 3; i++ {
   122  			fenceGates = append(fenceGates, WoodFenceGate{Wood: w, Facing: i, Open: false, Lowered: false})
   123  			fenceGates = append(fenceGates, WoodFenceGate{Wood: w, Facing: i, Open: false, Lowered: true})
   124  			fenceGates = append(fenceGates, WoodFenceGate{Wood: w, Facing: i, Open: true, Lowered: true})
   125  			fenceGates = append(fenceGates, WoodFenceGate{Wood: w, Facing: i, Open: true, Lowered: false})
   126  		}
   127  	}
   128  	return
   129  }