From 51589bbad427e8aaa516c46f6e44f49e8ee97860 Mon Sep 17 00:00:00 2001 From: octarine-noise Date: Sun, 10 Aug 2014 17:02:15 +0200 Subject: [PATCH] make leaf texture generator event based and more modular --- .../client/BetterFoliageClient.java | 6 +- .../client/resource/LeafGenerator.java | 85 ++------------ .../client/resource/LeafGeneratorBase.java | 111 ++++++++++++++++++ .../resource/LeafTextureEnumerator.java | 33 +++--- ...k_32_rough.png => leafmask_32_default.png} | Bin 5 files changed, 143 insertions(+), 92 deletions(-) create mode 100644 src/main/java/mods/betterfoliage/client/resource/LeafGeneratorBase.java rename src/main/resources/assets/betterfoliage/textures/blocks/{leafmask_32_rough.png => leafmask_32_default.png} (100%) diff --git a/src/main/java/mods/betterfoliage/client/BetterFoliageClient.java b/src/main/java/mods/betterfoliage/client/BetterFoliageClient.java index 09ad6c0..91473fa 100644 --- a/src/main/java/mods/betterfoliage/client/BetterFoliageClient.java +++ b/src/main/java/mods/betterfoliage/client/BetterFoliageClient.java @@ -63,11 +63,11 @@ public class BetterFoliageClient { grass.load(new File(BetterFoliage.configDir, "classesGrass.cfg"), new ResourceLocation("betterfoliage:classesGrassDefault.cfg")); MinecraftForge.EVENT_BUS.register(grass); - MinecraftForge.EVENT_BUS.register(new LeafTextureEnumerator("bf_leaves")); - BetterFoliage.log.info("Registering texture generators"); - leafGenerator = new LeafGenerator("bf_leaves", missingTexture); + leafGenerator = new LeafGenerator(); MinecraftForge.EVENT_BUS.register(leafGenerator); + MinecraftForge.EVENT_BUS.register(new LeafTextureEnumerator()); + MinecraftForge.EVENT_BUS.register(new ReedGenerator("bf_reed_bottom", missingTexture, true)); MinecraftForge.EVENT_BUS.register(new ReedGenerator("bf_reed_top", missingTexture, false)); MinecraftForge.EVENT_BUS.register(new ShortGrassGenerator("bf_shortgrass", missingTexture, false)); diff --git a/src/main/java/mods/betterfoliage/client/resource/LeafGenerator.java b/src/main/java/mods/betterfoliage/client/resource/LeafGenerator.java index 00528fa..2b03ba1 100644 --- a/src/main/java/mods/betterfoliage/client/resource/LeafGenerator.java +++ b/src/main/java/mods/betterfoliage/client/resource/LeafGenerator.java @@ -6,55 +6,28 @@ import java.io.IOException; import javax.imageio.ImageIO; -import cpw.mods.fml.common.eventhandler.SubscribeEvent; - import mods.betterfoliage.BetterFoliage; +import mods.betterfoliage.client.BetterFoliageClient; import mods.betterfoliage.client.ShadersModIntegration; -import mods.betterfoliage.common.util.Utils; import net.minecraft.client.Minecraft; -import net.minecraft.client.resources.IResource; -import net.minecraft.client.resources.IResourceManager; import net.minecraft.util.ResourceLocation; import net.minecraftforge.client.event.TextureStitchEvent.Post; -import net.minecraftforge.client.event.TextureStitchEvent.Pre; +import cpw.mods.fml.common.eventhandler.SubscribeEvent; -public class LeafGenerator extends BlockTextureGenerator { +public class LeafGenerator extends LeafGeneratorBase { - /** Resource domain name of pre-drawn textures */ - public String nonGeneratedDomain = "betterfoliage"; - - /** Number of textures generated in the current run */ - public int generatedCounter = 0; - - /** Number of pre-drawn textures found in the current run */ - public int drawnCounter = 0; - /** Name of the default alpha mask to use */ - public static String defaultMask = "rough"; + public static String defaultMask = "default"; - public LeafGenerator(String domainName, ResourceLocation missingResource) { - super(domainName, missingResource); + public LeafGenerator() { + super("bf_leaves", "betterfoliage", "textures/blocks/%s/%s", "betterfoliage:textures/blocks/leafmask_%d_%s.png", BetterFoliageClient.missingTexture); } @Override - public IResource getResource(ResourceLocation resourceLocation) throws IOException { - IResourceManager resourceManager = Minecraft.getMinecraft().getResourceManager(); - ResourceLocation originalNoDirs = unwrapResource(resourceLocation); - ResourceLocation originalWithDirs = new ResourceLocation(originalNoDirs.getResourceDomain(), "textures/blocks/" + originalNoDirs.getResourcePath()); - - // check for provided texture - ResourceLocation handDrawnLocation = new ResourceLocation(nonGeneratedDomain, String.format("textures/blocks/%s/%s", originalNoDirs.getResourceDomain(), originalNoDirs.getResourcePath())); - if (Utils.resourceExists(handDrawnLocation)) { - drawnCounter++; - return resourceManager.getResource(handDrawnLocation); - } - - // generate our own - if (!Utils.resourceExists(originalWithDirs)) return getMissingResource(); - + protected BufferedImage generateLeaf(ResourceLocation originalWithDirs) throws IOException, TextureGenerationException { // load normal leaf texture - BufferedImage origImage = ImageIO.read(resourceManager.getResource(originalWithDirs).getInputStream()); - if (origImage.getWidth() != origImage.getHeight()) return getMissingResource(); + BufferedImage origImage = ImageIO.read(Minecraft.getMinecraft().getResourceManager().getResource(originalWithDirs).getInputStream()); + if (origImage.getWidth() != origImage.getHeight()) throw new TextureGenerationException(); int size = origImage.getWidth(); // tile leaf texture 2x2 @@ -80,51 +53,15 @@ public class LeafGenerator extends BlockTextureGenerator { } } - generatedCounter++; - return new BufferedImageResource(overlayIcon); + return overlayIcon; } - /** Loads the alpha mask of the given type and size. If a mask of the exact size can not be found, - * will try to load progressively smaller masks down to 16x16 - * @param type mask type - * @param size texture size - * @return alpha mask - */ - protected BufferedImage loadLeafMaskImage(String type, int size) { - IResourceManager resourceManager = Minecraft.getMinecraft().getResourceManager(); - IResource maskResource = null; - - while (maskResource == null && size >= 16) { - try { - maskResource = resourceManager.getResource(new ResourceLocation(String.format("betterfoliage:textures/blocks/leafmask_%d_%s.png", size, type))); - } catch (Exception e) {} - size /= 2; - } - - try { - return maskResource == null ? null : ImageIO.read(maskResource.getInputStream()); - } catch (IOException e) { - return null; - } - } - - @Override - @SubscribeEvent - public void handleTextureReload(Pre event) { - super.handleTextureReload(event); - if (event.map.getTextureType() != 0) return; - generatedCounter = 0; - drawnCounter = 0; - } - @Override @SubscribeEvent public void endTextureReload(Post event) { super.endTextureReload(event); if (event.map.getTextureType() != 0) return; BetterFoliage.log.info(String.format("Found %d pre-drawn leaf textures", drawnCounter)); - BetterFoliage.log.info(String.format("Found %d leaf textures", generatedCounter)); + BetterFoliage.log.info(String.format("Generated %d leaf textures", generatedCounter)); } - - } diff --git a/src/main/java/mods/betterfoliage/client/resource/LeafGeneratorBase.java b/src/main/java/mods/betterfoliage/client/resource/LeafGeneratorBase.java new file mode 100644 index 0000000..de1a0cf --- /dev/null +++ b/src/main/java/mods/betterfoliage/client/resource/LeafGeneratorBase.java @@ -0,0 +1,111 @@ +package mods.betterfoliage.client.resource; + +import java.awt.image.BufferedImage; +import java.io.IOException; + +import javax.imageio.ImageIO; + +import mods.betterfoliage.client.resource.LeafTextureEnumerator.LeafTextureFoundEvent; +import mods.betterfoliage.common.util.Utils; +import net.minecraft.client.Minecraft; +import net.minecraft.client.resources.IResource; +import net.minecraft.client.resources.IResourceManager; +import net.minecraft.util.ResourceLocation; +import net.minecraftforge.client.event.TextureStitchEvent.Pre; +import cpw.mods.fml.common.eventhandler.SubscribeEvent; + +public abstract class LeafGeneratorBase extends BlockTextureGenerator { + + public static class TextureGenerationException extends Exception { + private static final long serialVersionUID = 7339757761980002651L; + }; + + /** Format string of pre-drawn texture location */ + public String handDrawnLocationFormat; + + /** Format string of alpha mask location */ + public String maskImageLocationFormat; + + /** Resource domain name of pre-drawn textures */ + public String nonGeneratedDomain; + + /** Number of textures generated in the current run */ + public int generatedCounter = 0; + + /** Number of pre-drawn textures found in the current run */ + public int drawnCounter = 0; + + public LeafGeneratorBase(String domain, String nonGeneratedDomain, String handDrawnLocationFormat, String maskImageLocationFormat, ResourceLocation missingResource) { + super(domain, missingResource); + this.nonGeneratedDomain = nonGeneratedDomain; + this.handDrawnLocationFormat = handDrawnLocationFormat; + this.maskImageLocationFormat = maskImageLocationFormat; + } + + @Override + public IResource getResource(ResourceLocation resourceLocation) throws IOException { + IResourceManager resourceManager = Minecraft.getMinecraft().getResourceManager(); + ResourceLocation originalNoDirs = unwrapResource(resourceLocation); + ResourceLocation originalWithDirs = new ResourceLocation(originalNoDirs.getResourceDomain(), "textures/blocks/" + originalNoDirs.getResourcePath()); + + // check for provided texture + ResourceLocation handDrawnLocation = new ResourceLocation(nonGeneratedDomain, String.format(handDrawnLocationFormat, originalNoDirs.getResourceDomain(), originalNoDirs.getResourcePath())); + if (Utils.resourceExists(handDrawnLocation)) { + drawnCounter++; + return resourceManager.getResource(handDrawnLocation); + } + + // generate our own + if (!Utils.resourceExists(originalWithDirs)) return getMissingResource(); + + BufferedImage result; + try { + result = generateLeaf(originalWithDirs); + } catch (TextureGenerationException e) { + return getMissingResource(); + } + generatedCounter++; + return new BufferedImageResource(result); + } + + protected abstract BufferedImage generateLeaf(ResourceLocation originalWithDirs) throws IOException, TextureGenerationException; + + /** Loads the alpha mask of the given type and size. If a mask of the exact size can not be found, + * will try to load progressively smaller masks down to 16x16 + * @param type mask type + * @param size texture size + * @return alpha mask + */ + protected BufferedImage loadLeafMaskImage(String type, int size) { + IResourceManager resourceManager = Minecraft.getMinecraft().getResourceManager(); + IResource maskResource = null; + + while (maskResource == null && size >= 1) { + try { + maskResource = resourceManager.getResource(new ResourceLocation(String.format(maskImageLocationFormat, size, type))); + } catch (Exception e) {} + size /= 2; + } + + try { + return maskResource == null ? null : ImageIO.read(maskResource.getInputStream()); + } catch (IOException e) { + return null; + } + } + + @Override + @SubscribeEvent + public void handleTextureReload(Pre event) { + super.handleTextureReload(event); + if (event.map.getTextureType() != 0) return; + generatedCounter = 0; + drawnCounter = 0; + } + + @SubscribeEvent + public void handleRegisterTexture(LeafTextureFoundEvent event) { + event.blockTextures.registerIcon(new ResourceLocation(domainName, event.icon.getIconName()).toString()); + } + +} diff --git a/src/main/java/mods/betterfoliage/client/resource/LeafTextureEnumerator.java b/src/main/java/mods/betterfoliage/client/resource/LeafTextureEnumerator.java index 4a16f29..b746c94 100644 --- a/src/main/java/mods/betterfoliage/client/resource/LeafTextureEnumerator.java +++ b/src/main/java/mods/betterfoliage/client/resource/LeafTextureEnumerator.java @@ -13,32 +13,36 @@ import net.minecraft.client.renderer.texture.IIconRegister; import net.minecraft.client.renderer.texture.TextureAtlasSprite; import net.minecraft.client.renderer.texture.TextureMap; import net.minecraft.util.IIcon; -import net.minecraft.util.ResourceLocation; import net.minecraftforge.client.event.TextureStitchEvent; +import net.minecraftforge.common.MinecraftForge; import com.google.common.collect.Sets; +import cpw.mods.fml.common.eventhandler.Event; import cpw.mods.fml.common.eventhandler.SubscribeEvent; public class LeafTextureEnumerator implements IIconRegister { - /** Resource domain name of generated textures */ - public String domainName; + public static class LeafTextureFoundEvent extends Event { + public TextureMap blockTextures; + public TextureAtlasSprite icon; + private LeafTextureFoundEvent(TextureMap blockTextures, TextureAtlasSprite icon) { + super(); + this.blockTextures = blockTextures; + this.icon = icon; + } + } /** Texture atlas for block textures used in the current run */ public TextureMap blockTextures; - public LeafTextureEnumerator(String domainName) { - this.domainName = domainName; - } - /** Leaf blocks register their textures here. An extra texture will be registered in the atlas * for each, with the resource domain of this generator. * @return the originally registered {@link IIcon} already in the atlas */ public IIcon registerIcon(String resourceLocation) { - IIcon original = blockTextures.getTextureExtry(resourceLocation); - blockTextures.registerIcon(new ResourceLocation(domainName, resourceLocation).toString()); + TextureAtlasSprite original = blockTextures.getTextureExtry(resourceLocation); + MinecraftForge.EVENT_BUS.post(new LeafTextureFoundEvent(blockTextures, original)); BetterFoliage.log.debug(String.format("Found leaf texture: %s", resourceLocation)); return original; } @@ -72,13 +76,12 @@ public class LeafTextureEnumerator implements IIconRegister { if (mapAtlas == null) { BetterFoliage.log.warn("Failed to reflect texture atlas, textures may be missing"); } else { - Set foundLeafTextures = Sets.newHashSet(); + Set foundLeafTextures = Sets.newHashSet(); for (TextureAtlasSprite icon : mapAtlas.values()) - if (BetterFoliageClient.isLeafTexture(icon)) - foundLeafTextures.add(icon.getIconName()); - for (String resourceLocation : foundLeafTextures) { - BetterFoliage.log.debug(String.format("Found non-block-registered leaf texture: %s", resourceLocation)); - blockTextures.registerIcon(new ResourceLocation(domainName, resourceLocation).toString()); + if (BetterFoliageClient.isLeafTexture(icon)) foundLeafTextures.add(icon); + for (TextureAtlasSprite icon : foundLeafTextures) { + BetterFoliage.log.debug(String.format("Found non-block-registered leaf texture: %s", icon.getIconName())); + MinecraftForge.EVENT_BUS.post(new LeafTextureFoundEvent(blockTextures, icon)); } } } diff --git a/src/main/resources/assets/betterfoliage/textures/blocks/leafmask_32_rough.png b/src/main/resources/assets/betterfoliage/textures/blocks/leafmask_32_default.png similarity index 100% rename from src/main/resources/assets/betterfoliage/textures/blocks/leafmask_32_rough.png rename to src/main/resources/assets/betterfoliage/textures/blocks/leafmask_32_default.png