mod.rankshank.arbitraria.client.texture.TextureTiled.java Source code

Java tutorial

Introduction

Here is the source code for mod.rankshank.arbitraria.client.texture.TextureTiled.java

Source

/*
 * This class was created by: RANKSHANK as a part of  the Arbitraria mod.
 * Full source can be found @ https://github.com/RANKSHANK/Arbitraria
 *
 * Arbitraria is provided in binary and source forms under the Arbitraria License.
 * Arbitraria License can be found in the repository @  https://github.com/RANKSHANK/Arbitraria
 */

package mod.rankshank.arbitraria.client.texture;

import mod.rankshank.arbitraria.common.init.Arbitraria;
import net.minecraft.client.renderer.texture.AbstractTexture;
import net.minecraft.client.renderer.texture.TextureUtil;
import net.minecraft.client.resources.IResource;
import net.minecraft.client.resources.IResourceManager;
import net.minecraft.client.resources.data.TextureMetadataSection;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.apache.commons.io.IOUtils;

import java.awt.image.BufferedImage;
import java.io.IOException;

@SideOnly(Side.CLIENT)
public class TextureTiled extends AbstractTexture {

    private final ResourceLocation tileSource;

    private final int targetTile = 128;

    public TextureTiled(ResourceLocation tileSource) {
        this.tileSource = tileSource;
    }

    @Override
    public void loadTexture(IResourceManager resourceManager) throws IOException {
        this.deleteGlTexture();
        IResource iresource = null;
        try {
            iresource = resourceManager.getResource(tileSource);
            BufferedImage img = TextureUtil.readBufferedImage(iresource.getInputStream());
            boolean blur = false;
            boolean clamp = false;

            if (iresource.hasMetadata()) {
                try {
                    TextureMetadataSection texturemetadatasection = iresource.getMetadata("texture");

                    if (texturemetadatasection != null) {
                        blur = texturemetadatasection.getTextureBlur();
                        clamp = texturemetadatasection.getTextureClamp();
                    }
                } catch (RuntimeException runtimeexception) {
                    Arbitraria.error(new Throwable(
                            String.format("Issue loading tiled texture for %s, aborting", tileSource.toString()),
                            runtimeexception));
                }
            }

            int width = img.getWidth();
            int height = img.getHeight();
            if (width != height) {
                Arbitraria.error(new Throwable(String.format("Tiled image failed for %s [Width = %s, Height = %s",
                        tileSource, width, height)));
                return;
            }
            int factor = targetTile / width;

            BufferedImage finalImg = new BufferedImage(width * factor, height * factor, img.getType());

            int num = 0;
            for (int i = 0; i < factor; i++) {
                for (int j = 0; j < factor; j++) {
                    finalImg.createGraphics().drawImage(img, width * j, height * i, null);
                    num++;
                }
            }
            TextureUtil.uploadTextureImageAllocate(this.getGlTextureId(), finalImg, blur, clamp);
        } finally {
            IOUtils.closeQuietly(iresource);
        }
    }
}