github.com/df-mc/dragonfly@v0.9.13/server/block/wood_trapdoor.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  	"math"
    11  	"time"
    12  )
    13  
    14  // WoodTrapdoor is a block that can be used as an openable 1x1 barrier.
    15  type WoodTrapdoor struct {
    16  	transparent
    17  	bass
    18  	sourceWaterDisplacer
    19  
    20  	// Wood is the type of wood of the trapdoor. This field must have one of the values found in the material
    21  	// package.
    22  	Wood WoodType
    23  	// Facing is the direction the trapdoor is facing.
    24  	Facing cube.Direction
    25  	// Open is whether the trapdoor is open.
    26  	Open bool
    27  	// Top is whether the trapdoor occupies the top or bottom part of a block.
    28  	Top bool
    29  }
    30  
    31  // FlammabilityInfo ...
    32  func (t WoodTrapdoor) FlammabilityInfo() FlammabilityInfo {
    33  	if !t.Wood.Flammable() {
    34  		return newFlammabilityInfo(0, 0, false)
    35  	}
    36  	return newFlammabilityInfo(0, 0, true)
    37  }
    38  
    39  // Model ...
    40  func (t WoodTrapdoor) Model() world.BlockModel {
    41  	return model.Trapdoor{Facing: t.Facing, Top: t.Top, Open: t.Open}
    42  }
    43  
    44  // UseOnBlock handles the directional placing of trapdoors and makes sure they are properly placed upside down
    45  // when needed.
    46  func (t WoodTrapdoor) UseOnBlock(pos cube.Pos, face cube.Face, clickPos mgl64.Vec3, w *world.World, user item.User, ctx *item.UseContext) bool {
    47  	pos, face, used := firstReplaceable(w, pos, face, t)
    48  	if !used {
    49  		return false
    50  	}
    51  	t.Facing = user.Rotation().Direction().Opposite()
    52  	t.Top = (clickPos.Y() > 0.5 && face != cube.FaceUp) || face == cube.FaceDown
    53  
    54  	place(w, pos, t, user, ctx)
    55  	return placed(ctx)
    56  }
    57  
    58  // Activate ...
    59  func (t WoodTrapdoor) Activate(pos cube.Pos, _ cube.Face, w *world.World, _ item.User, _ *item.UseContext) bool {
    60  	t.Open = !t.Open
    61  	w.SetBlock(pos, t, nil)
    62  	if t.Open {
    63  		w.PlaySound(pos.Vec3Centre(), sound.TrapdoorOpen{Block: t})
    64  		return true
    65  	}
    66  	w.PlaySound(pos.Vec3Centre(), sound.TrapdoorClose{Block: t})
    67  	return true
    68  }
    69  
    70  // BreakInfo ...
    71  func (t WoodTrapdoor) BreakInfo() BreakInfo {
    72  	return newBreakInfo(3, alwaysHarvestable, axeEffective, oneOf(t))
    73  }
    74  
    75  // FuelInfo ...
    76  func (WoodTrapdoor) FuelInfo() item.FuelInfo {
    77  	return newFuelInfo(time.Second * 15)
    78  }
    79  
    80  // SideClosed ...
    81  func (t WoodTrapdoor) SideClosed(cube.Pos, cube.Pos, *world.World) bool {
    82  	return false
    83  }
    84  
    85  // EncodeItem ...
    86  func (t WoodTrapdoor) EncodeItem() (name string, meta int16) {
    87  	if t.Wood == OakWood() {
    88  		return "minecraft:trapdoor", 0
    89  	}
    90  	return "minecraft:" + t.Wood.String() + "_trapdoor", 0
    91  }
    92  
    93  // EncodeBlock ...
    94  func (t WoodTrapdoor) EncodeBlock() (name string, properties map[string]any) {
    95  	if t.Wood == OakWood() {
    96  		return "minecraft:trapdoor", map[string]any{"direction": int32(math.Abs(float64(t.Facing) - 3)), "open_bit": t.Open, "upside_down_bit": t.Top}
    97  	}
    98  	return "minecraft:" + t.Wood.String() + "_trapdoor", map[string]any{"direction": int32(math.Abs(float64(t.Facing) - 3)), "open_bit": t.Open, "upside_down_bit": t.Top}
    99  }
   100  
   101  // allTrapdoors returns a list of all trapdoor types
   102  func allTrapdoors() (trapdoors []world.Block) {
   103  	for _, w := range WoodTypes() {
   104  		for i := cube.Direction(0); i <= 3; i++ {
   105  			trapdoors = append(trapdoors, WoodTrapdoor{Wood: w, Facing: i, Open: false, Top: false})
   106  			trapdoors = append(trapdoors, WoodTrapdoor{Wood: w, Facing: i, Open: false, Top: true})
   107  			trapdoors = append(trapdoors, WoodTrapdoor{Wood: w, Facing: i, Open: true, Top: true})
   108  			trapdoors = append(trapdoors, WoodTrapdoor{Wood: w, Facing: i, Open: true, Top: false})
   109  		}
   110  	}
   111  	return
   112  }