MidnightLib v0.1.0 for 21w08b

This commit is contained in:
Motschen
2021-02-28 12:36:43 +01:00
parent 5928257fab
commit a4be637128
40 changed files with 1495 additions and 1 deletions

View File

@@ -0,0 +1,48 @@
package eu.midnightdust.hats.web;
import com.google.common.reflect.TypeToken;
import net.minecraft.client.MinecraftClient;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Type;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import static net.minecraft.datafixer.fix.BlockEntitySignTextStrictJsonFix.GSON;
public class HatLoader {
public static final Logger logger = LogManager.getLogger("MidnightLib");
private final static String HATS_URL = "https://raw.githubusercontent.com/TeamMidnightDust/MidnightHats/master/hats.json";
public static final Type HAT_TYPE = new TypeToken<Map<UUID, PlayerHatData>>(){}.getType();
public static Map<UUID, PlayerHatData> PLAYER_HATS;
public static void init() {
CompletableFuture.supplyAsync(() -> {
try (Reader reader = new InputStreamReader(new URL(HATS_URL).openStream())) {
Map<UUID, PlayerHatData> playerData = GSON.fromJson(reader, HAT_TYPE);
return playerData;
} catch (MalformedURLException error) {
logger.log(Level.ERROR, "Unable to load player hats because of connection problems: " + error.getMessage());
} catch (IOException error) {
logger.log(Level.ERROR, "Unable to load player hats because of an I/O Exception: " + error.getMessage());
}
return null;
}).thenAcceptAsync(playerData -> {
if (playerData != null) {
PLAYER_HATS = playerData;
logger.log(Level.INFO, "Player hats successfully loaded!");
} else {
PLAYER_HATS = Collections.emptyMap();
logger.log(Level.WARN, "A problem with the database occured, the hats could not be initialized.");
}
}, MinecraftClient.getInstance());
}
}