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

View File

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