Cache leaf block IDs

This commit is contained in:
octarine-noise
2014-06-30 22:15:57 +02:00
parent 220f2356d8
commit 37ffa219fc
2 changed files with 25 additions and 5 deletions

View File

@@ -1,6 +1,7 @@
package mods.betterfoliage.client;
import java.io.File;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
@@ -17,19 +18,23 @@ import net.minecraft.block.BlockLeavesBase;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
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 Set<Class<?>> blockLeavesClasses = Sets.newHashSet();
public static LeafTextureGenerator leafGenerator;
public static Set<Class<?>> blockLeavesClasses = Sets.newHashSet();
public static Set<Integer> leafBlockIDs = Sets.newHashSet();
public static void preInit() {
FMLCommonHandler.instance().bus().register(new KeyHandler());
@@ -48,6 +53,8 @@ public class BetterFoliageClient implements ILeafTextureRecognizer {
MinecraftForge.EVENT_BUS.register(leafGenerator);
leafGenerator.recognizers.add(new BetterFoliageClient());
leafGenerator.loadLeafMappings(new File(BetterFoliage.configDir, "leafMask.properties"));
MinecraftForge.EVENT_BUS.register(new BetterFoliageClient());
}
public boolean isLeafTexture(TextureAtlasSprite icon) {
@@ -81,4 +88,20 @@ public class BetterFoliageClient implements ILeafTextureRecognizer {
} 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

@@ -24,10 +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;
for (Class<?> clazz : BetterFoliageClient.blockLeavesClasses)
if (clazz.isAssignableFrom(block.getClass()))
return !isBlockSurrounded(blockAccess, x, y, z);
return false;
return BetterFoliageClient.leafBlockIDs.contains(Block.blockRegistry.getIDForObject(block)) && !isBlockSurrounded(blockAccess, x, y, z);
}
public boolean renderWorldBlock(IBlockAccess world, int x, int y, int z, Block block, int modelId, RenderBlocks renderer) {