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)
62 new_temp_path = temppath .. DIR_DELIM .. "/skin_"..name..".png"
64 file = io.open(new_temp_path, "wb")
69 minetest.dynamic_add_media(new_temp_path)
71 file = "skin_"..name..".png" -- reuse the data
73 player = minetest.get_player_by_name(name)
75 player:set_properties({textures = {file, "blank_skin.png"}})
79 recalculate_armor(player)
92 -- simple degrees calculation
93 local degrees = function(yaw)
97 -- built in engine trigonometry
98 local pitch = function(pos,pos2)
126 -- calculation to calculate the yaw of the old position
127 local cape_yaw_calculation = function(pos,pos2)
147 yaw_correction = function(yaw)
150 elseif yaw > 180 then
156 -- returns if the cape can be "blown"
158 local move_cape = function(yaw,yaw2)
159 cape_yaw = yaw_correction(degrees(yaw-yaw2))
160 return(cape_yaw >= -90 and cape_yaw <= 90)
163 -- applies movement to the cape
164 local cape_smoothing = function(object,current,cape_goal)
165 if current ~= cape_goal then
166 if math.abs(current-cape_goal) <= 3 then --this stops jittering
167 object:set_animation({x=cape_goal,y=cape_goal}, 0, 0, false)
168 elseif current < cape_goal then
169 object:set_animation({x=current+3,y=current+3}, 0, 0, false)
170 elseif current > cape_goal then
171 object:set_animation({x=current-3,y=current-3}, 0, 0, false)
176 local cape_object = {}
177 cape_object.initial_properties = {
180 textures = {"cape_core.png"},
182 collisionbox = {0, 0, 0, 0, 0, 0}
184 cape_object.texture_set = false
185 cape_object.on_activate = function(self)
186 minetest.after(0,function()
187 --don't waste any cpu
188 if not self.owner or not self.owner:is_player() then
194 if self.texture_type and not self.texture_set then
195 self.object:set_properties({textures={self.texture_type}})
196 self.texture_type = nil
197 self.texture_set = nil
205 local current_animation
206 local current_animation
210 cape_object.on_step = function(self,dtime)
212 pos = object:get_pos()
213 current_animation = object:get_animation() -- if fails assign other values to nil
214 current_animation = current_animation.x
216 if minetest.is_player(self.owner) and self.old_pos then
217 --do not allow cape to flutter if player is moving backwards
218 cape_yaw = cape_yaw_calculation(pos,self.old_pos)
219 body_yaw = self.owner:get_look_horizontal()
221 if move_cape(cape_yaw,body_yaw) then
222 goal = pitch(pos,self.old_pos)
227 cape_smoothing(object,current_animation,goal)
228 elseif not minetest.is_player(self.owner) then
235 minetest.register_entity("skins:cape",cape_object)
267 -- simple check if has cape
270 local get_texture = function(player)
271 name = string.lower(player:get_player_name())
276 temp_cape = "cape_"..name..".png"
277 elseif core_devs[name] then
278 temp_cape = "cape_core.png"
279 elseif patrons[name] then
280 temp_cape = "cape_patron.png"
285 -- adds cape to player
291 local add_cape = function(player)
292 if get_texture(player) then
293 texture = get_texture(player)
295 name = player:get_player_name()
296 temp_pool = pool2[name]
298 object = minetest.add_entity(player:get_pos(),"skins:cape")
300 lua_entity = object:get_luaentity()
302 lua_entity.owner = player
303 lua_entity.texture_type = texture
304 object:set_attach(player, "Cape_bone", vector.new(0,0,0), vector.new(0,0,0))
310 -- looping check to see if cape deleted
312 local function readd_capes()
313 for name,def in pairs(pool2) do
314 player = minetest.get_player_by_name(name)
315 if pool2[name] and not pool2[name]:get_luaentity() then
317 elseif not minetest.is_player(name) then
321 minetest.after(3,function()
326 minetest.register_on_mods_loaded(function()
327 minetest.after(3,function()
333 minetest.register_on_joinplayer(function(player)
336 minetest.after(0,function()
337 fetch_function(player:get_player_name())
338 recalculate_armor(player)