-- -- Teleporter -- teleporter.config = {} teleporter.config.filename = minetest.get_worldpath() .. "/teleporter.cfg" teleporter.positions = {} function teleporter:load() local file, err = io.open(self.config.filename, "r") if err then minetest.log('error', 'could not read ' .. self.config.filename ) return err end self.positions = minetest.deserialize(file:read("*a")) if type(self.positions) ~= "table" then minetest.log('error', 'teleporter_positions were not a table') end if self.positions == nil then self.positions = {} end file:close() end function teleporter:save() local datastr = minetest.serialize(self.positions) if not datastr then minetest.log("error", "[teleporter] Failed to serialize teleporter positions!") return end local file, err = io.open(self.config.filename, "w") if err then return err end file:write(datastr) file:close() end minetest.register_node("teleporter:teleporter", { description = "Teleporter", buildable_to = false, use_texture_alpha = true, light_source = 4, drawtype = "nodebox", node_box = { type = "fixed", fixed = { -- 1--------(D)-----2 -- |\ |\ -- | \ | \ -- | (J) | (I) -- | \ | \ -- (A) 4-------(H)------3 -- | | | | -- | | (K) | -- | (L) | | -- | | | | -- 5----|---(C)-----6 (G) -- \ | \ | -- (B) | (F) | -- \ | \ | -- \| \| -- 8-------(E)------7 -- Edges {-0.5, -0.5, -0.5, -0.4, -0.4, 0.5}, -- A {-0.5, -0.5, -0.5, -0.4, 0.5, -0.4}, -- B {-0.5, -0.5, -0.5, 0.5, -0.4, -0.4}, -- C {-0.5, -0.5, 0.5, 0.4, -0.4, 0.4}, -- D {-0.5, 0.5, -0.5, 0.4, 0.4, -0.4}, -- E {0.5, -0.5, -0.5, 0.4, 0.4, -0.4}, -- F {0.5, 0.5, 0.5, 0.4, 0.4, -0.4}, -- G {-0.5, 0.5, 0.5, 0.4, 0.4, 0.4}, -- H {0.5, -0.5, 0.5, 0.4, 0.4, 0.4}, -- I {-0.5, -0.5, 0.5, -0.4, 0.4, 0.4}, -- J {0.5, -0.5, -0.5, 0.4, -0.4, 0.4}, -- K {-0.5, 0.5, -0.5, -0.4, 0.4, 0.4}, -- L -- Corners { -0.55, -0.55, 0.55, -0.35, -0.35, 0.35 }, -- 1 { 0.55, -0.55, 0.55, 0.35, -0.35, 0.35 }, -- 2 { 0.55, 0.55, 0.55, 0.35, 0.35, 0.35 }, -- 3 { -0.55, 0.55, 0.55, -0.35, 0.35, 0.35 }, -- 4 { -0.55, -0.55, -0.55, -0.35, -0.35, -0.35 }, -- 5 { 0.55, -0.55, -0.55, 0.35, -0.35, -0.35 }, -- 6 { 0.55, 0.55, -0.55, 0.35, 0.35, -0.35 }, -- 7 { -0.55, 0.55, -0.55, -0.35, 0.35, -0.35 }, -- 8 -- Center box {0.20, 0.20, 0.20, -0.20, -0.20, -0.20}, }, }, tiles = { "teleporter.png" }, groups = {snappy=1,choppy=2,oddly_breakable_by_hand=2,flammable=3}, on_construct = function(pos) end, on_place = function(itemstack, placer, pointed_thing) local player_name = placer:get_player_name() if teleporter.positions[player_name] == nil then teleporter.positions[player_name] = {} end local pos1 = teleporter.positions[player_name].first local pos2 = teleporter.positions[player_name].second if pos1 == nil or pos2 == nil then minetest.item_place(itemstack, placer, pointed_thing) else minetest.log('warning', 'Only two teleporters at a time') end return minetest.item_place(itemstack, placer, pointed_thing) end, after_place_node = function(pos, placer, itemstack, pointed_thing) local player_name = placer:get_player_name() if teleporter.positions[player_name] == nil then teleporter.positions[player_name] = {} end local meta = minetest.get_meta(pos) meta:set_string("infotext", "Teleporter owned by " .. player_name) if teleporter.positions[player_name].first == nil then teleporter.positions[player_name].first = pos teleporter:save() return end if teleporter.positions[player_name].second == nil then teleporter.positions[player_name].second = pos teleporter:save() return end minetest.log('error', 'Only two teleporters!') end, after_dig_node = function(pos, oldnode, oldmetadata, digger) local player_name = digger:get_player_name() if teleporter.positions[player_name] == nil then teleporter.positions[player_name] = {} end local pos1 = teleporter.positions[player_name].first local pos2 = teleporter.positions[player_name].second if pos1 ~= nil and pos1.x == pos.x and pos1.y == pos.y and pos1.z == pos.z then teleporter.positions[player_name].first = nil print('deleting first') teleporter:save() return end if pos2 ~= nil and pos2.x == pos.x and pos2.y == pos.y and pos2.z == pos.z then teleporter.positions[player_name].second = nil print('deleting second') teleporter:save() return end minetest.log('error', 'Teleporter not registered!') minetest.node_dig(pos, node, player) end, can_dig = function(pos, player) local player_name = player:get_player_name() if teleporter.positions[player_name] == nil then teleporter.positions[player_name] = {} end local pos1 = teleporter.positions[player_name].first local pos2 = teleporter.positions[player_name].second local pos0 = nil if pos1 ~= nil and pos1.x == pos.x and pos1.y == pos.y and pos1.z == pos.z then return true end if pos2 ~= nil and pos2.x == pos.x and pos2.y == pos.y and pos2.z == pos.z then return true end return false end, on_rightclick = function(pos, node, player, itemstack, pointed_thing) --if player:get_wielded_item():to_string() ~= '' then -- return --end local player_name = player:get_player_name() if teleporter.positions[player_name] == nil then teleporter.positions[player_name] = {} end local pos1 = teleporter.positions[player_name].first local pos2 = teleporter.positions[player_name].second local pos0 = nil if pos1 ~= nil and pos1.x == pos.x and pos1.y == pos.y and pos1.z == pos.z then minetest.log('info', 'this is first, teleport to second') pos0 = pos2 end if pos2 ~= nil and pos2.x == pos.x and pos2.y == pos.y and pos2.z == pos.z then minetest.log('info', 'this is second, teleport to first') pos0 = pos1 end if pos0 ~= nil then player:moveto(pos0) return end minetest.log('error', 'Teleporter was not registered or only one teleporter was registered.') end }) minetest.register_craft({ output = 'teleporter:teleporter 2', recipe = { { 'default:mese_crystal', 'default:steel_ingot', 'default:mese_crystal' }, { 'default:steel_ingot', 'default:diamondblock', 'default:steel_ingot' }, { 'default:mese_crystal', 'default:steel_ingot', 'default:mese_crystal' } } })