blacklist / whitelist for block matcher

This commit is contained in:
octarine-noise
2014-07-01 00:36:31 +02:00
parent 3a391f1677
commit 8a94867cd8
2 changed files with 43 additions and 16 deletions

View File

@@ -12,7 +12,12 @@ import mods.betterfoliage.client.render.impl.RenderBlockBetterLilypad;
import mods.betterfoliage.client.resource.ILeafTextureRecognizer;
import mods.betterfoliage.client.resource.LeafTextureGenerator;
import net.minecraft.block.Block;
import net.minecraft.block.BlockCarrot;
import net.minecraft.block.BlockCrops;
import net.minecraft.block.BlockDoublePlant;
import net.minecraft.block.BlockLeavesBase;
import net.minecraft.block.BlockPotato;
import net.minecraft.block.BlockReed;
import net.minecraft.block.BlockTallGrass;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.init.Blocks;
@@ -44,12 +49,18 @@ public class BetterFoliageClient implements ILeafTextureRecognizer {
leaves = new BlockMatcher(BlockLeavesBase.class.getName(),
"forestry.arboriculture.gadgets.BlockLeaves",
"thaumcraft.common.blocks.BlockMagicalLeaves");
leaves.load(new File(BetterFoliage.configDir, "whitelistLeaves.cfg"));
leaves.load(new File(BetterFoliage.configDir, "classesLeaves.cfg"));
crops = new BlockMatcher(BlockTallGrass.class.getName(),
crops = new BlockMatcher(BlockCrops.class.getName(),
"-" + BlockCarrot.class.getName(),
"-" + BlockPotato.class.getName(),
BlockTallGrass.class.getName(),
BlockDoublePlant.class.getName(),
BlockReed.class.getName(),
"biomesoplenty.common.blocks.BlockBOPFlower",
"biomesoplenty.common.blocks.BlockBOPFlower2");
crops.load(new File(BetterFoliage.configDir, "whitelistCrops.cfg"));
"biomesoplenty.common.blocks.BlockBOPFlower2",
"tconstruct.blocks.slime.SlimeTallGrass");
crops.load(new File(BetterFoliage.configDir, "classesCrops.cfg"));
BetterFoliage.log.info("Registering leaf texture generator");
leafGenerator = new LeafTextureGenerator();

View File

@@ -6,7 +6,6 @@ import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
@@ -21,24 +20,35 @@ import cpw.mods.fml.common.eventhandler.SubscribeEvent;
public class BlockMatcher {
public Set<String> defaultClasses = Sets.newHashSet();
public Set<Class<?>> classes = Sets.newHashSet();
public Set<String> whiteList = Sets.newHashSet();
public Set<String> blackList = Sets.newHashSet();
public Set<Integer> blockIDs = Sets.newHashSet();
public BlockMatcher(String... defaults) {
Collections.addAll(defaultClasses, defaults);
for (String clazz : defaults) addClass(clazz);
MinecraftForge.EVENT_BUS.register(this);
}
public void addClass(String className) {
try {
classes.add(Class.forName(className));
} catch(ClassNotFoundException e) {
}
if (className.startsWith("-"))
blackList.add(className.substring(1));
else
whiteList.add(className);
}
public boolean matchesClass(Block block) {
for (Class<?> clazz : classes) if (clazz.isAssignableFrom(block.getClass())) return true;
for (String className : blackList) {
try {
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;
}
@@ -54,6 +64,8 @@ public class BlockMatcher {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
whiteList.clear();
blackList.clear();
String line = reader.readLine();
while(line != null) {
addClass(line.trim());
@@ -62,7 +74,6 @@ public class BlockMatcher {
reader.close();
} catch (FileNotFoundException e) {
saveDefaults(file);
for (String clazz : defaultClasses) addClass(clazz);
} catch (IOException e) {
BetterFoliage.log.warn(String.format("Error reading configuration: %s", file.getName()));
}
@@ -72,8 +83,13 @@ public class BlockMatcher {
FileWriter writer = null;
try {
writer = new FileWriter(file);
for (String clazz : defaultClasses) {
writer.write(clazz);
for (String className : whiteList) {
writer.write(className);
writer.write("\n");
}
for (String className : blackList) {
writer.write("-");
writer.write(className);
writer.write("\n");
}
writer.close();