Jump to content

[1.7.10] Changing blockIcon on block placement


Cilka

Recommended Posts

Hello, I'm currently stuck trying to make a block to change its icon/texture based on a relative position to other blocks. Here is the code as it stands now. Any help would be appreciated. Thanks!

package com.hexopygate.Telgttatium;

import static net.minecraftforge.common.util.ForgeDirection.EAST;
import static net.minecraftforge.common.util.ForgeDirection.NORTH;
import static net.minecraftforge.common.util.ForgeDirection.SOUTH;
import static net.minecraftforge.common.util.ForgeDirection.WEST;

import java.util.HashMap;
import java.util.List;

import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.Block;
import net.minecraft.block.BlockPane;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.IIcon;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;

public class TelNormalGlassPane extends BlockPane {
	protected enum Position {
		None, Center, Top, Bottom, Left, Right, TopRight, TopLeft, BottomRight, BottomLeft, CenterFull, CenterWalledHorizontal, CenterWalledVertical, BottomWalled, TopWalled
	}
 
	public HashMap<String, IIcon>icons;
	String FaceTex;
	static String RimText = "Glass_Trim";
	String name = this.getClass().getSimpleName();
	private Position position;
	IIcon currentIcon;
	public TelNormalGlassPane(String faceTexture, String rimTexture) {

		super("tel:" + faceTexture, "tel:" + rimTexture, Material.glass, true);
		// TODO Auto-generated constructor stub
		position = Position.None;
		FaceTex = faceTexture;
		icons = new HashMap<String,IIcon>();
		this.setBlockName(name).setBlockTextureName("tel:" + name);
		GameRegistry.registerBlock(this, this.getUnlocalizedName().substring(5));

	}

	public TelNormalGlassPane(String texture) {
		this(texture, RimText);
	}

	@Override
	@SideOnly(Side.CLIENT)
	public int getRenderBlockPass() {
		return 1;
	}
	


	@Override
	public void onBlockPlacedBy(World world, int x, int y, int z,EntityLivingBase base, ItemStack itemstack ) {

		this.position = Position.None;
		System.out.println("Initial Position: " + this.position);
		HashMap<String, Block> neighboringBlocks = new HashMap<String, Block>();
		neighboringBlocks.put("north", world.getBlock(x, y + 1, z));
		neighboringBlocks.put("south", world.getBlock(x, y - 1, z));
		neighboringBlocks.put("east", world.getBlock(x + 1, y, z));
		neighboringBlocks.put("west", world.getBlock(x - 1, y, z));
		System.out.println("Set Neighboring Blocks");

		/*
		 * for(String s : neighboringBlocks.keySet()) {
		 * 
		 * if(neighboringBlocks.get(s).getMaterial() == Material.air) {
		 * neighboringBlocks.remove(s); continue; }
		 * 
		 * }
		 */
		
		SetPosition(neighboringBlocks, x, y);
		System.out.println("postiion for " + FaceTex + " is " +this.position + " at " + String.format("(%s,%s ,%s)", x,y,z));
		String texName =  FaceTex;
		switch(position)
		{
		case Top:
		texName +="_Top_Frame";
			break;
		case Bottom:
			texName +="_Down_Frame";
			break;
		case BottomLeft:
			texName +="_Left_Down_Corner_Frame";
			break;
		case BottomRight:
			texName +="_Right_Down_Corner_Frame";
			break;
		case BottomWalled:
			texName +="_Down_Single_Frame";
			break;
		case Center:
			texName +="_Middle_Frame";
			break;
		case CenterFull:
			texName +="_Full_Frame";
			break;
		case CenterWalledHorizontal:
			texName +="_Middle_Single_Frame";
			break;
		case CenterWalledVertical:
			texName += "_Vertical_Middle_Single_Frame";
			break;
		case Left:
			texName +="_Left_Frame";
			break;
		case Right:
			texName +="_Right_Frame";
			break;
		case TopLeft:
			texName +="_Left_Top_Frame";
			break;
		case TopRight:
			texName +="_Right_Top_Frame";
			break;
		case TopWalled:
			texName +="_Top_Single_Frame";
			break;
		default:
			break;
		}
		System.out.println("Texture name is " + texName);
		this.blockIcon = icons.get(texName);
		
	}
	   @SideOnly(Side.CLIENT)
	   @Override 
	   public IIcon func_150097_e()
	    {
	        return this.blockIcon;
	    }
	   @SideOnly(Side.CLIENT)
	    public void registerBlockIcons(IIconRegister registry)
	    {
		   icons.put(FaceTex+"_Full_Frame", registry.registerIcon("tel:"+FaceTex+"_Full_Frame"));
		   icons.put(FaceTex+"_Top_Frame", registry.registerIcon("tel:"+FaceTex+"_Top_Frame"));
		   icons.put(FaceTex+"_Down_Frame", registry.registerIcon("tel:"+FaceTex+"_Down_Frame"));
		   icons.put(FaceTex+"_Left_Down_Corner_Frame", registry.registerIcon("tel:"+FaceTex+"_Left_Down_Corner_Frame"));
		   icons.put(FaceTex+"_Right_Down_Corner_Frame", registry.registerIcon("tel:"+FaceTex+"_Right_Down_Corner_Frame"));
		   icons.put(FaceTex+"_Down_Single_Frame", registry.registerIcon("tel:"+FaceTex+"_Down_Single_Frame"));
		   icons.put(FaceTex+"_Middle_Frame", registry.registerIcon("tel:"+FaceTex+"_Middle_Frame"));
		   icons.put(FaceTex+"_Middle_Single_Frame", registry.registerIcon("tel:"+FaceTex+"_Middle_Single_Frame"));
		   icons.put(FaceTex+"_Vertical_Middle_Single_Frame", registry.registerIcon("tel:"+FaceTex+"_Vertical_Middle_Single_Frame"));
		   
		   icons.put(FaceTex+"_Left_Frame", registry.registerIcon("tel:"+FaceTex+"_Left_Frame"));
		   icons.put(FaceTex+"_Right_Frame", registry.registerIcon("tel:"+FaceTex+"_Right_Frame"));  
		   icons.put(FaceTex+"_Left_Top_Frame", registry.registerIcon("tel:"+FaceTex+"_Left_Top_Frame"));
		   icons.put(FaceTex+"_Right_Top_Frame", registry.registerIcon("tel:"+FaceTex+"_Right_Top_Frame"));
		   icons.put(FaceTex+"_Top_Single_Frame", registry.registerIcon("tel:"+FaceTex+"_Top_Single_Frame"));
		   this.blockIcon = icons.get(FaceTex+"_Full_Frame");
		   this.currentIcon =  this.blockIcon;
	    }
 
	private void SetPosition(HashMap<String, Block> neighbors, int x, int y) {
		if (this.position != Position.None) {
			return;
		}
/*
		if (neighbors.get("north") instanceof TelNormalGlassPane && neighbors.get("south") instanceof TelNormalGlassPane
				&& neighbors.get("east") instanceof TelNormalGlassPane
				&& neighbors.get("west") instanceof TelNormalGlassPane) {
			this.position = Position.Center;
			return;
		}
		if ((neighbors.get("north") instanceof Block && neighbors.get("north").getMaterial() != Material.air)
				&& (neighbors.get("south") instanceof Block && neighbors.get("south").getMaterial() != Material.air)
				&& (neighbors.get("east") instanceof Block && neighbors.get("east").getMaterial() != Material.air)
				&& (neighbors.get("west") instanceof Block && neighbors.get("west").getMaterial() != Material.air)) {
			this.position = Position.CenterFull;
			return;
		}
		if ((neighbors.get("north") instanceof Block && neighbors.get("north").getMaterial() != Material.air)
				&& (neighbors.get("south") instanceof Block && neighbors.get("south").getMaterial() != Material.air)
				&& (neighbors.get("east") instanceof TelNormalGlassPane
						|| neighbors.get("east").getMaterial() == Material.air)
				&& (neighbors.get("west") instanceof TelNormalGlassPane
						|| neighbors.get("west").getMaterial() == Material.air)) {
			this.position = Position.CenterWalledHorizontal;
			return;
		}

		if ((neighbors.get("north") instanceof TelNormalGlassPane
				|| neighbors.get("north").getMaterial() == Material.air)
				&& (neighbors.get("south") instanceof TelNormalGlassPane
						|| neighbors.get("south").getMaterial() == Material.air)
				&& (neighbors.get("east") instanceof Block && neighbors.get("east").getMaterial() != Material.air)
				&& (neighbors.get("west") instanceof Block && neighbors.get("west").getMaterial() != Material.air)) {
			this.position = Position.CenterWalledVertical;
			return;
		}

		if ((neighbors.get("south") instanceof Block && neighbors.get("south").getMaterial() != Material.air)
				&& (neighbors.get("east") instanceof Block && neighbors.get("east").getMaterial() != Material.air)
				&& (neighbors.get("west") instanceof Block && neighbors.get("west").getMaterial() != Material.air)) {
			this.position = Position.BottomWalled;
			return;
		}
		if ((neighbors.get("north") instanceof Block && neighbors.get("north").getMaterial() != Material.air)
				&& (neighbors.get("east") instanceof Block && neighbors.get("east").getMaterial() != Material.air)
				&& (neighbors.get("west") instanceof Block && neighbors.get("west").getMaterial() != Material.air)) {
			this.position = Position.TopWalled;
			return;
		}
		if ((neighbors.get("north") instanceof Block && neighbors.get("north").getMaterial() != Material.air)
				&& (neighbors.get("east") instanceof Block && neighbors.get("east").getMaterial() != Material.air)) {
			this.position = Position.TopLeft;
			return;
		}
		if ((neighbors.get("north") instanceof Block && neighbors.get("north").getMaterial() != Material.air)
				&& (neighbors.get("west") instanceof Block && neighbors.get("west").getMaterial() != Material.air)) {
			this.position = Position.TopRight;
			return;
		}
		if ((neighbors.get("south") instanceof Block && neighbors.get("south").getMaterial() != Material.air)
				&& (neighbors.get("west") instanceof Block && neighbors.get("west").getMaterial() != Material.air)) {
			this.position = Position.BottomRight;
			return;
		}
		if ((neighbors.get("south") instanceof Block && neighbors.get("south").getMaterial() != Material.air)
				&& (neighbors.get("east") instanceof Block && neighbors.get("east").getMaterial() != Material.air)) {
			this.position = Position.BottomLeft;
			return;
		}
		if ((neighbors.get("south") instanceof Block && (neighbors.get("south").getMaterial() != Material.air)||)) {
			this.position = Position.Bottom;
			return;
		}
		if ((neighbors.get("north") instanceof Block && neighbors.get("north").getMaterial() != Material.air)) {
			this.position = Position.Top;
			return;
		}
		if ((neighbors.get("east") instanceof Block && neighbors.get("east").getMaterial() != Material.air)) {
			this.position = Position.Left;
			return;
		}
		this.position = Position.Right;
		*/
		if(neighbors.get("north").getMaterial() == Material.air)
		{
			this.position = Position.Bottom;
		}
		else
		{
			this.position = Position.Top;
		}
	}

}

 

Link to comment
Share on other sites

1.7 is not supported on this forum. Update to 1.12 and use an ISmartModel

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

  • Guest locked this topic
Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.