Skip to content

Commit 7a199fe

Browse files
committed
port: coloured torch (df-mc/dragonfly#1173)
- Move coloured torch implementation to dedicated files. - Add TorchColour enum + registration helpers. - Keep compatibility aliases for existing ColoredTorch API. - Update blockhash generator for TorchColour and regenerate hashes.
1 parent 95a4a6a commit 7a199fe

6 files changed

Lines changed: 207 additions & 142 deletions

File tree

cmd/blockhash/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ func (b *hashBuilder) ftype(structName, s string, expr ast.Expr, directives map[
262262
return "uint64(" + s + ".Uint8())", 3
263263
case "AnvilType", "SandstoneType", "PrismarineType", "StoneBricksType", "NetherBricksType", "FroglightType",
264264
"WallConnectionType", "BlackstoneType", "DeepslateType", "TallGrassType", "CopperType", "OxidationType",
265-
"SeaGrassType", "MushroomBlockKind", "ColoredTorchColour":
265+
"SeaGrassType", "MushroomBlockKind", "ColoredTorchColour", "TorchColour":
266266
return "uint64(" + s + ".Uint8())", 2
267267
case "ShulkerBoxType":
268268
return "uint64(" + s + ".Uint8())", 5
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package block
2+
3+
import "github.com/df-mc/dragonfly/server/world"
4+
5+
// ColoredTorch is an alias for ColouredTorch.
6+
type ColoredTorch = ColouredTorch
7+
8+
// ColoredTorchColour is an alias for TorchColour.
9+
type ColoredTorchColour = TorchColour
10+
11+
// BlueTorch returns the blue torch colour.
12+
func BlueTorch() ColoredTorchColour {
13+
return TorchColourBlue()
14+
}
15+
16+
// GreenTorch returns the green torch colour.
17+
func GreenTorch() ColoredTorchColour {
18+
return TorchColourGreen()
19+
}
20+
21+
// PurpleTorch returns the purple torch colour.
22+
func PurpleTorch() ColoredTorchColour {
23+
return TorchColourPurple()
24+
}
25+
26+
// RedTorch returns the red torch colour.
27+
func RedTorch() ColoredTorchColour {
28+
return TorchColourRed()
29+
}
30+
31+
func allColoredTorches() []world.Block {
32+
return allColouredTorches()
33+
}

server/block/coloured_torch.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
)
9+
10+
// ColouredTorch is a non-solid block that emits light.
11+
type ColouredTorch struct {
12+
transparent
13+
empty
14+
15+
// Facing is the direction from the torch to the block it is attached to.
16+
Facing cube.Face
17+
// Colour is the colour of the torch.
18+
Colour TorchColour
19+
}
20+
21+
// BreakInfo ...
22+
func (t ColouredTorch) BreakInfo() BreakInfo {
23+
return newBreakInfo(0, alwaysHarvestable, nothingEffective, oneOf(t))
24+
}
25+
26+
// LightEmissionLevel ...
27+
func (ColouredTorch) LightEmissionLevel() uint8 {
28+
return 14
29+
}
30+
31+
// UseOnBlock ...
32+
func (t ColouredTorch) UseOnBlock(pos cube.Pos, face cube.Face, _ mgl64.Vec3, tx *world.Tx, user item.User, ctx *item.UseContext) bool {
33+
pos, face, used := firstReplaceable(tx, pos, face, t)
34+
if !used {
35+
return false
36+
}
37+
if face == cube.FaceDown {
38+
return false
39+
}
40+
if !tx.Block(pos.Side(face.Opposite())).Model().FaceSolid(pos.Side(face.Opposite()), face, tx) {
41+
found := false
42+
for _, i := range []cube.Face{cube.FaceSouth, cube.FaceWest, cube.FaceNorth, cube.FaceEast, cube.FaceDown} {
43+
if tx.Block(pos.Side(i)).Model().FaceSolid(pos.Side(i), i.Opposite(), tx) {
44+
found = true
45+
face = i.Opposite()
46+
break
47+
}
48+
}
49+
if !found {
50+
return false
51+
}
52+
}
53+
t.Facing = face.Opposite()
54+
55+
place(tx, pos, t, user, ctx)
56+
return placed(ctx)
57+
}
58+
59+
// NeighbourUpdateTick ...
60+
func (t ColouredTorch) NeighbourUpdateTick(pos, _ cube.Pos, tx *world.Tx) {
61+
if !tx.Block(pos.Side(t.Facing)).Model().FaceSolid(pos.Side(t.Facing), t.Facing.Opposite(), tx) {
62+
breakBlock(t, pos, tx)
63+
}
64+
}
65+
66+
// HasLiquidDrops ...
67+
func (ColouredTorch) HasLiquidDrops() bool {
68+
return true
69+
}
70+
71+
// EncodeItem ...
72+
func (t ColouredTorch) EncodeItem() (name string, meta int16) {
73+
return "minecraft:colored_torch_" + t.Colour.String(), 0
74+
}
75+
76+
// EncodeBlock ...
77+
func (t ColouredTorch) EncodeBlock() (name string, properties map[string]any) {
78+
var face string
79+
if t.Facing == cube.FaceDown {
80+
face = "top"
81+
} else if t.Facing == unknownFace {
82+
face = "unknown"
83+
} else {
84+
face = t.Facing.String()
85+
}
86+
87+
return "minecraft:colored_torch_" + t.Colour.String(), map[string]any{"torch_facing_direction": face}
88+
}
89+
90+
// allColouredTorches returns all possible coloured torch states.
91+
func allColouredTorches() (torch []world.Block) {
92+
for _, face := range cube.Faces() {
93+
if face == cube.FaceUp {
94+
face = unknownFace
95+
}
96+
97+
for _, colour := range TorchColours() {
98+
torch = append(torch, ColouredTorch{Facing: face, Colour: colour})
99+
}
100+
}
101+
return
102+
}

server/block/hash.go

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/block/plants_misc.go

Lines changed: 0 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -204,144 +204,6 @@ func (t UnderwaterTorch) EncodeBlock() (name string, properties map[string]any)
204204
return "minecraft:underwater_torch", map[string]any{"torch_facing_direction": face}
205205
}
206206

207-
// ColoredTorchColour is the colour of a colored torch.
208-
type ColoredTorchColour struct {
209-
colour
210-
}
211-
212-
type colour uint8
213-
214-
// BlueTorch returns the blue torch colour.
215-
func BlueTorch() ColoredTorchColour {
216-
return ColoredTorchColour{0}
217-
}
218-
219-
// GreenTorch returns the green torch colour.
220-
func GreenTorch() ColoredTorchColour {
221-
return ColoredTorchColour{1}
222-
}
223-
224-
// PurpleTorch returns the purple torch colour.
225-
func PurpleTorch() ColoredTorchColour {
226-
return ColoredTorchColour{2}
227-
}
228-
229-
// RedTorch returns the red torch colour.
230-
func RedTorch() ColoredTorchColour {
231-
return ColoredTorchColour{3}
232-
}
233-
234-
// Uint8 returns the torch colour as a uint8.
235-
func (c colour) Uint8() uint8 {
236-
return uint8(c)
237-
}
238-
239-
// String returns the torch colour as a string.
240-
func (c colour) String() string {
241-
switch c {
242-
case 0:
243-
return "blue"
244-
case 1:
245-
return "green"
246-
case 2:
247-
return "purple"
248-
case 3:
249-
return "red"
250-
}
251-
panic("unknown torch colour")
252-
}
253-
254-
// ColoredTorch is a decorative torch with a colored flame.
255-
type ColoredTorch struct {
256-
transparent
257-
empty
258-
259-
Colour ColoredTorchColour
260-
Facing cube.Face
261-
}
262-
263-
// BreakInfo ...
264-
func (t ColoredTorch) BreakInfo() BreakInfo {
265-
return newBreakInfo(0, alwaysHarvestable, nothingEffective, oneOf(t))
266-
}
267-
268-
// LightEmissionLevel ...
269-
func (ColoredTorch) LightEmissionLevel() uint8 {
270-
return 14
271-
}
272-
273-
// UseOnBlock ...
274-
func (t ColoredTorch) UseOnBlock(pos cube.Pos, face cube.Face, _ mgl64.Vec3, tx *world.Tx, user item.User, ctx *item.UseContext) bool {
275-
pos, face, used := firstReplaceable(tx, pos, face, t)
276-
if !used {
277-
return false
278-
}
279-
if face == cube.FaceDown {
280-
return false
281-
}
282-
if !tx.Block(pos.Side(face.Opposite())).Model().FaceSolid(pos.Side(face.Opposite()), face, tx) {
283-
found := false
284-
for _, i := range []cube.Face{cube.FaceSouth, cube.FaceWest, cube.FaceNorth, cube.FaceEast, cube.FaceDown} {
285-
if tx.Block(pos.Side(i)).Model().FaceSolid(pos.Side(i), i.Opposite(), tx) {
286-
found = true
287-
face = i.Opposite()
288-
break
289-
}
290-
}
291-
if !found {
292-
return false
293-
}
294-
}
295-
t.Facing = face.Opposite()
296-
297-
place(tx, pos, t, user, ctx)
298-
return placed(ctx)
299-
}
300-
301-
// NeighbourUpdateTick ...
302-
func (t ColoredTorch) NeighbourUpdateTick(pos, _ cube.Pos, tx *world.Tx) {
303-
if !tx.Block(pos.Side(t.Facing)).Model().FaceSolid(pos.Side(t.Facing), t.Facing.Opposite(), tx) {
304-
breakBlock(t, pos, tx)
305-
}
306-
}
307-
308-
// HasLiquidDrops ...
309-
func (ColoredTorch) HasLiquidDrops() bool {
310-
return true
311-
}
312-
313-
// EncodeItem ...
314-
func (t ColoredTorch) EncodeItem() (name string, meta int16) {
315-
return "minecraft:colored_torch_" + t.Colour.String(), 0
316-
}
317-
318-
// EncodeBlock ...
319-
func (t ColoredTorch) EncodeBlock() (name string, properties map[string]any) {
320-
var face string
321-
if t.Facing == cube.FaceDown {
322-
face = "top"
323-
} else if t.Facing == unknownFace {
324-
face = "unknown"
325-
} else {
326-
face = t.Facing.String()
327-
}
328-
return "minecraft:colored_torch_" + t.Colour.String(), map[string]any{"torch_facing_direction": face}
329-
}
330-
331-
// allColoredTorches returns all colored torch states.
332-
func allColoredTorches() (b []world.Block) {
333-
colours := []ColoredTorchColour{BlueTorch(), GreenTorch(), PurpleTorch(), RedTorch()}
334-
for _, colour := range colours {
335-
for _, face := range cube.Faces() {
336-
if face == cube.FaceUp {
337-
face = unknownFace
338-
}
339-
b = append(b, ColoredTorch{Colour: colour, Facing: face})
340-
}
341-
}
342-
return
343-
}
344-
345207
// FireflyBush is a decorative plant.
346208
type FireflyBush struct {
347209
transparent

server/block/torch_colour.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package block
2+
3+
// TorchColour is the colour of a coloured torch.
4+
type TorchColour struct {
5+
colouredTorch
6+
}
7+
8+
type colouredTorch uint8
9+
10+
// TorchColourPurple returns the purple torch colour.
11+
func TorchColourPurple() TorchColour {
12+
return TorchColour{0}
13+
}
14+
15+
// TorchColourBlue returns the blue torch colour.
16+
func TorchColourBlue() TorchColour {
17+
return TorchColour{1}
18+
}
19+
20+
// TorchColourGreen returns the green torch colour.
21+
func TorchColourGreen() TorchColour {
22+
return TorchColour{2}
23+
}
24+
25+
// TorchColourRed returns the red torch colour.
26+
func TorchColourRed() TorchColour {
27+
return TorchColour{3}
28+
}
29+
30+
// Name returns the human-readable name of the torch colour.
31+
func (c colouredTorch) Name() string {
32+
switch c {
33+
case 0:
34+
return "Purple Torch"
35+
case 1:
36+
return "Blue Torch"
37+
case 2:
38+
return "Green Torch"
39+
case 3:
40+
return "Red Torch"
41+
}
42+
panic("unknown torch colour")
43+
}
44+
45+
// String returns the torch colour as a string.
46+
func (c colouredTorch) String() string {
47+
switch c {
48+
case 0:
49+
return "purple"
50+
case 1:
51+
return "blue"
52+
case 2:
53+
return "green"
54+
case 3:
55+
return "red"
56+
}
57+
panic("unknown torch colour")
58+
}
59+
60+
// Uint8 returns the torch colour as a uint8.
61+
func (c colouredTorch) Uint8() uint8 {
62+
return uint8(c)
63+
}
64+
65+
// TorchColours returns all possible torch colours.
66+
func TorchColours() []TorchColour {
67+
return []TorchColour{TorchColourPurple(), TorchColourBlue(), TorchColourGreen(), TorchColourRed()}
68+
}

0 commit comments

Comments
 (0)