diff options
Diffstat (limited to 'src')
22 files changed, 638 insertions, 46 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/OneConfig.java b/src/main/java/cc/polyfrost/oneconfig/OneConfig.java index 0eb9ec3..60bb37e 100644 --- a/src/main/java/cc/polyfrost/oneconfig/OneConfig.java +++ b/src/main/java/cc/polyfrost/oneconfig/OneConfig.java @@ -12,6 +12,7 @@ import cc.polyfrost.oneconfig.lwjgl.RenderManager; import cc.polyfrost.oneconfig.lwjgl.font.Fonts; import cc.polyfrost.oneconfig.lwjgl.image.Images; import cc.polyfrost.oneconfig.test.TestConfig; +import cc.polyfrost.oneconfig.utils.hypixel.HypixelUtils; import net.minecraft.launchwrapper.Launch; import net.minecraftforge.client.ClientCommandHandler; import net.minecraftforge.fml.common.DummyModContainer; @@ -50,7 +51,8 @@ public class OneConfig { BlurHandler.INSTANCE.load(); testConfig = new TestConfig(); ClientCommandHandler.instance.registerCommand(new OneConfigCommand()); - EventManager.INSTANCE.getEventBus().register(new HudCore()); + EventManager.INSTANCE.register(new HudCore()); + EventManager.INSTANCE.register(HypixelUtils.INSTANCE); RenderManager.setupAndDraw((vg) -> { RenderManager.drawRoundedRect(vg, -100, -100, 50, 50, -1, 12f); RenderManager.drawString(vg, "OneConfig loading...", -100, -100, -1, 12f, Fonts.MEDIUM); diff --git a/src/main/java/cc/polyfrost/oneconfig/events/EventManager.java b/src/main/java/cc/polyfrost/oneconfig/events/EventManager.java index 956c9b6..e1c1427 100644 --- a/src/main/java/cc/polyfrost/oneconfig/events/EventManager.java +++ b/src/main/java/cc/polyfrost/oneconfig/events/EventManager.java @@ -14,4 +14,16 @@ public final class EventManager { public EventBus getEventBus() { return eventBus; } + + public void register(Object object) { + eventBus.register(object); + } + + public void unregister(Object object) { + eventBus.unregister(object); + } + + public void post(Object event) { + eventBus.post(event); + } } diff --git a/src/main/java/cc/polyfrost/oneconfig/events/event/ChatReceiveEvent.java b/src/main/java/cc/polyfrost/oneconfig/events/event/ChatReceiveEvent.java new file mode 100644 index 0000000..b69b89b --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/events/event/ChatReceiveEvent.java @@ -0,0 +1,12 @@ +package cc.polyfrost.oneconfig.events.event; + + +import net.minecraft.util.IChatComponent; + +public class ChatReceiveEvent extends CancellableEvent { + public final IChatComponent message; + + public ChatReceiveEvent(IChatComponent message) { + this.message = message; + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/events/event/LocrawEvent.java b/src/main/java/cc/polyfrost/oneconfig/events/event/LocrawEvent.java new file mode 100644 index 0000000..42565f7 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/events/event/LocrawEvent.java @@ -0,0 +1,11 @@ +package cc.polyfrost.oneconfig.events.event; + +import cc.polyfrost.oneconfig.utils.hypixel.LocrawInfo; + +public class LocrawEvent { + public final LocrawInfo info; + + public LocrawEvent(LocrawInfo info) { + this.info = info; + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/events/event/WorldLoadEvent.java b/src/main/java/cc/polyfrost/oneconfig/events/event/WorldLoadEvent.java new file mode 100644 index 0000000..1583fe6 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/events/event/WorldLoadEvent.java @@ -0,0 +1,4 @@ +package cc.polyfrost.oneconfig.events.event; + +public class WorldLoadEvent { +} diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/pages/CreditsPage.java b/src/main/java/cc/polyfrost/oneconfig/gui/pages/CreditsPage.java index d6c00ab..e41cc9e 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/pages/CreditsPage.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/pages/CreditsPage.java @@ -29,7 +29,8 @@ public class CreditsPage extends Page { RenderManager.drawString(vg, " - NanoVG (memononen) - NanoVG Library", x + 20, y + 370, -1, 12, Fonts.REGULAR); RenderManager.drawString(vg, " - UniversalCraft (Sk1er LLC) - Multiversioning bindings", x + 20, y + 385, -1, 12, Fonts.REGULAR); RenderManager.drawString(vg, " - Easing Functions (jesusgollonet)", x + 20, y + 400, -1, 12, Fonts.REGULAR); - RenderManager.drawString(vg, " - Quiltflower (W-OVERFLOW) - Gradle toolkit", x + 20, y + 415, -1, 12, Fonts.REGULAR); + RenderManager.drawString(vg, " - Quiltflower (Quilt Team) - Gradle decompiler", x + 20, y + 415, -1, 12, Fonts.REGULAR); + RenderManager.drawString(vg, " - Seraph (Scherso) - Locraw and Multithreading utilities", x + 20, y + 430, -1, 12, Fonts.REGULAR); RenderManager.drawString(vg, "Terms Of Service & Licensing", x + 20, y + 517, -1, 24, Fonts.SEMIBOLD); RenderManager.drawString(vg, " - License Summary goes here", x + 20, y + 540, -1, 12, Fonts.REGULAR); diff --git a/src/main/java/cc/polyfrost/oneconfig/lwjgl/BlurHandler.java b/src/main/java/cc/polyfrost/oneconfig/lwjgl/BlurHandler.java index a769e12..d2b4811 100644 --- a/src/main/java/cc/polyfrost/oneconfig/lwjgl/BlurHandler.java +++ b/src/main/java/cc/polyfrost/oneconfig/lwjgl/BlurHandler.java @@ -45,16 +45,16 @@ public class BlurHandler { * Simply initializes the blur mod so events are properly handled by forge. */ public void load() { - EventManager.INSTANCE.getEventBus().register(this); + EventManager.INSTANCE.register(this); } @Subscribe - public void onGuiChange(ScreenOpenEvent event) { + private void onGuiChange(ScreenOpenEvent event) { reloadBlur(event.screen); } @Subscribe - public void onRenderTick(RenderEvent event) { + private void onRenderTick(RenderEvent event) { if (event.stage != Stage.END) { return; } @@ -113,7 +113,7 @@ public class BlurHandler { * one of many conditions are met, such as no current other shader * is being used, we actually have the blur setting enabled */ - public void reloadBlur(GuiScreen gui) { + private void reloadBlur(GuiScreen gui) { // Don't do anything if no world is loaded if (UMinecraft.getWorld() == null) { return; diff --git a/src/main/java/cc/polyfrost/oneconfig/mixin/GuiIngameForgeMixin.java b/src/main/java/cc/polyfrost/oneconfig/mixin/GuiIngameForgeMixin.java index 1774fdc..36ec90b 100644 --- a/src/main/java/cc/polyfrost/oneconfig/mixin/GuiIngameForgeMixin.java +++ b/src/main/java/cc/polyfrost/oneconfig/mixin/GuiIngameForgeMixin.java @@ -8,10 +8,10 @@ import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -@Mixin(GuiIngameForge.class) +@Mixin(value = GuiIngameForge.class, remap = false) public class GuiIngameForgeMixin { - @Inject(method = "renderGameOverlay", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/client/GuiIngameForge;post(Lnet/minecraftforge/client/event/RenderGameOverlayEvent$ElementType;)V", shift = At.Shift.AFTER)) + @Inject(method = "renderGameOverlay", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/client/GuiIngameForge;post(Lnet/minecraftforge/client/event/RenderGameOverlayEvent$ElementType;)V", shift = At.Shift.AFTER, remap = false), remap = true) private void onRenderGameOverlay(float partialTicks, CallbackInfo ci) { - EventManager.INSTANCE.getEventBus().post(new HudRenderEvent(partialTicks)); + EventManager.INSTANCE.post(new HudRenderEvent(partialTicks)); } } diff --git a/src/main/java/cc/polyfrost/oneconfig/mixin/MinecraftMixin.java b/src/main/java/cc/polyfrost/oneconfig/mixin/MinecraftMixin.java index 05d2819..97b843a 100644 --- a/src/main/java/cc/polyfrost/oneconfig/mixin/MinecraftMixin.java +++ b/src/main/java/cc/polyfrost/oneconfig/mixin/MinecraftMixin.java @@ -19,30 +19,30 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; public class MinecraftMixin { @Shadow private Timer timer; - @Inject(method = "runGameLoop", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/fml/common/FMLCommonHandler;onRenderTickStart(F)V", shift = At.Shift.AFTER)) + @Inject(method = "runGameLoop", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/fml/common/FMLCommonHandler;onRenderTickStart(F)V", shift = At.Shift.AFTER, remap = false), remap = true) private void onRenderTickStart(CallbackInfo ci) { - EventManager.INSTANCE.getEventBus().post(new RenderEvent(Stage.START, timer.renderPartialTicks)); + EventManager.INSTANCE.post(new RenderEvent(Stage.START, timer.renderPartialTicks)); } - @Inject(method = "runGameLoop", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/fml/common/FMLCommonHandler;onRenderTickEnd(F)V", shift = At.Shift.AFTER)) + @Inject(method = "runGameLoop", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/fml/common/FMLCommonHandler;onRenderTickEnd(F)V", shift = At.Shift.AFTER, remap = false), remap = true) private void onRenderTickEnd(CallbackInfo ci) { - EventManager.INSTANCE.getEventBus().post(new RenderEvent(Stage.END, timer.renderPartialTicks)); + EventManager.INSTANCE.post(new RenderEvent(Stage.END, timer.renderPartialTicks)); } - @Inject(method = "runTick", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/fml/common/FMLCommonHandler;onPreClientTick()V", shift = At.Shift.AFTER)) + @Inject(method = "runTick", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/fml/common/FMLCommonHandler;onPreClientTick()V", shift = At.Shift.AFTER, remap = false), remap = true) private void onClientTickStart(CallbackInfo ci) { - EventManager.INSTANCE.getEventBus().post(new TickEvent(Stage.START)); + EventManager.INSTANCE.post(new TickEvent(Stage.START)); } - @Inject(method = "runTick", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/fml/common/FMLCommonHandler;onPostClientTick()V", shift = At.Shift.AFTER)) + @Inject(method = "runTick", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/fml/common/FMLCommonHandler;onPostClientTick()V", shift = At.Shift.AFTER, remap = false), remap = true) private void onClientTickEnd(CallbackInfo ci) { - EventManager.INSTANCE.getEventBus().post(new TickEvent(Stage.END)); + EventManager.INSTANCE.post(new TickEvent(Stage.END)); } - @ModifyExpressionValue(method = "displayGuiScreen", at = @At(value = "NEW", target = "Lnet/minecraftforge/client/event/GuiOpenEvent;<init>(Lnet/minecraft/client/gui/GuiScreen;)V")) + @ModifyExpressionValue(method = "displayGuiScreen", at = @At(value = "NEW", target = "Lnet/minecraftforge/client/event/GuiOpenEvent;<init>(Lnet/minecraft/client/gui/GuiScreen;)V", remap = false), remap = true) private GuiOpenEvent onGuiOpenEvent(GuiOpenEvent screen) { ScreenOpenEvent event = new ScreenOpenEvent(screen.gui); - EventManager.INSTANCE.getEventBus().post(event); + EventManager.INSTANCE.post(event); if (event.isCancelled) { screen.setCanceled(true); } diff --git a/src/main/java/cc/polyfrost/oneconfig/mixin/NetHandlerPlayClientMixin.java b/src/main/java/cc/polyfrost/oneconfig/mixin/NetHandlerPlayClientMixin.java new file mode 100644 index 0000000..c7056d5 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/mixin/NetHandlerPlayClientMixin.java @@ -0,0 +1,24 @@ +package cc.polyfrost.oneconfig.mixin; + +import cc.polyfrost.oneconfig.events.EventManager; +import cc.polyfrost.oneconfig.events.event.ChatReceiveEvent; +import net.minecraft.client.network.NetHandlerPlayClient; +import net.minecraft.network.play.server.S02PacketChat; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(NetHandlerPlayClient.class) +public class NetHandlerPlayClientMixin { + @Inject(method = "handleChat", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/event/ForgeEventFactory;onClientChat(BLnet/minecraft/util/IChatComponent;)Lnet/minecraft/util/IChatComponent;", remap = false), cancellable = true, remap = true) + private void onClientChat(S02PacketChat packetIn, CallbackInfo ci) { + if (packetIn.getType() == 0) { + ChatReceiveEvent event = new ChatReceiveEvent(packetIn.getChatComponent()); + EventManager.INSTANCE.post(event); + if (event.isCancelled) { + ci.cancel(); + } + } + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/mixin/WorldClientMixin.java b/src/main/java/cc/polyfrost/oneconfig/mixin/WorldClientMixin.java new file mode 100644 index 0000000..13a3821 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/mixin/WorldClientMixin.java @@ -0,0 +1,21 @@ +package cc.polyfrost.oneconfig.mixin; + +import cc.polyfrost.oneconfig.events.EventManager; +import cc.polyfrost.oneconfig.events.event.WorldLoadEvent; +import net.minecraft.client.multiplayer.WorldClient; +import net.minecraft.client.network.NetHandlerPlayClient; +import net.minecraft.profiler.Profiler; +import net.minecraft.world.EnumDifficulty; +import net.minecraft.world.WorldSettings; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(WorldClient.class) +public class WorldClientMixin { + @Inject(method = "<init>", at = @At("RETURN")) + private void onWorldLoad(NetHandlerPlayClient p_i45063_1_, WorldSettings p_i45063_2_, int p_i45063_3_, EnumDifficulty p_i45063_4_, Profiler p_i45063_5_, CallbackInfo ci) { + EventManager.INSTANCE.post(new WorldLoadEvent()); + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/plugin/asm/tweakers/VigilantTransformer.java b/src/main/java/cc/polyfrost/oneconfig/plugin/asm/tweakers/VigilantTransformer.java index 45dc1e2..fecdeed 100644 --- a/src/main/java/cc/polyfrost/oneconfig/plugin/asm/tweakers/VigilantTransformer.java +++ b/src/main/java/cc/polyfrost/oneconfig/plugin/asm/tweakers/VigilantTransformer.java @@ -93,12 +93,11 @@ public class VigilantTransformer implements ITransformer { public static VigilanceConfig returnNewConfig(Vigilant vigilant, File file) { if (vigilant != null && Minecraft.getMinecraft().isCallingFromMinecraftThread()) { String name = !vigilant.getGuiTitle().equals("Settings") ? vigilant.getGuiTitle() : Loader.instance().activeModContainer() == null ? "Unknown" : Loader.instance().activeModContainer().getName(); - String imageName = Loader.instance().activeModContainer() == null || Loader.instance().activeModContainer().getMetadata().logoFile.trim().equals("") ? null : "/" + Loader.instance().activeModContainer().getMetadata().logoFile; if (name.equals("OneConfig")) name = "Essential"; String finalName = name; // duplicate fix if (ConfigCore.oneConfigMods.stream().anyMatch(mod -> mod.name.equals(finalName))) return null; - return new VigilanceConfig(new Mod(name, ModType.THIRD_PARTY, imageName), file.getAbsolutePath(), vigilant); + return new VigilanceConfig(new Mod(name, ModType.THIRD_PARTY), file.getAbsolutePath(), vigilant); } else { return null; } diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/IOUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/IOUtils.java index 64cabfd..efb0c16 100644 --- a/src/main/java/cc/polyfrost/oneconfig/utils/IOUtils.java +++ b/src/main/java/cc/polyfrost/oneconfig/utils/IOUtils.java @@ -10,6 +10,9 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.file.Files; +/** + * Utility class for I/O operations. + */ public final class IOUtils { /** diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/InputUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/InputUtils.java index 86e6c86..0ba4c00 100644 --- a/src/main/java/cc/polyfrost/oneconfig/utils/InputUtils.java +++ b/src/main/java/cc/polyfrost/oneconfig/utils/InputUtils.java @@ -25,27 +25,78 @@ public final class InputUtils { return mouseX > x && mouseY > y && mouseX < x + width && mouseY < y + height; } + /** + * Checks whether the mouse is currently over a specific region and clicked. + * + * @param x the x position of the region + * @param y the y position of the region + * @param width the width of the region + * @param height the height of the region + * @param ignoreBlock if true, will ignore {@link InputUtils#blockClicks(boolean)} + * @return true if the mouse is clicked and is over the region, false if not + * @see InputUtils#isAreaHovered(int, int, int, int) + */ public static boolean isAreaClicked(int x, int y, int width, int height, boolean ignoreBlock) { return isAreaHovered(x, y, width, height) && isClicked(ignoreBlock); } + /** + * Checks whether the mouse is currently over a specific region and clicked. + * + * @param x the x position of the region + * @param y the y position of the region + * @param width the width of the region + * @param height the height of the region + * @return true if the mouse is clicked and is over the region, false if not + * @see InputUtils#isAreaClicked(int, int, int, int, boolean) + */ public static boolean isAreaClicked(int x, int y, int width, int height) { return isAreaClicked(x, y, width, height, false); } + /** + * Checks whether the mouse is clicked or not. + * + * @param ignoreBlock if true, will ignore {@link InputUtils#blockClicks(boolean)} + * @return true if the mouse is clicked, false if not + */ public static boolean isClicked(boolean ignoreBlock) { return OneConfigGui.INSTANCE != null && OneConfigGui.INSTANCE.mouseDown && !Mouse.isButtonDown(0) && (!blockClicks || ignoreBlock); } + /** + * Checks whether the mouse is clicked or not. + * + * @return true if the mouse is clicked, false if not + * @see InputUtils#isClicked(boolean) + */ public static boolean isClicked() { return isClicked(false); } + /** + * Gets the current mouse X position. + * <p> + * All values returned from this class are not scaled to Minecraft's GUI scale. + * For scaled values, see {@link cc.polyfrost.oneconfig.libs.universal.UMouse}. + * </p> + * + * @return the current mouse X position + */ public static int mouseX() { if (OneConfigGui.INSTANCE == null) return Mouse.getX(); return (int) (Mouse.getX() / OneConfigGui.INSTANCE.getScaleFactor()); } + /** + * Gets the current mouse Y position. + * <p> + * All values returned from this class are not scaled to Minecraft's GUI scale. + * For scaled values, see {@link cc.polyfrost.oneconfig.libs.universal.UMouse}. + * </p> + * + * @return the current mouse Y position + */ public static int mouseY() { if (OneConfigGui.INSTANCE == null) return UResolution.getWindowHeight() - Math.abs(Mouse.getY()); return (int) ((UResolution.getWindowHeight() - Math.abs(Mouse.getY())) / OneConfigGui.INSTANCE.getScaleFactor()); @@ -58,6 +109,10 @@ public final class InputUtils { blockClicks = value; } + /** + * Whether clicks are blocked + * @return true if clicks are blocked, false if not + */ public static boolean isBlockingClicks() { return blockClicks; } diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/JsonUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/JsonUtils.java index 4ea9ce2..67881e9 100644 --- a/src/main/java/cc/polyfrost/oneconfig/utils/JsonUtils.java +++ b/src/main/java/cc/polyfrost/oneconfig/utils/JsonUtils.java @@ -3,9 +3,24 @@ package cc.polyfrost.oneconfig.utils; import com.google.gson.JsonElement; import com.google.gson.JsonParser; +/** + * Various utility methods for working with JSON. + */ public final class JsonUtils { + + /** + * The instance of the parser. + */ public static final JsonParser PARSER = new JsonParser(); + /** + * Parses a string into a {@link JsonElement}. + * + * @param string The string to parse. + * @param catchExceptions Whether to catch exceptions. + * @return The {@link JsonElement}. + * @see JsonParser#parse(String) + */ public static JsonElement parseString(String string, boolean catchExceptions) { try { return PARSER.parse(string); @@ -18,6 +33,13 @@ public final class JsonUtils { } } + /** + * Parses a string into a {@link JsonElement}. + * + * @param string The string to parse. + * @return The {@link JsonElement}. + * @see JsonUtils#parseString(String, boolean) + */ public static JsonElement parseString(String string) { return parseString(string, true); } diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/Multithreading.java b/src/main/java/cc/polyfrost/oneconfig/utils/Multithreading.java new file mode 100644 index 0000000..518d699 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/utils/Multithreading.java @@ -0,0 +1,75 @@ +package cc.polyfrost.oneconfig.utils; + +import com.google.common.util.concurrent.ThreadFactoryBuilder; + +import java.util.concurrent.*; + +/** + * Allows for easy multithreading. + * <p> + * Taken from Seraph by Scherso under LGPL-2.1 + * <a href="https://github.com/Scherso/Seraph/blob/master/LICENSE">https://github.com/Scherso/Seraph/blob/master/LICENSE</a> + * </p> + */ +public class Multithreading { + private static final ExecutorService executorService = Executors.newCachedThreadPool(new ThreadFactoryBuilder().setNameFormat("OneConfig-%d").build()); + private static final ScheduledExecutorService runnableExecutor = new ScheduledThreadPoolExecutor(Runtime.getRuntime().availableProcessors() + 1); + + /** + * Runs the runnable asynchronously. + * + * @param runnable The runnable to run. + * @see Multithreading#submit(Runnable) + */ + public static void runAsync(Runnable runnable) { + submit(runnable); + } + + /** + * Runs the provided runnables asynchronously. + * + * @param runnables The runnables to run. + * @see Multithreading#runAsync(Runnable) + */ + public static void runAsync(Runnable... runnables) { + for (Runnable runnable : runnables) { + runAsync(runnable); + } + } + + /** + * Submits the Runnable to the executor, making it run asynchronously. + * + * @param runnable The runnable to run. + * @return The future representing the submitted runnable. + * @see ExecutorService#submit(Runnable) + */ + public static Future<?> submit(Runnable runnable) { + return executorService.submit(runnable); + } + + /** + * Schedules the runnable to run asynchronously after the specified delay. + * + * @param runnable The runnable to run. + * @param delay The delay before the runnable is run. + * @param timeUnit The {@link TimeUnit} of the delay. + * @see Multithreading#submitScheduled(Runnable, long, TimeUnit) + */ + public static void schedule(Runnable runnable, long delay, TimeUnit timeUnit) { + submitScheduled(runnable, delay, timeUnit); + } + + /** + * Submits the Runnable to the executor after a delay, making it run asynchronously. + * + * @param runnable The runnable to run. + * @param delay The delay before the runnable is run. + * @param timeUnit The {@link TimeUnit} of the delay. + * @return The future representing the submitted runnable. + * @see ScheduledExecutorService#schedule(Runnable, long, TimeUnit) + */ + public static ScheduledFuture<?> submitScheduled(Runnable runnable, long delay, TimeUnit timeUnit) { + return runnableExecutor.schedule(runnable, delay, timeUnit); + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/NetworkUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/NetworkUtils.java index 34d4382..767f36f 100644 --- a/src/main/java/cc/polyfrost/oneconfig/utils/NetworkUtils.java +++ b/src/main/java/cc/polyfrost/oneconfig/utils/NetworkUtils.java @@ -11,18 +11,19 @@ import java.net.URL; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; +/** + * Utility class for accessing the internet. + */ public final class NetworkUtils { - private static InputStream setupConnection(String url, String userAgent, int timeout, boolean useCaches) throws IOException { - HttpURLConnection connection = ((HttpURLConnection) new URL(url).openConnection()); - connection.setRequestMethod("GET"); - connection.setUseCaches(useCaches); - connection.addRequestProperty("User-Agent", userAgent); - connection.setReadTimeout(timeout); - connection.setConnectTimeout(timeout); - connection.setDoOutput(true); - return connection.getInputStream(); - } + /** + * Gets the contents of a URL as a String. + * @param url The URL to read. + * @param userAgent The user agent to use. + * @param timeout The timeout in milliseconds. + * @param useCaches Whether to use caches. + * @return The contents of the URL. + */ public static String getString(String url, String userAgent, int timeout, boolean useCaches) { try (InputStreamReader input = new InputStreamReader(setupConnection(url, userAgent, timeout, useCaches), StandardCharsets.UTF_8)) { return IOUtils.toString(input); @@ -32,23 +33,52 @@ public final class NetworkUtils { } } + /** + * Gets the contents of a URL as a String. + * + * @param url The URL to read. + * @return The contents of the URL. + * @see NetworkUtils#getString(String, String, int, boolean) + */ public static String getString(String url) { return getString(url, "OneConfig/1.0.0", 5000, false); } + /** + * Gets the contents of a URL as a JsonElement. + * + * @param url The URL to read. + * @param userAgent The user agent to use. + * @param timeout The timeout in milliseconds. + * @param useCaches Whether to use caches. + * @return The contents of the URL. + * @see NetworkUtils#getString(String, String, int, boolean) + * @see JsonUtils#parseString(String) + */ public static JsonElement getJsonElement(String url, String userAgent, int timeout, boolean useCaches) { return JsonUtils.parseString(getString(url, userAgent, timeout, useCaches)); } + /** + * Gets the contents of a URL as a JsonElement. + * + * @param url The URL to read. + * @return The contents of the URL. + * @see NetworkUtils#getJsonElement(String, String, int, boolean) + */ public static JsonElement getJsonElement(String url) { return getJsonElement(url, "OneConfig/1.0.0", 5000, false); } - - public static boolean downloadFile(String url, File file) { - return downloadFile(url, file, "OneConfig/1.0.0", 5000, false); - } - + /** + * Downloads a file from a URL. + * @param url The URL to download from. + * @param file The file to download to. + * @param userAgent The user agent to use. + * @param timeout The timeout in milliseconds. + * @param useCaches Whether to use caches. + * @return Whether the download was successful. + */ public static boolean downloadFile(String url, File file, String userAgent, int timeout, boolean useCaches) { url = url.replace(" ", "%20"); try (FileOutputStream fileOut = new FileOutputStream(file); BufferedInputStream in = new BufferedInputStream(setupConnection(url, userAgent, timeout, useCaches))) { @@ -60,8 +90,24 @@ public final class NetworkUtils { return true; } - public static String getFileChecksum(String filename) { - try (FileInputStream inputStream = new FileInputStream(filename)) { + /** + * Downloads a file from a URL. + * @param url The URL to download from. + * @param file The file to download to. + * @return Whether the download was successful. + * @see NetworkUtils#downloadFile(String, File, String, int, boolean) + */ + public static boolean downloadFile(String url, File file) { + return downloadFile(url, file, "OneConfig/1.0.0", 5000, false); + } + + /** + * Gets the SHA-256 hash of a file. + * @param file The file to hash. + * @return The SHA-256 hash of the file. + */ + public static String getFileChecksum(File file) { + try (FileInputStream inputStream = new FileInputStream(file)) { MessageDigest digest = MessageDigest.getInstance("SHA-256"); byte[] bytesBuffer = new byte[1024]; int bytesRead; @@ -77,6 +123,28 @@ public final class NetworkUtils { return ""; } + /** + * Launches a URL in the default browser. + * + * @param uri The URI to launch. + * @see UDesktop#browse(URI) + * @see java.awt.Desktop#browse(URI) + */ + public static void browseLink(String uri) { + UDesktop.browse(URI.create(uri)); + } + + private static InputStream setupConnection(S |
