Initial mod code

Features:
- Config
- Chest block clone
master
BodgeMaster 2022-12-15 08:30:07 +01:00
parent cff3ed370e
commit 425ff9ba34
5 changed files with 161 additions and 0 deletions

View File

@ -0,0 +1,56 @@
package lostcave.deathchests;
import lostcave.deathchests.block.BlockDeathChest;
import lostcave.deathchests.util.Config;
import lostcave.deathchests.util.Debug;
import net.minecraft.init.Blocks;
import java.io.File;
import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.event.FMLConstructionEvent;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.registry.GameRegistry;
@Mod(modid = DeathChests.MODID, version = DeathChests.VERSION)
public class DeathChests {
public static final String MODID = "deathchests";
public static final String VERSION = "0-SNAPSHOT";
@EventHandler
public void init(FMLConstructionEvent event) {
Debug.out("Received FMLConstructionEvent");
Config.configDir = Loader.instance().getConfigDir();
Debug.out("Config file is: " + Config.configDir.toString() + System.getProperty("file.separator") + Config.configFileName);
Config.loadConfig();
Debug.out("Loaded config.");
if (Config.fixLog4J) {
// TODO: Mitigate the Log4J security vulnerability if it's in any way convenient
Debug.err("Log4J fix is currently not implemented!");
}
}
@EventHandler
public void init(FMLPreInitializationEvent event) {
System.out.println("Received FMLPreInitializationEvent");
}
@EventHandler
public void init(FMLInitializationEvent event) {
System.out.println("Received FMLInitializationEvent");
GameRegistry.registerBlock(new BlockDeathChest(), "death_chest");
// TODO (probably here): Add death event handler
}
@EventHandler
public void init(FMLPostInitializationEvent event) {
System.out.println("Received FMLPostInitializationEvent");
}
}

View File

@ -0,0 +1,19 @@
package lostcave.deathchests.block;
import net.minecraft.block.BlockChest;
public class BlockDeathChest extends BlockChest{
public BlockDeathChest() {
super(0);
this.setBlockName("death_chest");
}
//TODO: Make death chest unobtanium (dont drop the chest when broken)
//TODO: Change capacity somehow?
//TODO: Custom texture? (optional)
//TODO: Make it so items can only be taken out of the chest?
// Custom GUI? Maybe remove GUI altogether and have it drop the items when broken?
}

View File

@ -0,0 +1,74 @@
package lostcave.deathchests.util;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import cpw.mods.fml.common.FMLCommonHandler;
public class Config {
public static final String configFileName = "deathchests.txt";
public static File configDir = null;
public static boolean debug = true;
public static boolean fixLog4J = true;
private static final String configHeader = "# The format of this file is strictly `option=value` (no spaces).\n" + "# Lines starting with # and empty lines are ignored.\n";
public static void writeConfig() {
String configOut = configHeader;
configOut += "\n\n# Enable debug output?\n";
configOut += "debug=";
configOut += debug ? "true" : "false";
configOut += "\n\n# Enable Log4Shell counter measures?\n";
configOut += "fixLog4J=";
configOut += fixLog4J ? "true" : "false";
File configFile = new File (configDir, configFileName);
try {
Files.write(configFile.toPath(), configOut.getBytes());
} catch (IOException e) {
System.err.println("Failed to write to file at " + configFile.toString() + ":");
e.printStackTrace();
System.err.println("Proceeding as if nothing happened...");
}
}
public static void loadConfig() {
List<String> configData = null;
File configFile = new File(configDir, configFileName);
if (configFile.exists()) {
try {
configData = Files.readAllLines(configFile.toPath());
for (int i = 0; i < configData.size(); i++) {
if (configData.get(i).length() != 0 && configData.get(i).charAt(0) != '#') {
int equalsSign = configData.get(i).indexOf('=');
switch (configData.get(i).substring(0, equalsSign)) {
case ("debug"): {
debug = configData.get(i).substring(equalsSign + 1) == "true" ? true : false;
break;
}
case ("fixLog4J"): {
fixLog4J = configData.get(i).substring(equalsSign + 1) == "true" ? true : false;
break;
}
default: {
System.err.println("Failed parsing config entry: " + configData.get(i));
FMLCommonHandler.instance().exitJava(1, false);
}
}
}
}
} catch (IOException e) {
System.err.println("An error was encountered while trying to read the file at " + configFile.toString() + ":");
e.printStackTrace();
System.err.println("Proceeding with default configuration...");
}
} else {
// Skip reading config and set up initial config file with default values
writeConfig();
}
}
}

View File

@ -0,0 +1,11 @@
package lostcave.deathchests.util;
public class Debug {
public static void out(String string) {
if (Config.debug) System.out.println("DEBUG: " + string);
}
public static void err(String string) {
if (Config.debug) System.err.println("DEBUG: " + string);
}
}

View File

@ -0,0 +1 @@
tile.death_chest.name=Death Chest