Wrote better overwrite code
parent
f8be087f98
commit
90517050b3
|
@ -1,13 +1,9 @@
|
|||
package net.einsteinsci.betterbeginnings.config.json;
|
||||
|
||||
import net.einsteinsci.betterbeginnings.util.LogUtil;
|
||||
import net.einsteinsci.betterbeginnings.util.FileUtil;
|
||||
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
|
||||
import org.apache.logging.log4j.Level;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
@ -29,23 +25,13 @@ public class AdvancedCraftingConfig implements IJsonConfig
|
|||
public String getMainJson(File subfolder)
|
||||
{
|
||||
File mainf = new File(subfolder, "main.json");
|
||||
if (!mainf.exists())
|
||||
String json = FileUtil.readAllText(mainf);
|
||||
if (json == null)
|
||||
{
|
||||
return "{}";
|
||||
json = "{}";
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return new String(Files.readAllBytes(mainf.toPath()));
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
LogUtil.log(Level.ERROR, "IOException occurred opening config/betterbeginnings/advancedcrafting/main.json!");
|
||||
LogUtil.log("");
|
||||
LogUtil.log(Level.ERROR, e.toString());
|
||||
|
||||
return "{}";
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -67,23 +53,8 @@ public class AdvancedCraftingConfig implements IJsonConfig
|
|||
for (String fileName : mainRecipes.getIncludes())
|
||||
{
|
||||
File incf = new File(subfolder, fileName);
|
||||
if (!incf.exists())
|
||||
{
|
||||
LogUtil.log(Level.ERROR, "Included file not found: config/betterbeginnings/advancedcrafting/" +
|
||||
fileName + " - Skipping.");
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
res.add(new String(Files.readAllBytes(incf.toPath())));
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
LogUtil.log(Level.ERROR, "IOException occurred opening config/betterbeginnings/advancedcrafting/" + fileName);
|
||||
LogUtil.log("");
|
||||
LogUtil.log(Level.ERROR, ex.toString());
|
||||
}
|
||||
String json = FileUtil.readAllText(incf);
|
||||
res.add(json);
|
||||
}
|
||||
|
||||
return res;
|
||||
|
@ -125,17 +96,8 @@ public class AdvancedCraftingConfig implements IJsonConfig
|
|||
public void savePostLoad(File subfolder)
|
||||
{
|
||||
String json = BBJsonLoader.serializeObject(mainRecipes);
|
||||
try
|
||||
{
|
||||
File mainf = new File(subfolder, "main.json");
|
||||
Files.write(mainf.toPath(), json.getBytes(), StandardOpenOption.CREATE);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
LogUtil.log(Level.ERROR, "IOException occurred saving config/betterbeginnings/advancedcrafting/main.json!");
|
||||
LogUtil.log("");
|
||||
LogUtil.log(Level.ERROR, e.toString());
|
||||
}
|
||||
File mainf = new File(subfolder, "main.json");
|
||||
FileUtil.overwriteAllText(mainf, json);
|
||||
}
|
||||
|
||||
public JsonAdvancedCraftingHandler getMainRecipes()
|
||||
|
|
|
@ -1,13 +1,9 @@
|
|||
package net.einsteinsci.betterbeginnings.config.json;
|
||||
|
||||
import net.einsteinsci.betterbeginnings.util.LogUtil;
|
||||
import net.einsteinsci.betterbeginnings.util.FileUtil;
|
||||
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
|
||||
import org.apache.logging.log4j.Level;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
@ -28,23 +24,13 @@ public class BrickOvenConfig implements IJsonConfig
|
|||
public String getMainJson(File subfolder)
|
||||
{
|
||||
File mainf = new File(subfolder, "main.json");
|
||||
if (!mainf.exists())
|
||||
String json = FileUtil.readAllText(mainf);
|
||||
if (json == null)
|
||||
{
|
||||
return "{}";
|
||||
json = "{}";
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return new String(Files.readAllBytes(mainf.toPath()));
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
LogUtil.log(Level.ERROR, "IOException occurred opening config/betterbeginnings/brickoven/main.json!");
|
||||
LogUtil.log("");
|
||||
LogUtil.log(Level.ERROR, e.toString());
|
||||
|
||||
return "{}";
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -66,23 +52,8 @@ public class BrickOvenConfig implements IJsonConfig
|
|||
for (String fileName : mainRecipes.getIncludes())
|
||||
{
|
||||
File incf = new File(subfolder, fileName);
|
||||
if (!incf.exists())
|
||||
{
|
||||
LogUtil.log(Level.ERROR, "Included file not found: config/betterbeginnings/brickoven/" +
|
||||
fileName + " - Skipping.");
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
res.add(new String(Files.readAllBytes(incf.toPath())));
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
LogUtil.log(Level.ERROR, "IOException occurred opening config/betterbeginnings/brickoven/" + fileName);
|
||||
LogUtil.log("");
|
||||
LogUtil.log(Level.ERROR, ex.toString());
|
||||
}
|
||||
String json = FileUtil.readAllText(incf);
|
||||
res.add(json);
|
||||
}
|
||||
|
||||
return res;
|
||||
|
@ -134,17 +105,8 @@ public class BrickOvenConfig implements IJsonConfig
|
|||
public void savePostLoad(File subfolder)
|
||||
{
|
||||
String json = BBJsonLoader.serializeObject(mainRecipes);
|
||||
try
|
||||
{
|
||||
File mainf = new File(subfolder, "main.json");
|
||||
Files.write(mainf.toPath(), json.getBytes(), StandardOpenOption.CREATE);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
LogUtil.log(Level.ERROR, "IOException occurred saving config/betterbeginnings/brickoven/main.json!");
|
||||
LogUtil.log("");
|
||||
LogUtil.log(Level.ERROR, e.toString());
|
||||
}
|
||||
File mainf = new File(subfolder, "main.json");
|
||||
FileUtil.overwriteAllText(mainf, json);
|
||||
}
|
||||
|
||||
public JsonBrickOvenRecipeHandler getMainRecipes()
|
||||
|
|
|
@ -14,12 +14,18 @@ public class JsonBrickOvenRecipeHandler
|
|||
|
||||
private List<String> includes;
|
||||
|
||||
private List<String> __COMMENTS;
|
||||
|
||||
public JsonBrickOvenRecipeHandler()
|
||||
{
|
||||
shaped = new ArrayList<>();
|
||||
shapeless = new ArrayList<>();
|
||||
includes = new ArrayList<>();
|
||||
|
||||
__COMMENTS = new ArrayList<>();
|
||||
__COMMENTS.add("Brick Oven recipes do not support ore dictionary in ingredients.");
|
||||
__COMMENTS.add("This will change eventually. This only applies to Brick Oven recipes.");
|
||||
|
||||
// TESTING ONLY
|
||||
shaped.add(new JsonBrickOvenShapedRecipe(new ItemStack(Items.beef), "x ", "ox", 'x',
|
||||
Blocks.bedrock, 'o', Items.chainmail_chestplate));
|
||||
|
|
|
@ -1,13 +1,9 @@
|
|||
package net.einsteinsci.betterbeginnings.config.json;
|
||||
|
||||
import net.einsteinsci.betterbeginnings.util.LogUtil;
|
||||
import net.einsteinsci.betterbeginnings.util.FileUtil;
|
||||
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
|
||||
import org.apache.logging.log4j.Level;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
@ -29,23 +25,13 @@ public class KilnConfig implements IJsonConfig
|
|||
public String getMainJson(File subfolder)
|
||||
{
|
||||
File mainf = new File(subfolder, "main.json");
|
||||
if (!mainf.exists())
|
||||
String json = FileUtil.readAllText(mainf);
|
||||
if (json == null)
|
||||
{
|
||||
return "{}";
|
||||
json = "{}";
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return new String(Files.readAllBytes(mainf.toPath()));
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
LogUtil.log(Level.ERROR, "IOException occurred opening config/betterbeginnings/kiln/main.json!");
|
||||
LogUtil.log("");
|
||||
LogUtil.log(Level.ERROR, e.toString());
|
||||
|
||||
return "{}";
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -67,23 +53,8 @@ public class KilnConfig implements IJsonConfig
|
|||
for (String fileName : mainRecipes.getIncludes())
|
||||
{
|
||||
File incf = new File(subfolder, fileName);
|
||||
if (!incf.exists())
|
||||
{
|
||||
LogUtil.log(Level.ERROR, "Included file not found: config/betterbeginnings/kiln/" +
|
||||
fileName + " - Skipping.");
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
res.add(new String(Files.readAllBytes(incf.toPath())));
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
LogUtil.log(Level.ERROR, "IOException occurred opening config/betterbeginnings/kiln/" + fileName);
|
||||
LogUtil.log("");
|
||||
LogUtil.log(Level.ERROR, ex.toString());
|
||||
}
|
||||
String json = FileUtil.readAllText(incf);
|
||||
res.add(json);
|
||||
}
|
||||
|
||||
return res;
|
||||
|
@ -125,18 +96,8 @@ public class KilnConfig implements IJsonConfig
|
|||
public void savePostLoad(File subfolder)
|
||||
{
|
||||
String json = BBJsonLoader.serializeObject(mainRecipes);
|
||||
|
||||
try
|
||||
{
|
||||
File mainf = new File(subfolder, "main.json");
|
||||
Files.write(mainf.toPath(), json.getBytes(), StandardOpenOption.CREATE);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
LogUtil.log(Level.ERROR, "IOException occurred saving config/betterbeginnings/kiln/main.json!");
|
||||
LogUtil.log("");
|
||||
LogUtil.log(Level.ERROR, e.toString());
|
||||
}
|
||||
File mainf = new File(subfolder, "main.json");
|
||||
FileUtil.overwriteAllText(mainf, json);
|
||||
}
|
||||
|
||||
public JsonKilnRecipeHandler getMainRecipes()
|
||||
|
|
|
@ -1,13 +1,9 @@
|
|||
package net.einsteinsci.betterbeginnings.config.json;
|
||||
|
||||
import net.einsteinsci.betterbeginnings.util.LogUtil;
|
||||
import net.einsteinsci.betterbeginnings.util.FileUtil;
|
||||
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
|
||||
import org.apache.logging.log4j.Level;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
@ -28,23 +24,13 @@ public class SmelterConfig implements IJsonConfig
|
|||
public String getMainJson(File subfolder)
|
||||
{
|
||||
File mainf = new File(subfolder, "main.json");
|
||||
if (!mainf.exists())
|
||||
String json = FileUtil.readAllText(mainf);
|
||||
if (json == null)
|
||||
{
|
||||
return "{}";
|
||||
json = "{}";
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return new String(Files.readAllBytes(mainf.toPath()));
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
LogUtil.log(Level.ERROR, "IOException occurred opening config/betterbeginnings/smelter/main.json!");
|
||||
LogUtil.log("");
|
||||
LogUtil.log(Level.ERROR, e.toString());
|
||||
|
||||
return "{}";
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -66,23 +52,8 @@ public class SmelterConfig implements IJsonConfig
|
|||
for (String fileName : mainRecipes.getIncludes())
|
||||
{
|
||||
File incf = new File(subfolder, fileName);
|
||||
if (!incf.exists())
|
||||
{
|
||||
LogUtil.log(Level.ERROR, "Included file not found: config/betterbeginnings/smelter/" +
|
||||
fileName + " - Skipping.");
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
res.add(new String(Files.readAllBytes(incf.toPath())));
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
LogUtil.log(Level.ERROR, "IOException occurred opening config/betterbeginnings/smelter/" + fileName);
|
||||
LogUtil.log("");
|
||||
LogUtil.log(Level.ERROR, ex.toString());
|
||||
}
|
||||
String json = FileUtil.readAllText(incf);
|
||||
res.add(json);
|
||||
}
|
||||
|
||||
return res;
|
||||
|
@ -124,17 +95,8 @@ public class SmelterConfig implements IJsonConfig
|
|||
public void savePostLoad(File subfolder)
|
||||
{
|
||||
String json = BBJsonLoader.serializeObject(mainRecipes);
|
||||
try
|
||||
{
|
||||
File mainf = new File(subfolder, "main.json");
|
||||
Files.write(mainf.toPath(), json.getBytes(), StandardOpenOption.CREATE);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
LogUtil.log(Level.ERROR, "IOException occurred saving config/betterbeginnings/smelter/main.json!");
|
||||
LogUtil.log("");
|
||||
LogUtil.log(Level.ERROR, e.toString());
|
||||
}
|
||||
File mainf = new File(subfolder, "main.json");
|
||||
FileUtil.overwriteAllText(mainf, json);
|
||||
}
|
||||
|
||||
public JsonSmelterRecipeHandler getMainRecipes()
|
||||
|
|
|
@ -0,0 +1,56 @@
|
|||
package net.einsteinsci.betterbeginnings.util;
|
||||
|
||||
import org.apache.logging.log4j.Level;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
|
||||
public class FileUtil
|
||||
{
|
||||
public static String readAllText(File file)
|
||||
{
|
||||
if (!file.exists())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return new String(Files.readAllBytes(file.toPath()));
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
LogUtil.log(Level.ERROR, "IOException occurred opening " + file.getPath());
|
||||
LogUtil.log("");
|
||||
LogUtil.log(Level.ERROR, ex.toString());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean overwriteAllText(File file, String text)
|
||||
{
|
||||
if (file.exists())
|
||||
{
|
||||
if (!file.delete())
|
||||
{
|
||||
LogUtil.log(Level.ERROR, "Could not delete file to be overwritten: " + file.getPath());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Files.write(file.toPath(), text.getBytes(), StandardOpenOption.CREATE);
|
||||
return true;
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
LogUtil.log(Level.ERROR, "IOException occurred saving " + file.getPath());
|
||||
LogUtil.log("");
|
||||
LogUtil.log(Level.ERROR, e.toString());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue