All Downloads are FREE. Search and download functionalities are using the official Maven repository.

se.llbit.chunky.resources.texturepack.TextureRef Maven / Gradle / Ivy

There is a newer version: 1.4.5
Show newest version
/* Copyright (c) 2013 Jesper Öqvist 
 *
 * This file is part of Chunky.
 *
 * Chunky is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Chunky is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with Chunky.  If not, see .
 */
package se.llbit.chunky.resources.texturepack;

import se.llbit.chunky.resources.BitmapImage;
import se.llbit.log.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * Reference to a texture file in a Minecraft texture pack
 *
 * @author Jesper Öqvist 
 */
public abstract class TextureRef {

  /**
   * Default constructor
   */
  protected TextureRef() {
  }

  /**
   * Attempt to load a texture from a texture pack
   *
   * @param texturePack Reference to the texture pack zip file
   * @return true if the texture was successfully loaded
   */
  public abstract boolean load(ZipFile texturePack);

  /**
   * Attempt to load a texture from a PNG image file.
   *
   * @param file the texture file
   * @return true if the texture was successfully loaded
   * @throws TextureFormatError
   * @throws IOException
   */
  public boolean load(File file) throws IOException, TextureFormatError {
    FileInputStream in = new FileInputStream(file);
    boolean result = load(in);
    in.close();
    return result;
  }

  /**
   * Attempt to load a texture from a texture pack
   *
   * @param file        Path of texture in texture pack
   * @param texturePack Reference to the texture pack zip file
   * @return true if the texture was successfully loaded
   */
  protected boolean load(String file, ZipFile texturePack) {
    try (InputStream in = texturePack.getInputStream(new ZipEntry(file + ".png"))) {
      if (in != null) {
        return load(in);
      }
    } catch (TextureFormatError e) {
      Log.info(e.getMessage());
    } catch (IOException e) {
      // Safe to ignore - will be handled implicitly later.
    }
    return false;
  }

  /**
   * Load this texture from the terrain spritemap.
   *
   * @return true if the texture was successfully loaded
   */
  public boolean loadFromTerrain(BitmapImage[] terrain) {
    return false;
  }

  protected abstract boolean load(InputStream imageStream) throws IOException, TextureFormatError;
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy