diff --git a/build.gradle b/build.gradle index 60d0a49..45d1550 100644 --- a/build.gradle +++ b/build.gradle @@ -11,7 +11,7 @@ apply plugin: 'net.minecraftforge.gradle.forge' //Only edit below this line, the above code adds and enables the necessary things for Forge to be setup. -version = "1.3" +version = "1.4" group = "ch.m4th1eu.richpresence" // http://maven.apache.org/guides/mini/guide-naming-conventions.html archivesBaseName = "richpresence" diff --git a/src/main/java/ch/m4th1eu/richpresence/Main.java b/src/main/java/ch/m4th1eu/richpresence/Main.java index 5a831ff..e58d1ae 100644 --- a/src/main/java/ch/m4th1eu/richpresence/Main.java +++ b/src/main/java/ch/m4th1eu/richpresence/Main.java @@ -10,13 +10,13 @@ import net.minecraftforge.fml.common.Mod.EventHandler; import net.minecraftforge.fml.common.SidedProxy; import net.minecraftforge.fml.common.event.FMLInitializationEvent; import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; +import org.apache.commons.io.IOUtils; import org.apache.logging.log4j.Logger; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.PrintWriter; -import java.io.UnsupportedEncodingException; -import java.net.URISyntaxException; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; /** * @author M4TH1EU_#0001 @@ -25,7 +25,7 @@ import java.net.URISyntaxException; public class Main { public static final String MODID = "richpresence"; public static final String NAME = "Discord Rich Presence"; - public static final String VERSION = "1.3"; + public static final String VERSION = "1.4"; @Mod.Instance(Main.MODID) public static Main instance; @@ -37,6 +37,8 @@ public class Main { * Variables pour la config */ public static String applicationId, largeimage, largeimagetext; + public static String config_file_text = ""; + public static JSONObject config_object; public static Logger logger; @@ -50,60 +52,21 @@ public class Main { //Configuration event.getModConfigurationDirectory().mkdir(); - File config_file = null; + + InputStream in = getClass().getResourceAsStream("/config/richpresence.json"); + BufferedReader reader = new BufferedReader(new InputStreamReader(in)); + try { - config_file = new File(getClass().getResource("/config/richpresence.json").toURI()); - } catch (URISyntaxException e) { + config_file_text = IOUtils.toString(reader); + config_object = new JSONObject(config_file_text); + reader.close(); + } catch (IOException e) { e.printStackTrace(); } - if (!config_file.exists() || config_file.length() < 10) { - try { - event.getModLog().warn("Impossible de charger la configuration du mod : " + Main.MODID); - event.getModLog().warn("Création du fichier de configuration"); + if (config_file_text != null || config_file_text.length() < 10) { + event.getModLog().warn("Impossible de charger la configuration du mod : " + Main.MODID); - PrintWriter writer = new PrintWriter(config_file, "UTF-8"); - writer.println("{\n" + - " \"_comment\": \"Variables disponibles :\",\n" + - " \"_comment2\": \"%player-name% - Nom du joueur.\",\n" + - " \"_comment3\": \"%server-connected-player% - Nombre de joueur connecté au serveur.\",\n" + - " \"_comment4\": \"%server-max-slot% - Nombre de slots du serveur\",\n" + - " \"server-ip\": \"mc.hypixel.net\",\n" + - " \"server-port\": \"25565\",\n" + - " \"application-settings\": {\n" + - " \"applicationID\": \"601875975533232158\",\n" + - " \"large-image-name\": \"discord_logo\",\n" + - " \"large-image-text\": \"En train de tester ce mod !\"\n" + - " },\n" + - " \"advanced-status-custom\": {\n" + - " \"onJoinServer\": {\n" + - " \"enable\": true,\n" + - " \"message\": \"En jeu.\"\n" + - " },\n" + - " \"onQuitServer\": {\n" + - " \"enable\": true,\n" + - " \"message\": \"Dans le menu principal.\"\n" + - " },\n" + - " \"inPauseMenu\": {\n" + - " \"enable\": true,\n" + - " \"message\": \"Dans le menu pause.\"\n" + - " },\n" + - " \"inMainMenu\": {\n" + - " \"enable\": true,\n" + - " \"message\": \"Dans le menu principal.\"\n" + - " },\n" + - " \"inInventory\": {\n" + - " \"enable\": false,\n" + - " \"message\": \"Dans l'inventaire.\"\n" + - " }\n" + - " }\n" + - "}"); - writer.close(); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - } } } @@ -112,24 +75,18 @@ public class Main { MinecraftForge.EVENT_BUS.register(instance); MinecraftForge.EVENT_BUS.register(new AdvancedStatusEvent()); - JSONObject config = null; - try { - config = new JSONObject(Utils.readFileToString(new File(getClass().getResource("/config/richpresence.json").toURI()))); - } catch (URISyntaxException e) { - e.printStackTrace(); - } - applicationId = config.getJSONObject("application-settings").getString("applicationID"); - largeimage = config.getJSONObject("application-settings").getString("large-image-name"); - largeimagetext = Utils.instance.replaceArgsString(config.getJSONObject("application-settings").getString("large-image-text")); + applicationId = config_object.getJSONObject("application-settings").getString("applicationID"); + largeimage = config_object.getJSONObject("application-settings").getString("large-image-name"); + largeimagetext = Utils.instance.replaceArgsString(config_object.getJSONObject("application-settings").getString("large-image-text")); proxy.init(); rpcClient = new EventPresence(); proxy.rpcinit(); - if (config.getJSONObject("advanced-status-custom").getJSONObject("inMainMenu").getBoolean("enable")) { - proxy.rpcupdate(config.getJSONObject("advanced-status-custom").getJSONObject("inMainMenu").getString("message"), null, true); + if (config_object.getJSONObject("advanced-status-custom").getJSONObject("inMainMenu").getBoolean("enable")) { + proxy.rpcupdate(config_object.getJSONObject("advanced-status-custom").getJSONObject("inMainMenu").getString("message"), null, true); } else { proxy.rpcupdate("", null, false); } diff --git a/src/main/java/ch/m4th1eu/richpresence/Utils.java b/src/main/java/ch/m4th1eu/richpresence/Utils.java index fb51ade..9a663e6 100644 --- a/src/main/java/ch/m4th1eu/richpresence/Utils.java +++ b/src/main/java/ch/m4th1eu/richpresence/Utils.java @@ -3,8 +3,10 @@ package ch.m4th1eu.richpresence; import ch.m4th1eu.json.JSONObject; import net.minecraft.client.Minecraft; -import java.io.*; -import java.net.URISyntaxException; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import java.nio.charset.Charset; @@ -13,40 +15,6 @@ public class Utils { public static final Utils instance = new Utils(); - private JSONObject config; - - { - try { - config = new JSONObject(Utils.readFileToString(new File(getClass().getResource("/config/richpresence.json").toURI()))); - } catch (URISyntaxException e) { - e.printStackTrace(); - } - } - - - /** - * @author Nathanaël#4314 - */ - public static String readFileToString(File file) { - try { - BufferedReader reader = new BufferedReader(new FileReader(file)); - StringBuilder stringBuilder = new StringBuilder(); - String line; - - String ls = System.getProperty("line.separator"); - while ((line = reader.readLine()) != null) { - stringBuilder.append(line); - stringBuilder.append(ls); - } - stringBuilder.deleteCharAt(stringBuilder.length() - 1); - reader.close(); - return stringBuilder.toString(); - } catch (Exception e) { - e.printStackTrace(); - return "ERROR"; - } - } - public static String readTextFromURL(String url) throws IOException { URL urlObject; URLConnection uc; @@ -72,16 +40,8 @@ public class Utils { * @author M4TH1EU_ */ public String replaceArgsString(String variable) { - File config_file = null; - try { - config_file = new File(getClass().getResource("/config/richpresence.json").toURI()); - } catch (URISyntaxException e) { - e.printStackTrace(); - } - JSONObject config = new JSONObject(Utils.readFileToString(config_file)); - - String serverip = config.getString("server-ip"); - String serverport = config.getString("server-port"); + String serverip = Main.config_object.getString("server-ip"); + String serverport = Main.config_object.getString("server-port"); try { variable = variable.replaceAll("%player-name%", Minecraft.getMinecraft().getSession().getUsername()); @@ -96,15 +56,15 @@ public class Utils { public void updateStatus(int id) { Thread t = new Thread(() -> { - JSONObject onQuitServer = config.getJSONObject("advanced-status-custom").getJSONObject("onQuitServer"); - JSONObject onJoinServer = config.getJSONObject("advanced-status-custom").getJSONObject("onJoinServer"); - JSONObject inPauseMenu = config.getJSONObject("advanced-status-custom").getJSONObject("inPauseMenu"); - JSONObject inMainMenu = config.getJSONObject("advanced-status-custom").getJSONObject("inMainMenu"); - JSONObject inInventory = config.getJSONObject("advanced-status-custom").getJSONObject("inInventory"); + JSONObject onQuitServer = Main.config_object.getJSONObject("advanced-status-custom").getJSONObject("onQuitServer"); + JSONObject onJoinServer = Main.config_object.getJSONObject("advanced-status-custom").getJSONObject("onJoinServer"); + JSONObject inPauseMenu = Main.config_object.getJSONObject("advanced-status-custom").getJSONObject("inPauseMenu"); + JSONObject inMainMenu = Main.config_object.getJSONObject("advanced-status-custom").getJSONObject("inMainMenu"); + JSONObject inInventory = Main.config_object.getJSONObject("advanced-status-custom").getJSONObject("inInventory"); switch (id) { case 0: - Main.proxy.rpcupdate(replaceArgsString(config.getJSONObject("advanced-status-custom").getJSONObject("inMainMenu").getString("message")), null, false); + Main.proxy.rpcupdate(replaceArgsString(Main.config_object.getJSONObject("advanced-status-custom").getJSONObject("inMainMenu").getString("message")), null, false); break; case 1: Main.proxy.rpcupdate(replaceArgsString(onJoinServer.getString("message")), null, false); @@ -122,7 +82,7 @@ public class Utils { Main.proxy.rpcupdate(replaceArgsString(inInventory.getString("message")), null, false); break; case 6: - Main.proxy.rpcupdate(replaceArgsString(config.getJSONObject("advanced-status-custom").getJSONObject("onJoinServer").getString("message")), null, false); + Main.proxy.rpcupdate(replaceArgsString(Main.config_object.getJSONObject("advanced-status-custom").getJSONObject("onJoinServer").getString("message")), null, false); break; default: break; diff --git a/src/main/java/ch/m4th1eu/richpresence/events/AdvancedStatusEvent.java b/src/main/java/ch/m4th1eu/richpresence/events/AdvancedStatusEvent.java index 04ffb74..41ff4f8 100644 --- a/src/main/java/ch/m4th1eu/richpresence/events/AdvancedStatusEvent.java +++ b/src/main/java/ch/m4th1eu/richpresence/events/AdvancedStatusEvent.java @@ -1,6 +1,7 @@ package ch.m4th1eu.richpresence.events; import ch.m4th1eu.json.JSONObject; +import ch.m4th1eu.richpresence.Main; import ch.m4th1eu.richpresence.Utils; import net.minecraft.client.gui.GuiIngameMenu; import net.minecraft.client.gui.GuiMainMenu; @@ -10,29 +11,16 @@ import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.network.FMLNetworkEvent; -import java.io.File; -import java.net.URISyntaxException; - /** * @author M4TH1EU_#0001 */ @Mod.EventBusSubscriber public class AdvancedStatusEvent { - JSONObject config = null; - - { - try { - config = new JSONObject(Utils.readFileToString(new File(getClass().getResource("/config/richpresence.json").toURI()))); - } catch ( - URISyntaxException e) { - e.printStackTrace(); - } - } @SubscribeEvent public void onJoinServer(FMLNetworkEvent.ClientConnectedToServerEvent event) { - JSONObject onJoinServer = config.getJSONObject("advanced-status-custom").getJSONObject("onJoinServer"); + JSONObject onJoinServer = Main.config_object.getJSONObject("advanced-status-custom").getJSONObject("onJoinServer"); if (onJoinServer.getBoolean("enable")) { Utils.instance.updateStatus(1); @@ -41,7 +29,7 @@ public class AdvancedStatusEvent { @SubscribeEvent public void onQuitServer(FMLNetworkEvent.ClientDisconnectionFromServerEvent event) { - JSONObject onQuitServer = config.getJSONObject("advanced-status-custom").getJSONObject("onQuitServer"); + JSONObject onQuitServer = Main.config_object.getJSONObject("advanced-status-custom").getJSONObject("onQuitServer"); if (onQuitServer.getBoolean("enable")) { Utils.instance.updateStatus(1); @@ -51,9 +39,9 @@ public class AdvancedStatusEvent { @SubscribeEvent public void onGuiOpen(GuiOpenEvent event) { - JSONObject inPauseMenu = config.getJSONObject("advanced-status-custom").getJSONObject("inPauseMenu"); - JSONObject inMainMenu = config.getJSONObject("advanced-status-custom").getJSONObject("inMainMenu"); - JSONObject inInventory = config.getJSONObject("advanced-status-custom").getJSONObject("inInventory"); + JSONObject inPauseMenu = Main.config_object.getJSONObject("advanced-status-custom").getJSONObject("inPauseMenu"); + JSONObject inMainMenu = Main.config_object.getJSONObject("advanced-status-custom").getJSONObject("inMainMenu"); + JSONObject inInventory = Main.config_object.getJSONObject("advanced-status-custom").getJSONObject("inInventory"); if (inPauseMenu.getBoolean("enable")) { if (event.getGui() instanceof GuiIngameMenu) { @@ -74,7 +62,7 @@ public class AdvancedStatusEvent { } if (event.getGui() == null) { - if (config.getJSONObject("advanced-status-custom").getJSONObject("onJoinServer").getBoolean("enable")) { + if (Main.config_object.getJSONObject("advanced-status-custom").getJSONObject("onJoinServer").getBoolean("enable")) { Utils.instance.updateStatus(6); } } diff --git a/src/main/resources/config/richpresence.json b/src/main/resources/config/richpresence.json index 7a73a41..1ad96f3 100644 --- a/src/main/resources/config/richpresence.json +++ b/src/main/resources/config/richpresence.json @@ -1,2 +1,35 @@ { + "_comment": "Variables disponibles :", + "_comment2": "%player-name% - Nom du joueur.", + "_comment3": "%server-connected-player% - Nombre de joueur connecté au serveur.", + "_comment4": "%server-max-slot% - Nombre de slots du serveur", + "server-ip": "mc.hypixel.net", + "server-port": "25565", + "application-settings": { + "applicationID": "601875975533232158", + "large-image-name": "discord_logo", + "large-image-text": "En train de tester ce mod !" + }, + "advanced-status-custom": { + "onJoinServer": { + "enable": true, + "message": "En jeu." + }, + "onQuitServer": { + "enable": true, + "message": "Dans le menu principal." + }, + "inPauseMenu": { + "enable": true, + "message": "Dans le menu pause." + }, + "inMainMenu": { + "enable": true, + "message": "Dans le menu principal." + }, + "inInventory": { + "enable": false, + "message": "Dans l'inventaire." + } + } } \ No newline at end of file