Skip to content

Instantly share code, notes, and snippets.

@izzyaxel
Created September 8, 2015 01:36
Show Gist options
  • Save izzyaxel/2bf4ea57c814efbf962a to your computer and use it in GitHub Desktop.
Save izzyaxel/2bf4ea57c814efbf962a to your computer and use it in GitHub Desktop.
Connected Texture Manager
package izzyaxel.arcaneartificing.main.helpers;
import net.minecraft.block.Block;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.util.IIcon;
import net.minecraft.world.IBlockAccess;
@SuppressWarnings("all")
public class AAConnectedTextureManager
{
private static AAConnectedTextureManager INSTANCE = new AAConnectedTextureManager();
/**
* Get the manager instance
* @return Returns the singleton instance of the Connected Texture Manager
*/
public static AAConnectedTextureManager getManager()
{
return INSTANCE;
}
private boolean shouldTextureConnect(IBlockAccess world, int x, int y, int z, Block block, int dir)
{
//0 - Bottom, y neg
//1 - Top, y pos
//2 - North, z neg
//3 = South, z pos
//4 - West, x neg
//5 - East, x pos
switch(dir)
{
case 0: return world.getBlock(x, y - 1, z) == block;
case 1: return world.getBlock(x, y + 1, z) == block;
case 2: return world.getBlock(x, y, z - 1) == block;
case 3: return world.getBlock(x, y, z + 1) == block;
case 4: return world.getBlock(x - 1, y, z) == block;
case 5: return world.getBlock(x + 1, y, z) == block;
default: return false;
}
}
/**
* Gets the appropriate IIcon texture for the given side based on the blocks surrounding the current one<br><br>
* Use this in conjunction with {@link Block#shouldSideBeRendered(IBlockAccess, int, int, int, int)} for transparent blocks<br><br>
* Follow this format for Block texture permutations:<br>
* 0 - None Connected<br>
* 1 - Up Connected<br>
* 2 - Right Connected<br>
* 3 - Down Connected<br>
* 4 - Left Connected<br>
* 5 - Up & Right Connected<br>
* 6 - Up, Right & Down Connected<br>
* 7 - Right & Down Connected<br>
* 8 - Right, Down & Left Connected<br>
* 9 - Down & Left Connected<br>
* 10 - Down, Left & Up Connected<br>
* 11 - Left & Up Connected<br>
* 12 - Left, Up & Right Connected<br>
* 13 - Up & Down Connected<br>
* 14 - Right & Left Connected<br>
* 15 - All Connected<br><br>
* Override {@link Block#getIcon(int, int)} to return a constant icon index to be used for the inventory render's texture<br>
* Override {@link Block#getIcon(IBlockAccess, int, int, int, int)} to call this method, such as:
* <br><br>return AAConnectedTextureManager.getManager().getIconForSide(iblockAccess, x, y, z, side, this#IIconArray, YourBlocks#blockInstance);<br><br>
* And finally, override {@link Block#registerBlockIcons(IIconRegister)} to register the 16 icons required.<br><br>
* @param world IBlockAccess, pass in from {@link Block#getIcon(IBlockAccess, int, int, int, int)}
* @param x X coordinate of the block to check, pass in from {@link Block#getIcon(IBlockAccess, int, int, int, int)}
* @param y Y coordinate of the block to check, pass in from {@link Block#getIcon(IBlockAccess, int, int, int, int)}
* @param z Z coordinate of the block to check, pass in from {@link Block#getIcon(IBlockAccess, int, int, int, int)}
* @param side Side of the block to check, pass in from {@link Block#getIcon(IBlockAccess, int, int, int, int)}
* @param icons IIcon array, must have a length of 16 or it'll cause crashes
* @param block Block to check against
* @return Returns an index in the passed in IIcon array, which contains the texture suited for the side passed in based on the surrounding blocks
*/
public IIcon getIconForSide(IBlockAccess world, int x, int y, int z, int side, IIcon[] icons, Block block)
{
boolean up, down, north, south, east, west;
down = this.shouldTextureConnect(world, x, y, z, block, 0);
up = this.shouldTextureConnect(world, x, y, z, block, 1);
north = this.shouldTextureConnect(world, x, y, z, block, 2);
south = this.shouldTextureConnect(world, x, y, z, block, 3);
west = this.shouldTextureConnect(world, x, y, z, block, 4);
east = this.shouldTextureConnect(world, x, y, z, block, 5);
//If no valid blocks are adjacent to this one
if(!up && !down && !north && !south && !east && !west)
{
return icons[0];
}
//If there are valid blocks surrounding this one, texture doesn't matter for opaque blocks, shouldn't be rendered on transparent ones
if(up && down && north && south && east && west)
{
return icons[15];
}
switch(side)
{
case 0:
if(!north && !south && !east && !west)
{
return icons[0];
}
else if(north && !south && !east && !west)
{
return icons[1];
}
else if(!north && !south && east && !west)
{
return icons[2];
}
else if(!north && south && !east && !west)
{
return icons[3];
}
else if(!north && !south && !east && west)
{
return icons[4];
}
else if(north && !south && east && !west)
{
return icons[5];
}
else if(north && south && east && !west)
{
return icons[6];
}
else if(!north && south && east && !west)
{
return icons[7];
}
else if(!north && south && east && west)
{
return icons[8];
}
else if(!north && south && !east && west)
{
return icons[9];
}
else if(north && south && !east && west)
{
return icons[10];
}
else if(north && !south && !east && west)
{
return icons[11];
}
else if(north && !south && east && west)
{
return icons[12];
}
else if(north && south && !east && !west)
{
return icons[13];
}
else if(!north && !south && west && east)
{
return icons[14];
}
else if(north && south && east && west)
{
return icons[15];
}
case 1:
if(!north && !south && !east && !west)
{
return icons[0];
}
else if(north && !south && !east && !west)
{
return icons[1];
}
else if(!north && !south && east && !west)
{
return icons[2];
}
else if(!north && south && !east && !west)
{
return icons[3];
}
else if(!north && !south && !east && west)
{
return icons[4];
}
else if(north && !south && east && !west)
{
return icons[5];
}
else if(north && south && east && !west)
{
return icons[6];
}
else if(!north && south && east && !west)
{
return icons[7];
}
else if(!north && south && east && west)
{
return icons[8];
}
else if(!north && south && !east && west)
{
return icons[9];
}
else if(north && south && !east && west)
{
return icons[10];
}
else if(north && !south && !east && west)
{
return icons[11];
}
else if(north && !south && east && west)
{
return icons[12];
}
else if(north && south && !east && !west)
{
return icons[13];
}
else if(!north && !south && west && east)
{
return icons[14];
}
else if(north && south && east && west)
{
return icons[15];
}
case 2:
if(!up && !down && !east && !west)
{
return icons[0];
}
else if(up && !down && !east && !west)
{
return icons[1];
}
else if(!up && !down && !east && west)
{
return icons[2];
}
else if(!up && down && !east && !west)
{
return icons[3];
}
else if(!up && !down && east && !west)
{
return icons[4];
}
else if(up && !down && !east && west)
{
return icons[5];
}
else if(up && down && !east && west)
{
return icons[6];
}
else if(!up && down && !east && west)
{
return icons[7];
}
else if(!up && down && east && west)
{
return icons[8];
}
else if(!up && down && east && !west)
{
return icons[9];
}
else if(up && down && east && !west)
{
return icons[10];
}
else if(up && !down && east && !west)
{
return icons[11];
}
else if(up && !down && east && west)
{
return icons[12];
}
else if(up && down && !east && !west)
{
return icons[13];
}
else if(!up && !down && east && west)
{
return icons[14];
}
else if(up && down && east && west)
{
return icons[15];
}
case 3:
if(!up && !down && !east && !west)
{
return icons[0];
}
else if(up && !down && !east && !west)
{
return icons[1];
}
else if(!up && !down && east && !west)
{
return icons[2];
}
else if(!up && down && !east && !west)
{
return icons[3];
}
else if(!up && !down && !east && west)
{
return icons[4];
}
else if(up && !down && east && !west)
{
return icons[5];
}
else if(up && down && east && !west)
{
return icons[6];
}
else if(!up && down && east && !west)
{
return icons[7];
}
else if(!up && down && east && west)
{
return icons[8];
}
else if(!up && down && !east && west)
{
return icons[9];
}
else if(up && down && !east && west)
{
return icons[10];
}
else if(up && !down && !east && west)
{
return icons[11];
}
else if(up && !down && east && west)
{
return icons[12];
}
else if(up && down && !east && !west)
{
return icons[13];
}
else if(!up && !down && east && west)
{
return icons[14];
}
else if(up && down && east && west)
{
return icons[15];
}
case 4:
if(!up && !down && !north && !south)
{
return icons[0];
}
else if(up && !down && !north && !south)
{
return icons[1];
}
else if(!up && !down && !north && south)
{
return icons[2];
}
else if(!up && down && !north && !south)
{
return icons[3];
}
else if(!up && !down && north && !south)
{
return icons[4];
}
else if(up && !down && !north && south)
{
return icons[5];
}
else if(up && down && !north && south)
{
return icons[6];
}
else if(!up && down && !north && south)
{
return icons[7];
}
else if(!up && down && north && south)
{
return icons[8];
}
else if(!up && down && north && !south)
{
return icons[9];
}
else if(up && down && north && !south)
{
return icons[10];
}
else if(up && !down && north && !south)
{
return icons[11];
}
else if(up && !down && north && south)
{
return icons[12];
}
else if(up && down && !north && !south)
{
return icons[13];
}
else if(!up && !down && north && south)
{
return icons[14];
}
else if(up && down && north && south)
{
return icons[15];
}
case 5:
if(!up && !down && !north && !south)
{
return icons[0];
}
else if(up && !down && !north && !south)
{
return icons[1];
}
else if(!up && !down && north && !south)
{
return icons[2];
}
else if(!up && down && !north && !south)
{
return icons[3];
}
else if(!up && !down && !north && south)
{
return icons[4];
}
else if(up && !down && north && !south)
{
return icons[5];
}
else if(up && down && north && !south)
{
return icons[6];
}
else if(!up && down && north && !south)
{
return icons[7];
}
else if(!up && down && north && south)
{
return icons[8];
}
else if(!up && down && !north && south)
{
return icons[9];
}
else if(up && down && !north && south)
{
return icons[10];
}
else if(up && !down && !north && south)
{
return icons[11];
}
else if(up && !down && north && south)
{
return icons[12];
}
else if(up && down && !north && !south)
{
return icons[13];
}
else if(!up && !down && north && south)
{
return icons[14];
}
else if(up && down && north && south)
{
return icons[15];
}
default: return icons[0];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment