toolrepair.lua 3.87 KB
Newer Older
SmallJoker's avatar
SmallJoker committed
1
2
3
4
--Created by Krock for the BitChange mod
--License: WTFPL

local function set_infotext(meta, mode)
SmallJoker's avatar
SmallJoker committed
5
	if mode == meta:get_int("state") then
SmallJoker's avatar
SmallJoker committed
6
7
8
9
10
		return
	end
	local owner = meta:get_string("owner")
	local text = "Tool Repair "
	local text2 = "[Inactive]"
SmallJoker's avatar
SmallJoker committed
11
	if mode == 0 then
SmallJoker's avatar
SmallJoker committed
12
		text = text.."(constructing)"
SmallJoker's avatar
SmallJoker committed
13
	elseif mode == 1 then
SmallJoker's avatar
SmallJoker committed
14
		text2 = "Inactive"
SmallJoker's avatar
SmallJoker committed
15
	elseif mode == 2 then
SmallJoker's avatar
SmallJoker committed
16
17
		text2 = "Active"
	end
SmallJoker's avatar
SmallJoker committed
18
	if mode ~= 0 then
SmallJoker's avatar
SmallJoker committed
19
20
21
22
23
24
25
26
		 text = text.."["..text2.."] (owned by "..owner..")"
	end
	
	meta:set_int("state", mode)
	meta:set_string("infotext", text)
	
	local formspec =
		"size[8,9]"..
27
		"label[1,1;Damaged tool:]"..
SmallJoker's avatar
SmallJoker committed
28
29
		"list[current_name;src;3.5,1;1,1;]"..
		"label[3.4,2;\\["..text2.."\\]]"..
SmallJoker's avatar
Updatez    
SmallJoker committed
30
		"label[1.5,3;MineNinth:]]"..
SmallJoker's avatar
SmallJoker committed
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
		"list[current_name;fuel;3.5,3;1,1;]"..
		"list[current_player;main;0,5;8,4;]"
	meta:set_string("formspec", formspec)
end

minetest.register_node("bitchange:toolrepair", {
	description = "Tool Repair",
	tiles = {"bitchange_toolrepair_top.png", "bitchange_toolrepair_bottom.png", 
		"bitchange_toolrepair_side.png", "bitchange_toolrepair_side.png", 
		"bitchange_toolrepair_side.png", "bitchange_toolrepair_side.png"},
	groups = {cracky=1},
	sounds = default.node_sound_stone_defaults(),
	after_place_node = function(pos, placer, itemstack)
		local meta = minetest.get_meta(pos)
		meta:set_string("owner", placer:get_player_name())
		meta:set_int("state", 0)
		set_infotext(meta, 1)
	end,
	on_construct = function(pos)
		local meta = minetest.get_meta(pos)
		meta:set_string("owner", "")
		set_infotext(meta, 0)
		local inv = meta:get_inventory()
		inv:set_size("src", 1)
		inv:set_size("fuel", 1)
	end,
	allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
		return 0
	end,
	allow_metadata_inventory_put = function(pos, listname, index, stack, player)
		local meta = minetest.get_meta(pos)
SmallJoker's avatar
SmallJoker committed
62
		if player:get_player_name() ~= meta:get_string("owner") then
SmallJoker's avatar
SmallJoker committed
63
64
65
			return 0
		end
		
SmallJoker's avatar
SmallJoker committed
66
		if listname == "src" then
SmallJoker's avatar
SmallJoker committed
67
68
69
70
71
72
			if(stack:get_wear() > 0
				and stack:get_wear() < 65535
				and stack:get_name() ~= "technic:water_can"
				and stack:get_name() ~= "technic:lava_can") then
				return 1
			end
SmallJoker's avatar
SmallJoker committed
73
74
		elseif listname == "fuel" then
			if stack:get_name() == "bitchange:mineninth" then
SmallJoker's avatar
SmallJoker committed
75
76
77
78
79
80
81
				return stack:get_count()
			end
		end
		return 0
	end,
	allow_metadata_inventory_take = function(pos, listname, index, stack, player)
		local meta = minetest.get_meta(pos)
SmallJoker's avatar
SmallJoker committed
82
		if bitchange.has_access(meta:get_string("owner"), player:get_player_name()) then
SmallJoker's avatar
SmallJoker committed
83
84
85
86
87
88
89
			return stack:get_count()
		end
		return 0
	end,
	can_dig = function(pos, player)
		local meta = minetest.get_meta(pos)
		local inv = meta:get_inventory()
SmallJoker's avatar
SmallJoker committed
90
		if bitchange.has_access(meta:get_string("owner"), player:get_player_name()) then
SmallJoker's avatar
SmallJoker committed
91
92
93
94
95
96
97
			return inv:is_empty("src") and inv:is_empty("fuel")
		end
		return 0
	end
})

minetest.register_craft({
SmallJoker's avatar
SmallJoker committed
98
	output = "bitchange:toolrepair",
SmallJoker's avatar
SmallJoker committed
99
	recipe = {
SmallJoker's avatar
SmallJoker committed
100
		{"default:steel_ingot", "default:stick", "default:steel_ingot"},
101
102
		{"group:tree", "default:mese_crystal", "group:tree"},
		{"group:tree", "bitchange:minecoinblock", "group:tree"}
SmallJoker's avatar
SmallJoker committed
103
104
105
106
107
	}
})

minetest.register_abm({
	nodenames = {"bitchange:toolrepair"},
SmallJoker's avatar
Updatez    
SmallJoker committed
108
	interval = 5,
SmallJoker's avatar
SmallJoker committed
109
110
111
112
113
114
	chance = 1,
	action = function(pos, node, active_object_count, active_object_count_wider)
		local meta = minetest.get_meta(pos)
		local inv = meta:get_inventory()
		local src = inv:get_stack("src", 1)
		local wear = src:get_wear()
SmallJoker's avatar
Updatez    
SmallJoker committed
115
		local repair = -1400
SmallJoker's avatar
SmallJoker committed
116
117
		
		if(src:is_empty() 
SmallJoker's avatar
Updatez    
SmallJoker committed
118
			or wear == 0
SmallJoker's avatar
SmallJoker committed
119
120
121
122
123
124
125
			or wear == 65535
			or src:get_name() == "technic:water_can"
			or src:get_name() == "technic:lava_can") then
			set_infotext(meta, 1)
			return
		end
		local fuel = inv:get_stack("fuel", 1)
SmallJoker's avatar
Updatez    
SmallJoker committed
126
		if(fuel:is_empty() or fuel:get_name() ~= "bitchange:mineninth") then
SmallJoker's avatar
SmallJoker committed
127
128
129
130
			set_infotext(meta, 1)
			return
		end
		
SmallJoker's avatar
Updatez    
SmallJoker committed
131
132
		if(wear + repair < 0) then
			src:add_wear(repair + wear)
SmallJoker's avatar
SmallJoker committed
133
		else
SmallJoker's avatar
Updatez    
SmallJoker committed
134
			src:add_wear(repair)
SmallJoker's avatar
SmallJoker committed
135
136
137
		end
		inv:set_stack("src", 1, src)
		set_infotext(meta, 2)
SmallJoker's avatar
Updatez    
SmallJoker committed
138
		inv:remove_item("fuel", "bitchange:mineninth 1")
SmallJoker's avatar
SmallJoker committed
139
	end
140
})