-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructure.go
More file actions
148 lines (126 loc) · 3.7 KB
/
structure.go
File metadata and controls
148 lines (126 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package schem
import (
_ "unsafe"
"github.com/df-mc/dragonfly/server/block"
"github.com/df-mc/dragonfly/server/world"
"github.com/oriumgames/crocon"
"github.com/oriumgames/schem/format"
"github.com/sandertv/gophertunnel/minecraft/protocol"
)
// Structure wraps a format.Schematic and implements world.Structure.
// It can be placed in a Dragonfly world using world.BuildStructure.
type Structure struct {
schematic format.Schematic
converter *crocon.Converter
}
// NewStructure creates a new Structure from a format.Schematic.
func NewStructure(s format.Schematic) *Structure {
c, _ := crocon.NewConverter()
return &Structure{
schematic: s,
converter: c,
}
}
// Dimensions implements world.Structure.
func (s *Structure) Dimensions() [3]int {
w, h, l := s.schematic.Dimensions()
return [3]int{w, h, l}
}
// At implements world.Structure.
// It converts format.BlockState to world.Block using the crocon conversion system.
func (s *Structure) At(x, y, z int, _ func(x, y, z int) world.Block) (world.Block, world.Liquid) {
state := s.schematic.Block(x, y, z)
if state == nil {
// Return air for nil blocks
return block.Air{}, nil
}
// Special case: air should explicitly set air
if state.Name == "minecraft:air" || state.Name == "air" {
return block.Air{}, nil
}
// Determine source version from data version
fromVersion := s.schematic.Version()
if fromVersion == "" {
return block.Air{}, nil
}
// Convert Java block to Bedrock
b, err := s.converter.ConvertBlock(crocon.BlockRequest{
ConversionRequest: crocon.ConversionRequest{
FromVersion: fromVersion,
ToVersion: protocol.CurrentVersion,
FromEdition: crocon.JavaEdition,
ToEdition: crocon.BedrockEdition,
},
Block: crocon.Block{
ID: state.Name,
States: state.Properties,
},
})
if err != nil {
// Failed to convert - return air to skip
return block.Air{}, nil
}
// Filter invalid properties
validProps := blockProperties[b.ID]
for k := range b.States {
if _, ok := validProps[k]; !ok {
delete(b.States, k)
}
}
// Get the Bedrock block
ret, ok := world.BlockByName(b.ID, b.States)
if !ok {
// Failed to convert - return air to skip
return block.Air{}, nil
}
// Handle block entity data if present
if nbter, ok := ret.(world.NBTer); ok {
ent := s.schematic.BlockEntity(x, y, z)
if ent != nil {
from := crocon.BlockEntity(ent.Data)
from["id"] = ent.ID
be, err := s.converter.ConvertBlockEntity(crocon.BlockEntityRequest{
ConversionRequest: crocon.ConversionRequest{
FromVersion: fromVersion,
ToVersion: protocol.CurrentVersion,
FromEdition: crocon.JavaEdition,
ToEdition: crocon.BedrockEdition,
},
BlockEntity: from,
})
if err != nil {
// Failed to convert - return air to skip
return block.Air{}, nil
}
m, ok := any(be).(*map[string]any)
if !ok || m == nil {
return block.Air{}, nil
}
tag, ok := (*m)["tag"].(map[string]any)
if !ok {
return block.Air{}, nil
}
return nbter.DecodeNBT(tag).(world.Block), nil
} else {
ret = nbter.DecodeNBT(map[string]any{}).(world.Block)
}
}
// Handle waterlogged blocks
var liquid world.Liquid
if waterlogged, ok := state.Properties["waterlogged"].(bool); ok && waterlogged {
liquid = block.Water{}
}
return ret, liquid
}
// Schematic returns the underlying format.Schematic.
func (s *Structure) Schematic() format.Schematic {
return s.schematic
}
// Offset returns the structure's offset.
func (s *Structure) Offset() (x, y, z int) {
return s.schematic.Offset()
}
// blockProperties is linked from dragonfly to validate block properties.
//
//go:linkname blockProperties github.com/df-mc/dragonfly/server/world.blockProperties
var blockProperties map[string]map[string]any