-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.lua
138 lines (123 loc) · 3.65 KB
/
init.lua
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
local have_creative = minetest.get_modpath("creative")
local function try_staple(itemstack, placer, pointed)
if pointed.type ~= "node" then
return
end
if pointed.above.y - pointed.under.y ~= 0 then
return
end
-- Node under must be a tree
local node_under = minetest.get_node(pointed.under)
local def_under = minetest.registered_nodes[node_under.name] or {}
if not def_under.groups or not def_under.groups.tree then
return
end
if def_under.paramtype2 == "facedir"
or def_under.paramtype2 == "colorfacedir" then
-- Only vertical facedirs are allowed
local dir = minetest.facedir_to_dir(node_under.param2)
if dir.y ~= 0 then
return
end
end
-- Node above must be airlike, but not ignore
local node_above = minetest.get_node(pointed.above)
local def_above = minetest.registered_nodes[node_above.name] or {}
if node_above.name == "ignore" or not def_above.buildable_to then
return
end
-- Staple to the tree
local dir = vector.direction(pointed.above, pointed.under)
local pos = minetest.pointed_thing_to_face_pos(placer, pointed)
pos.x = pos.x + (math.random() - 0.5) / 100 -- avoid Z-fighting
pos.z = pos.z + (math.random() - 0.5) / 100
local obj = minetest.add_entity(
vector.add(pos, vector.multiply(dir, -0.01)),
"stapled_bread:bread_slice"
)
if obj then
local entity = obj:get_luaentity()
entity.object:set_yaw(math.atan2(-dir.x, dir.z))
entity.object:set_properties({
textures = { itemstack:get_name() }
})
-- Take the item *after* specifying the texture
-- or we might up with stapled hands (ouch!!)
if not have_creative or not creative.is_enabled_for(
placer:get_player_name()) then
itemstack:take_item(1)
end
end
return itemstack
end
local function staple_place_wrapper(itemstack, placer, pointed)
return try_staple(itemstack, placer, pointed) or itemstack
end
local extent = 0.12
local box = {-extent, -extent, -extent, extent, extent, extent}
minetest.register_entity("stapled_bread:bread_slice", {
initial_properties = {
visual = "wielditem",
textures = { "unknown_node.png" },
visual_size = {x = 0.2, y = 0.2},
physical = false,
collisionbox = box,
selectionbox = box
},
get_staticdata = function(self)
return self.object:get_properties().textures[1]
end,
on_activate = function(self, staticdata, dtime_s)
if staticdata and staticdata ~= "" then
self.object:set_properties({
textures = { staticdata }
})
end
end,
on_punch = function(self, puncher, _, _, dir, _)
local obj_dir = minetest.yaw_to_dir(self.object:get_yaw())
local diff = vector.dot(obj_dir, dir)
if diff > 0.2 then
return
end
if math.random() > 0.4 then
return
end
local itemname = self.object:get_properties().textures[1]
local stack = ItemStack(itemname)
if minetest.registered_items[stack:get_name()] then
-- I would not recommend eating this piece
puncher:get_inventory():add_item("main", stack)
end
self.object:remove()
end
})
-- REGISTER BREAD STUFF
local function make_stapleable(itemname)
minetest.override_item(itemname, {
on_place = staple_place_wrapper
})
end
if minetest.get_modpath("farming") then
make_stapleable("farming:bread")
minetest.register_craftitem("stapled_bread:bread_slice", {
description = "Bread Slice",
inventory_image = "stapled_bread_slice.png",
on_use = minetest.item_eat(1),
on_place = staple_place_wrapper
})
minetest.register_craft({
type = "shapeless",
output = "stapled_bread:bread_slice 5",
recipe = {
"farming:bread"
}
})
if farming.mod == "redo" then
make_stapleable("farming:bread_slice")
make_stapleable("farming:garlic_bread")
end
end
if minetest.get_modpath("sandwiches") then
make_stapleable("sandwiches:bread_slice")
end