diff --git a/gradle.properties b/gradle.properties index aa42e58..c77de4e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ modGroup=club.sk1er -modVersion=2.1 +modVersion=2.2 modBaseName=UHC Stars forgeVersion=1.8.9-11.15.1.2318-1.8.9 mcpVersion=stable_22 diff --git a/libs/modcore.jar b/libs/modcore.jar index 9d51af4..efeb256 100644 Binary files a/libs/modcore.jar and b/libs/modcore.jar differ diff --git a/src/main/java/club/sk1er/modcore/ModCoreInstaller.java b/src/main/java/club/sk1er/modcore/ModCoreInstaller.java index ecf3720..a7a9c33 100644 --- a/src/main/java/club/sk1er/modcore/ModCoreInstaller.java +++ b/src/main/java/club/sk1er/modcore/ModCoreInstaller.java @@ -1,3 +1,14 @@ +/* + * Copyright © 2020 by Sk1er LLC + * + * All rights reserved. + * + * Sk1er LLC + * 444 S Fulton Ave + * Mount Vernon, NY + * sk1er.club + */ + package club.sk1er.modcore; import com.google.gson.JsonArray; @@ -9,13 +20,14 @@ import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; +import javax.swing.GroupLayout; import javax.swing.JFrame; +import javax.swing.JLabel; import javax.swing.JProgressBar; -import java.awt.Dimension; +import javax.swing.LayoutStyle; +import javax.swing.SwingConstants; +import javax.swing.UIManager; import java.awt.Font; -import java.awt.GridLayout; -import java.awt.TextArea; -import java.awt.Toolkit; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @@ -29,6 +41,7 @@ import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.List; +import java.util.Map; import java.util.Set; /* @@ -37,424 +50,450 @@ public class ModCoreInstaller { - private static final String VERSION_URL = "https://api.sk1er.club/modcore_versions"; - private static final String className = "club.sk1er.mods.core.ModCore"; - private static boolean errored = false; - private static String error; - private static File dataDir = null; - private static boolean isRunningModCore = false; - - public static boolean isIsRunningModCore() { - return isRunningModCore; - } - - private static boolean isInitalized() { - try { - LinkedHashSet objects = new LinkedHashSet<>(); - objects.add(className); - Launch.classLoader.clearNegativeEntries(objects); - Field invalidClasses = LaunchClassLoader.class.getDeclaredField("invalidClasses"); - invalidClasses.setAccessible(true); - Object obj = invalidClasses.get(ModCoreInstaller.class.getClassLoader()); - ((Set) obj).remove(className); - return Class.forName("club.sk1er.mods.core.ModCore") != null; - } catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException ignored) { - ignored.printStackTrace(); - } - return false; - } + private static final String VERSION_URL = "https://api.sk1er.club/modcore_versions"; + private static final String className = "club.sk1er.mods.core.ModCore"; + private static boolean errored = false; + private static String error; + private static File dataDir = null; + private static boolean isRunningModCore = false; - public static boolean isErrored() { - return errored; - } - - public static String getError() { - return error; - } + public static boolean isIsRunningModCore() { + return isRunningModCore; + } - private static void bail(String error) { - errored = true; - ModCoreInstaller.error = error; - } + private static boolean isInitalized() { + try { + LinkedHashSet objects = new LinkedHashSet<>(); + objects.add(className); + Launch.classLoader.clearNegativeEntries(objects); + Field invalidClasses = LaunchClassLoader.class.getDeclaredField("invalidClasses"); + invalidClasses.setAccessible(true); + Object obj = invalidClasses.get(ModCoreInstaller.class.getClassLoader()); + ((Set) obj).remove(className); + return Class.forName("club.sk1er.mods.core.ModCore") != null; + } catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException ignored) { + ignored.printStackTrace(); + } + return false; + } - private static JsonHolder readFile(File in) { - try { - return new JsonHolder(FileUtils.readFileToString(in)); - } catch (IOException ignored) { + public static boolean isErrored() { + return errored; + } + public static String getError() { + return error; } - return new JsonHolder(); - } - public static void initializeModCore(File gameDir) { - if (!isIsRunningModCore()) { - return; + private static void bail(String error) { + errored = true; + ModCoreInstaller.error = error; } - try { - Class modCore = Class.forName(className); - Method instanceMethod = modCore.getMethod("getInstance"); - Method initialize = modCore.getMethod("initialize", File.class); - Object modCoreObject = instanceMethod.invoke(null); - initialize.invoke(modCoreObject, gameDir); - System.out.println("Loaded ModCore Successfully"); - return; - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { - e.printStackTrace(); + + private static JsonHolder readFile(File in) { + try { + return new JsonHolder(FileUtils.readFileToString(in)); + } catch (IOException ignored) { + + } + return new JsonHolder(); } - System.out.println("Did NOT ModCore Successfully"); - } - - public static int initialize(File gameDir, String minecraftVersion) { - if (isInitalized()) return -1; - dataDir = new File(gameDir, "modcore"); - if (!dataDir.exists()) { - if (!dataDir.mkdirs()) { - bail("Unable to create necessary files"); - return 1; - } + + public static void initializeModCore(File gameDir) { + if (!isIsRunningModCore()) { + return; + } + try { + Class modCore = Class.forName(className); + Method instanceMethod = modCore.getMethod("getInstance"); + Method initialize = modCore.getMethod("initialize", File.class); + Object modCoreObject = instanceMethod.invoke(null); + initialize.invoke(modCoreObject, gameDir); + System.out.println("Loaded ModCore Successfully"); + return; + } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } + System.out.println("Did NOT ModCore Successfully"); } - JsonHolder jsonHolder = fetchJSON(VERSION_URL); - String latestRemote = jsonHolder.optString(minecraftVersion); - boolean failed = jsonHolder.getKeys().size() == 0 || (jsonHolder.has("success") && !jsonHolder.optBoolean("success")); - File metadataFile = new File(dataDir, "metadata.json"); - JsonHolder localMetadata = readFile(metadataFile); - if (failed) latestRemote = localMetadata.optString(minecraftVersion); - File modcoreFile = new File(dataDir, "Sk1er Modcore-" + latestRemote + " (" + minecraftVersion + ").jar"); + public static int initialize(File gameDir, String minecraftVersion) { + if (isInitalized()) return -1; + dataDir = new File(gameDir, "modcore"); + if (!dataDir.exists()) { + if (!dataDir.mkdirs()) { + bail("Unable to create necessary files"); + return 1; + } + } + JsonHolder jsonHolder = fetchJSON(VERSION_URL); + String latestRemote = jsonHolder.optString(minecraftVersion); + boolean failed = jsonHolder.getKeys().size() == 0 || (jsonHolder.has("success") && !jsonHolder.optBoolean("success")); - if (!modcoreFile.exists() || !localMetadata.optString(minecraftVersion).equalsIgnoreCase(latestRemote) && !failed) { - //File does not exist, or is out of date, download it - File old = new File(dataDir, "Sk1er Modcore-" + localMetadata.optString(minecraftVersion) + " (" + minecraftVersion + ").jar"); - if (old.exists()) old.delete(); + File metadataFile = new File(dataDir, "metadata.json"); + JsonHolder localMetadata = readFile(metadataFile); + if (failed) latestRemote = localMetadata.optString(minecraftVersion); + File modcoreFile = new File(dataDir, "Sk1er Modcore-" + latestRemote + " (" + minecraftVersion + ").jar"); - if (!download("https://static.sk1er.club/repo/mods/modcore/" + latestRemote + "/" + minecraftVersion + "/ModCore-" + latestRemote + " (" + minecraftVersion + ").jar", latestRemote, modcoreFile, minecraftVersion, localMetadata)) { - bail("Unable to download"); - return 2; - } + if (!modcoreFile.exists() || !localMetadata.optString(minecraftVersion).equalsIgnoreCase(latestRemote) && !failed) { + //File does not exist, or is out of date, download it + File old = new File(dataDir, "Sk1er Modcore-" + localMetadata.optString(minecraftVersion) + " (" + minecraftVersion + ").jar"); + if (old.exists()) old.delete(); - } + if (!download("https://static.sk1er.club/repo/mods/modcore/" + latestRemote + "/" + minecraftVersion + "/ModCore-" + latestRemote + " (" + minecraftVersion + ").jar", latestRemote, modcoreFile, minecraftVersion, localMetadata)) { + bail("Unable to download"); + return 2; + } + + } - addToClasspath(modcoreFile); + addToClasspath(modcoreFile); - if (!isInitalized()) { - bail("Something went wrong and it did not add the jar to the class path. Local file exists? " + modcoreFile.exists()); - return 3; + if (!isInitalized()) { + bail("Something went wrong and it did not add the jar to the class path. Local file exists? " + modcoreFile.exists()); + return 3; + } + isRunningModCore = true; + return 0; } - isRunningModCore = true; - return 0; - } - public static void addToClasspath(File file) { - try { - URL url = file.toURI().toURL(); + public static void addToClasspath(File file) { + try { + URL url = file.toURI().toURL(); - ClassLoader classLoader = ModCoreInstaller.class.getClassLoader(); - Method method = classLoader.getClass().getDeclaredMethod("addURL", URL.class); - method.setAccessible(true); - method.invoke(classLoader, url); - } catch (Exception e) { - throw new RuntimeException("Unexpected exception", e); - } - } - - private static boolean download(String url, String version, File file, String mcver, JsonHolder versionData) { - url = url.replace(" ", "%20"); - System.out.println("Downloading ModCore " + " version " + version + " from: " + url); - JFrame frame = new JFrame("ModCore Initializer"); - JProgressBar bar = new JProgressBar(); - TextArea comp = new TextArea("", 1, 1, TextArea.SCROLLBARS_NONE); - frame.getContentPane().add(comp); - frame.getContentPane().add(bar); - GridLayout manager = new GridLayout(); - frame.setLayout(manager); - manager.setColumns(1); - manager.setRows(2); - comp.setText("Downloading Sk1er ModCore Library Version " + version + " for Minecraft " + mcver); - comp.setSize(399, 80); - comp.setEditable(false); - Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); - - - Dimension preferredSize = new Dimension(400, 225); - bar.setSize(preferredSize); - frame.setSize(preferredSize); - frame.setResizable(false); - bar.setBorderPainted(true); - bar.setMinimum(0); - bar.setStringPainted(true); - frame.setVisible(true); - frame.setLocation(dim.width / 2 - frame.getSize().width / 2, dim.height / 2 - frame.getSize().height / 2); - Font font = bar.getFont(); - bar.setFont(new Font(font.getName(), font.getStyle(), font.getSize() * 4)); - comp.setFont(new Font(font.getName(), font.getStyle(), font.getSize() * 2)); - - try { - - URL u = new URL(url); - HttpURLConnection connection = (HttpURLConnection) u.openConnection(); - connection.setRequestMethod("GET"); - connection.setUseCaches(true); - connection.addRequestProperty("User-Agent", "Mozilla/4.76 (Sk1er Modcore Initializer)"); - connection.setReadTimeout(15000); - connection.setConnectTimeout(15000); - connection.setDoOutput(true); - InputStream is = connection.getInputStream(); - int contentLength = connection.getContentLength(); - FileOutputStream outputStream = new FileOutputStream(file); - byte[] buffer = new byte[1024]; - System.out.println("MAX: " + contentLength); - bar.setMaximum(contentLength); - int read; - bar.setValue(0); - while ((read = is.read(buffer)) > 0) { - outputStream.write(buffer, 0, read); - bar.setValue(bar.getValue() + 1024); - } - outputStream.close(); - FileUtils.write(new File(dataDir, "metadata.json"), versionData.put(mcver, version).toString()); - } catch (Exception e) { - e.printStackTrace(); - frame.dispose(); - return false; - } - frame.dispose(); - return true; - } - - public static JsonHolder fetchJSON(String url) { - return new JsonHolder(fetchString(url)); - } - - public static String fetchString(String url) { - url = url.replace(" ", "%20"); - System.out.println("Fetching " + url); - try { - URL u = new URL(url); - HttpURLConnection connection = (HttpURLConnection) u.openConnection(); - connection.setRequestMethod("GET"); - connection.setUseCaches(true); - connection.addRequestProperty("User-Agent", "Mozilla/4.76 (Sk1er ModCore)"); - connection.setReadTimeout(15000); - connection.setConnectTimeout(15000); - connection.setDoOutput(true); - InputStream is = connection.getInputStream(); - return IOUtils.toString(is, Charset.defaultCharset()); - } catch (Exception e) { - e.printStackTrace(); + ClassLoader classLoader = ModCoreInstaller.class.getClassLoader(); + Method method = classLoader.getClass().getDeclaredMethod("addURL", URL.class); + method.setAccessible(true); + method.invoke(classLoader, url); + } catch (Exception e) { + throw new RuntimeException("Unexpected exception", e); + } } - return "Failed to fetch"; - } + private static boolean download(String url, String version, File file, String mcver, JsonHolder versionData) { + url = url.replace(" ", "%20"); + System.out.println("Downloading ModCore " + " version " + version + " from: " + url); - //Added because we need to use before ModCore is loaded - static class JsonHolder { - private JsonObject object; + try { + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); + } catch (Exception e) { + e.printStackTrace(); + } + + JFrame frame = new JFrame("ModCore Initializer"); + JProgressBar bar = new JProgressBar(); + JLabel label = new JLabel("Downloading ModCore " + version, SwingConstants.CENTER); + label.setSize(600, 120); + frame.getContentPane().add(label); + frame.getContentPane().add(bar); + GroupLayout layout = new GroupLayout(frame.getContentPane()); + frame.getContentPane().setLayout(layout); + layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) + .addComponent(label, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE) + .addComponent(bar, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + .addContainerGap())); + layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(label, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE) + .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) + .addComponent(bar, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE) + .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); + frame.setResizable(false); + bar.setBorderPainted(true); + bar.setMinimum(0); + bar.setStringPainted(true); + Font font = bar.getFont(); + bar.setFont(new Font(font.getName(), font.getStyle(), font.getSize() * 2)); + label.setFont(new Font(font.getName(), font.getStyle(), font.getSize() * 2)); + frame.pack(); + frame.setLocationRelativeTo(null); + frame.setVisible(true); + + HttpURLConnection connection = null; + try (FileOutputStream outputStream = new FileOutputStream(file)) { + URL u = new URL(url); + connection = (HttpURLConnection) u.openConnection(); + connection.setRequestMethod("GET"); + connection.setUseCaches(true); + connection.addRequestProperty("User-Agent", "Mozilla/4.76 (Sk1er Modcore Initializer)"); + connection.setReadTimeout(15000); + connection.setConnectTimeout(15000); + connection.setDoOutput(true); + try (InputStream is = connection.getInputStream()) { + int contentLength = connection.getContentLength(); + byte[] buffer = new byte[1024]; + System.out.println("MAX: " + contentLength); + bar.setMaximum(contentLength); + int read; + bar.setValue(0); + while ((read = is.read(buffer)) > 0) { + outputStream.write(buffer, 0, read); + bar.setValue(bar.getValue() + 1024); + } + } + FileUtils.write(new File(dataDir, "metadata.json"), versionData.put(mcver, version).toString()); + } catch (Exception e) { + e.printStackTrace(); + frame.dispose(); + return false; + } finally { + if (connection != null) { + connection.disconnect(); + } + } + + frame.dispose(); + return true; + } - public JsonHolder(JsonObject object) { - this.object = object; + public static JsonHolder fetchJSON(String url) { + return new JsonHolder(fetchString(url)); } - public JsonHolder(String raw) { - if (raw == null) - object = new JsonObject(); - else + public static String fetchString(String url) { + url = url.replace(" ", "%20"); + System.out.println("Fetching " + url); + + HttpURLConnection connection = null; try { - this.object = new JsonParser().parse(raw).getAsJsonObject(); + URL u = new URL(url); + connection = (HttpURLConnection) u.openConnection(); + connection.setRequestMethod("GET"); + connection.setUseCaches(true); + connection.addRequestProperty("User-Agent", "Mozilla/4.76 (Sk1er ModCore)"); + connection.setReadTimeout(15000); + connection.setConnectTimeout(15000); + connection.setDoOutput(true); + try (InputStream is = connection.getInputStream()) { + return IOUtils.toString(is, Charset.defaultCharset()); + } } catch (Exception e) { - this.object = new JsonObject(); - e.printStackTrace(); + e.printStackTrace(); + } finally { + if (connection != null) { + connection.disconnect(); + } } - } - public JsonHolder() { - this(new JsonObject()); + return "Failed to fetch"; } - @Override - public String toString() { - if (object != null) - return object.toString(); - return "{}"; - } - public JsonHolder put(String key, boolean value) { - object.addProperty(key, value); - return this; - } + //Added because we need to use before ModCore is loaded + static class JsonHolder { + private JsonObject object; - public void mergeNotOverride(JsonHolder merge) { - merge(merge, false); - } + public JsonHolder(JsonObject object) { + this.object = object; + } - public void mergeOverride(JsonHolder merge) { - merge(merge, true); - } + public JsonHolder(String raw) { + if (raw == null) + object = new JsonObject(); + else + try { + this.object = new JsonParser().parse(raw).getAsJsonObject(); + } catch (Exception e) { + this.object = new JsonObject(); + e.printStackTrace(); + } + } - public void merge(JsonHolder merge, boolean override) { - JsonObject object = merge.getObject(); - for (String s : merge.getKeys()) { - if (override || !this.has(s)) - put(s, object.get(s)); - } - } + public JsonHolder() { + this(new JsonObject()); + } - private void put(String s, JsonElement element) { - this.object.add(s, element); - } + @Override + public String toString() { + if (object != null) + return object.toString(); + return "{}"; + } - public JsonHolder put(String key, String value) { - object.addProperty(key, value); - return this; - } + public JsonHolder put(String key, boolean value) { + object.addProperty(key, value); + return this; + } - public JsonHolder put(String key, int value) { - object.addProperty(key, value); - return this; - } + public void mergeNotOverride(JsonHolder merge) { + merge(merge, false); + } - public JsonHolder put(String key, double value) { - object.addProperty(key, value); - return this; - } + public void mergeOverride(JsonHolder merge) { + merge(merge, true); + } - public JsonHolder put(String key, long value) { - object.addProperty(key, value); - return this; - } + public void merge(JsonHolder merge, boolean override) { + JsonObject object = merge.getObject(); + for (String s : merge.getKeys()) { + if (override || !this.has(s)) + put(s, object.get(s)); + } + } - private JsonHolder defaultOptJSONObject(String key, JsonObject fallBack) { - try { - return new JsonHolder(object.get(key).getAsJsonObject()); - } catch (Exception e) { - return new JsonHolder(fallBack); - } - } + private void put(String s, JsonElement element) { + this.object.add(s, element); + } - public JsonArray defaultOptJSONArray(String key, JsonArray fallback) { - try { - return object.get(key).getAsJsonArray(); - } catch (Exception e) { - return fallback; - } - } + public JsonHolder put(String key, String value) { + object.addProperty(key, value); + return this; + } - public JsonArray optJSONArray(String key) { - return defaultOptJSONArray(key, new JsonArray()); - } + public JsonHolder put(String key, int value) { + object.addProperty(key, value); + return this; + } + public JsonHolder put(String key, double value) { + object.addProperty(key, value); + return this; + } - public boolean has(String key) { - return object.has(key); - } + public JsonHolder put(String key, long value) { + object.addProperty(key, value); + return this; + } - public long optLong(String key, long fallback) { - try { - return object.get(key).getAsLong(); - } catch (Exception e) { - return fallback; - } - } + private JsonHolder defaultOptJSONObject(String key, JsonObject fallBack) { + try { + return new JsonHolder(object.get(key).getAsJsonObject()); + } catch (Exception e) { + return new JsonHolder(fallBack); + } + } - public long optLong(String key) { - return optLong(key, 0); - } + public JsonArray defaultOptJSONArray(String key, JsonArray fallback) { + try { + return object.get(key).getAsJsonArray(); + } catch (Exception e) { + return fallback; + } + } - public boolean optBoolean(String key, boolean fallback) { - try { - return object.get(key).getAsBoolean(); - } catch (Exception e) { - return fallback; - } - } + public JsonArray optJSONArray(String key) { + return defaultOptJSONArray(key, new JsonArray()); + } - public boolean optBoolean(String key) { - return optBoolean(key, false); - } - public JsonObject optActualJSONObject(String key) { - try { - return object.get(key).getAsJsonObject(); - } catch (Exception e) { - return new JsonObject(); - } - } + public boolean has(String key) { + return object.has(key); + } - public JsonHolder optJSONObject(String key) { - return defaultOptJSONObject(key, new JsonObject()); - } + public long optLong(String key, long fallback) { + try { + return object.get(key).getAsLong(); + } catch (Exception e) { + return fallback; + } + } + public long optLong(String key) { + return optLong(key, 0); + } - public int optInt(String key, int fallBack) { - try { - return object.get(key).getAsInt(); - } catch (Exception e) { - return fallBack; - } - } + public boolean optBoolean(String key, boolean fallback) { + try { + return object.get(key).getAsBoolean(); + } catch (Exception e) { + return fallback; + } + } - public int optInt(String key) { - return optInt(key, 0); - } + public boolean optBoolean(String key) { + return optBoolean(key, false); + } + public JsonObject optActualJSONObject(String key) { + try { + return object.get(key).getAsJsonObject(); + } catch (Exception e) { + return new JsonObject(); + } + } - public String defaultOptString(String key, String fallBack) { - try { - return object.get(key).getAsString(); - } catch (Exception e) { - return fallBack; - } - } + public JsonHolder optJSONObject(String key) { + return defaultOptJSONObject(key, new JsonObject()); + } - public String optString(String key) { - return defaultOptString(key, ""); - } + public int optInt(String key, int fallBack) { + try { + return object.get(key).getAsInt(); + } catch (Exception e) { + return fallBack; + } + } - public double optDouble(String key, double fallBack) { - try { - return object.get(key).getAsDouble(); - } catch (Exception e) { - return fallBack; - } - } + public int optInt(String key) { + return optInt(key, 0); + } - public List getKeys() { - List tmp = new ArrayList<>(); - object.entrySet().forEach(e -> tmp.add(e.getKey())); - return tmp; - } - public double optDouble(String key) { - return optDouble(key, 0.0); - } + public String defaultOptString(String key, String fallBack) { + try { + return object.get(key).getAsString(); + } catch (Exception e) { + return fallBack; + } + } + public String optString(String key) { + return defaultOptString(key, ""); + } - public JsonObject getObject() { - return object; - } - public boolean isNull(String key) { - return object.has(key) && object.get(key).isJsonNull(); - } + public double optDouble(String key, double fallBack) { + try { + return object.get(key).getAsDouble(); + } catch (Exception e) { + return fallBack; + } + } - public JsonHolder put(String values, JsonHolder values1) { - return put(values, values1.getObject()); - } + public List getKeys() { + List tmp = new ArrayList<>(); + for (Map.Entry e : object.entrySet()) { + tmp.add(e.getKey()); + } + return tmp; + } - public JsonHolder put(String values, JsonObject object) { - this.object.add(values, object); - return this; - } + public double optDouble(String key) { + return optDouble(key, 0.0); + } - public void put(String blacklisted, JsonArray jsonElements) { - this.object.add(blacklisted, jsonElements); - } - public void remove(String header) { - object.remove(header); + public JsonObject getObject() { + return object; + } + + public boolean isNull(String key) { + return object.has(key) && object.get(key).isJsonNull(); + } + + public JsonHolder put(String values, JsonHolder values1) { + return put(values, values1.getObject()); + } + + public JsonHolder put(String values, JsonObject object) { + this.object.add(values, object); + return this; + } + + public void put(String blacklisted, JsonArray jsonElements) { + this.object.add(blacklisted, jsonElements); + } + + public void remove(String header) { + object.remove(header); + } } - } } \ No newline at end of file diff --git a/src/main/java/club/sk1er/uhcstars/UHCStars.java b/src/main/java/club/sk1er/uhcstars/UHCStars.java index e20ed91..471fbb4 100644 --- a/src/main/java/club/sk1er/uhcstars/UHCStars.java +++ b/src/main/java/club/sk1er/uhcstars/UHCStars.java @@ -8,12 +8,12 @@ import net.minecraftforge.fml.common.Mod.EventHandler; import net.minecraftforge.fml.common.event.FMLInitializationEvent; -@Mod(modid = "Sk1er-UHCstars", name = "UHC Stars", version = "2.1") +@Mod(modid = "Sk1er-UHCstars", name = "UHC Stars", version = "2.2") public class UHCStars { - @EventHandler - public void init(FMLInitializationEvent event) { - ModCoreInstaller.initializeModCore(Minecraft.getMinecraft().mcDataDir); - ClientCommandHandler.instance.registerCommand(new StarsCommand()); - } + @EventHandler + public void init(FMLInitializationEvent event) { + ModCoreInstaller.initializeModCore(Minecraft.getMinecraft().mcDataDir); + ClientCommandHandler.instance.registerCommand(new StarsCommand()); + } } diff --git a/src/main/java/club/sk1er/uhcstars/command/StarsCommand.java b/src/main/java/club/sk1er/uhcstars/command/StarsCommand.java index aa9c5b7..80b2ab5 100644 --- a/src/main/java/club/sk1er/uhcstars/command/StarsCommand.java +++ b/src/main/java/club/sk1er/uhcstars/command/StarsCommand.java @@ -11,76 +11,78 @@ public class StarsCommand extends CommandBase { - private final int[] scores = {10, 50, 150, 250, 500, 750, 1000, 2500, 5000}; + private final int[] scores = {10, 50, 150, 250, 500, 750, 1000, 2500, 5000}; - /** Gets the name of the command */ - @Override - public String getCommandName() { - return "stars"; - } + /** + * Gets the name of the command + */ + @Override + public String getCommandName() { + return "stars"; + } - /** - * Gets the usage string for the command. - * - * @param sender - */ - @Override - public String getCommandUsage(ICommandSender sender) { - return "/stars "; - } + /** + * Gets the usage string for the command. + * + * @param sender + */ + @Override + public String getCommandUsage(ICommandSender sender) { + return "/stars "; + } - /** - * Callback when the command is invoked - * - * @param sender - * @param args - */ - @Override - public void processCommand(ICommandSender sender, String[] args) throws CommandException { - if (args.length == 1) { - Multithreading.runAsync( - () -> { - try { - JsonHolder object = WebUtil.fetchJSON("https://api.sk1er.club/player/" + args[0]); - if (!object.optBoolean("success")) { - MinecraftUtils.sendMessage(EnumChatFormatting.RED + "Player not found!"); - } else { - JsonHolder uhc = - object.optJSONObject("player").optJSONObject("stats").optJSONObject("UHC"); - int score = uhc.optInt("score"); - MinecraftUtils.sendMessage(EnumChatFormatting.YELLOW + "[UHC Stars] ", - EnumChatFormatting.BLUE - + object.optJSONObject("player").optString("displayname") - + " is a " - + Math.min(getStars(score), 15) - + " star!"); - } - } catch (Exception e) { - MinecraftUtils.sendMessage(EnumChatFormatting.RED + "Player not found!"); - } - }); - } else { - MinecraftUtils.sendMessage(EnumChatFormatting.RED + getCommandUsage(sender)); + /** + * Callback when the command is invoked + * + * @param sender + * @param args + */ + @Override + public void processCommand(ICommandSender sender, String[] args) throws CommandException { + if (args.length == 1) { + Multithreading.runAsync( + () -> { + try { + JsonHolder object = WebUtil.fetchJSON("https://api.sk1er.club/player/" + args[0]); + if (!object.optBoolean("success")) { + MinecraftUtils.sendMessage(EnumChatFormatting.RED + "Player not found!"); + } else { + JsonHolder uhc = + object.optJSONObject("player").optJSONObject("stats").optJSONObject("UHC"); + int score = uhc.optInt("score"); + MinecraftUtils.sendMessage(EnumChatFormatting.YELLOW + "[UHC Stars] ", + EnumChatFormatting.BLUE + + object.optJSONObject("player").optString("displayname") + + " is a " + + Math.min(getStars(score), 15) + + " star!"); + } + } catch (Exception e) { + MinecraftUtils.sendMessage(EnumChatFormatting.RED + "Player not found!"); + } + }); + } else { + MinecraftUtils.sendMessage(EnumChatFormatting.RED + getCommandUsage(sender)); + } } - } - @Override - public int getRequiredPermissionLevel() { - return -1; - } + @Override + public int getRequiredPermissionLevel() { + return -1; + } - private int getStars(int score) { - int stars = 1; + private int getStars(int score) { + int stars = 1; - for (int i : scores) { - if (score < i) { - break; - } + for (int i : scores) { + if (score < i) { + break; + } - score -= i; - ++stars; - } + score -= i; + ++stars; + } - return stars + score / 3000; - } + return stars + score / 3000; + } } diff --git a/src/main/java/club/sk1er/uhcstars/tweaker/UHCStarsTweaker.java b/src/main/java/club/sk1er/uhcstars/tweaker/UHCStarsTweaker.java index 0ea5928..dc51d4e 100644 --- a/src/main/java/club/sk1er/uhcstars/tweaker/UHCStarsTweaker.java +++ b/src/main/java/club/sk1er/uhcstars/tweaker/UHCStarsTweaker.java @@ -1,80 +1,82 @@ package club.sk1er.uhcstars.tweaker; import club.sk1er.modcore.ModCoreInstaller; -import java.util.Map; import net.minecraft.launchwrapper.Launch; import net.minecraftforge.common.ForgeVersion; import net.minecraftforge.fml.relauncher.IFMLLoadingPlugin; import net.minecraftforge.fml.relauncher.IFMLLoadingPlugin.MCVersion; +import java.util.Map; + @MCVersion(ForgeVersion.mcVersion) public class UHCStarsTweaker implements IFMLLoadingPlugin { - /** - * Return a list of classes that implements the IClassTransformer interface - * - * @return a list of classes that implements the IClassTransformer interface - */ - @Override - public String[] getASMTransformerClass() { - int initialize = ModCoreInstaller.initialize(Launch.minecraftHome, "1.8.9"); + /** + * Return a list of classes that implements the IClassTransformer interface + * + * @return a list of classes that implements the IClassTransformer interface + */ + @Override + public String[] getASMTransformerClass() { + int initialize = ModCoreInstaller.initialize(Launch.minecraftHome, "1.8.9"); - if (ModCoreInstaller.isErrored() || initialize != 0 && initialize != -1) { - // Technically wouldn't happen in simulated installed but is important for actual impl - System.out.println( - "Failed to load Sk1er Modcore - " + initialize + " - " + ModCoreInstaller.getError()); - } + if (ModCoreInstaller.isErrored() || initialize != 0 && initialize != -1) { + // Technically wouldn't happen in simulated installed but is important for actual impl + System.out.println( + "Failed to load Sk1er Modcore - " + initialize + " - " + ModCoreInstaller.getError()); + } - // If true the classes are loaded - if (ModCoreInstaller.isIsRunningModCore()) { - return new String[] {"club.sk1er.mods.core.forge.ClassTransformer"}; - } + // If true the classes are loaded + if (ModCoreInstaller.isIsRunningModCore()) { + return new String[]{"club.sk1er.mods.core.forge.ClassTransformer"}; + } - return new String[] {}; - } + return new String[]{}; + } - /** - * Return a class name that implements "ModContainer" for injection into the mod list The - * "getName" function should return a name that other mods can, if need be, depend on. Trivially, - * this modcontainer will be loaded before all regular mod containers, which means it will be - * forced to be "immutable" - not susceptible to normal sorting behaviour. All other mod - * behaviours are available however- this container can receive and handle normal loading events - */ - @Override - public String getModContainerClass() { - return null; - } + /** + * Return a class name that implements "ModContainer" for injection into the mod list The + * "getName" function should return a name that other mods can, if need be, depend on. Trivially, + * this modcontainer will be loaded before all regular mod containers, which means it will be + * forced to be "immutable" - not susceptible to normal sorting behaviour. All other mod + * behaviours are available however- this container can receive and handle normal loading events + */ + @Override + public String getModContainerClass() { + return null; + } - /** - * Return the class name of an implementor of "IFMLCallHook", that will be run, in the main - * thread, to perform any additional setup this coremod may require. It will be run - * prior to Minecraft starting, so it CANNOT operate on minecraft itself. The - * game will deliberately crash if this code is detected to trigger a minecraft class loading - * (TODO: implement crash ;) ) - */ - @Override - public String getSetupClass() { - return null; - } + /** + * Return the class name of an implementor of "IFMLCallHook", that will be run, in the main + * thread, to perform any additional setup this coremod may require. It will be run + * prior to Minecraft starting, so it CANNOT operate on minecraft itself. The + * game will deliberately crash if this code is detected to trigger a minecraft class loading + * (TODO: implement crash ;) ) + */ + @Override + public String getSetupClass() { + return null; + } - /** - * Inject coremod data into this coremod This data includes: "mcLocation" : the location of the - * minecraft directory, "coremodList" : the list of coremods "coremodLocation" : the file this - * coremod loaded from, - * - * @param data - */ - @Override - public void injectData(Map data) {} + /** + * Inject coremod data into this coremod This data includes: "mcLocation" : the location of the + * minecraft directory, "coremodList" : the list of coremods "coremodLocation" : the file this + * coremod loaded from, + * + * @param data + */ + @Override + public void injectData(Map data) { + } - /** - * Return an optional access transformer class for this coremod. It will be injected post-deobf so - * ensure your ATs conform to the new srgnames scheme. - * - * @return the name of an access transformer class or null if none is provided - */ - @Override - public String getAccessTransformerClass() { - return null; - } + /** + * Return an optional access transformer class for this coremod. It will be injected post-deobf so + * ensure your ATs conform to the new srgnames scheme. + * + * @return the name of an access transformer class or null if none is provided + */ + @Override + public String getAccessTransformerClass() { + return null; + } }