avoid Class.forName() in world loading event
This commit is contained in:
@@ -2,10 +2,7 @@ package mods.betterfoliage.client;
|
|||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.FileWriter;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
@@ -21,30 +18,22 @@ import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
|||||||
|
|
||||||
public class BlockMatcher {
|
public class BlockMatcher {
|
||||||
|
|
||||||
public Set<String> whiteList = Sets.newHashSet();
|
public Set<Class<?>> whiteList = Sets.newHashSet();
|
||||||
public Set<String> blackList = Sets.newHashSet();
|
public Set<Class<?>> blackList = Sets.newHashSet();
|
||||||
public Set<Integer> blockIDs = Sets.newHashSet();
|
public Set<Integer> blockIDs = Sets.newHashSet();
|
||||||
|
|
||||||
public void addClass(String className) {
|
public void addClass(String className) {
|
||||||
|
try {
|
||||||
if (className.startsWith("-"))
|
if (className.startsWith("-"))
|
||||||
blackList.add(className.substring(1));
|
blackList.add(Class.forName(className.substring(1)));
|
||||||
else
|
else
|
||||||
whiteList.add(className);
|
whiteList.add(Class.forName(className));
|
||||||
|
} catch(ClassNotFoundException e) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean matchesClass(Block block) {
|
public boolean matchesClass(Block block) {
|
||||||
for (String className : blackList) {
|
for (Class<?> clazz : blackList) if (clazz.isAssignableFrom(block.getClass())) return false;
|
||||||
try {
|
for (Class<?> clazz : whiteList) if (clazz.isAssignableFrom(block.getClass())) return true;
|
||||||
Class<?> clazz = Class.forName(className);
|
|
||||||
if (clazz.isAssignableFrom(block.getClass())) return false;
|
|
||||||
} catch(ClassNotFoundException e) {}
|
|
||||||
}
|
|
||||||
for (String className : whiteList) {
|
|
||||||
try {
|
|
||||||
Class<?> clazz = Class.forName(className);
|
|
||||||
if (clazz.isAssignableFrom(block.getClass())) return true;
|
|
||||||
} catch(ClassNotFoundException e) {}
|
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,34 +59,11 @@ public class BlockMatcher {
|
|||||||
line = reader.readLine();
|
line = reader.readLine();
|
||||||
}
|
}
|
||||||
reader.close();
|
reader.close();
|
||||||
} catch (FileNotFoundException e) {
|
} catch (Exception e) {
|
||||||
saveDefaults(file);
|
|
||||||
} catch (IOException e) {
|
|
||||||
BetterFoliage.log.warn(String.format("Error reading configuration: %s", file.getName()));
|
BetterFoliage.log.warn(String.format("Error reading configuration: %s", file.getName()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void saveDefaults(File file) {
|
|
||||||
FileWriter writer = null;
|
|
||||||
try {
|
|
||||||
writer = new FileWriter(file);
|
|
||||||
for (String className : whiteList) {
|
|
||||||
writer.write(className);
|
|
||||||
writer.write("\n");
|
|
||||||
}
|
|
||||||
for (String className : blackList) {
|
|
||||||
writer.write("-");
|
|
||||||
writer.write(className);
|
|
||||||
writer.write("\n");
|
|
||||||
}
|
|
||||||
writer.close();
|
|
||||||
} catch (FileNotFoundException e) {
|
|
||||||
saveDefaults(file);
|
|
||||||
} catch (IOException e) {
|
|
||||||
BetterFoliage.log.warn(String.format("Error writing default configuration: %s", file.getName()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Caches block IDs on world load for fast lookup
|
/** Caches block IDs on world load for fast lookup
|
||||||
* @param event
|
* @param event
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user