new feature: use pre-drawn leaf textures if available
some refactoring
This commit is contained in:
@@ -1,11 +1,14 @@
|
|||||||
package mods.betterfoliage.client.render;
|
package mods.betterfoliage.client.render;
|
||||||
|
|
||||||
|
import cpw.mods.fml.relauncher.Side;
|
||||||
|
import cpw.mods.fml.relauncher.SideOnly;
|
||||||
import net.minecraft.block.Block;
|
import net.minecraft.block.Block;
|
||||||
import net.minecraft.util.IIcon;
|
import net.minecraft.util.IIcon;
|
||||||
|
|
||||||
/** Same as {@link RenderBlockAOBase}, but does not actually render anything.
|
/** Same as {@link RenderBlockAOBase}, but does not actually render anything.
|
||||||
* @author octarine-noise
|
* @author octarine-noise
|
||||||
*/
|
*/
|
||||||
|
@SideOnly(Side.CLIENT)
|
||||||
public class FakeRenderBlockAOBase extends RenderBlockAOBase {
|
public class FakeRenderBlockAOBase extends RenderBlockAOBase {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
package mods.betterfoliage.client.render;
|
package mods.betterfoliage.client.render;
|
||||||
|
|
||||||
import cpw.mods.fml.client.registry.ISimpleBlockRenderingHandler;
|
import cpw.mods.fml.client.registry.ISimpleBlockRenderingHandler;
|
||||||
|
import cpw.mods.fml.relauncher.Side;
|
||||||
|
import cpw.mods.fml.relauncher.SideOnly;
|
||||||
import net.minecraft.block.Block;
|
import net.minecraft.block.Block;
|
||||||
import net.minecraft.world.IBlockAccess;
|
import net.minecraft.world.IBlockAccess;
|
||||||
|
|
||||||
|
@SideOnly(Side.CLIENT)
|
||||||
public interface IRenderBlockDecorator extends ISimpleBlockRenderingHandler {
|
public interface IRenderBlockDecorator extends ISimpleBlockRenderingHandler {
|
||||||
|
|
||||||
public void init();
|
public void init();
|
||||||
|
|||||||
@@ -1,17 +1,16 @@
|
|||||||
package mods.betterfoliage.client.render;
|
package mods.betterfoliage.client.render;
|
||||||
|
|
||||||
import java.io.IOException;
|
import mods.betterfoliage.common.util.Utils;
|
||||||
|
|
||||||
import net.minecraft.client.Minecraft;
|
|
||||||
import net.minecraft.client.renderer.texture.IIconRegister;
|
import net.minecraft.client.renderer.texture.IIconRegister;
|
||||||
import net.minecraft.client.resources.IResource;
|
|
||||||
import net.minecraft.client.resources.IResourceManager;
|
|
||||||
import net.minecraft.util.IIcon;
|
import net.minecraft.util.IIcon;
|
||||||
import net.minecraft.util.ResourceLocation;
|
import net.minecraft.util.ResourceLocation;
|
||||||
|
import cpw.mods.fml.relauncher.Side;
|
||||||
|
import cpw.mods.fml.relauncher.SideOnly;
|
||||||
|
|
||||||
/** Loads an indexed set of textures
|
/** Loads an indexed set of textures
|
||||||
* @author octarine-noise
|
* @author octarine-noise
|
||||||
*/
|
*/
|
||||||
|
@SideOnly(Side.CLIENT)
|
||||||
public class IconSet {
|
public class IconSet {
|
||||||
|
|
||||||
/** Icon array */
|
/** Icon array */
|
||||||
@@ -33,18 +32,13 @@ public class IconSet {
|
|||||||
|
|
||||||
public void registerIcons(IIconRegister register) {
|
public void registerIcons(IIconRegister register) {
|
||||||
numLoaded = 0;
|
numLoaded = 0;
|
||||||
IResourceManager manager = Minecraft.getMinecraft().getResourceManager();
|
|
||||||
|
|
||||||
for (int idx = 0; idx < 16; idx++) {
|
for (int idx = 0; idx < 16; idx++) {
|
||||||
icons[idx] = null;
|
icons[idx] = null;
|
||||||
// if the path contains a domain, use that to check if the resource exists
|
// if the path contains a domain, use that to check if the resource exists
|
||||||
String resolvedDomain = path.contains(":") ? new ResourceLocation(path).getResourceDomain() : domain;
|
String resolvedDomain = path.contains(":") ? new ResourceLocation(path).getResourceDomain() : domain;
|
||||||
String resolvedPath = String.format("textures/blocks/" + (path.contains(":") ? new ResourceLocation(path).getResourcePath() : path) + ".png", idx);
|
String resolvedPath = String.format("textures/blocks/" + (path.contains(":") ? new ResourceLocation(path).getResourcePath() : path) + ".png", idx);
|
||||||
try {
|
if (Utils.resourceExists(new ResourceLocation(resolvedDomain, resolvedPath)))
|
||||||
IResource resource = manager.getResource(new ResourceLocation(resolvedDomain, resolvedPath));
|
icons[numLoaded++] = register.registerIcon(domain + ":" + String.format(path, idx));
|
||||||
if (resource != null) icons[numLoaded++] = register.registerIcon(domain + ":" + String.format(path, idx));
|
|
||||||
} catch (IOException e) {
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import mods.betterfoliage.client.BetterFoliageClient;
|
|||||||
import mods.betterfoliage.client.render.IRenderBlockDecorator;
|
import mods.betterfoliage.client.render.IRenderBlockDecorator;
|
||||||
import mods.betterfoliage.client.render.RenderBlockAOBase;
|
import mods.betterfoliage.client.render.RenderBlockAOBase;
|
||||||
import mods.betterfoliage.common.util.Double3;
|
import mods.betterfoliage.common.util.Double3;
|
||||||
import mods.betterfoliage.common.util.ReflectionUtil;
|
import mods.betterfoliage.common.util.Utils;
|
||||||
import net.minecraft.block.Block;
|
import net.minecraft.block.Block;
|
||||||
import net.minecraft.client.Minecraft;
|
import net.minecraft.client.Minecraft;
|
||||||
import net.minecraft.client.renderer.RenderBlocks;
|
import net.minecraft.client.renderer.RenderBlocks;
|
||||||
@@ -37,7 +37,7 @@ public class RenderBlockBetterLeaves extends RenderBlockAOBase implements IRende
|
|||||||
if (block.getRenderType() == 0) {
|
if (block.getRenderType() == 0) {
|
||||||
renderStandardBlock(block, x, y, z);
|
renderStandardBlock(block, x, y, z);
|
||||||
} else {
|
} else {
|
||||||
ISimpleBlockRenderingHandler handler = ReflectionUtil.getRenderingHandler(block.getRenderType());
|
ISimpleBlockRenderingHandler handler = Utils.getRenderingHandler(block.getRenderType());
|
||||||
handler.renderWorldBlock(world, x, y, z, block, block.getRenderType(), this);
|
handler.renderWorldBlock(world, x, y, z, block, block.getRenderType(), this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,7 +20,10 @@ import net.minecraftforge.client.event.TextureStitchEvent;
|
|||||||
import net.minecraftforge.common.util.ForgeDirection;
|
import net.minecraftforge.common.util.ForgeDirection;
|
||||||
import net.minecraftforge.event.world.WorldEvent;
|
import net.minecraftforge.event.world.WorldEvent;
|
||||||
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
||||||
|
import cpw.mods.fml.relauncher.Side;
|
||||||
|
import cpw.mods.fml.relauncher.SideOnly;
|
||||||
|
|
||||||
|
@SideOnly(Side.CLIENT)
|
||||||
public class RenderBlockBetterReed extends RenderBlockAOBase implements IRenderBlockDecorator {
|
public class RenderBlockBetterReed extends RenderBlockAOBase implements IRenderBlockDecorator {
|
||||||
|
|
||||||
public IconSet reedBottomIcons = new IconSet("bf_reed_bottom", "bettergrassandleaves:better_reed_%d");
|
public IconSet reedBottomIcons = new IconSet("bf_reed_bottom", "bettergrassandleaves:better_reed_%d");
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import java.util.Map;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import mods.betterfoliage.BetterFoliage;
|
import mods.betterfoliage.BetterFoliage;
|
||||||
import mods.betterfoliage.common.util.ReflectionUtil;
|
import mods.betterfoliage.common.util.Utils;
|
||||||
import net.minecraft.client.Minecraft;
|
import net.minecraft.client.Minecraft;
|
||||||
import net.minecraft.client.renderer.texture.TextureMap;
|
import net.minecraft.client.renderer.texture.TextureMap;
|
||||||
import net.minecraft.client.resources.IResource;
|
import net.minecraft.client.resources.IResource;
|
||||||
@@ -18,7 +18,10 @@ import com.google.common.collect.ImmutableList;
|
|||||||
import com.google.common.collect.ImmutableSet;
|
import com.google.common.collect.ImmutableSet;
|
||||||
|
|
||||||
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
||||||
|
import cpw.mods.fml.relauncher.Side;
|
||||||
|
import cpw.mods.fml.relauncher.SideOnly;
|
||||||
|
|
||||||
|
@SideOnly(Side.CLIENT)
|
||||||
public abstract class BlockTextureGenerator implements IResourceManager {
|
public abstract class BlockTextureGenerator implements IResourceManager {
|
||||||
|
|
||||||
/** Resource domain name of generated textures */
|
/** Resource domain name of generated textures */
|
||||||
@@ -48,7 +51,7 @@ public abstract class BlockTextureGenerator implements IResourceManager {
|
|||||||
|
|
||||||
blockTextures = event.map;
|
blockTextures = event.map;
|
||||||
counter = 0;
|
counter = 0;
|
||||||
Map<String, IResourceManager> domainManagers = ReflectionUtil.getDomainResourceManagers();
|
Map<String, IResourceManager> domainManagers = Utils.getDomainResourceManagers();
|
||||||
if (domainManagers == null) {
|
if (domainManagers == null) {
|
||||||
BetterFoliage.log.warn("Failed to inject texture generator");
|
BetterFoliage.log.warn("Failed to inject texture generator");
|
||||||
return;
|
return;
|
||||||
@@ -64,7 +67,7 @@ public abstract class BlockTextureGenerator implements IResourceManager {
|
|||||||
if (event.map.getTextureType() != 0) return;
|
if (event.map.getTextureType() != 0) return;
|
||||||
|
|
||||||
// don't leave a mess
|
// don't leave a mess
|
||||||
Map<String, IResourceManager> domainManagers = ReflectionUtil.getDomainResourceManagers();
|
Map<String, IResourceManager> domainManagers = Utils.getDomainResourceManagers();
|
||||||
if (domainManagers != null) domainManagers.remove(domainName);
|
if (domainManagers != null) domainManagers.remove(domainName);
|
||||||
|
|
||||||
onStitchEnd(event);
|
onStitchEnd(event);
|
||||||
|
|||||||
@@ -9,8 +9,9 @@ import java.util.Set;
|
|||||||
import mods.betterfoliage.BetterFoliage;
|
import mods.betterfoliage.BetterFoliage;
|
||||||
import mods.betterfoliage.client.BetterFoliageClient;
|
import mods.betterfoliage.client.BetterFoliageClient;
|
||||||
import mods.betterfoliage.common.util.DeobfNames;
|
import mods.betterfoliage.common.util.DeobfNames;
|
||||||
import mods.betterfoliage.common.util.ReflectionUtil;
|
import mods.betterfoliage.common.util.Utils;
|
||||||
import net.minecraft.block.Block;
|
import net.minecraft.block.Block;
|
||||||
|
import net.minecraft.client.Minecraft;
|
||||||
import net.minecraft.client.renderer.texture.IIconRegister;
|
import net.minecraft.client.renderer.texture.IIconRegister;
|
||||||
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
||||||
import net.minecraft.client.resources.IResource;
|
import net.minecraft.client.resources.IResource;
|
||||||
@@ -31,6 +32,10 @@ import cpw.mods.fml.relauncher.SideOnly;
|
|||||||
@SideOnly(Side.CLIENT)
|
@SideOnly(Side.CLIENT)
|
||||||
public class LeafTextureGenerator extends BlockTextureGenerator implements IIconRegister {
|
public class LeafTextureGenerator extends BlockTextureGenerator implements IIconRegister {
|
||||||
|
|
||||||
|
public String nonGeneratedDomain = "betterfoliage";
|
||||||
|
|
||||||
|
public int nonGeneratedCounter = 0;
|
||||||
|
|
||||||
public LeafTextureGenerator() {
|
public LeafTextureGenerator() {
|
||||||
super("bf_leaves_autogen", new ResourceLocation("betterfoliage", "textures/blocks/missing_leaf.png"));
|
super("bf_leaves_autogen", new ResourceLocation("betterfoliage", "textures/blocks/missing_leaf.png"));
|
||||||
}
|
}
|
||||||
@@ -39,7 +44,17 @@ public class LeafTextureGenerator extends BlockTextureGenerator implements IIcon
|
|||||||
public List<ILeafTextureRecognizer> recognizers = Lists.newLinkedList();
|
public List<ILeafTextureRecognizer> recognizers = Lists.newLinkedList();
|
||||||
|
|
||||||
public IResource getResource(ResourceLocation resourceLocation) throws IOException {
|
public IResource getResource(ResourceLocation resourceLocation) throws IOException {
|
||||||
LeafTextureResource result = new LeafTextureResource(unwrapResource(resourceLocation), getMissingResource());
|
ResourceLocation original = unwrapResource(resourceLocation);
|
||||||
|
|
||||||
|
// check for provided texture
|
||||||
|
ResourceLocation handDrawnLocation = new ResourceLocation(nonGeneratedDomain, String.format("textures/blocks/%s/%s", original.getResourceDomain(), original.getResourcePath()));
|
||||||
|
if (Utils.resourceExists(handDrawnLocation)) {
|
||||||
|
nonGeneratedCounter++;
|
||||||
|
return Minecraft.getMinecraft().getResourceManager().getResource(handDrawnLocation);
|
||||||
|
}
|
||||||
|
|
||||||
|
// generate our own
|
||||||
|
LeafTextureResource result = new LeafTextureResource(original, getMissingResource());
|
||||||
if (result.data != null) counter++;
|
if (result.data != null) counter++;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -62,6 +77,7 @@ public class LeafTextureGenerator extends BlockTextureGenerator implements IIcon
|
|||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@Override
|
@Override
|
||||||
public void onStitchStart(Pre event) {
|
public void onStitchStart(Pre event) {
|
||||||
|
nonGeneratedCounter = 0;
|
||||||
BetterFoliage.log.info("Reloading leaf textures");
|
BetterFoliage.log.info("Reloading leaf textures");
|
||||||
|
|
||||||
// register simple block textures
|
// register simple block textures
|
||||||
@@ -76,8 +92,8 @@ public class LeafTextureGenerator extends BlockTextureGenerator implements IIcon
|
|||||||
|
|
||||||
// enumerate all registered textures, find leaf textures among them
|
// enumerate all registered textures, find leaf textures among them
|
||||||
Map<String, TextureAtlasSprite> mapAtlas = null;
|
Map<String, TextureAtlasSprite> mapAtlas = null;
|
||||||
mapAtlas = ReflectionUtil.getField(blockTextures, DeobfNames.TM_MRS_SRG, Map.class);
|
mapAtlas = Utils.getField(blockTextures, DeobfNames.TM_MRS_SRG, Map.class);
|
||||||
if (mapAtlas == null) mapAtlas = ReflectionUtil.getField(blockTextures, DeobfNames.TM_MRS_MCP, Map.class);
|
if (mapAtlas == null) mapAtlas = Utils.getField(blockTextures, DeobfNames.TM_MRS_MCP, Map.class);
|
||||||
if (mapAtlas == null) {
|
if (mapAtlas == null) {
|
||||||
BetterFoliage.log.warn("Failed to reflect texture atlas, textures may be missing");
|
BetterFoliage.log.warn("Failed to reflect texture atlas, textures may be missing");
|
||||||
} else {
|
} else {
|
||||||
@@ -95,6 +111,7 @@ public class LeafTextureGenerator extends BlockTextureGenerator implements IIcon
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onStitchEnd(Post event) {
|
public void onStitchEnd(Post event) {
|
||||||
|
BetterFoliage.log.info(String.format("Found %d pre-drawn leaf textures", nonGeneratedCounter));
|
||||||
BetterFoliage.log.info(String.format("Generated %d leaf textures", counter));
|
BetterFoliage.log.info(String.format("Generated %d leaf textures", counter));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,16 +1,19 @@
|
|||||||
package mods.betterfoliage.common.util;
|
package mods.betterfoliage.common.util;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import net.minecraft.client.Minecraft;
|
import net.minecraft.client.Minecraft;
|
||||||
|
import net.minecraft.client.resources.IResource;
|
||||||
import net.minecraft.client.resources.IResourceManager;
|
import net.minecraft.client.resources.IResourceManager;
|
||||||
|
import net.minecraft.util.ResourceLocation;
|
||||||
import cpw.mods.fml.client.registry.ISimpleBlockRenderingHandler;
|
import cpw.mods.fml.client.registry.ISimpleBlockRenderingHandler;
|
||||||
import cpw.mods.fml.client.registry.RenderingRegistry;
|
import cpw.mods.fml.client.registry.RenderingRegistry;
|
||||||
|
|
||||||
public class ReflectionUtil {
|
public class Utils {
|
||||||
|
|
||||||
private ReflectionUtil() {}
|
private Utils() {}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public static Map<String, IResourceManager> getDomainResourceManagers() {
|
public static Map<String, IResourceManager> getDomainResourceManagers() {
|
||||||
@@ -55,4 +58,13 @@ public class ReflectionUtil {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean resourceExists(ResourceLocation resourceLocation) {
|
||||||
|
try {
|
||||||
|
IResource resource = Minecraft.getMinecraft().getResourceManager().getResource(resourceLocation);
|
||||||
|
if (resource != null) return true;
|
||||||
|
} catch (IOException e) {
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user