moved BlockMatcher configuration into main config file

This commit is contained in:
octarine-noise
2014-08-20 11:59:25 +02:00
parent 8b4967584e
commit f8092685a8
16 changed files with 305 additions and 139 deletions
@@ -1,19 +1,19 @@
package mods.betterfoliage.client;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import mods.betterfoliage.BetterFoliage;
import mods.betterfoliage.common.util.Utils;
import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.WorldClient;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.event.world.WorldEvent;
import com.google.common.base.Charsets;
import com.google.common.collect.Sets;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
@@ -23,16 +23,7 @@ public class BlockMatcher {
public Set<Class<?>> whiteList = Sets.newHashSet();
public Set<Class<?>> blackList = Sets.newHashSet();
public Set<Integer> blockIDs = Sets.newHashSet();
public void addClass(String className) {
try {
if (className.startsWith("-"))
blackList.add(Class.forName(className.substring(1)));
else
whiteList.add(Class.forName(className));
} catch(ClassNotFoundException e) {}
}
public boolean matchesClass(Block block) {
for (Class<?> clazz : blackList) if (clazz.isAssignableFrom(block.getClass())) return false;
for (Class<?> clazz : whiteList) if (clazz.isAssignableFrom(block.getClass())) return true;
@@ -47,43 +38,56 @@ public class BlockMatcher {
return blockIDs.contains(Block.blockRegistry.getIDForObject(block));
}
public void load(File file, ResourceLocation defaults) {
if (!file.exists()) try {
Utils.copyFromTextResource(defaults, file);
} catch (IOException e) {
BetterFoliage.log.error(String.format("Error copying default resource: %s", defaults.toString()));
e.printStackTrace();
}
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
whiteList.clear();
blackList.clear();
String line = reader.readLine();
while(line != null) {
addClass(line.trim());
line = reader.readLine();
}
reader.close();
} catch (Exception e) {
BetterFoliage.log.warn(String.format("Error reading configuration: %s", file.getName()));
}
public void updateClassLists(String[] newWhitelist, String[] newBlacklist) {
whiteList.clear();
for(String className : newWhitelist) try {
whiteList.add(Class.forName(className));
} catch(ClassNotFoundException e) {}
blackList.clear();
for(String className : newBlacklist) try {
blackList.add(Class.forName(className));
} catch(ClassNotFoundException e) {}
updateBlockIDs();
}
@SuppressWarnings("unchecked")
public void updateBlockIDs() {
blockIDs.clear();
Iterator<Block> iter = Block.blockRegistry.iterator();
while (iter.hasNext()) {
Block block = iter.next();
if (matchesClass(block)) blockIDs.add(Block.blockRegistry.getIDForObject(block));
}
}
public static void loadDefaultLists(ResourceLocation defaults, Collection<String> blacklist, Collection<String> whitelist) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(Minecraft.getMinecraft().getResourceManager().getResource(defaults).getInputStream(), Charsets.UTF_8));
String line = reader.readLine();
while(line != null) {
line = line.trim();
if (!line.isEmpty() && !line.startsWith("//")) {
if (line.startsWith("-"))
blacklist.add(line.substring(1));
else
whitelist.add(line);
}
line = reader.readLine();
}
reader.close();
} catch (Exception e) {
BetterFoliage.log.warn(String.format("Error reading configuration %s", defaults.toString()));
}
}
/** Caches block IDs on world load for fast lookup
* @param event
*/
@SuppressWarnings("unchecked")
@SubscribeEvent
public void handleWorldLoad(WorldEvent.Load event) {
if (!(event.world instanceof WorldClient)) return;
blockIDs.clear();
Iterator<Block> iter = Block.blockRegistry.iterator();
while (iter.hasNext()) {
Block block = iter.next();
if (matchesClass(block)) blockIDs.add(Block.blockRegistry.getIDForObject(block));
}
if (event.world instanceof WorldClient) updateBlockIDs();
}
}