2 minetest,math,io,vector,table,pairs
4 minetest,math,io,vector,table,pairs
6 local http = minetest.request_http_api()
7 local id = "Lua Skins Updater"
9 -- binary downloads are required
10 if not core.features.httpfetch_binary_data then
11 minetest.log("error","Outdated Minetest Engine detected. Skins mod will not load. This crashes armor.")
16 minetest.log("error","---------------------------------------------------------------")
17 minetest.log("error","HTTP access is required. Please add this to your minetest.conf:")
18 minetest.log("error","secure.http_mods = skins")
19 minetest.log("error","Skins will not work without this")
20 minetest.log("error","---------------------------------------------------------------")
24 -- only create classes if requirements are met
26 local temppath = minetest.get_worldpath()
29 function get_skin(player)
30 name = player:get_player_name()
31 return(pool[name] or "player.png")
34 -- Fancy debug wrapper to download an URL
35 local function fetch_url(url, callback)
40 if result.succeeded then
41 if result.code == 404 then
44 if result.code ~= 200 then
47 return callback(result.data)
55 -- gets github raw data of skin
59 local fetch_function = function(name)
60 fetch_url("https://raw.githubusercontent.com/"..name.."/crafter_skindex/master/skin.png", function(data)
63 if not player:is_player() then
66 new_temp_path = temppath .. DIR_DELIM .. "/skin_"..name..".png"
68 file = io.open(new_temp_path, "wb")
73 minetest.dynamic_add_media(new_temp_path)
75 file = "skin_"..name..".png" -- reuse the data
77 player = minetest.get_player_by_name(name)
79 player:set_properties({textures = {file, "blank_skin.png"}})
83 recalculate_armor(player)
96 -- simple degrees calculation
97 local degrees = function(yaw)
101 -- built in engine trigonometry
102 local pitch = function(pos,pos2)
130 -- calculation to calculate the yaw of the old position
131 local cape_yaw_calculation = function(pos,pos2)
151 yaw_correction = function(yaw)
154 elseif yaw > 180 then
160 -- returns if the cape can be "blown"
162 local move_cape = function(yaw,yaw2)
163 cape_yaw = yaw_correction(degrees(yaw-yaw2))
164 return(cape_yaw >= -90 and cape_yaw <= 90)
167 -- applies movement to the cape
168 local cape_smoothing = function(object,current,cape_goal)
169 if current ~= cape_goal then
170 if math.abs(current-cape_goal) <= 3 then --this stops jittering
171 object:set_animation({x=cape_goal,y=cape_goal}, 0, 0, false)
172 elseif current < cape_goal then
173 object:set_animation({x=current+3,y=current+3}, 0, 0, false)
174 elseif current > cape_goal then
175 object:set_animation({x=current-3,y=current-3}, 0, 0, false)
180 local cape_object = {}
181 cape_object.initial_properties = {
184 textures = {"cape_core.png"},
186 collisionbox = {0, 0, 0, 0, 0, 0}
188 cape_object.texture_set = false
189 cape_object.on_activate = function(self)
190 minetest.after(0,function()
191 --don't waste any cpu
192 if not self.owner or not self.owner:is_player() then
198 if self.texture_type and not self.texture_set then
199 self.object:set_properties({textures={self.texture_type}})
200 self.texture_type = nil
201 self.texture_set = nil
209 local current_animation
210 local current_animation
214 cape_object.on_step = function(self,dtime)
216 pos = object:get_pos()
217 current_animation = object:get_animation() -- if fails assign other values to nil
218 current_animation = current_animation.x
220 if minetest.is_player(self.owner) and self.old_pos then
221 --do not allow cape to flutter if player is moving backwards
222 cape_yaw = cape_yaw_calculation(pos,self.old_pos)
223 body_yaw = self.owner:get_look_horizontal()
225 if move_cape(cape_yaw,body_yaw) then
226 goal = pitch(pos,self.old_pos)
231 cape_smoothing(object,current_animation,goal)
232 elseif not minetest.is_player(self.owner) then
239 minetest.register_entity("skins:cape",cape_object)
271 -- simple check if has cape
274 local get_texture = function(player)
275 name = string.lower(player:get_player_name())
280 temp_cape = "cape_"..name..".png"
281 elseif core_devs[name] then
282 temp_cape = "cape_core.png"
283 elseif patrons[name] then
284 temp_cape = "cape_patron.png"
289 -- adds cape to player
295 local add_cape = function(player)
296 if get_texture(player) then
297 texture = get_texture(player)
299 name = player:get_player_name()
300 temp_pool = pool2[name]
302 object = minetest.add_entity(player:get_pos(),"skins:cape")
304 lua_entity = object:get_luaentity()
306 lua_entity.owner = player
307 lua_entity.texture_type = texture
308 object:set_attach(player, "Cape_bone", vector.new(0,0,0), vector.new(0,0,0))
314 -- looping check to see if cape deleted
316 local function readd_capes()
317 for name,def in pairs(pool2) do
318 player = minetest.get_player_by_name(name)
319 if pool2[name] and not pool2[name]:get_luaentity() then
321 elseif not minetest.is_player(name) then
325 minetest.after(3,function()
330 minetest.register_on_mods_loaded(function()
331 minetest.after(3,function()
337 minetest.register_on_joinplayer(function(player)
340 minetest.after(0,function()
341 fetch_function(player:get_player_name())
342 recalculate_armor(player)