make leaf texture generator event based and more modular
This commit is contained in:
@@ -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));
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<String> foundLeafTextures = Sets.newHashSet();
|
||||
Set<TextureAtlasSprite> 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Before Width: | Height: | Size: 219 B After Width: | Height: | Size: 219 B |
Reference in New Issue
Block a user