made block recognition cleaner

This commit is contained in:
octarine-noise
2014-07-01 00:04:26 +02:00
parent d5dd1a36e3
commit 3a391f1677
4 changed files with 114 additions and 34 deletions

View File

@@ -1,9 +1,7 @@
package mods.betterfoliage.client;
import java.io.File;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import mods.betterfoliage.BetterFoliage;
import mods.betterfoliage.client.render.IRenderBlockDecorator;
@@ -15,25 +13,24 @@ import mods.betterfoliage.client.resource.ILeafTextureRecognizer;
import mods.betterfoliage.client.resource.LeafTextureGenerator;
import net.minecraft.block.Block;
import net.minecraft.block.BlockLeavesBase;
import net.minecraft.block.BlockTallGrass;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.init.Blocks;
import net.minecraft.world.IBlockAccess;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.world.WorldEvent;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import cpw.mods.fml.client.registry.RenderingRegistry;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
public class BetterFoliageClient implements ILeafTextureRecognizer {
public static Map<Integer, IRenderBlockDecorator> decorators = Maps.newHashMap();
public static LeafTextureGenerator leafGenerator;
public static Set<Class<?>> blockLeavesClasses = Sets.newHashSet();
public static Set<Integer> leafBlockIDs = Sets.newHashSet();
public static BlockMatcher leaves;
public static BlockMatcher crops;
public static void preInit() {
FMLCommonHandler.instance().bus().register(new KeyHandler());
@@ -44,9 +41,15 @@ public class BetterFoliageClient implements ILeafTextureRecognizer {
registerRenderer(new RenderBlockBetterCactus());
registerRenderer(new RenderBlockBetterLilypad());
blockLeavesClasses.add(BlockLeavesBase.class);
addLeafBlockClass("forestry.arboriculture.gadgets.BlockLeaves");
addLeafBlockClass("thaumcraft.common.blocks.BlockMagicalLeaves");
leaves = new BlockMatcher(BlockLeavesBase.class.getName(),
"forestry.arboriculture.gadgets.BlockLeaves",
"thaumcraft.common.blocks.BlockMagicalLeaves");
leaves.load(new File(BetterFoliage.configDir, "whitelistLeaves.cfg"));
crops = new BlockMatcher(BlockTallGrass.class.getName(),
"biomesoplenty.common.blocks.BlockBOPFlower",
"biomesoplenty.common.blocks.BlockBOPFlower2");
crops.load(new File(BetterFoliage.configDir, "whitelistCrops.cfg"));
BetterFoliage.log.info("Registering leaf texture generator");
leafGenerator = new LeafTextureGenerator();
@@ -81,26 +84,4 @@ public class BetterFoliageClient implements ILeafTextureRecognizer {
decorator.init();
}
protected static void addLeafBlockClass(String className) {
try {
blockLeavesClasses.add(Class.forName(className));
} catch(ClassNotFoundException e) {
}
}
/** Caches leaf block IDs on world load for fast lookup
* @param event
*/
@SuppressWarnings("unchecked")
@SubscribeEvent
public void handleWorldLoad(WorldEvent.Load event) {
Iterator<Block> iter = Block.blockRegistry.iterator();
while (iter.hasNext()) {
Block block = iter.next();
int blockId = Block.blockRegistry.getIDForObject(block);
for (Class<?> clazz : BetterFoliageClient.blockLeavesClasses)
if (clazz.isAssignableFrom(block.getClass()))
leafBlockIDs.add(blockId);
}
}
}

View File

@@ -0,0 +1,99 @@
package mods.betterfoliage.client;
import java.io.BufferedReader;
import java.io.File;
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;
import mods.betterfoliage.BetterFoliage;
import net.minecraft.block.Block;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.world.WorldEvent;
import com.google.common.collect.Sets;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
public class BlockMatcher {
public Set<String> defaultClasses = Sets.newHashSet();
public Set<Class<?>> classes = Sets.newHashSet();
public Set<Integer> blockIDs = Sets.newHashSet();
public BlockMatcher(String... defaults) {
Collections.addAll(defaultClasses, defaults);
MinecraftForge.EVENT_BUS.register(this);
}
public void addClass(String className) {
try {
classes.add(Class.forName(className));
} catch(ClassNotFoundException e) {
}
}
public boolean matchesClass(Block block) {
for (Class<?> clazz : classes) if (clazz.isAssignableFrom(block.getClass())) return true;
return false;
}
public boolean matchesID(int blockId) {
return blockIDs.contains(blockId);
}
public boolean matchesID(Block block) {
return blockIDs.contains(Block.blockRegistry.getIDForObject(block));
}
public void load(File file) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String line = reader.readLine();
while(line != null) {
addClass(line.trim());
line = reader.readLine();
}
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()));
}
}
public void saveDefaults(File file) {
FileWriter writer = null;
try {
writer = new FileWriter(file);
for (String clazz : defaultClasses) {
writer.write(clazz);
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
* @param event
*/
@SuppressWarnings("unchecked")
@SubscribeEvent
public void handleWorldLoad(WorldEvent.Load event) {
Iterator<Block> iter = Block.blockRegistry.iterator();
while (iter.hasNext()) {
Block block = iter.next();
if (matchesClass(block)) blockIDs.add(Block.blockRegistry.getIDForObject(block));
}
}
}

View File

@@ -24,7 +24,7 @@ public class RenderBlockBetterLeaves extends RenderBlockAOBase implements IRende
public boolean isBlockAccepted(IBlockAccess blockAccess, int x, int y, int z, Block block, int original) {
if (!Config.leavesEnabled) return false;
if (original > 0 && original < 42) return false;
return BetterFoliageClient.leafBlockIDs.contains(Block.blockRegistry.getIDForObject(block)) && !isBlockSurrounded(blockAccess, x, y, z);
return BetterFoliageClient.leaves.matchesID(block) && !isBlockSurrounded(blockAccess, x, y, z);
}
public boolean renderWorldBlock(IBlockAccess world, int x, int y, int z, Block block, int modelId, RenderBlocks renderer) {

View File

@@ -106,7 +106,7 @@ public class LeafTextureGenerator implements IIconRegister, IResourceManager {
Iterator<Block> iter = Block.blockRegistry.iterator();
while(iter.hasNext()) {
Block block = iter.next();
for (Class<?> clazz : BetterFoliageClient.blockLeavesClasses) if (clazz.isAssignableFrom(block.getClass())) {
if (BetterFoliageClient.leaves.matchesClass(block)) {
BetterFoliage.log.debug(String.format("Inspecting leaf block: %s", block.getClass().getName()));
block.registerBlockIcons(this);
}