-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.lua
More file actions
3003 lines (2576 loc) · 97.6 KB
/
api.lua
File metadata and controls
3003 lines (2576 loc) · 97.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[
X Farming. Extends Luanti farming mod with new plants, crops and ice fishing.
Copyright (C) 2025 SaKeL
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to juraj.vajda@gmail.com
--]]
local S = core.get_translator(core.get_current_modname())
-- main class
x_farming = {
hunger_ng = core.get_modpath('hunger_ng'),
hbhunger = core.get_modpath('hbhunger') and core.global_exists('hbhunger'),
vessels = core.get_modpath('vessels'),
bucket = core.get_modpath('bucket'),
colors = {
brown = '#DEB887',
red = '#FF8080',
green = '#98E698'
},
beehive_distance = core.setting_get("x_farming.beehive_radius") or 5,
x_bonemeal = {
tree_defs = {
['x_farming:christmas_tree_sapling'] = {
-- christmas tree
name = 'x_farming:christmas_tree_sapling',
chance = 2,
grow_tree = function(pos)
if not x_farming.x_bonemeal.is_on_soil(pos) then
return false
end
x_farming.grow_christmas_tree(pos)
return true
end
},
['x_farming:kiwi_sapling'] = {
-- Kiwi Tree
name = 'x_farming:kiwi_sapling',
chance = 2,
grow_tree = function(pos)
if not x_farming.x_bonemeal.is_on_soil(pos) then
return false
end
x_farming.grow_kiwi_tree(pos)
return true
end
},
['x_farming:large_cactus_with_fruit_seedling'] = {
-- Cactus Seedling
name = 'x_farming:large_cactus_with_fruit_seedling',
chance = 2,
grow_tree = function(pos)
if not x_farming.x_bonemeal.is_on_sand(pos) then
return false
end
x_farming.grow_large_cactus(pos)
return true
end
},
['x_farming:jungle_with_cocoa_sapling'] = {
-- Jungle Tree with Cocoa
name = 'x_farming:jungle_with_cocoa_sapling',
chance = 2,
grow_tree = function(pos)
if not x_farming.x_bonemeal.is_on_soil(pos) then
return false
end
x_farming.grow_jungle_tree(pos)
return true
end
},
['x_farming:pine_nut_sapling'] = {
-- Pine Nut Tree
name = 'x_farming:pine_nut_sapling',
chance = 2,
grow_tree = function(pos)
if not x_farming.x_bonemeal.is_on_soil(pos) then
return false
end
x_farming.grow_pine_nut_tree(pos)
return true
end
},
}
},
allowed_crate_items = {},
allowed_bag_items = {},
registered_crates = {},
lbm_nodenames_crates = {},
registered_plants = {},
mcl = {},
candle_colors = {
black = {
name = S('Black'),
hex = '#2B2B2B',
mcl_groups = { basecolor_black = 1, excolor_black = 1, unicolor_black = 1 }
},
dark_grey = {
name = S('Dark Grey'),
hex = '#4E4E4E',
mcl_groups = { basecolor_grey = 1, excolor_darkgrey = 1, unicolor_darkgrey = 1 }
},
grey = {
name = S('Grey'),
hex = '#A5A5A5',
mcl_groups = { basecolor_grey = 1, excolor_grey = 1, unicolor_grey = 1 }
},
red = {
name = S('Red'),
hex = '#AB5C4A',
mcl_groups = { basecolor_red = 1, excolor_red = 1, unicolor_red = 1 }
},
violet = {
name = S('Violet'),
hex = '#595287',
mcl_groups = { basecolor_magenta = 1, excolor_violet = 1, unicolor_violet = 1 }
},
magenta = {
name = S('Magenta'),
hex = '#A25B5D',
mcl_groups = { basecolor_magenta = 1, excolor_red_violet = 1, unicolor_red_violet = 1 }
},
pink = {
name = S('Pink'),
hex = '#FFA6A6',
mcl_groups = { basecolor_red = 1, excolor_red = 1, unicolor_light_red = 1 }
},
dark_green = {
name = S('Dark Green'),
hex = '#556E48',
mcl_groups = { basecolor_green = 1, excolor_green = 1, unicolor_dark_green = 1 }
},
green = {
name = S('Green'),
hex = '#779154',
mcl_groups = { basecolor_green = 1, excolor_green = 1, unicolor_green = 1 }
},
cyan = {
name = S('Cyan'),
hex = '#4E7683',
mcl_groups = { basecolor_cyan = 1, excolor_cyan = 1, unicolor_cyan = 1 }
},
blue = {
name = S('Blue'),
hex = '#4B6696',
mcl_groups = { basecolor_blue = 1, excolor_blue = 1, unicolor_blue = 1 }
},
light_blue = {
name = S('Light Blue'),
hex = '#648CB4',
craft_dye = 'group:dye,color_light_blue',
mcl_groups = { basecolor_blue = 1, excolor_blue = 1, unicolor_light_blue = 1 }
},
orange = {
name = S('Orange'),
hex = '#A86A4D',
mcl_groups = { basecolor_orange = 1, excolor_orange = 1, unicolor_orange = 1 }
},
yellow = {
name = S('Yellow'),
hex = '#BD8D39',
mcl_groups = { basecolor_yellow = 1, excolor_yellow = 1, unicolor_yellow = 1 }
},
brown = {
name = S('Brown'),
hex = '#684E45',
mcl_groups = { basecolor_brown = 1, excolor_orange = 1, unicolor_dark_orange = 1 }
}
},
}
function x_farming.node_sound_grass_defaults(table)
table = table or {}
table.footstep = table.footstep or { name = 'x_farming_grass_footstep', gain = 0.4 }
table.dig = table.dig or { name = 'x_farming_grass_hit', gain = 1.2 }
table.dug = table.dug or { name = 'x_farming_dirt_hit', gain = 1.0 }
table.place = table.place or { name = 'x_farming_dirt_hit', gain = 1.0 }
return table
end
function x_farming.node_sound_leaves_defaults(table)
table = table or {}
table.footstep = table.footstep or { name = 'x_farming_leaves_footstep', gain = 0.1 }
table.dig = table.dig or { name = 'x_farming_leaves_hit', gain = 0.25 }
table.dug = table.dug or { name = 'x_farming_leaves_dug', gain = 0.5 }
table.place = table.place or { name = 'x_farming_leaves_place', gain = 0.4 }
return table
end
function x_farming.node_sound_wood_defaults(table)
table = table or {}
table.footstep = table.footstep or { name = 'x_farming_wood_footstep', gain = 0.15 }
table.dig = table.dig or { name = 'x_farming_wood_hit', gain = 0.5 }
table.dug = table.dug or { name = 'x_farming_wood_place', gain = 0.1 }
table.place = table.place or { name = 'x_farming_wood_place', gain = 0.15 }
return table
end
function x_farming.node_sound_sand_defaults(table)
table = table or {}
table.footstep = table.footstep or { name = 'x_farming_sand_footstep', gain = 0.1 }
table.dig = table.dig or { name = 'x_farming_sand_hit', gain = 0.5 }
table.dug = table.dug or { name = 'x_farming_sand_dug', gain = 0.1 }
table.place = table.place or { name = 'x_farming_sand_place', gain = 0.15 }
return table
end
function x_farming.node_sound_thin_glass_defaults(table)
table = table or {}
table.footstep = table.footstep or { name = 'x_farming_thin_glass_footstep', gain = 0.3 }
table.dig = table.dig or { name = 'x_farming_thin_glass_footstep', gain = 0.5 }
table.dug = table.dug or { name = 'x_farming_break_thin_glass', gain = 1.0 }
table.place = table.place or { name = 'x_farming_glass_place', gain = 0.2 }
return table
end
function x_farming.node_sound_dirt_defaults(table)
table = table or {}
table.footstep = table.footstep or { name = 'x_farming_dirt_footstep', gain = 0.15 }
table.dig = table.dig or { name = 'x_farming_dirt_hit', gain = 0.4 }
table.dug = table.dug or { name = 'x_farming_dirt_hit', gain = 1.0 }
table.place = table.place or { name = 'x_farming_dirt_hit', gain = 1.0 }
return table
end
function x_farming.node_sound_ice_defaults(table)
table = table or {}
table.footstep = table.footstep or { name = 'x_farming_ice_footstep', gain = 0.2 }
table.dig = table.dig or { name = 'x_farming_ice_hit', gain = 0.4 }
table.dug = table.dug or { name = 'x_farming_ice_hit', gain = 1.0 }
table.place = table.place or { name = 'x_farming_ice_hit', gain = 1.0 }
return table
end
function x_farming.node_sound_stone_defaults(table)
table = table or {}
table.footstep = table.footstep or { name = 'x_farming_stone_footstep', gain = 0.2 }
table.dig = table.dig or { name = 'x_farming_stone_hit', gain = 1.0 }
table.dug = table.dug or { name = 'x_farming_stone_dug', gain = 0.6 }
table.place = table.place or { name = 'x_farming_stone_place', gain = 1.0 }
return table
end
function x_farming.node_sound_slime_defaults(table)
table = table or {}
table.footstep = table.footstep or { name = 'x_farming_slime_footstep', gain = 0.2 }
table.dig = table.dig or { name = 'x_farming_slime_dig', gain = 1.0 }
table.dug = table.dug or { name = 'x_farming_slime_dug', gain = 0.3 }
table.place = table.place or { name = 'x_farming_slime_footstep', gain = 1.0 }
return table
end
function x_farming.node_sound_rope_defaults(table)
table = table or {}
table.footstep = table.footstep or { name = 'x_farming_rope_footstep', gain = 0.05 }
table.dig = table.dig or { name = 'x_farming_rope_hit', gain = 0.7 }
table.dug = table.dug or { name = 'x_farming_rope_dug', gain = 0.2 }
table.place = table.place or { name = 'x_farming_rope_hit', gain = 0.8 }
return table
end
function x_farming.node_sound_pillow_defaults(table)
table = table or {}
table.footstep = table.footstep or { name = 'x_farming_pillow_footstep', gain = 0.1 }
table.dig = table.dig or { name = 'x_farming_pillow_hit', gain = 0.15 }
table.dug = table.dug or { name = 'x_farming_pillow_dug', gain = 0.2 }
table.place = table.place or { name = 'x_farming_pillow_footstep', gain = 0.25 }
return table
end
---how often node timers for plants will tick, +/- some random value
function x_farming.tick_block(pos)
core.get_node_timer(pos):start(math.random(498, 1287))
end
---how often a growth failure tick is retried (e.g. too dark)
function x_farming.tick_block_short(pos)
core.get_node_timer(pos):start(math.random(332, 858))
end
-- how often node timers for plants will tick, +/- some random value
function x_farming.tick(pos)
core.get_node_timer(pos):start(math.random(166, 286))
end
-- how often a growth failure tick is retried (e.g. too dark)
function x_farming.tick_again(pos)
core.get_node_timer(pos):start(math.random(40, 80))
end
---just shorthand for minetest metadata handling
function x_farming.meta_get_str(key, pos)
local meta = core.get_meta(pos)
return meta:get_string(key)
end
---just shorthand for minetest metadata handling
function x_farming.meta_set_str(key, value, pos)
local meta = core.get_meta(pos)
meta:set_string(key, value)
end
---merge two indexed tables
function x_farming.mergeTables(t1, t2)
local _t1 = { unpack(t1) }
local _t2 = { unpack(t2) }
for k, v in ipairs(_t2) do
table.insert(_t1, v)
end
return _t1
end
---Change an entire string to Title Case (i.e. capitalise the first letter of each word)
function x_farming.tchelper(first, rest)
return first:upper() .. rest:lower()
end
--- Hoes - copy from MTG
x_farming.hoe_on_use = function(itemstack, user, pointed_thing, uses)
local pt = pointed_thing
-- check if pointing at a node
if not pt then
return
end
if pt.type ~= 'node' then
return
end
local under = core.get_node(pt.under)
local p = { x = pt.under.x, y = pt.under.y + 1, z = pt.under.z }
local above = core.get_node(p)
-- return if any of the nodes is not registered
if not core.registered_nodes[under.name] then
return
end
if not core.registered_nodes[above.name] then
return
end
-- check if the node above the pointed thing is air
if above.name ~= 'air' then
return
end
-- check if pointing at soil
if core.get_item_group(under.name, 'soil') ~= 1 then
return
end
-- check if (wet) soil defined
local regN = core.registered_nodes
if regN[under.name].soil == nil or regN[under.name].soil.wet == nil or regN[under.name].soil.dry == nil then
return
end
local player_name = user and user:get_player_name() or ''
if core.is_protected(pt.under, player_name) then
core.record_protection_violation(pt.under, player_name)
return
end
if core.is_protected(pt.above, player_name) then
core.record_protection_violation(pt.above, player_name)
return
end
-- turn the node into soil and play sound
core.set_node(pt.under, { name = regN[under.name].soil.dry })
core.sound_play('x_farming_dirt_hit', {
pos = pt.under,
gain = 0.3,
}, true)
if not core.is_creative_enabled(player_name) then
-- wear tool
local wdef = itemstack:get_definition()
itemstack:add_wear_by_uses(uses)
-- tool break sound
if itemstack:get_count() == 0 and wdef.sound and wdef.sound.breaks then
core.sound_play(wdef.sound.breaks, {pos = pt.above,
gain = 0.5}, true)
end
end
return itemstack
end
-- Register new hoes
x_farming.register_hoe = function(name, def)
local _def = table.copy(def)
-- Check for : prefix (register new hoes in your mod's namespace)
if name:sub(1, 1) ~= ':' then
name = ':' .. name
end
-- Check def table
if _def.description == nil then
_def.description = S('Hoe')
end
if _def.inventory_image == nil then
_def.inventory_image = 'x_farming_unknown_item.png'
end
if _def.max_uses == nil then
_def.max_uses = 30
end
if core.get_modpath('farming') then
_def.on_use = function(itemstack, user, pointed_thing)
return x_farming.hoe_on_use(itemstack, user, pointed_thing, _def.max_uses)
end
end
if core.get_modpath('mcl_farming') then
_def.on_place = x_farming.mcl.hoe_on_place_function(_def.max_uses)
end
-- MCL
_def.groups = _def.groups
_def.sound = { breaks = 'x_farming_tool_breaks' }
_def.wield_scale = _def.wield_scale or { x = 1, y = 1, z = 1 }
-- Register the tool
core.register_tool(name, _def)
-- Register its recipe
if _def.recipe then
core.register_craft({
output = name:sub(2),
recipe = _def.recipe
})
elseif _def.material then
core.register_craft({
output = name:sub(2),
recipe = {
{ _def.material, 'group:stick' },
{ _def.material, 'group:stick' },
{ '', 'group:stick' }
}
})
end
end
--
-- Log API / helpers - copy from MTG
--
local log_non_player_actions = core.settings:get_bool('log_non_player_actions', false)
local is_pos = function(v)
return type(v) == 'table' and
type(v.x) == 'number' and type(v.y) == 'number' and type(v.z) == 'number'
end
function x_farming.log_player_action(player, ...)
local msg = player:get_player_name()
if player.is_fake_player or not player:is_player() then
if not log_non_player_actions then
return
end
msg = msg .. '(' .. (type(player.is_fake_player) == 'string'
and player.is_fake_player or '*') .. ')'
end
for _, v in ipairs({ ... }) do
-- translate pos
local part = is_pos(v) and core.pos_to_string(v) or v
-- no leading spaces before punctuation marks
msg = msg .. (string.match(part, '^[;,.]') and '' or ' ') .. part
end
core.log('action', msg)
end
function x_farming.set_inventory_action_loggers(def, name)
def.on_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
x_farming.log_player_action(player, 'moves stuff in', name, 'at', pos)
end
def.on_metadata_inventory_put = function(pos, listname, index, stack, player)
x_farming.log_player_action(player, 'moves', stack:get_name(), 'to', name, 'at', pos)
end
def.on_metadata_inventory_take = function(pos, listname, index, stack, player)
x_farming.log_player_action(player, 'takes', stack:get_name(), 'from', name, 'at', pos)
end
end
--
-- Sapling 'on place' function to check protection of node and resulting tree volume
--
function x_farming.sapling_on_place(itemstack, placer, pointed_thing, sapling_name, minp_relative, maxp_relative, interval)
-- Position of sapling
local pos = pointed_thing.under
local node = core.get_node_or_nil(pos)
local pdef = node and core.registered_nodes[node.name]
if not node then
return itemstack
end
if pdef and pdef.on_rightclick and
not (placer and placer:is_player() and
placer:get_player_control().sneak)
then
return pdef.on_rightclick(pos, node, placer, itemstack, pointed_thing)
end
if not pdef or not pdef.buildable_to then
pos = pointed_thing.above
node = core.get_node_or_nil(pos)
pdef = node and core.registered_nodes[node.name]
if not pdef or not pdef.buildable_to then
return itemstack
end
end
local player_name = placer and placer:get_player_name() or ''
-- Check sapling position for protection
if core.is_protected(pos, player_name) then
core.record_protection_violation(pos, player_name)
return itemstack
end
-- Check tree volume for protection
if core.is_area_protected(
vector.add(pos, minp_relative),
vector.add(pos, maxp_relative),
player_name,
interval) then
core.record_protection_violation(pos, player_name)
-- Print extra information to explain
-- core.chat_send_player(player_name,
-- itemstack:get_definition().description .. ' will intersect protection ' ..
-- 'on growth')
core.chat_send_player(
player_name,
S('@1 will intersect protection on growth.', itemstack:get_definition().description)
)
return itemstack
end
x_farming.log_player_action(placer, 'places node', sapling_name, 'at', pos)
local take_item = not core.is_creative_enabled(player_name)
local newnode = { name = sapling_name }
local ndef = core.registered_nodes[sapling_name]
core.set_node(pos, newnode)
-- Run callback
if ndef and ndef.after_place_node then
-- Deepcopy place_to and pointed_thing because callback can modify it
if ndef.after_place_node(table.copy(pos), placer, itemstack, table.copy(pointed_thing)) then
take_item = false
end
end
-- Run script hook
for _, callback in ipairs(core.registered_on_placenodes) do
-- Deepcopy pos, node and pointed_thing because callback can modify them
if callback(table.copy(pos), table.copy(newnode),
placer, table.copy(node or {}),
itemstack, table.copy(pointed_thing)) then
take_item = false
end
end
if take_item then
itemstack:take_item()
end
return itemstack
end
--
-- Leafdecay
--
-- Prevent decay of placed leaves
function x_farming.after_place_leaves(pos, placer, itemstack, pointed_thing)
if placer and placer:is_player() then
local node = core.get_node(pos)
node.param2 = 1
core.set_node(pos, node)
end
end
-- Leafdecay
local function leafdecay_after_destruct(pos, oldnode, def)
for _, v in pairs(core.find_nodes_in_area(vector.subtract(pos, def.radius), vector.add(pos, def.radius), def.leaves)) do
local node = core.get_node(v)
local timer = core.get_node_timer(v)
if (node.param2 ~= 1 or core.get_item_group('cocoa') == 0) and not timer:is_started() then
timer:start(math.random(20, 120) / 10)
end
end
end
local movement_gravity = tonumber(core.settings:get('movement_gravity')) or 9.81
local function leafdecay_on_timer(pos, def)
if core.find_node_near(pos, def.radius, def.trunks) then
return false
end
local node = core.get_node(pos)
local drops = core.get_node_drops(node.name)
for _, item in ipairs(drops) do
local is_leaf
for _, v in pairs(def.leaves) do
if v == item then
is_leaf = true
end
end
if core.get_item_group(item, 'leafdecay_drop') ~= 0 or not is_leaf then
core.add_item({
x = pos.x - 0.5 + math.random(),
y = pos.y - 0.5 + math.random(),
z = pos.z - 0.5 + math.random(),
}, item)
end
end
core.remove_node(pos)
core.check_for_falling(pos)
-- spawn a few particles for the removed node
core.add_particlespawner({
amount = 8,
time = 0.001,
minpos = vector.subtract(pos, { x = 0.5, y = 0.5, z = 0.5 }),
maxpos = vector.add(pos, { x = 0.5, y = 0.5, z = 0.5 }),
minvel = vector.new(-0.5, -1, -0.5),
maxvel = vector.new(0.5, 0, 0.5),
minacc = vector.new(0, -movement_gravity, 0),
maxacc = vector.new(0, -movement_gravity, 0),
minsize = 0,
maxsize = 0,
node = node,
})
end
function x_farming.register_leafdecay(def)
assert(def.leaves)
assert(def.trunks)
assert(def.radius)
for _, v in pairs(def.trunks) do
core.override_item(v, {
after_destruct = function(pos, oldnode)
leafdecay_after_destruct(pos, oldnode, def)
end,
})
end
for _, v in pairs(def.leaves) do
core.override_item(v, {
on_timer = function(pos)
leafdecay_on_timer(pos, def)
end,
})
end
end
-- Seed placement - copy from MTG
function x_farming.place_seed(itemstack, placer, pointed_thing, plantname)
local pt = pointed_thing
-- check if pointing at a node
if not pt then
return itemstack
end
if pt.type ~= 'node' then
return itemstack
end
local under = core.get_node(pt.under)
local above = core.get_node(pt.above)
local player_name = placer and placer:get_player_name() or ''
if core.is_protected(pt.under, player_name) then
core.record_protection_violation(pt.under, player_name)
return itemstack
end
if core.is_protected(pt.above, player_name) then
core.record_protection_violation(pt.above, player_name)
return itemstack
end
-- return if any of the nodes is not registered
if not core.registered_nodes[under.name] then
return itemstack
end
if not core.registered_nodes[above.name] then
return itemstack
end
-- check if pointing at the top of the node
if pt.above.y ~= pt.under.y + 1 then
return itemstack
end
-- check if you can replace the node above the pointed node
if not core.registered_nodes[above.name].buildable_to then
return itemstack
end
-- check if pointing at soil
if core.get_item_group(under.name, 'soil') < 2 then
return itemstack
end
-- add the node and remove 1 item from the itemstack
x_farming.log_player_action(placer, 'places node', plantname, 'at', pt.above)
core.add_node(pt.above, { name = plantname, param2 = 1 })
x_farming.tick(pt.above)
if not core.is_creative_enabled(player_name) then
itemstack:take_item()
end
return itemstack
end
x_farming.grow_plant = function(pos, elapsed)
local node = core.get_node(pos)
local name = node.name
local def = core.registered_nodes[name]
if not def.next_plant then
-- disable timer for fully grown plant
return
end
-- grow seed
if core.get_item_group(node.name, 'seed') and def.fertility then
local soil_node = core.get_node_or_nil({ x = pos.x, y = pos.y - 1, z = pos.z })
if not soil_node then
x_farming.tick_again(pos)
return
end
-- omitted is a check for light, we assume seeds can germinate in the dark.
for _, v in pairs(def.fertility) do
if core.get_item_group(soil_node.name, v) ~= 0 or string.find(soil_node.name, 'mcl_farming:soil') then
local placenode = { name = def.next_plant }
if def.place_param2 then
placenode.param2 = def.place_param2
end
core.swap_node(pos, placenode)
if core.registered_nodes[def.next_plant].next_plant then
x_farming.tick(pos)
return
end
end
end
return
end
-- check if on wet soil
local below = core.get_node({ x = pos.x, y = pos.y - 1, z = pos.z })
if core.get_item_group(below.name, 'soil') < 3 then
x_farming.tick_again(pos)
return
end
-- check light
local light = core.get_node_light(pos)
if not light or light < def.minlight or light > def.maxlight then
x_farming.tick_again(pos)
return
end
-- grow
local placenode = { name = def.next_plant }
if def.place_param2 then
placenode.param2 = def.place_param2
end
core.swap_node(pos, placenode)
-- new timer needed?
if core.registered_nodes[def.next_plant].next_plant then
x_farming.tick(pos)
end
return
end
-- Register plants
x_farming.register_plant = function(name, def)
local mname = name:split(':')[1]
local pname = name:split(':')[2]
-- Check def table
if not def.description then
def.description = S('Seed')
end
if not def.harvest_description then
def.harvest_description = pname:gsub('^%l', string.upper)
end
if not def.inventory_image then
def.inventory_image = 'unknown_item.png'
end
if not def.steps then
return nil
end
if not def.minlight then
def.minlight = 1
end
if not def.maxlight then
def.maxlight = 14
end
if not def.fertility then
def.fertility = {}
end
x_farming.registered_plants[pname] = def
-- Register seed
local lbm_nodes = { mname .. ':seed_' .. pname }
local g = {
-- MTG
seed = 1,
snappy = 3,
attached_node = 1,
flammable = 2,
-- MCL
handy = 1,
shearsy = 1,
deco_block = 1,
dig_by_water = 1,
destroy_by_lava_flow = 1,
dig_by_piston = 1
}
for k, v in pairs(def.fertility) do
g[v] = 1
end
if def.groups then
for group, value in pairs(def.groups) do
g[group] = value
end
end
core.register_node(':' .. mname .. ':seed_' .. pname, {
description = def.description,
short_description = def.description,
tiles = def.tiles or { def.inventory_image },
inventory_image = def.inventory_image,
wield_image = def.inventory_image,
drawtype = def.drawtype or 'signlike',
groups = g,
paramtype = 'light',
paramtype2 = def.paramtype2 or 'wallmounted',
place_param2 = def.place_param2 or nil, -- this isn't actually used for placement
walkable = false,
sunlight_propagates = true,
selection_box = def.selection_box or {
type = 'fixed',
fixed = { -0.5, -0.5, -0.5, 0.5, -5 / 16, 0.5 },
},
fertility = def.fertility,
sounds = x_farming.node_sound_grass_defaults(),
special_tiles = def.special_tiles and { { name = def.special_tiles, tileable_vertical = true } } or nil,
visual_scale = def.visual_scale or 1,
node_dig_prediction = def.node_dig_prediction or '',
node_placement_prediction = def.node_placement_prediction or nil,
on_place = def.on_place or function(itemstack, placer, pointed_thing)
local under = pointed_thing.under
local node = core.get_node(under)
local udef = core.registered_nodes[node.name]
if udef and udef.on_rightclick and
not (placer and placer:is_player() and
placer:get_player_control().sneak)
then
return udef.on_rightclick(under, node, placer, itemstack, pointed_thing) or itemstack
end
return x_farming.place_seed(itemstack, placer, pointed_thing, mname .. ':seed_' .. pname)
end,
after_destruct = def.after_destruct or nil,
next_plant = mname .. ':' .. pname .. '_1',
on_timer = def.on_timer or x_farming.grow_plant,
minlight = def.minlight,
maxlight = def.maxlight,
_mcl_blast_resistance = 0,
_mcl_hardness = 0,
})
-- Register harvest
core.register_craftitem(':' .. mname .. ':' .. pname, {
description = def.harvest_description,
short_description = def.harvest_description,
inventory_image = mname .. '_' .. pname .. '.png',
groups = def.groups or { flammable = 2 },
})
-- Register growing steps
for i = 1, def.steps do
local base_rarity = 1
if def.steps ~= 1 then
base_rarity = 8 - (i - 1) * 7 / (def.steps - 1)
end
local drop = {
items = {
{items = { mname .. ':' .. pname }, rarity = base_rarity },
{items = { mname .. ':' .. pname }, rarity = base_rarity * 2 },
{items = { mname .. ':seed_' .. pname }, rarity = base_rarity },
{items = { mname .. ':seed_' .. pname }, rarity = base_rarity * 2 },
}
}
local nodegroups = {
farmable = 1,
-- MTG
snappy = 3,
flammable = 2,
plant = 1,
not_in_creative_inventory = 1,
attached_node = 1,
-- MCL
handy = 1,
shearsy = 1,
deco_block = 1,
dig_by_water = 1,
destroy_by_lava_flow = 1,
dig_by_piston = 1
}
nodegroups[pname] = i
if i < def.steps and i > 1 then
nodegroups['bees_pollinate_crop'] = 1
end
if def.groups then
for group, value in pairs(def.groups) do
nodegroups[group] = value
end
end
local next_plant = nil
if i < def.steps then
next_plant = mname .. ':' .. pname .. '_' .. (i + 1)
lbm_nodes[#lbm_nodes + 1] = mname .. ':' .. pname .. '_' .. i
end
local _buildable_to = true
if def.buildable_to ~= nil then
_buildable_to = def.buildable_to
end
core.register_node(':' .. mname .. ':' .. pname .. '_' .. i, {
drawtype = def.drawtype or 'plantlike',
waving = 1,
tiles = def.tiles or { mname .. '_' .. pname .. '_' .. i .. '.png' },
special_tiles = def.special_tiles and { { name = mname .. '_' .. pname .. '_' .. i .. '.png', tileable_vertical = true } } or nil,
paramtype = 'light',
paramtype2 = def.paramtype2 or nil,
place_param2 = def.place_param2 or nil,
visual_scale = def.visual_scale or 1,
node_dig_prediction = def.node_dig_prediction or '',
node_placement_prediction = def.node_placement_prediction or nil,
walkable = false,
buildable_to = _buildable_to,
drop = drop,
selection_box = def['selection_box_' .. i] and def['selection_box_' .. i] or {
type = 'fixed',
fixed = { -0.5, -0.5, -0.5, 0.5, -5 / 16, 0.5 },
},
groups = nodegroups,
sounds = x_farming.node_sound_leaves_defaults(),
next_plant = next_plant,
on_timer = def.on_timer or x_farming.grow_plant,
minlight = def.minlight,
maxlight = def.maxlight,
_mcl_blast_resistance = 0,
_mcl_hardness = 0,
after_destruct = def.after_destruct or nil,