-- override the protection function to handle the
-- sealing spell and amulets
do local nextfn = minetest.is_protected
	minetest.is_protected = function(pos,name)
		local meta = minetest.get_meta(pos)
		if meta:contains('owner') and meta:contains('sorcery_wand_key') then
			if meta:get_string('sorcery_seal_mode') == 'wand' or name ~= meta:get_string('owner') then
				return true
			end
		end
		return nextfn(pos,name)
	end
end

if minetest.get_modpath('hopper') then
	hopper:add_container {
		{'side',  'group:sorcery_device_generator','fuel'};
		{'bottom','group:sorcery_device_generator','fuel'};

		{'side',  'sorcery:coin_press','ingot'};
		{'bottom','sorcery:coin_press','gem'};
		-- output handled on our side; requires automation

		{'bottom','sorcery:infuser','infusion'};
		{'side',  'sorcery:infuser','potions'};
		-- output handled on our side

		{'top',   'sorcery:displacer','cache'};
		{'side',  'sorcery:displacer','cache'};
		{'bottom','sorcery:displacer','cache'};

		{'side','sorcery:displacer_transmit_gem','code'};
		{'side','sorcery:displacer_receive_gem', 'code'};

		{'side','group:sorcery_device_kiln','fuel'};
		{'top', 'group:sorcery_device_kiln','output'};

		{'top',   'group:sorcery_device_smelter','output'};
		{'bottom','group:sorcery_device_smelter','input'};
		{'side',  'group:sorcery_device_smelter','fuel'};

		{'top',    'sorcery:mill', 'output'};
		{'side',   'sorcery:mill', 'grinder'};
		{'bottom', 'sorcery:mill', 'input'};

		{'bottom', 'sorcery:harvester', 'charge'};
		-- output handled on our side

		{'bottom', 'sorcery:runeforge', 'amulet'};
		-- output handled on our side
	}
end

if minetest.get_modpath('mtg_craftguide') and minetest.get_modpath('sfinv') then
-- the craft guide is handy, but not only is it glitched to the point of enabling
-- trivial denial of service attacks against a server, it breaks some of the most
-- basic mechanics of the sorcery mod. we disable it except for players with a
-- specific debugging privilege. i suppose we could also add a 'potion of
-- omniscience' that allows brief access, but i'm disinclined to; it feels gross.
	local pg = sfinv.pages['mtg_craftguide:craftguide']
	local cb = pg.is_in_nav
	-- currently this isn't used by mtgcg, but doing this gives us some future-
	-- proofing, and keeps us from fucking up any competing access control that
	-- might be in use.
	pg.is_in_nav = function(self,player, ...)
		-- unfortunately, this is a purely cosmetic "access control" mechanism;
		-- sfinv doesn't actually check if a page is available to a player before
		-- showing it to them. ironic, given how the author specifically warns
		-- people in his modding tutorial that the client can submit any form it
		-- wants at any time… 🙄
		if not minetest.check_player_privs(player, 'sorcery:omniscience') then
			return false
		end
		if cb
			then return cb(self,player,...)
			else return true
		end
	end
end
