Skip to content

Instantly share code, notes, and snippets.

@ljfa-ag
Last active Feb 10, 2016
Embed
What would you like to do?
An example for rendering blocks into GUIs. Video: http://gfycat.com/LivelyUnimportantBullfrog
import java.util.Random;
import org.apache.logging.log4j.Level;
import org.lwjgl.opengl.GL11;
import cpw.mods.fml.common.FMLLog;
import net.minecraft.block.Block;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.renderer.RenderBlocks;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher;
import net.minecraft.tileentity.TileEntity;
public class GuiTest extends GuiScreen {
private int guiWidth = 256;
private int guiHeight = 256;
private final TestBlockAccess tba;
private final RenderBlocks rb;
public GuiTest() {
int maxX = 4, maxY = 4, maxZ = 4;
tba = new TestBlockAccess(maxX, maxY, maxZ);
Random rand = new Random();
for(int x = 0; x < maxX; x++)
for(int y = 0; y < maxY; y++)
for(int z = 0; z < maxZ; z++) {
Block block = Block.getBlockById(rand.nextInt(176));
int meta = 0;
tba.blocks[x][y][z] = block;
//tba.metas[x][y][z] = (byte)meta;
if(block.hasTileEntity(meta))
tba.tiles[x][y][z] = block.createTileEntity(null, meta);
}
rb = new RenderBlocks(tba);
}
@Override
public void drawScreen(int mouseX, int mouseY, float partialTick) {
Tessellator tes = Tessellator.instance;
TileEntityRendererDispatcher terd = TileEntityRendererDispatcher.instance;
drawDefaultBackground();
mc.getTextureManager().bindTexture(optionsBackground);
GL11.glColor3f(0.5f, 0.5f, 0.5f);
drawTexturedModalRect((width - guiWidth) / 2, (height - guiHeight) / 2, 0, 0, guiWidth, guiHeight);
super.drawScreen(mouseX, mouseY, partialTick);
float scale = 32;
float rotX = 30 + (mouseY - height/2);
float rotY = 45 + (mouseX - width/2);
mc.getTextureManager().bindTexture(mc.getTextureManager().getResourceLocation(0));
GL11.glPushMatrix();
GL11.glTranslatef((width)/2, (height)/2, 256);
GL11.glScalef(scale, -scale, scale);
GL11.glRotatef(rotX, 1, 0, 0);
GL11.glRotatef(rotY, 0, 1, 0);
GL11.glTranslatef(-tba.xSize/2, -tba.ySize/2, -tba.zSize/2);
tes.startDrawingQuads();
for(int x = 0; x < tba.xSize; x++)
for(int y = 0; y < tba.ySize; y++)
for(int z = 0; z < tba.zSize; z++) {
Block block = tba.getBlock(x, y, z);
rb.renderBlockByRenderType(block, x, y, z);
}
tes.draw();
for(int x = 0; x < tba.xSize; x++)
for(int y = 0; y < tba.ySize; y++)
for(int z = 0; z < tba.zSize; z++) {
TileEntity te = tba.getTileEntity(x, y, z);
if(te != null) {
try {
terd.renderTileEntityAt(te, x, y, z, partialTick);
}
catch(Exception e) { //Some vanilla tile entities don't like it when their world is null
FMLLog.log("test", Level.ERROR, e, "Could not render tile entity %s, removing it", te);
tba.tiles[x][y][z] = null;
}
}
}
GL11.glPopMatrix();
}
}
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.biome.BiomeGenBase;
import net.minecraftforge.common.util.ForgeDirection;
public class TestBlockAccess implements IBlockAccess {
public final Block[][][] blocks;
public final byte[][][] metas;
public final TileEntity[][][] tiles;
public final int xSize, ySize, zSize;
public TestBlockAccess(int xSize, int ySize, int zSize) {
blocks = new Block[xSize][ySize][zSize];
metas = new byte[xSize][ySize][zSize];
tiles = new TileEntity[xSize][ySize][zSize];
this.xSize = xSize;
this.ySize = ySize;
this.zSize = zSize;
}
public boolean isInRange(int x, int y, int z) {
return 0 <= x && x < xSize
&& 0 <= y && y < ySize
&& 0 <= z && z < zSize;
}
@Override
public Block getBlock(int x, int y, int z) {
if(isInRange(x, y, z))
return blocks[x][y][z];
else
return Blocks.air;
}
@Override
public TileEntity getTileEntity(int x, int y, int z) {
if(isInRange(x, y, z))
return tiles[x][y][z];
else
return null;
}
@Override
public int getLightBrightnessForSkyBlocks(int p_72802_1_, int p_72802_2_, int p_72802_3_, int p_72802_4_) {
return 0;
}
@Override
public int getBlockMetadata(int x, int y, int z) {
if(isInRange(x, y, z))
return metas[x][y][z];
else
return 0;
}
@Override
public int isBlockProvidingPowerTo(int p_72879_1_, int p_72879_2_, int p_72879_3_, int p_72879_4_) {
return 0;
}
@Override
public boolean isAirBlock(int x, int y, int z) {
return getBlock(x, y, z).isAir(this, x, y, z);
}
@Override
public BiomeGenBase getBiomeGenForCoords(int p_72807_1_, int p_72807_2_) {
return BiomeGenBase.plains;
}
@Override
public int getHeight() {
return 0;
}
@Override
public boolean extendedLevelsInChunkCache() {
return false;
}
@Override
public boolean isSideSolid(int x, int y, int z, ForgeDirection side, boolean _default) {
Block block = getBlock(x, y, z);
return block.isSideSolid(this, x, y, z, side);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment