made block recognition cleaner
This commit is contained in:
@@ -1,9 +1,7 @@
|
|||||||
package mods.betterfoliage.client;
|
package mods.betterfoliage.client;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import mods.betterfoliage.BetterFoliage;
|
import mods.betterfoliage.BetterFoliage;
|
||||||
import mods.betterfoliage.client.render.IRenderBlockDecorator;
|
import mods.betterfoliage.client.render.IRenderBlockDecorator;
|
||||||
@@ -15,25 +13,24 @@ 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.BlockLeavesBase;
|
import net.minecraft.block.BlockLeavesBase;
|
||||||
|
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.world.IBlockAccess;
|
import net.minecraft.world.IBlockAccess;
|
||||||
import net.minecraftforge.common.MinecraftForge;
|
import net.minecraftforge.common.MinecraftForge;
|
||||||
import net.minecraftforge.event.world.WorldEvent;
|
|
||||||
|
|
||||||
import com.google.common.collect.Maps;
|
import com.google.common.collect.Maps;
|
||||||
import com.google.common.collect.Sets;
|
|
||||||
|
|
||||||
import cpw.mods.fml.client.registry.RenderingRegistry;
|
import cpw.mods.fml.client.registry.RenderingRegistry;
|
||||||
import cpw.mods.fml.common.FMLCommonHandler;
|
import cpw.mods.fml.common.FMLCommonHandler;
|
||||||
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
|
||||||
|
|
||||||
public class BetterFoliageClient implements ILeafTextureRecognizer {
|
public class BetterFoliageClient implements ILeafTextureRecognizer {
|
||||||
|
|
||||||
public static Map<Integer, IRenderBlockDecorator> decorators = Maps.newHashMap();
|
public static Map<Integer, IRenderBlockDecorator> decorators = Maps.newHashMap();
|
||||||
public static LeafTextureGenerator leafGenerator;
|
public static LeafTextureGenerator leafGenerator;
|
||||||
|
|
||||||
public static Set<Class<?>> blockLeavesClasses = Sets.newHashSet();
|
public static BlockMatcher leaves;
|
||||||
public static Set<Integer> leafBlockIDs = Sets.newHashSet();
|
public static BlockMatcher crops;
|
||||||
|
|
||||||
public static void preInit() {
|
public static void preInit() {
|
||||||
FMLCommonHandler.instance().bus().register(new KeyHandler());
|
FMLCommonHandler.instance().bus().register(new KeyHandler());
|
||||||
@@ -44,9 +41,15 @@ public class BetterFoliageClient implements ILeafTextureRecognizer {
|
|||||||
registerRenderer(new RenderBlockBetterCactus());
|
registerRenderer(new RenderBlockBetterCactus());
|
||||||
registerRenderer(new RenderBlockBetterLilypad());
|
registerRenderer(new RenderBlockBetterLilypad());
|
||||||
|
|
||||||
blockLeavesClasses.add(BlockLeavesBase.class);
|
leaves = new BlockMatcher(BlockLeavesBase.class.getName(),
|
||||||
addLeafBlockClass("forestry.arboriculture.gadgets.BlockLeaves");
|
"forestry.arboriculture.gadgets.BlockLeaves",
|
||||||
addLeafBlockClass("thaumcraft.common.blocks.BlockMagicalLeaves");
|
"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");
|
BetterFoliage.log.info("Registering leaf texture generator");
|
||||||
leafGenerator = new LeafTextureGenerator();
|
leafGenerator = new LeafTextureGenerator();
|
||||||
@@ -81,26 +84,4 @@ public class BetterFoliageClient implements ILeafTextureRecognizer {
|
|||||||
decorator.init();
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
99
src/main/java/mods/betterfoliage/client/BlockMatcher.java
Normal file
99
src/main/java/mods/betterfoliage/client/BlockMatcher.java
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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) {
|
public boolean isBlockAccepted(IBlockAccess blockAccess, int x, int y, int z, Block block, int original) {
|
||||||
if (!Config.leavesEnabled) return false;
|
if (!Config.leavesEnabled) return false;
|
||||||
if (original > 0 && original < 42) 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) {
|
public boolean renderWorldBlock(IBlockAccess world, int x, int y, int z, Block block, int modelId, RenderBlocks renderer) {
|
||||||
|
|||||||
@@ -106,7 +106,7 @@ public class LeafTextureGenerator implements IIconRegister, IResourceManager {
|
|||||||
Iterator<Block> iter = Block.blockRegistry.iterator();
|
Iterator<Block> iter = Block.blockRegistry.iterator();
|
||||||
while(iter.hasNext()) {
|
while(iter.hasNext()) {
|
||||||
Block block = iter.next();
|
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()));
|
BetterFoliage.log.debug(String.format("Inspecting leaf block: %s", block.getClass().getName()));
|
||||||
block.registerBlockIcons(this);
|
block.registerBlockIcons(this);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user