Skip to content
This repository has been archived by the owner on Mar 15, 2024. It is now read-only.

Commit

Permalink
Fix crash on join if utility websites are down
Browse files Browse the repository at this point in the history
  • Loading branch information
rettichlp committed Aug 10, 2022
1 parent 10b755b commit 6893a1f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ apply plugin: 'net.minecraftforge.gradle'
apply plugin: 'idea'
apply plugin: 'maven-publish'

version = '1.1.1'
version = '1.1.2'
group = 'com.rettichlp' // http://maven.apache.org/guides/mini/guide-naming-conventions.html
archivesBaseName = 'UnicacityAddon'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
*/
public class UnicacityAddon extends LabyModAddon {

public static final String VERSION = "1.1.1";
public static final String VERSION = "1.1.2";
public static final Minecraft MINECRAFT = Minecraft.getMinecraft();
public static final LabyMod LABYMOD = LabyMod.getInstance();
public static UnicacityAddon ADDON;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ public static Map<String, Integer> getPlayerRankMap() {
}

public static boolean checkPlayerHouseBan(String playerName) {
if (websiteSource.isEmpty()) websiteSource = WebsiteAPI.websiteToString("https://fuzzlemann.de/commons/houseBans");
if (websiteSource.isEmpty()) {
Thread thread = new Thread(() -> websiteSource = WebsiteAPI.websiteToString("https://fuzzlemann.de/commons/houseBans"));
thread.start();
}
return websiteSource.contains(playerName);
}

Expand All @@ -51,26 +54,32 @@ public static boolean checkPlayerDuty(String playerName) {
private static Map<String,Faction> getPlayerFactions() {
Map<String,Faction> playerFactions = new HashMap<>();

for (Faction faction : Faction.values()) {
List<String> nameList = ListUtils.getAllMatchesFromString(PatternHandler.NAME_PATTERN, faction.getWebsiteSource());
nameList.forEach(name -> playerFactions.put(name.replace("<h4 class=\"h5 g-mb-5\"><strong>", ""), faction));
}
Thread thread = new Thread(() -> {
for (Faction faction : Faction.values()) {
List<String> nameList = ListUtils.getAllMatchesFromString(PatternHandler.NAME_PATTERN, faction.getWebsiteSource());
nameList.forEach(name -> playerFactions.put(name.replace("<h4 class=\"h5 g-mb-5\"><strong>", ""), faction));
}
});
thread.start();

return playerFactions;
}

private static Map<String,Integer> getPlayerRanks() {
Map<String,Integer> playerRankMap = new HashMap<>();

for (Faction faction : Faction.values()) {
List<String> nameList = ListUtils.getAllMatchesFromString(PatternHandler.NAME_PATTERN, faction.getWebsiteSource());
List<String> rankList = ListUtils.getAllMatchesFromString(PatternHandler.RANK_PATTERN, faction.getWebsiteSource());
nameList.forEach(name -> playerRankMap.put(
name.replace("<h4 class=\"h5 g-mb-5\"><strong>", ""),
Integer.parseInt(String.valueOf(rankList.get(nameList.indexOf(name))
.replace("<strong>Rang ", "")
.charAt(0)))));
}
Thread thread = new Thread(() -> {
for (Faction faction : Faction.values()) {
List<String> nameList = ListUtils.getAllMatchesFromString(PatternHandler.NAME_PATTERN, faction.getWebsiteSource());
List<String> rankList = ListUtils.getAllMatchesFromString(PatternHandler.RANK_PATTERN, faction.getWebsiteSource());
nameList.forEach(name -> playerRankMap.put(
name.replace("<h4 class=\"h5 g-mb-5\"><strong>", ""),
Integer.parseInt(String.valueOf(rankList.get(nameList.indexOf(name))
.replace("<strong>Rang ", "")
.charAt(0)))));
}
});
thread.start();

return playerRankMap;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.rettichlp.UnicacityAddon.base.utils;

import joptsimple.internal.Strings;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;

/**
Expand All @@ -12,14 +14,14 @@
public class WebsiteAPI {

public static String websiteToString(String urlString) {

Scanner scanner;
StringBuilder websiteSource = new StringBuilder();

if (urlString != null) {
try {
URLConnection openConnection = new URL(urlString).openConnection();
HttpURLConnection openConnection = (HttpURLConnection) new URL(urlString).openConnection();
openConnection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0");
if (openConnection.getResponseCode() != HttpURLConnection.HTTP_OK) return Strings.EMPTY;
scanner = new Scanner(new InputStreamReader(openConnection.getInputStream()));
while (scanner.hasNextLine()) websiteSource.append(scanner.nextLine()).append("\n\r");
} catch (IOException e) {
Expand Down

0 comments on commit 6893a1f

Please sign in to comment.