From bc9fd11f8fb4712a1278b742922880cdf5b31c1d Mon Sep 17 00:00:00 2001 From: kirillsaint Date: Wed, 1 Nov 2023 22:40:41 +0600 Subject: [PATCH] Better News --- .../java/net/silentclient/client/Client.java | 10 +- .../silentclient/client/gui/lite/GuiNews.java | 67 +++++---- .../client/gui/lite/LiteMainMenu.java | 25 ++-- .../gui/silentmainmenu/MainMenuConcept.java | 25 ++-- .../gui/silentmainmenu/SilentMainMenu.java | 17 ++- .../client/utils/PromoController.java | 128 ++++++++++++++++++ 6 files changed, 206 insertions(+), 66 deletions(-) create mode 100644 src/main/java/net/silentclient/client/utils/PromoController.java diff --git a/src/main/java/net/silentclient/client/Client.java b/src/main/java/net/silentclient/client/Client.java index b26ce66..2a02ccf 100644 --- a/src/main/java/net/silentclient/client/Client.java +++ b/src/main/java/net/silentclient/client/Client.java @@ -309,13 +309,13 @@ public class Client { Client.logger.info("STARTING > launching-detector"); Requests.post("https://api.silentclient.net/_next/launch_v2", new JSONObject().put("branch", getBuildData().getBranch()).toString()); - logger.info("STARTING > news"); - String content = Requests.get("https://api.silentclient.net/_next/news/latest"); - if(content != null) { + logger.info("STARTING > promo"); + String panelContent = Requests.get("https://assets.silentclient.net/client/promo.json"); + if(panelContent != null) { try { - NewsResponse newsResponse = getGson().fromJson(content, NewsResponse.class); + PromoController.PromoResponse promoResponse = getGson().fromJson(panelContent, PromoController.PromoResponse.class); - GuiNews.news = newsResponse; + PromoController.setResponse(promoResponse); } catch (Exception err) { logger.catching(err); } diff --git a/src/main/java/net/silentclient/client/gui/lite/GuiNews.java b/src/main/java/net/silentclient/client/gui/lite/GuiNews.java index fa9c126..2a29b8f 100644 --- a/src/main/java/net/silentclient/client/gui/lite/GuiNews.java +++ b/src/main/java/net/silentclient/client/gui/lite/GuiNews.java @@ -5,7 +5,6 @@ import net.minecraft.client.gui.Gui; import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.ScaledResolution; import net.minecraft.client.renderer.GlStateManager; -import net.minecraft.client.renderer.texture.DynamicTexture; import net.minecraft.util.ResourceLocation; import net.silentclient.client.Client; import net.silentclient.client.gui.SilentScreen; @@ -13,25 +12,17 @@ import net.silentclient.client.gui.elements.Button; import net.silentclient.client.gui.lite.clickgui.utils.MouseUtils; import net.silentclient.client.gui.util.RenderUtil; import net.silentclient.client.utils.MouseCursorHandler; -import net.silentclient.client.utils.SCTextureManager; +import net.silentclient.client.utils.PromoController; import net.silentclient.client.utils.TimerUtils; -import net.silentclient.client.utils.types.NewsResponse; import java.awt.*; -import java.awt.image.BufferedImage; import java.io.IOException; import java.net.URI; -import java.util.Random; public class GuiNews extends SilentScreen { - public static ResourceLocation imageLocation = null; - public static BufferedImage image = null; - public static boolean loading = false; - public static boolean initPanel = false; private TimerUtils logoTimer = new TimerUtils(); private int logoY = 0; private final boolean firstTime; - public static NewsResponse news; public GuiNews() { this.firstTime = false; @@ -41,25 +32,9 @@ public class GuiNews extends SilentScreen { this.firstTime = firstTime; } - public static void loadPanel() { - if(image == null && !loading && news != null) { - loading = true; - (new Thread("NewsThread") { - public void run() { - GuiNews.image = SCTextureManager.getImage(news.getItem().getCover()); - GuiNews.loading = false; - } - }).start(); - } - if(image != null && !initPanel) { - imageLocation = Minecraft.getMinecraft().getTextureManager().getDynamicTextureLocation("image_panel_" + new Random().nextLong(), new DynamicTexture(image)); - initPanel = true; - } - } - @Override public void drawScreen(int mouseX, int mouseY, float partialTicks) { - loadPanel(); + PromoController.getResponse().update(); MouseCursorHandler.CursorType cursorType = getCursor(silentInputs, buttonList); GlStateManager.disableAlpha(); Client.backgroundPanorama.renderSkybox(mouseX, mouseY, partialTicks); @@ -71,11 +46,23 @@ public class GuiNews extends SilentScreen { } GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F); - if(imageLocation != null) { - RenderUtil.drawImage(imageLocation, (this.width / 2) - (300 / 2), (this.height / 2) - (168 / 2) - 10, 300, 168, false); - if(MouseUtils.isInside(mouseX, mouseY, (this.width / 2) - (300 / 2), (this.height / 2) - (168 / 2) - 10, 300, 168)) { + if(PromoController.getResponse().getPanels().size() > 1) { + if(MouseUtils.isInside(mouseX, mouseY, 40, this.height / 2 - 10, 20, 20) || MouseUtils.isInside(mouseX, mouseY, this.width - 40 - 20, this.height / 2 - 10, 20, 20)) { cursorType = MouseCursorHandler.CursorType.POINTER; } + RenderUtil.drawImage(new ResourceLocation("silentclient/icons/back.png"), 40, this.height / 2 - 10, 20, 20); + RenderUtil.drawImage(new ResourceLocation("silentclient/icons/next.png"), this.width - 40 - 20, this.height / 2 - 10, 20, 20); + } + + if(PromoController.getResponse().getCurrentPanel() != null) { + if(PromoController.getResponse().getCurrentPanel().getImageLocation() != null) { + RenderUtil.drawImage(PromoController.getResponse().getCurrentPanel().getImageLocation(), (this.width / 2) - (300 / 2), (this.height / 2) - (168 / 2) - 10, 300, 168, false); + if(MouseUtils.isInside(mouseX, mouseY, (this.width / 2) - (300 / 2), (this.height / 2) - (168 / 2) - 10, 300, 168)) { + cursorType = MouseCursorHandler.CursorType.POINTER; + } + } else { + PromoController.getResponse().getCurrentPanel().loadImage(); + } } super.drawScreen(mouseX, mouseY, partialTicks); @@ -100,6 +87,7 @@ public class GuiNews extends SilentScreen { Client.backgroundPanorama.updateWidthHeight(this.width, this.height); this.buttonList.add(new Button(1, (this.width / 2) - (98 / 2), this.height - 30, 98, 20, "Exit")); super.initGui(); + PromoController.getResponse().setPanelIndex(0); } @Override @@ -112,11 +100,22 @@ public class GuiNews extends SilentScreen { @Override protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException { super.mouseClicked(mouseX, mouseY, mouseButton); - if(MouseUtils.isInside(mouseX, mouseY, (this.width / 2) - (300 / 2), (this.height / 2) - (168 / 2) - 10, 300, 168) && news != null) { - try { + if(PromoController.getResponse().getPanels().size() > 1) { + if(MouseUtils.isInside(mouseX, mouseY, 40, this.height / 2 - 10, 20, 20)) { + PromoController.getResponse().prevPanel(); + return; + } + + if(MouseUtils.isInside(mouseX, mouseY, this.width - 40 - 20, this.height / 2 - 10, 20, 20)) { + PromoController.getResponse().nextPanel(); + return; + } + } + if(MouseUtils.isInside(mouseX, mouseY, (this.width / 2) - (300 / 2), (this.height / 2) - (168 / 2) - 10, 300, 168) && PromoController.getResponse().getCurrentPanel() != null && PromoController.getResponse().getCurrentPanel().getImageLocation() != null) { + try { Class oclass = Class.forName("java.awt.Desktop"); - Object object = oclass.getMethod("getDesktop", new Class[0]).invoke((Object)null, new Object[0]); - oclass.getMethod("browse", new Class[] {URI.class}).invoke(object, new Object[] {new URI("https://silentclient.net/news/" + GuiNews.news.getItem().id)}); + Object object = oclass.getMethod("getDesktop", new Class[0]).invoke(null, new Object[0]); + oclass.getMethod("browse", new Class[] {URI.class}).invoke(object, new Object[] {new URI(PromoController.getResponse().getCurrentPanel().getRedirectUrl())}); } catch (Throwable err) { err.printStackTrace(); } diff --git a/src/main/java/net/silentclient/client/gui/lite/LiteMainMenu.java b/src/main/java/net/silentclient/client/gui/lite/LiteMainMenu.java index 1f8b1bf..4dece1a 100644 --- a/src/main/java/net/silentclient/client/gui/lite/LiteMainMenu.java +++ b/src/main/java/net/silentclient/client/gui/lite/LiteMainMenu.java @@ -16,6 +16,7 @@ import net.silentclient.client.gui.silentmainmenu.components.AccountPicker; import net.silentclient.client.gui.util.RenderUtil; import net.silentclient.client.utils.ColorUtils; import net.silentclient.client.utils.MouseCursorHandler; +import net.silentclient.client.utils.PromoController; import java.awt.*; import java.io.IOException; @@ -56,7 +57,7 @@ public class LiteMainMenu extends SilentScreen @Override public void drawScreen(int mouseX, int mouseY, float partialTicks) { - GuiNews.loadPanel(); + PromoController.getResponse().update(); MouseCursorHandler.CursorType cursorType = getCursor(silentInputs, buttonList); GlStateManager.disableAlpha(); Client.backgroundPanorama.renderSkybox(mouseX, mouseY, partialTicks); @@ -74,14 +75,18 @@ public class LiteMainMenu extends SilentScreen ColorUtils.setColor(-1); Client.getInstance().getSilentFontRenderer().drawString(3, height - 14, "SLC Lite 1.8.9", 12, SilentFontRenderer.FontType.TITLE); - if(GuiNews.imageLocation != null) { - if(MouseUtils.isInside(mouseX, mouseY, this.width - 74, this.height - 42, 71, 40)) { - cursorType = MouseCursorHandler.CursorType.POINTER; - this.bannerAnimation.setAnimation(4, 20); + if(PromoController.getResponse().getCurrentPanel() != null) { + if(PromoController.getResponse().getCurrentPanel().getImageLocation() != null) { + if(MouseUtils.isInside(mouseX, mouseY, this.width - 74, this.height - 42, 71, 40)) { + cursorType = MouseCursorHandler.CursorType.POINTER; + this.bannerAnimation.setAnimation(4, 20); + } else { + this.bannerAnimation.setAnimation(0, 30); + } + RenderUtil.drawImage(PromoController.getResponse().getCurrentPanel().getImageLocation(), this.width - 74 + (-this.bannerAnimation.getValue()), this.height - 42 + (-this.bannerAnimation.getValue()), 71, 40, false); } else { - this.bannerAnimation.setAnimation(0, 30); + PromoController.getResponse().getCurrentPanel().loadImage(); } - RenderUtil.drawImage(GuiNews.imageLocation, this.width - 74 + (-this.bannerAnimation.getValue()), this.height - 42 + (-this.bannerAnimation.getValue()), 71, 40, false); } MouseCursorHandler.CursorType accountCursor = accountPicker.draw(mc, mouseX, mouseY); @@ -140,11 +145,11 @@ public class LiteMainMenu extends SilentScreen @Override protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException { super.mouseClicked(mouseX, mouseY, mouseButton); - if(MouseUtils.isInside(mouseX, mouseY, this.width - 74, this.height - 42, 71, 40)) { + if(MouseUtils.isInside(mouseX, mouseY, this.width - 74, this.height - 42, 71, 40) && PromoController.getResponse().getCurrentPanel() != null && PromoController.getResponse().getCurrentPanel().getImageLocation() != null) { try { Class oclass = Class.forName("java.awt.Desktop"); - Object object = oclass.getMethod("getDesktop", new Class[0]).invoke((Object)null, new Object[0]); - oclass.getMethod("browse", new Class[] {URI.class}).invoke(object, new Object[] {new URI("https://silentclient.net/news/" + GuiNews.news.getItem().id)}); + Object object = oclass.getMethod("getDesktop", new Class[0]).invoke(null, new Object[0]); + oclass.getMethod("browse", new Class[] {URI.class}).invoke(object, new Object[] {new URI(PromoController.getResponse().getCurrentPanel().getRedirectUrl())}); } catch (Throwable err) { err.printStackTrace(); } diff --git a/src/main/java/net/silentclient/client/gui/silentmainmenu/MainMenuConcept.java b/src/main/java/net/silentclient/client/gui/silentmainmenu/MainMenuConcept.java index 5b2bdba..9111640 100644 --- a/src/main/java/net/silentclient/client/gui/silentmainmenu/MainMenuConcept.java +++ b/src/main/java/net/silentclient/client/gui/silentmainmenu/MainMenuConcept.java @@ -13,7 +13,6 @@ import net.silentclient.client.cosmetics.gui.CosmeticsGui; import net.silentclient.client.gui.SilentScreen; import net.silentclient.client.gui.elements.IconButton; import net.silentclient.client.gui.font.SilentFontRenderer; -import net.silentclient.client.gui.lite.GuiNews; import net.silentclient.client.gui.lite.LiteMainMenu; import net.silentclient.client.gui.lite.clickgui.utils.MouseUtils; import net.silentclient.client.gui.lite.clickgui.utils.RenderUtils; @@ -22,6 +21,7 @@ import net.silentclient.client.gui.silentmainmenu.components.MenuOption; import net.silentclient.client.gui.theme.button.IButtonTheme; import net.silentclient.client.gui.util.RenderUtil; import net.silentclient.client.utils.MouseCursorHandler; +import net.silentclient.client.utils.PromoController; import net.silentclient.client.utils.SCTextureManager; import java.awt.*; @@ -79,7 +79,6 @@ public class MainMenuConcept extends SilentScreen { @Override public void drawScreen(int mouseX, int mouseY, float partialTicks) { - GuiNews.loadPanel(); MouseCursorHandler.CursorType cursorType = getCursor(silentInputs, buttonList); this.loadSkin(); GlStateManager.disableAlpha(); @@ -93,12 +92,18 @@ public class MainMenuConcept extends SilentScreen { RenderUtil.drawImage(new ResourceLocation("silentclient/images/steve.png"), width - 120, height / 2 - (75), 62, 150, false); } - if(GuiNews.imageLocation != null) { - if(MouseUtils.isInside(mouseX, mouseY, 10, 10, 109, 63)) { - cursorType = MouseCursorHandler.CursorType.POINTER; + PromoController.getResponse().update(); + + if(PromoController.getResponse().getCurrentPanel() != null) { + if(PromoController.getResponse().getCurrentPanel().getImageLocation() != null) { + if(MouseUtils.isInside(mouseX, mouseY, 10, 10, 109, 63)) { + cursorType = MouseCursorHandler.CursorType.POINTER; + } + RenderUtils.drawRect(10, 10, 109, 63, -1); + RenderUtil.drawImage(PromoController.getResponse().getCurrentPanel().getImageLocation(), 12, 12, 105, 59, false); + } else { + PromoController.getResponse().getCurrentPanel().loadImage(); } - RenderUtils.drawRect(10, 10, 109, 63, -1); - RenderUtil.drawImage(GuiNews.imageLocation, 12, 12, 105, 59, false); } Client.getInstance().getSilentFontRenderer().drawString(3, height - 14, "Silent Client 1.8.9", 12, SilentFontRenderer.FontType.TITLE); @@ -153,11 +158,11 @@ public class MainMenuConcept extends SilentScreen { @Override protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException { super.mouseClicked(mouseX, mouseY, mouseButton); - if(MouseUtils.isInside(mouseX, mouseY, 10, 10, 109, 63)) { + if(MouseUtils.isInside(mouseX, mouseY, 10, 10, 109, 63) && PromoController.getResponse().getCurrentPanel() != null) { try { Class oclass = Class.forName("java.awt.Desktop"); - Object object = oclass.getMethod("getDesktop", new Class[0]).invoke((Object)null, new Object[0]); - oclass.getMethod("browse", new Class[] {URI.class}).invoke(object, new Object[] {new URI("https://silentclient.net/news/" + GuiNews.news.getItem().id)}); + Object object = oclass.getMethod("getDesktop", new Class[0]).invoke(null, new Object[0]); + oclass.getMethod("browse", new Class[] {URI.class}).invoke(object, new Object[] {new URI(PromoController.getResponse().getCurrentPanel().getRedirectUrl())}); } catch (Throwable err) { err.printStackTrace(); } diff --git a/src/main/java/net/silentclient/client/gui/silentmainmenu/SilentMainMenu.java b/src/main/java/net/silentclient/client/gui/silentmainmenu/SilentMainMenu.java index 2c1ae1e..6661a78 100644 --- a/src/main/java/net/silentclient/client/gui/silentmainmenu/SilentMainMenu.java +++ b/src/main/java/net/silentclient/client/gui/silentmainmenu/SilentMainMenu.java @@ -7,11 +7,11 @@ import net.silentclient.client.Client; import net.silentclient.client.cosmetics.gui.CosmeticsGui; import net.silentclient.client.gui.SilentScreen; import net.silentclient.client.gui.font.SilentFontRenderer; -import net.silentclient.client.gui.lite.GuiNews; import net.silentclient.client.gui.lite.LiteMainMenu; import net.silentclient.client.gui.lite.clickgui.utils.MouseUtils; import net.silentclient.client.gui.lite.clickgui.utils.RenderUtils; import net.silentclient.client.gui.util.RenderUtil; +import net.silentclient.client.utils.PromoController; import java.awt.*; import java.io.IOException; @@ -31,7 +31,7 @@ public class SilentMainMenu extends SilentScreen { @Override public void drawScreen(int mouseX, int mouseY, float partialTicks) { - GuiNews.loadPanel(); + PromoController.getResponse().update(); GlStateManager.disableAlpha(); Client.backgroundPanorama.renderSkybox(mouseX, mouseY, partialTicks); GlStateManager.enableAlpha(); @@ -51,8 +51,12 @@ public class SilentMainMenu extends SilentScreen { blockY = 70; } RenderUtils.drawRect(blockX, blockY, 320, 150, new Color(20, 20, 20).getRGB()); - if(GuiNews.imageLocation != null) { - RenderUtil.drawImage(GuiNews.imageLocation, blockX + 78, blockY + 10, 231, 130, false); + if(PromoController.getResponse().getCurrentPanel() != null) { + if(PromoController.getResponse().getCurrentPanel().getImageLocation() != null) { + RenderUtil.drawImage(PromoController.getResponse().getCurrentPanel().getImageLocation(), blockX + 78, blockY + 10, 231, 130, false); + } else { + PromoController.getResponse().getCurrentPanel().loadImage(); + } } int optionY = blockY + 10; @@ -121,12 +125,11 @@ public class SilentMainMenu extends SilentScreen { if(blockY < 70) { blockY = 70; } - - if(MouseUtils.isInside(mouseX, mouseY, blockX + 78, blockY + 10, 231, 130)) { + if(MouseUtils.isInside(mouseX, mouseY, blockX + 78, blockY + 10, 231, 130) && PromoController.getResponse().getCurrentPanel() != null) { try { Class oclass = Class.forName("java.awt.Desktop"); Object object = oclass.getMethod("getDesktop", new Class[0]).invoke((Object)null, new Object[0]); - oclass.getMethod("browse", new Class[] {URI.class}).invoke(object, new Object[] {new URI("https://silentclient.net/news/" + GuiNews.news.getItem().id)}); + oclass.getMethod("browse", new Class[] {URI.class}).invoke(object, new Object[] {new URI(PromoController.getResponse().getCurrentPanel().getRedirectUrl())}); } catch (Throwable err) { err.printStackTrace(); } diff --git a/src/main/java/net/silentclient/client/utils/PromoController.java b/src/main/java/net/silentclient/client/utils/PromoController.java new file mode 100644 index 0000000..fa246f8 --- /dev/null +++ b/src/main/java/net/silentclient/client/utils/PromoController.java @@ -0,0 +1,128 @@ +package net.silentclient.client.utils; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.texture.DynamicTexture; +import net.minecraft.util.ResourceLocation; +import net.silentclient.client.Client; + +import java.awt.image.BufferedImage; +import java.util.ArrayList; +import java.util.Random; + +public class PromoController { + public static PromoResponse promo; + public static boolean loading = false; + + public static PromoResponse getResponse() { + return promo; + } + + public static void setResponse(PromoResponse promo) { + PromoController.promo = promo; + } + + public class PromoResponse { + public ArrayList panels; + private int panelIndex = 0; + private TimerUtils timer; + + public int getPanelIndex() { + return panelIndex; + } + + public void update() { + if(timer == null) { + timer = new TimerUtils(); + } + if(timer.delay(6000)) { + nextPanel(); + } + } + + public void setPanelIndex(int panelIndex) { + if(timer == null) { + timer = new TimerUtils(); + } + timer.reset(); + this.panelIndex = panelIndex; + } + + public void prevPanel() { + if(timer == null) { + timer = new TimerUtils(); + } + timer.reset(); + if(panelIndex == 0) { + this.panelIndex = PromoController.getResponse().getPanels().size() - 1; + } else { + this.panelIndex -= 1; + } + } + + public void nextPanel() { + if(timer == null) { + timer = new TimerUtils(); + } + timer.reset(); + if(panelIndex == PromoController.getResponse().getPanels().size() - 1) { + this.panelIndex = 0; + } else { + this.panelIndex += 1; + } + } + + public Panel getCurrentPanel() { + return panels.get(panelIndex) != null ? panels.get(panelIndex) : null; + } + + public ArrayList getPanels() { + return panels; + } + } + + public class Panel { + public String imageUrl; + public String redirectUrl; + + public ResourceLocation imageLocation; + public BufferedImage image = null; + public boolean initImage = false; + + public void loadImage() { + if(image == null && !PromoController.loading && !initImage) { + PromoController.loading = true; + (new Thread("PromoImageThread") { + public void run() { + Client.logger.info(String.format("Downloading promo image %s...", imageUrl)); + BufferedImage avatar = SCTextureManager.getImage(imageUrl); + if(avatar != null) { + Client.logger.info(String.format("Promo image %s successfully downloaded!", imageUrl)); + image = avatar; + } else { + Client.logger.info(String.format("Promo image %s downloading failed!", imageUrl)); + initImage = true; + } + PromoController.loading = false; + } + }).start(); + } + if(image != null && !initImage) { + Client.logger.info(String.format("Crating ResourceLocation of promo image %s...", imageUrl)); + imageLocation = Minecraft.getMinecraft().getTextureManager().getDynamicTextureLocation("promo_image_" + new Random().nextLong(), new DynamicTexture(image)); + initImage = true; + } + } + + public ResourceLocation getImageLocation() { + return imageLocation; + } + + public String getImageUrl() { + return imageUrl; + } + + public String getRedirectUrl() { + return redirectUrl; + } + } +}