Skip to content

Commit 41aa76c

Browse files
author
Otho
committed
Adding default config items images
1 parent db0d32d commit 41aa76c

25 files changed

+82
-175
lines changed

src/main/java/me/otho/customItems/CustomItems.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ public void preInit(FMLPreInitializationEvent event) throws IOException, URISynt
2828
{
2929
String folderPath = event.getModConfigurationDirectory().toString()+File.separator+ModReference.MOD_ID+File.separator;
3030

31-
if(ForgeConfig.remake)
32-
JsonConfigurationHandler.unpackConfigFile(CustomItems.class, "defaultConfigs", folderPath);
33-
3431
ForgeConfig.init(event.getSuggestedConfigurationFile());
3532

33+
// if(ForgeConfig.remake)
34+
// JsonConfigurationHandler.unpackConfigFile(CustomItems.class, "defaultConfigs", folderPath);
35+
3636
customItemsTab.init();
3737

3838
JsonConfigurationHandler.init(folderPath);

src/main/java/me/otho/customItems/configuration/ForgeConfig.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ public class ForgeConfig
88
{
99
public static Configuration config;
1010

11-
public static boolean debug;
11+
public static boolean debug = true;
1212
public static boolean defaultTab;
13-
public static boolean remake;
13+
public static boolean remake = false;
1414

1515
public static void init(File configFile)
1616
{
@@ -22,8 +22,7 @@ public static void init(File configFile)
2222

2323
private static void loadConfiguration()
2424
{
25-
remake = config.getBoolean("remake", "OPTIONS", true, "Set true if you want to restore the default config on the next time the mod is loaded");
26-
debug = config.getBoolean("debug", "DEBUG", false, "Set to true if you wish to output debug info");
25+
// remake = config.getBoolean("remake", "OPTIONS", true, "Set true if you want to restore the default config on the next time the mod is loaded");
2726
defaultTab = config.getBoolean("defaultTab", "OPTIONS", true, "Set to false, if you dont want the default creative tab");
2827

2928
if(config.hasChanged()) {

src/main/java/me/otho/customItems/configuration/JsonConfigurationHandler.java

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,16 @@
99
import java.io.InputStream;
1010
import java.net.URISyntaxException;
1111
import java.net.URL;
12+
import java.net.URLDecoder;
13+
import java.util.Arrays;
1214
import java.util.Enumeration;
15+
import java.util.HashSet;
16+
import java.util.Set;
17+
import java.util.jar.JarEntry;
18+
import java.util.jar.JarFile;
1319

1420
import me.otho.customItems.registry.Registry;
21+
import me.otho.customItems.util.LogHelper;
1522

1623
import org.apache.commons.lang3.ArrayUtils;
1724

@@ -96,6 +103,7 @@ private static void mergeGson(JsonSchema data, JsonSchema mergeTo)
96103
public static boolean unpackConfigFile(Class obj, String path, String destinationPath) throws IOException, URISyntaxException
97104
{
98105
String[] resources = getResourceListing(obj, path);
106+
LogHelper.info(Arrays.deepToString(resources));
99107
int i;
100108
if(resources != null){
101109
for(i=0; i< resources.length; i++){
@@ -131,16 +139,52 @@ public static boolean unpackConfigFile(Class obj, String path, String destinatio
131139
}
132140
}
133141

134-
return true;
142+
LogHelper.info("Failed to load default config files - 2");
143+
return false;
135144
}
136145

137146
public static String[] getResourceListing(Class clazz, String path) throws URISyntaxException, IOException {
138147
URL dirURL = clazz.getClassLoader().getResource(path);
148+
139149
if (dirURL != null && dirURL.getProtocol().equals("file")) {
140150
String[] list = new File(dirURL.toURI()).list();
141151
return list;
142-
}
152+
}
153+
154+
if (dirURL == null) {
155+
/*
156+
* In case of a jar file, we can't actually find a directory.
157+
* Have to assume the same jar as clazz.
158+
*/
159+
String me = clazz.getName().replace(".", "/")+".class";
160+
dirURL = clazz.getClassLoader().getResource(me);
161+
}
143162

163+
if (dirURL.getProtocol().equals("jar")) {
164+
/* A JAR path */
165+
String jarPath = dirURL.getPath().substring(5, dirURL.getPath().indexOf("!")); //strip out only the JAR file
166+
JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
167+
Enumeration<JarEntry> entries = jar.entries(); //gives ALL entries in jar
168+
Set<String> result = new HashSet<String>(); //avoid duplicates in case it is a subdirectory
169+
while(entries.hasMoreElements()) {
170+
String name = entries.nextElement().getName();
171+
if (name.startsWith(path)) { //filter according to the path
172+
String entry = name.substring(path.length());
173+
int checkSubdir = entry.indexOf("/");
174+
if (checkSubdir >= 0) {
175+
// if it is a subdirectory, we just return the directory name
176+
entry = entry.substring(0, checkSubdir);
177+
}
178+
result.add(entry);
179+
}
180+
}
181+
jar.close();
182+
String[] ret = result.toArray(new String[result.size()]);
183+
LogHelper.info("Return: " + ret);
184+
return ret;
185+
}
186+
187+
LogHelper.info("Failed to load default config files");
144188
throw new UnsupportedOperationException("Cannot list files for URL "+dirURL);
145189
}
146190

3.5 KB
Loading
3.38 KB
Loading
2.79 KB
Loading
3.49 KB
Loading
3.45 KB
Loading
3.38 KB
Loading
2.81 KB
Loading

0 commit comments

Comments
 (0)