Le fichier de config se créer bien + le temps ne se reset pas à chaque update.

This commit is contained in:
M4TH1EU 2019-07-21 14:03:21 +02:00
parent bdad98669b
commit 3a70986336
4 changed files with 30 additions and 33 deletions

View File

@ -13,7 +13,10 @@ import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import org.apache.logging.log4j.Logger;
import java.io.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
/**
* @author M4TH1EU_#0001
@ -23,6 +26,10 @@ public class Main {
public static final String MODID = "richpresence";
public static final String NAME = "Discord Rich Presence";
public static final String VERSION = "1.2";
@Mod.Instance(Main.MODID)
public static Main instance;
@SidedProxy(clientSide = "ch.m4th1eu.richpresence.proxy.ClientProxy", serverSide = "ch.m4th1eu.richpresence.proxy.CommonProxy")
public static CommonProxy proxy;
@ -35,33 +42,20 @@ public class Main {
public static Logger logger;
public EventPresence rpcClient;
public Main() {
MinecraftForge.EVENT_BUS.register(new AdvancedStatusEvent());
}
@EventHandler
public void preInit(FMLPreInitializationEvent event) {
logger = event.getModLog();
proxy.preInit(event.getSuggestedConfigurationFile());
//Configuration
event.getModConfigurationDirectory().mkdir();
File config_file = new File(event.getModConfigurationDirectory(), "\\" + Main.MODID + ".json");
if (!config_file.exists()) {
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");
config_file.createNewFile();
} catch (IOException ex) {
ex.printStackTrace();
}
}
if (config_file.length() < 10) {
try {
PrintWriter writer = new PrintWriter(config_file, "UTF-8");
writer.println("{\n" +
" \"_comment\": \"Variables disponibles :\",\n" +
@ -105,28 +99,27 @@ public class Main {
e.printStackTrace();
}
}
JSONObject config = new JSONObject(Utils.readFileToString(config_file));
applicationId = config.getJSONObject("application-settings").getString("applicationID");
largeimage = config.getJSONObject("application-settings").getString("large-image-name");
largeimagetext = Utils.replaceArgsString(config.getJSONObject("application-settings").getString("large-image-text"));
}
@EventHandler
public void init(FMLInitializationEvent event) {
MinecraftForge.EVENT_BUS.register(instance);
MinecraftForge.EVENT_BUS.register(new AdvancedStatusEvent());
JSONObject config = new JSONObject(Utils.readFileToString(new File(Minecraft.getMinecraft().mcDataDir, "\\config\\" + Main.MODID + ".json")));
applicationId = config.getJSONObject("application-settings").getString("applicationID");
largeimage = config.getJSONObject("application-settings").getString("large-image-name");
largeimagetext = Utils.replaceArgsString(config.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, false);
proxy.rpcupdate(config.getJSONObject("advanced-status-custom").getJSONObject("inMainMenu").getString("message"), null, true);
} else {
proxy.rpcupdate("", null, false);
}

View File

@ -2,7 +2,6 @@ package ch.m4th1eu.richpresence;
import ch.m4th1eu.json.JSONObject;
import net.minecraft.client.Minecraft;
import net.minecraft.network.INetHandler;
import java.io.*;
import java.net.URL;
@ -14,12 +13,13 @@ public class Utils {
/**
* @author Nathanaël#4314
*/
public static String readFileToString(final File file) {
public static String readFileToString(File file) {
try {
final BufferedReader reader = new BufferedReader(new FileReader(file));
final StringBuilder stringBuilder = new StringBuilder();
BufferedReader reader = new BufferedReader(new FileReader(file));
StringBuilder stringBuilder = new StringBuilder();
String line;
final String ls = System.getProperty("line.separator");
String ls = System.getProperty("line.separator");
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append(ls);

View File

@ -7,9 +7,7 @@ import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiIngameMenu;
import net.minecraft.client.gui.GuiMainMenu;
import net.minecraft.client.gui.inventory.GuiInventory;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraftforge.client.event.GuiOpenEvent;
import net.minecraftforge.fml.client.FMLClientHandler;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.network.FMLNetworkEvent;

View File

@ -10,6 +10,7 @@ import net.arikia.dev.drpc.DiscordRichPresence;
*/
public class EventPresence {
public static long oldTime;
private static Thread callbackRunner;
public synchronized static void init() {
@ -35,6 +36,11 @@ public class EventPresence {
if (changeTime) {
presence.startTimestamp = System.currentTimeMillis() / 1000L;
oldTime = presence.startTimestamp;
} else {
System.out.println(presence.startTimestamp);
System.out.println(oldTime);
presence.startTimestamp = oldTime;
}
} else if (action != null) {
presence.state = action;