This repository was archived by the owner on Oct 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructure.module
More file actions
419 lines (381 loc) · 14.6 KB
/
structure.module
File metadata and controls
419 lines (381 loc) · 14.6 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
<?php
/**
* Implements hook_node_info().
*/
function structure_node_info() {
return array(
'structure' => array(
'name' => t('Structure'),
'base' => 'structure',
'description' => t('Allows user to build Minecraft-esque structures.'),
)
);
}
/**
* Implements hook_form().
*/
function structure_form($node, $form_state) {
drupal_add_css(drupal_get_path('module', 'structure') . '/styles.css');
drupal_add_js(drupal_get_path('module', 'structure') . '/libs/Three.js');
drupal_add_js(drupal_get_path('module', 'structure') . '/libs/Stats.js');
drupal_add_js(drupal_get_path('module', 'structure') . '/libs/jquery-ui-1.8.16.custom.min.js');
drupal_add_js(drupal_get_path('module', 'structure') . '/libs/RequestAnimationFrame.js');
drupal_add_js(drupal_get_path('module', 'structure') . '/js/structure.js');
drupal_add_js(drupal_get_path('module', 'structure') . '/js/structure-3d.js');
drupal_add_js(drupal_get_path('module', 'structure') . '/libs/base64_decode.js');
drupal_add_js(drupal_get_path('module', 'structure') . '/js/update.js');
drupal_add_js(drupal_get_path('module', 'structure') . '/libs/JsNbtParser/js/NbtParser.js');
drupal_add_js(drupal_get_path('module', 'structure') . '/libs/JsNbtParser/js/binary-parser.js');
drupal_add_js(drupal_get_path('module', 'structure') . '/libs/JsNbtParser/js/World_Schematic.js');
drupal_add_js(array('structurePath' => drupal_get_path('module', 'structure')), 'setting');
if (isset($node->field_structurearray['und']['0']['value'])) {
drupal_add_js(array('structureArray' => $node->field_structurearray['und']['0']['value']), 'setting');
}
drupal_add_js(array('structureMode' => 'edit'), 'setting');
return structure_node_content_form($node, $form_state);
}
/**
* Implements hook_node_content_form
*/
function structure_node_content_form($node, $form_state) {
$form = array();
$form['#validate'][] = 'structure_node_content_form_validate';
$type = node_type_get_type($node);
if ($type->has_title) {
$form['title'] = array(
'#type' => 'textfield',
'#title' => check_plain($type->title_label),
'#required' => TRUE,
'#default_value' => $node->title,
'#maxlength' => 255,
'#weight' => -5,
);
}
$form['block_type'] = array(
'#type' => 'radios',
'#title' => t('Block Type'),
'#required' => TRUE,
'#options' => array(
'0' => t('Air'),
'1' => t('Stone'),
'2' => t('Grass'),
'3' => t('Dirt'),
'4' => t('Cobblestone'),
'5' => t('Wooden Plank'),
'6' => t('Sapling'),
'6:1' => t('Sapling (Pine)'),
'6:2' => t('Sapling (Birch'),
'7' => t('Bedrock'),
'8' => t('Water (No Spread)'),
'9' => t('Water'),
'10' => t('Lava (No Spread)'),
'11' => t('Lava'),
'12' => t('Sand'),
'13' => t('Gravel'),
'14' => t('Gold Ore'),
'15' => t('Iron Ore'),
'16' => t('Coal Ore'),
'17' => t('Wood'),
'17:1' => t('Wood (Pine)'),
'17:2' => t('Wood (Birch)'),
'18' => t('Leaves'),
'18:1' => t('Leaves (Pine)'),
'18:2' => t('Leaves (Birch)'),
'19' => t('Sponge'),
'20' => t('Glass'),
'21' => t('Lapis Lazuli Ore'),
'22' => t('Lapis Lazuli Block'),
'23' => t('Dispenser'),
'24' => t('Sandstone'),
'25' => t('Note Block'),
'26' => t('Bed (Block)'),
'27' => t('Powered Rail'),
'28' => t('Detector Rail'),
'29' => t('Sticky Piston'),
'30' => t('CobWeb'),
'31' => t('Tall Grass (Dead Bush)'),
'31:1' => t('Tall Grass'),
'31:2' => t('Tall Grass (Fern)'),
'32' => t('Dead Bush'),
'33' => t('Piston'),
'34' => t('Piston (Extension)'),
'35' => t('Wool'),
'35:1' => t('Orange Wool'),
'35:2' => t('Magenta Wool'),
'35:3' => t('Light Blue Wool'),
'35:4' => t('Yellow Wool'),
'35:5' => t('Lime Wool'),
'35:6' => t('Pink Wool'),
'35:7' => t('Gray Wool'),
'35:8' => t('Light Gray Wool'),
'35:9' => t('Cyan Wool'),
'35:10' => t('Purple Wool'),
'35:11' => t('Blue Wool'),
'35:12' => t('Brown Wool'),
'35:13' => t('Green Wool'),
'35:14' => t('Red Wool'),
'35:15' => t('Black Wool'),
'37' => t('Dandelion'),
'38' => t('Rose'),
'39' => t('Brown Mushroom'),
'40' => t('Red Mushroom'),
'41' => t('Gold Block'),
'42' => t('Iron Block'),
'43' => t('Stone Slab (Double)'),
'43:1' => t('Sandstone Slab (Double)'),
'43:2' => t('Wooden Slab (Double)'),
'43:3' => t('Cobblestone Slab (Double'),
'43:4' => t('Brick Slab (Double)'),
'43:5' => t('Stone Brick Slab (Double)'),
'44' => t('Stone Slab'),
'44:1' => t('Sandstone Slab'),
'44:2' => t('Wooden Slab'),
'44:3' => t('Cobblestone Slap'),
'44:4' => t('Brick Slab'),
'44:5' => t('Stone Brick Slab'),
'45' => t('Brick'),
'46' => t('TNT'),
'47' => t('Bookshelf'),
'48' => t('Moss Stone'),
'49' => t('Obsidian'),
'50' => t('Torch'),
'51' => t('Fire'),
'52' => t('Monster Spawner'),
'53' => t('Wooden Stairs'),
'54' => t('Chest'),
'55' => t('Redstone Wire'),
'56' => t('Diamond Ore'),
'57' => t('Diamond Block'),
'58' => t('Crafting Table'),
'59' => t('Seeds'),
'60' => t('Farmland'),
'61' => t('Furnace'),
'62' => t('Furnace (Smelting)'),
'63' => t('Sign (Post)'),
'64' => t('Wood Door (Block)'),
'65' => t('Ladder'),
'66' => t('Rails'),
'67' => t('Cobblestone Stairs'),
'68' => t('Sign (Wall)'),
'69' => t('Lever'),
'70' => t('Stone Pressure Plate'),
'71' => t('Iron Door (Block)'),
'72' => t('Wooden Pressure Plate'),
'72' => t('Wooden Pressure Plate'),
'73' => t('Redstone Ore'),
'74' => t('Redstone Ore (Glowing)'),
'75' => t('Redstone Torch (Off)'),
'76' => t('Redstone Torch'),
'77' => t('Stone Button'),
'78' => t('Snow'),
'79' => t('Ice'),
'80' => t('Snow Block'),
'81' => t('Cactus'),
'82' => t('Clay Block'),
'83' => t('Sugar Cane'),
'84' => t('Jukebox'),
'85' => t('Fence'),
'86' => t('Pumpkin'),
'87' => t('Netherrack'),
'88' => t('Soul Sand'),
'89' => t('Glowstone'),
'90' => t('Portal'),
'91' => t('Jack-O-Lantern'),
'92' => t('Cake (Block)'),
'93' => t('Redstone Repeater (Block Off)'),
'94' => t('Redstone Repeater (Block On)'),
'95' => t('Locked Chest'),
'96' => t('Trapdoor'),
'97' => t('Hidden Silverfish Stone Block'),
'98' => t('Stone Bricks'),
'98:1' => t('Mossy Stone Bricks'),
'98:2' => t('Cracked Stone Bricks'),
'99' => t('Huge Brown Mushroom (Block)'),
'100' => t('Huge Red Mushroom (Block)'),
'101' => t('Iron Bars'),
'102' => t('Glass Pane'),
'103' => t('Melon'),
'104' => t('Pumpkin Stem'),
'105' => t('Melon Stem'),
'106' => t('Vines'),
'107' => t('Fence Gate'),
'108' => t('Brick Stairs'),
'109' => t('Stone Brick Stairs'),
'110' => t('Mycelium'),
'111' => t('Lilypad'),
'112' => t('Nether Brick'),
'113' => t('Nether Brick Fence'),
'114' => t('Nether Brick Stairs'),
'115' => t('Nether Wart'),
'116' => t('Enchantment Table'),
'117' => t('Brewing Stand (Block)'),
'118' => t('Cauldron (Block)'),
'119' => t('End Portal'),
'120' => t('End Portal Frame'),
'121' => t('End Stone'),
'122' => t('Dragon Egg'),
),
);
$form['controls'] = array(
'#prefix' => '<div class="controls">',
'#suffix' => '</div>'
);
$form['controls']['xy_nav'] = array(
'#prefix' => '<div class="xy-nav">',
'#suffix' => '</div>',
);
$form['controls']['z_nav'] = array(
'#prefix' => '<div class="z-nav">',
'#suffix' => '</div>',
);
$form['controls']['z_nav']['up'] = array(
'#markup' => '<button type="button" value="Up a Layer" class="up-z">+</button>',
);
$form['controls']['z_nav']['down'] = array(
'#markup' => '<button type="button" value="Down a Layer" class="down-z">-</button>',
);
$form['controls']['xy_nav']['north'] = array(
'#markup' => '<button type="button" value="North" class="north-y">↑</button>',
);
$form['controls']['xy_nav']['east'] = array(
'#markup' => '<button type="button" value="East" class="east-x">→</button>',
);
$form['controls']['xy_nav']['south'] = array(
'#markup' => '<button type="button" value="South" class="south-y">↓</button>',
);
$form['controls']['xy_nav']['west'] = array(
'#markup' => '<button type="button" value="West" class="west-x">←</button>',
);
$form['controls']['xy_grid'] = array(
'#markup' => '<div class="xy-grid"></div>',
);
$form['mc_3d'] = array(
'#prefix' => '<div id="mc-3d">',
'#suffix' => '</div>',
);
$form['mc_3d']['zoom_out'] = array(
'#markup' => '<button type="button" value="Zoom Out" class="zoom-out">-</button>',
);
$form['mc_3d']['zoom_in'] = array(
'#markup' => '<button type="button" value="Zoom In" class="zoom-in">+</button>',
);
$form['mc_3d']['canvas'] = array(
'#markup' => '<div class="canvas-wrapper"><span class="enable-3d-warning">If you are using Google Chrome or Mozilla Firefox you can <a href="#enable3d" class="enable-3d">enable 3d</a>.</span></div>',
);
$form['file'] = array(
'#type' => 'file',
'#title' => t('Schematic File Upload'),
'#description' => 'Optionally upload a .schematic file. This will erase any other changes made on this page.',
'#size' => 22,
'#prefix' => '<div id="upload-wrapper">',
);
$form['upload'] = array(
'#type' => 'button',
'#value' => 'Upload',
'#tree' => TRUE,
'#ajax' => array(
'callback' => 'structure_node_content_form_ajax',
'wrapper' => 'upload-wrapper',
),
'#suffix' => '</div>',
);
return $form;
}
function structure_node_content_form_validate($form, &$form_state) {
}
function structure_node_content_form_ajax($form, &$form_state) {
function bin2asc ($binary) {
$val = 0;
for ($i = strlen($binary) - 1; $i >= 0; $i--) {
$ch = substr($binary, $i, 1);
$val = ($val << 8) | ord($ch);
}
return $val;
}
$file = file_save_upload('file', array(
'file_validate_extensions' => array('schematic'),
));
$filename = file_directory_temp() . $file->filename;
ob_start("ob_gzhandler");
$schematic = readFileAsBinary($filename);
drupal_add_js(array('schematicFile' => base64_encode($schematic)), 'setting');
unset($form['file']);
return t('File uploaded successfully.');
}
/**
* Implements hook_menu_local_tasks_alter().
*/
function structure_menu_local_tasks_alter(&$data, $router_item, $root_path) {
global $user;
// Add action link to 'node/add/structure' on 'structure' page.
if ($root_path == 'structure') {
$item = menu_get_item('node/add/structure');
if ($item['access']) {
$item['title'] = t('Create new structure');
$data['actions']['output'][] = array(
'#theme' => 'menu_local_action',
'#link' => $item,
);
}
}
// Provide a helper action link to the author on the 'structure/%' page.
elseif ($root_path == 'structure/%' && $router_item['page_arguments'][0]->uid == $user->uid) {
$data['actions']['output']['structure'] = array(
'#theme' => 'menu_local_action',
);
if (user_access('create structure content')) {
$data['actions']['output']['structure']['#link']['title'] = t('Post new structure.');
$data['actions']['output']['structure']['#link']['href'] = 'node/add/structure';
}
else {
$data['actions']['output']['structure']['#link']['title'] = t('You are not allowed to post a new structure.');
}
}
}
/**
* Implements hook_view().
*/
function structure_view($node, $view_mode) {
if ($view_mode == 'full' && node_is_page($node)) {
drupal_add_css(drupal_get_path('module', 'structure') . '/styles.css');
drupal_add_js(drupal_get_path('module', 'structure') . '/libs/Three.js');
drupal_add_js(drupal_get_path('module', 'structure') . '/libs/Stats.js');
drupal_add_js(drupal_get_path('module', 'structure') . '/libs/jquery-ui-1.8.16.custom.min.js');
drupal_add_js(drupal_get_path('module', 'structure') . '/libs/RequestAnimationFrame.js');
drupal_add_js(drupal_get_path('module', 'structure') . '/js/structure.js');
drupal_add_js(drupal_get_path('module', 'structure') . '/js/structure-3d.js');
drupal_add_js(drupal_get_path('module', 'structure') . '/js/update.js');
drupal_add_js(drupal_get_path('module', 'structure') . '/libs/base64_decode.js');
drupal_add_js(drupal_get_path('module', 'structure') . '/libs/JsNbtParser/js/NbtParser.js');
drupal_add_js(drupal_get_path('module', 'structure') . '/libs/JsNbtParser/js/binary-parser.js');
drupal_add_js(drupal_get_path('module', 'structure') . '/libs/JsNbtParser/js/World_Schematic.js');
drupal_add_js(array('structurePath' => drupal_get_path('module', 'structure')), 'setting');
drupal_add_js(array('structureArray' => $node->field_structurearray['und']['0']['value']), 'setting');
drupal_add_js(array('structureMode' => 'view'), 'setting');
$node->content['mc_3d'] = array(
'#prefix' => '<div id="mc-3d">',
'#suffix' => '</div>',
);
$node->content['mc_3d']['zoom_out'] = array(
'#markup' => '<button type="button" value="Zoom Out" class="zoom-out">-</button>',
);
$node->content['mc_3d']['zoom_in'] = array(
'#markup' => '<button type="button" value="Zoom In" class="zoom-in">+</button>',
);
$node->content['mc_3d']['canvas'] = array(
'#markup' => '<div class="canvas-wrapper"><span class="enable-3d-warning">If you are using Google Chrome or Mozilla Firefox you can <a href="#enable3d" class="enable-3d">enable 3d</a>.</span></div>',
);
}
return $node;
}
/**
* Reads a file and deflates it if it's gzencoded.
*
* @param {String} $filename The name of the file to read
* @return {String}
*/
function readFileAsBinary($filename) {
$handle = gzopen($filename, "rb"); //using gzopen auto detects whether or not the file is compressed
$contents = stream_get_contents($handle);
return $contents;
}