1
- /* global importPackage Packages context player argv BufferedReader FileReader Vector */
2
- const getProjection = require ( './getProjection ' )
1
+ /* global importPackage Packages context player argv BufferedReader FileReader */
2
+ const draw = require ( './modules/drawGeoJSON ' )
3
3
4
4
importPackage ( Packages . java . io )
5
5
importPackage ( Packages . java . awt )
6
6
importPackage ( Packages . com . sk89q . worldedit )
7
7
importPackage ( Packages . com . sk89q . worldedit . math )
8
8
importPackage ( Packages . com . sk89q . worldedit . blocks )
9
9
10
- const usage = `/cs draw <file> <block> [options ]
10
+ const usage = `/cs draw <file> <block> [flags ]
11
11
• §o/cs draw rails1 iron_block
12
12
• §o/cs draw file3 stone u
13
- Options :
13
+ Flags :
14
14
• §lu§r§c Draw a block above`
15
15
16
16
context . checkArgs ( 2 , 3 , usage )
@@ -21,15 +21,8 @@ if (argv[3]) {
21
21
options . up = argv [ 3 ] . includes ( 'u' )
22
22
}
23
23
24
- const blocks = context . remember ( )
25
- let changedBlocks = 0
26
-
27
- const ignoreBlocks = getIgnoredBlocks ( )
28
- const allowedBlocks = getAllowedBlocks ( )
29
-
30
- const DataError = new Error ( 'Incorrect data format' )
31
-
32
- draw ( parse ( readFile ( ) ) )
24
+ player . print ( '§7Please wait...' )
25
+ draw ( readFile ( ) , argv [ 2 ] , options )
33
26
34
27
function readFile ( ) {
35
28
const file = context . getSafeOpenFile ( 'drawings' , argv [ 1 ] , 'geojson' , [ 'json' , 'geojson' ] )
@@ -52,112 +45,3 @@ function readFile () {
52
45
return JSON . parse ( bufStr )
53
46
}
54
47
}
55
-
56
- function parse ( data ) {
57
- if ( data . type !== 'FeatureCollection' ) throw DataError
58
- if ( ! Array . isArray ( data . features ) ) throw DataError
59
-
60
- const geometry = [ ]
61
- let lines = 0
62
- for ( let i = 0 ; i < data . features . length ; i ++ ) {
63
- if ( data . features [ i ] . geometry . type !== 'LineString' ) continue
64
- const coordinates = data . features [ i ] . geometry . coordinates
65
- geometry . push ( coordinates )
66
- lines += coordinates . length
67
- }
68
-
69
- player . print ( `§7${ lines } lines to draw` )
70
-
71
- return geometry
72
- }
73
-
74
- function draw ( geometry ) {
75
- const y = player . getPosition ( ) . y
76
- const block = context . getBlock ( argv [ 2 ] )
77
-
78
- player . print ( '§7Please wait...' )
79
- const projection = getProjection ( )
80
-
81
- for ( let i = 0 ; i < geometry . length ; i ++ ) {
82
- const shape = geometry [ i ]
83
- for ( let j = 0 ; j < shape . length - 1 ; j ++ ) {
84
- const [ x1 , z1 ] = projection . fromGeo ( ...shape [ j ] )
85
- const [ x2 , z2 ] = projection . fromGeo ( ...shape [ j + 1 ] )
86
- drawLine ( x1 , y , z1 , x2 , y , z2 , block )
87
- }
88
- }
89
-
90
- player . print ( 'Operation completed (' + changedBlocks + ' blocks affected).' )
91
- }
92
-
93
- function drawLine ( x1 , y1 , z1 , x2 , y2 , z2 , block ) {
94
- const lenX = x2 - x1
95
- const lenY = y2 - y1
96
- const lenZ = z2 - z1
97
-
98
- const max = Math . max ( Math . abs ( lenX ) , Math . abs ( lenY ) , Math . abs ( lenZ ) )
99
-
100
- const incrX = lenX / max
101
- const incrY = lenY / max
102
- const incrZ = lenZ / max
103
-
104
- const incrMax = Math . max ( Math . abs ( incrX ) , Math . abs ( incrY ) , Math . abs ( incrZ ) )
105
-
106
- for ( var i = 0 ; i < max ; i += incrMax ) {
107
- var pos = new Vector ( Math . floor ( x1 + incrX * i ) , Math . floor ( y1 + incrY * i ) , Math . floor ( z1 + incrZ * i ) )
108
-
109
- while ( ! ignoreBlocks . includes ( blocks . getBlock ( pos . add ( new Vector ( 0 , 1 , 0 ) ) ) . id ) ) {
110
- pos = pos . add ( new Vector ( 0 , 1 , 0 ) )
111
- }
112
- while ( ignoreBlocks . includes ( blocks . getBlock ( pos ) . id ) ) {
113
- pos = pos . add ( new Vector ( 0 , - 1 , 0 ) )
114
- }
115
-
116
- if ( allowedBlocks . includes ( blocks . getBlock ( pos ) . id ) ) {
117
- if ( options . up ) {
118
- pos = pos . add ( new Vector ( 0 , 1 , 0 ) )
119
- }
120
- blocks . setBlock ( pos , block )
121
- changedBlocks ++
122
- }
123
- }
124
- }
125
-
126
- function getIgnoredBlocks ( ) {
127
- return [
128
- context . getBlock ( 'air' ) . id ,
129
- context . getBlock ( 'tallgrass' ) . id ,
130
- context . getBlock ( 'sapling' ) . id ,
131
- context . getBlock ( 'log' ) . id ,
132
- context . getBlock ( 'log2' ) . id ,
133
- context . getBlock ( 'leaves' ) . id ,
134
- context . getBlock ( 'leaves2' ) . id ,
135
- context . getBlock ( 'deadbush' ) . id ,
136
- context . getBlock ( 'red_flower' ) . id ,
137
- context . getBlock ( 'yellow_flower' ) . id ,
138
- context . getBlock ( 'red_mushroom' ) . id ,
139
- context . getBlock ( 'brown_mushroom' ) . id ,
140
- context . getBlock ( 'vine' ) . id ,
141
- context . getBlock ( 'waterlily' ) . id ,
142
- context . getBlock ( 'cactus' ) . id ,
143
- context . getBlock ( 'reeds' ) . id ,
144
- context . getBlock ( 'pumpkin' ) . id ,
145
- context . getBlock ( 'melon_block' ) . id ,
146
- context . getBlock ( 'snow_layer' ) . id ,
147
- context . getBlock ( 'double_plant' ) . id
148
- ]
149
- }
150
-
151
- function getAllowedBlocks ( ) {
152
- return [
153
- context . getBlock ( 'grass' ) . id ,
154
- context . getBlock ( 'dirt' ) . id ,
155
- context . getBlock ( 'stone' ) . id ,
156
- context . getBlock ( 'sand' ) . id ,
157
- context . getBlock ( 'grass_path' ) . id ,
158
- context . getBlock ( 'concrete' ) . id ,
159
- context . getBlock ( 'gravel' ) . id ,
160
- context . getBlock ( 'water' ) . id ,
161
- context . getBlock ( 'lava' ) . id
162
- ]
163
- }
0 commit comments