Fixed : Crash lors de la récupération du fichier de config.

This commit is contained in:
M4TH1EU 2019-07-29 15:23:44 +02:00
parent 21a77c799a
commit 726c4b0e67
5 changed files with 77 additions and 139 deletions

View File

@ -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. //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 group = "ch.m4th1eu.richpresence" // http://maven.apache.org/guides/mini/guide-naming-conventions.html
archivesBaseName = "richpresence" archivesBaseName = "richpresence"

View File

@ -10,13 +10,13 @@ import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.SidedProxy; import net.minecraftforge.fml.common.SidedProxy;
import net.minecraftforge.fml.common.event.FMLInitializationEvent; import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import java.io.File; import java.io.BufferedReader;
import java.io.FileNotFoundException; import java.io.IOException;
import java.io.PrintWriter; import java.io.InputStream;
import java.io.UnsupportedEncodingException; import java.io.InputStreamReader;
import java.net.URISyntaxException;
/** /**
* @author M4TH1EU_#0001 * @author M4TH1EU_#0001
@ -25,7 +25,7 @@ import java.net.URISyntaxException;
public class Main { public class Main {
public static final String MODID = "richpresence"; public static final String MODID = "richpresence";
public static final String NAME = "Discord Rich Presence"; 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) @Mod.Instance(Main.MODID)
public static Main instance; public static Main instance;
@ -37,6 +37,8 @@ public class Main {
* Variables pour la config * Variables pour la config
*/ */
public static String applicationId, largeimage, largeimagetext; public static String applicationId, largeimage, largeimagetext;
public static String config_file_text = "";
public static JSONObject config_object;
public static Logger logger; public static Logger logger;
@ -50,60 +52,21 @@ public class Main {
//Configuration //Configuration
event.getModConfigurationDirectory().mkdir(); event.getModConfigurationDirectory().mkdir();
File config_file = null;
InputStream in = getClass().getResourceAsStream("/config/richpresence.json");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
try { try {
config_file = new File(getClass().getResource("/config/richpresence.json").toURI()); config_file_text = IOUtils.toString(reader);
} catch (URISyntaxException e) { config_object = new JSONObject(config_file_text);
reader.close();
} catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
if (!config_file.exists() || config_file.length() < 10) { if (config_file_text != null || config_file_text.length() < 10) {
try { event.getModLog().warn("Impossible de charger la configuration du mod : " + Main.MODID);
event.getModLog().warn("Impossible de charger la configuration du mod : " + Main.MODID);
event.getModLog().warn("Création du fichier de configuration");
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(instance);
MinecraftForge.EVENT_BUS.register(new AdvancedStatusEvent()); 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"); applicationId = config_object.getJSONObject("application-settings").getString("applicationID");
largeimage = config.getJSONObject("application-settings").getString("large-image-name"); largeimage = config_object.getJSONObject("application-settings").getString("large-image-name");
largeimagetext = Utils.instance.replaceArgsString(config.getJSONObject("application-settings").getString("large-image-text")); largeimagetext = Utils.instance.replaceArgsString(config_object.getJSONObject("application-settings").getString("large-image-text"));
proxy.init(); proxy.init();
rpcClient = new EventPresence(); rpcClient = new EventPresence();
proxy.rpcinit(); proxy.rpcinit();
if (config.getJSONObject("advanced-status-custom").getJSONObject("inMainMenu").getBoolean("enable")) { if (config_object.getJSONObject("advanced-status-custom").getJSONObject("inMainMenu").getBoolean("enable")) {
proxy.rpcupdate(config.getJSONObject("advanced-status-custom").getJSONObject("inMainMenu").getString("message"), null, true); proxy.rpcupdate(config_object.getJSONObject("advanced-status-custom").getJSONObject("inMainMenu").getString("message"), null, true);
} else { } else {
proxy.rpcupdate("", null, false); proxy.rpcupdate("", null, false);
} }

View File

@ -3,8 +3,10 @@ package ch.m4th1eu.richpresence;
import ch.m4th1eu.json.JSONObject; import ch.m4th1eu.json.JSONObject;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import java.io.*; import java.io.BufferedReader;
import java.net.URISyntaxException; import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.nio.charset.Charset; import java.nio.charset.Charset;
@ -13,40 +15,6 @@ public class Utils {
public static final Utils instance = new 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 { public static String readTextFromURL(String url) throws IOException {
URL urlObject; URL urlObject;
URLConnection uc; URLConnection uc;
@ -72,16 +40,8 @@ public class Utils {
* @author M4TH1EU_ * @author M4TH1EU_
*/ */
public String replaceArgsString(String variable) { public String replaceArgsString(String variable) {
File config_file = null; String serverip = Main.config_object.getString("server-ip");
try { String serverport = Main.config_object.getString("server-port");
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");
try { try {
variable = variable.replaceAll("%player-name%", Minecraft.getMinecraft().getSession().getUsername()); variable = variable.replaceAll("%player-name%", Minecraft.getMinecraft().getSession().getUsername());
@ -96,15 +56,15 @@ public class Utils {
public void updateStatus(int id) { public void updateStatus(int id) {
Thread t = new Thread(() -> { Thread t = new Thread(() -> {
JSONObject onQuitServer = config.getJSONObject("advanced-status-custom").getJSONObject("onQuitServer"); JSONObject onQuitServer = Main.config_object.getJSONObject("advanced-status-custom").getJSONObject("onQuitServer");
JSONObject onJoinServer = config.getJSONObject("advanced-status-custom").getJSONObject("onJoinServer"); JSONObject onJoinServer = Main.config_object.getJSONObject("advanced-status-custom").getJSONObject("onJoinServer");
JSONObject inPauseMenu = config.getJSONObject("advanced-status-custom").getJSONObject("inPauseMenu"); JSONObject inPauseMenu = Main.config_object.getJSONObject("advanced-status-custom").getJSONObject("inPauseMenu");
JSONObject inMainMenu = config.getJSONObject("advanced-status-custom").getJSONObject("inMainMenu"); JSONObject inMainMenu = Main.config_object.getJSONObject("advanced-status-custom").getJSONObject("inMainMenu");
JSONObject inInventory = config.getJSONObject("advanced-status-custom").getJSONObject("inInventory"); JSONObject inInventory = Main.config_object.getJSONObject("advanced-status-custom").getJSONObject("inInventory");
switch (id) { switch (id) {
case 0: 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; break;
case 1: case 1:
Main.proxy.rpcupdate(replaceArgsString(onJoinServer.getString("message")), null, false); 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); Main.proxy.rpcupdate(replaceArgsString(inInventory.getString("message")), null, false);
break; break;
case 6: 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; break;
default: default:
break; break;

View File

@ -1,6 +1,7 @@
package ch.m4th1eu.richpresence.events; package ch.m4th1eu.richpresence.events;
import ch.m4th1eu.json.JSONObject; import ch.m4th1eu.json.JSONObject;
import ch.m4th1eu.richpresence.Main;
import ch.m4th1eu.richpresence.Utils; import ch.m4th1eu.richpresence.Utils;
import net.minecraft.client.gui.GuiIngameMenu; import net.minecraft.client.gui.GuiIngameMenu;
import net.minecraft.client.gui.GuiMainMenu; 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.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.network.FMLNetworkEvent; import net.minecraftforge.fml.common.network.FMLNetworkEvent;
import java.io.File;
import java.net.URISyntaxException;
/** /**
* @author M4TH1EU_#0001 * @author M4TH1EU_#0001
*/ */
@Mod.EventBusSubscriber @Mod.EventBusSubscriber
public class AdvancedStatusEvent { 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 @SubscribeEvent
public void onJoinServer(FMLNetworkEvent.ClientConnectedToServerEvent event) { 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")) { if (onJoinServer.getBoolean("enable")) {
Utils.instance.updateStatus(1); Utils.instance.updateStatus(1);
@ -41,7 +29,7 @@ public class AdvancedStatusEvent {
@SubscribeEvent @SubscribeEvent
public void onQuitServer(FMLNetworkEvent.ClientDisconnectionFromServerEvent event) { 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")) { if (onQuitServer.getBoolean("enable")) {
Utils.instance.updateStatus(1); Utils.instance.updateStatus(1);
@ -51,9 +39,9 @@ public class AdvancedStatusEvent {
@SubscribeEvent @SubscribeEvent
public void onGuiOpen(GuiOpenEvent event) { public void onGuiOpen(GuiOpenEvent event) {
JSONObject inPauseMenu = config.getJSONObject("advanced-status-custom").getJSONObject("inPauseMenu"); JSONObject inPauseMenu = Main.config_object.getJSONObject("advanced-status-custom").getJSONObject("inPauseMenu");
JSONObject inMainMenu = config.getJSONObject("advanced-status-custom").getJSONObject("inMainMenu"); JSONObject inMainMenu = Main.config_object.getJSONObject("advanced-status-custom").getJSONObject("inMainMenu");
JSONObject inInventory = config.getJSONObject("advanced-status-custom").getJSONObject("inInventory"); JSONObject inInventory = Main.config_object.getJSONObject("advanced-status-custom").getJSONObject("inInventory");
if (inPauseMenu.getBoolean("enable")) { if (inPauseMenu.getBoolean("enable")) {
if (event.getGui() instanceof GuiIngameMenu) { if (event.getGui() instanceof GuiIngameMenu) {
@ -74,7 +62,7 @@ public class AdvancedStatusEvent {
} }
if (event.getGui() == null) { 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); Utils.instance.updateStatus(6);
} }
} }

View File

@ -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."
}
}
} }