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

     1  package block
     2  
     3  import (
     4  	"github.com/df-mc/dragonfly/server/block/cube"
     5  	"github.com/df-mc/dragonfly/server/item"
     6  	"github.com/df-mc/dragonfly/server/world"
     7  	"github.com/go-gl/mathgl/mgl64"
     8  	"time"
     9  )
    10  
    11  // Log is a naturally occurring block found in trees, primarily used to create planks. It comes in six
    12  // species: oak, spruce, birch, jungle, acacia, and dark oak.
    13  // Stripped log is a variant obtained by using an axe on a log.
    14  type Log struct {
    15  	solid
    16  	bass
    17  
    18  	// Wood is the type of wood of the log. This field must have one of the values found in the material
    19  	// package.
    20  	Wood WoodType
    21  	// Stripped specifies if the log is stripped or not.
    22  	Stripped bool
    23  	// Axis is the axis which the log block faces.
    24  	Axis cube.Axis
    25  }
    26  
    27  // FlammabilityInfo ...
    28  func (l Log) FlammabilityInfo() FlammabilityInfo {
    29  	if !l.Wood.Flammable() {
    30  		return newFlammabilityInfo(0, 0, false)
    31  	}
    32  	return newFlammabilityInfo(5, 5, true)
    33  }
    34  
    35  // BreakInfo ...
    36  func (l Log) BreakInfo() BreakInfo {
    37  	b := newBreakInfo(2, alwaysHarvestable, axeEffective, oneOf(l))
    38  	if l.Wood == CrimsonWood() || l.Wood == WarpedWood() {
    39  		b = b.withBlastResistance(1.5)
    40  	}
    41  	return b
    42  }
    43  
    44  // SmeltInfo ...
    45  func (Log) SmeltInfo() item.SmeltInfo {
    46  	return newSmeltInfo(item.NewStack(item.Charcoal{}, 1), 0.15)
    47  }
    48  
    49  // FuelInfo ...
    50  func (Log) FuelInfo() item.FuelInfo {
    51  	return newFuelInfo(time.Second * 15)
    52  }
    53  
    54  // UseOnBlock handles the rotational placing of logs.
    55  func (l Log) UseOnBlock(pos cube.Pos, face cube.Face, _ mgl64.Vec3, w *world.World, user item.User, ctx *item.UseContext) (used bool) {
    56  	pos, face, used = firstReplaceable(w, pos, face, l)
    57  	if !used {
    58  		return
    59  	}
    60  	l.Axis = face.Axis()
    61  
    62  	place(w, pos, l, user, ctx)
    63  	return placed(ctx)
    64  }
    65  
    66  // Strip ...
    67  func (l Log) Strip() (world.Block, bool) {
    68  	return Log{Axis: l.Axis, Wood: l.Wood, Stripped: true}, !l.Stripped
    69  }
    70  
    71  // EncodeItem ...
    72  func (l Log) EncodeItem() (name string, meta int16) {
    73  	if !l.Stripped {
    74  		switch l.Wood {
    75  		case CrimsonWood(), WarpedWood():
    76  			return "minecraft:" + l.Wood.String() + "_stem", 0
    77  		default:
    78  			return "minecraft:" + l.Wood.String() + "_log", 0
    79  		}
    80  	}
    81  	switch l.Wood {
    82  	case CrimsonWood(), WarpedWood():
    83  		return "minecraft:stripped_" + l.Wood.String() + "_stem", 0
    84  	default:
    85  		return "minecraft:stripped_" + l.Wood.String() + "_log", 0
    86  	}
    87  }
    88  
    89  // EncodeBlock ...
    90  func (l Log) EncodeBlock() (name string, properties map[string]any) {
    91  	if !l.Stripped {
    92  		switch l.Wood {
    93  		case CrimsonWood(), WarpedWood():
    94  			return "minecraft:" + l.Wood.String() + "_stem", map[string]any{"pillar_axis": l.Axis.String()}
    95  		default:
    96  			return "minecraft:" + l.Wood.String() + "_log", map[string]any{"pillar_axis": l.Axis.String()}
    97  		}
    98  	}
    99  	switch l.Wood {
   100  	case CrimsonWood(), WarpedWood():
   101  		return "minecraft:stripped_" + l.Wood.String() + "_stem", map[string]any{"pillar_axis": l.Axis.String()}
   102  	default:
   103  		return "minecraft:stripped_" + l.Wood.String() + "_log", map[string]any{"pillar_axis": l.Axis.String()}
   104  	}
   105  }
   106  
   107  // allLogs returns a list of all possible log states.
   108  func allLogs() (logs []world.Block) {
   109  	for _, w := range WoodTypes() {
   110  		for axis := cube.Axis(0); axis < 3; axis++ {
   111  			logs = append(logs, Log{Axis: axis, Stripped: true, Wood: w})
   112  			logs = append(logs, Log{Axis: axis, Stripped: false, Wood: w})
   113  		}
   114  	}
   115  	return
   116  }