-
-
Save alexito4/23cbbe8eddcede6ca2de to your computer and use it in GitHub Desktop.
Real world weather into Minecraft http://www.alejandromp.com/blog/2015/1/17/real-world-weather-into-minecraft
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local wait = 60 | |
os.loadAPI("weather") | |
local city = "London,uk" | |
while true do | |
local commandBlock = peripheral.wrap("back") | |
local w = weather.getWeather(city) | |
local command = "weather " .. w .. " " .. wait | |
print(command) | |
commandBlock.setCommand(command) | |
commandBlock.runCommand() | |
sleep(wait) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
os.loadAPI("json") | |
local sun = "clear" | |
local rain = "rain" | |
local thunder = "thunder" | |
-- Relates id with weather | |
-- http://openweathermap.org/weather-conditions | |
local typeeq = { | |
{id = 960, w = "thunder"}, | |
{id = 961, w = "thunder"}, | |
{id = 962, w = "thunder"}, | |
} | |
local typeless = { | |
{id = 300, w = "thunder"}, | |
{id = 400, w = "rain"}, | |
{id = 600, w = "rain"}, | |
{id = 700, w = "rain"}, | |
{id = 800, w = "clear"}, | |
{id = 900, w = "clear"}, | |
{id = 1000, w = "thunder"}, | |
} | |
function getWeather(city) | |
local url = "http://api.openweathermap.org/data/2.5/weather?q=" .. city | |
local response = http.get(url) | |
local responseStr = response.readAll() | |
local obj = json.decode(responseStr) | |
local id = tonumber(obj["weather"][1]["id"]) | |
local w = clear | |
-- print("Id: " .. id) | |
-- Search for an special id | |
for i=1,#typeeq do | |
if typeeq[i]["id"] == id then | |
w = typeeq[i]["w"] | |
break | |
end | |
end | |
-- Search for an id in ranges | |
for i=1,#typeless do | |
if id < typeless[i]["id"] then | |
w = typeless[i]["w"] | |
break | |
end | |
end | |
return w | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment