Jump to content

Algorithm for changing textures based on position


Cilka

Recommended Posts

Hi everyone, right now I'm in middle of converting my 1.7.10 code to 1.12.2. One of the blocks I had in before was a glass pane that changed its texture based on the position of where the pane was in relative to other blocks around it. Before i only used a 2D plane, but I was able to determine what axis the block was placed on using a combo of  "canPaneConnectToBlock" and "world.isSideSolid". Now that those methods are gone, I'm trying a to use a 3D plane where North(-Z) , East(-X)  and South(+Z), West(+X) are interchangeable. Up and down will be the Y axis. For now, all of my panes, no matter what position i put them in, are calculated as a bottom pane. Can anyone point me in the direction of what I should do for this?

Spoiler

package com.hexopygate.Telgttatium.blockarchtype;

import java.util.HashMap;

import javax.swing.text.html.HTMLDocument.HTMLReader.FormAction;

import com.hexopygate.Telgttatium.Telgttatium;
import com.hexopygate.Telgttatium.utils.IHasModel;

import net.minecraft.block.Block;
import net.minecraft.block.BlockAir;
import net.minecraft.block.BlockFlower;
import net.minecraft.block.BlockPane;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.IStringSerializable;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

public class TelGlassFrame extends BlockPane implements IHasModel{

	protected static enum Position implements IStringSerializable {
		 
		Center("center",0), 
		Top("top",1), 
		Bottom("bottom",2), 
		Left("left",3),
		Right("right",4),
		TopRight("topright",5),
		TopLeft("topleft",6),
		BottomRight("bottomright",7), 
		BottomLeft("bottomleft",8), 
		CenterFull("centerfull",9),
		CenterWalledHorizontal("centerwalledhorizontal",10), 
		CenterWalledVertical("centerwalledvertical",11), 
		BottomWalled("bottomwalled",12), 
		TopWalled("topwalled",13),
		RightWalled("rightwalled",14), 
		LeftWalled("leftwalled",15);

		private int ID;
		private String name;
		@Override
		public String getName() {
			// TODO Auto-generated method stub
			return name;
		}
		public int getID()
		{
			return ID;
		}
		private Position(String Name, int ID)
		{
			this.name = Name;
			this.ID = ID;
		}
	}


	String FaceTex;
	static String RimText = "Glass_Trim";
	String name = this.getClass().getSimpleName();
	private Position position;
	static final PropertyEnum<Position> panePosition = PropertyEnum.create("paneposition",Position.class);
	public TelGlassFrame(Material materialIn, boolean canDrop) {
		super(materialIn, canDrop);
		String name  = this.getClass().getSimpleName();
		this.setUnlocalizedName(name);
		this.setRegistryName(name);
		this.setDefaultState(this.blockState.getBaseState().withProperty(panePosition, Position.Center));
		Telgttatium.itemRef.put(name,new ItemBlock(this).setRegistryName(this.getRegistryName()));
	}
	@Override
	public void registerModels() {
		// TODO Auto-generated method stub
		Telgttatium.proxy.registerItemRenderer(Item.getItemFromBlock(this), 0, "inventory");
	}
	public TelGlassFrame(boolean canDrop) {
		this(Material.GLASS,canDrop);
	}

	@Override
	public int getMetaFromState(IBlockState state)
	{
		Position type = (Position) state.getValue(panePosition);
		return type.ordinal();
	}
	@Override
	public IBlockState getStateFromMeta(int meta)
	{
		return this.getDefaultState().withProperty(panePosition, Position.values()[meta]);
	}
	@Override 
	protected BlockStateContainer createBlockState()
	{
		return new BlockStateContainer(this, new IProperty[] {panePosition,NORTH, EAST, WEST,SOUTH});
	}
	@Override
	public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {

		
		System.out.println("Initial Position: " + this.position);
		SetPosition(world,pos);
		
		//System.out.println("postiion for " + FaceTex + " is " +this.position + " at " + String.format("(%s,%s ,%s)", x,y,z));
		String texName =  "tel:"+FaceTex;
		state.withProperty(panePosition, position);
		switch(position)
		{
		case Top:
		texName +="_Top_Frame";
			break;
		case Bottom:
			texName +="_Down_Frame";
			break;
		case BottomLeft:
			texName +="_Down_Left_Frame";
			break;
		case BottomRight:
			texName +="_Down_Right_Frame";
			break;
		case BottomWalled:
			texName +="_Down_Single_Frame";
			break;
		case Center:
			texName +="_Middle_Frame";
			break;
		case CenterFull:
			texName +="_Full_Frame";
			break;
		case CenterWalledHorizontal:
			texName +="_Horizontal_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 +="_Top_Left_Frame";
			break;
		case TopRight:
			texName +="_Top_Right_Frame";
			break;
		case TopWalled:
			texName +="_Top_Single_Frame";
			break;
		case RightWalled:
			texName += "_Right_Single_Frame";
			break;
		case LeftWalled:
			texName += "_Left_Single_Frame";
			break;
		default:
			break;
		}
		System.out.println("Texture name is " + texName);
		//this.blockIcon = icons.get(texName);
		
			
			//world.setBlock(x, y, z, GameData.getBlockRegistry().getObject(texName));
	
	}

	private HashMap<String, Block> buildNeighbors(World world,BlockPos pos)
	{
		HashMap<String,Block> neighbors = new HashMap<String,Block>();
		//TODO don't look for angle direction just check for what is neighboring this block pos
		neighbors.put("up", world.getBlockState(new BlockPos(pos.getX(),pos.getY()+1,pos.getZ())).getBlock());
		neighbors.put("down", world.getBlockState(new BlockPos(pos.getX(),pos.getY()-1,pos.getZ())).getBlock());
		neighbors.put("north", world.getBlockState(new BlockPos(pos.getX(),pos.getY(),pos.getZ()-1)).getBlock());
		neighbors.put("south", world.getBlockState(new BlockPos(pos.getX(),pos.getY(),pos.getZ()+1)).getBlock());
		neighbors.put("east", world.getBlockState(new BlockPos(pos.getX()+1,pos.getY(),pos.getZ())).getBlock());
		neighbors.put("west", world.getBlockState(new BlockPos(pos.getX()-1,pos.getY(),pos.getZ())).getBlock());
		return neighbors;
	}
	
	private boolean isTelGlassFrame(Block block)
	{
		return block instanceof TelGlassFrame;
	}
	private boolean isAirBlock(Block block)
	{
		return block instanceof BlockAir;
	}
	
	//1.12.2
	private void SetPosition(World world,BlockPos pos)
	{
		HashMap<String,Block> neighbors = buildNeighbors(world,pos);
		if( !(isTelGlassFrame(neighbors.get("north")) || isTelGlassFrame(neighbors.get("east")) )  
				 && !(isAirBlock(neighbors.get("north")) || isAirBlock(neighbors.get("east")))
			 &&  !(isTelGlassFrame(neighbors.get("south")) || isTelGlassFrame(neighbors.get("west")) )  
			 && !(isAirBlock(neighbors.get("south")) || isAirBlock(neighbors.get("west")))&&
			 !(isTelGlassFrame(neighbors.get("up")) && isTelGlassFrame(neighbors.get("down")) )  
			 && !(isAirBlock(neighbors.get("up")) && isAirBlock(neighbors.get("east")))
				)
		{
			position = Position.CenterFull;
			return;
		}
		else if(!(isTelGlassFrame(neighbors.get("north") )|| isTelGlassFrame(neighbors.get("east"))) 
				&&
				!(isAirBlock(neighbors.get("north") )|| isAirBlock(neighbors.get("east")))
				&& 
				!(isTelGlassFrame(neighbors.get("south") )|| isTelGlassFrame(neighbors.get("west"))) 
				&&
				!(isAirBlock(neighbors.get("south") )|| isAirBlock(neighbors.get("west")))
				&&
				!isTelGlassFrame(neighbors.get("down"))  && !isAirBlock(neighbors.get("down"))
				)
		{
			position = Position.BottomWalled; 
			return;
		}
		else if(!(isTelGlassFrame(neighbors.get("north") )|| isTelGlassFrame(neighbors.get("east"))) 
				&&
				!(isAirBlock(neighbors.get("north") )|| isAirBlock(neighbors.get("east")))
				&& 
				!(isTelGlassFrame(neighbors.get("south") )|| isTelGlassFrame(neighbors.get("west"))) 
				&&
				!(isAirBlock(neighbors.get("south") )|| isAirBlock(neighbors.get("west")))
				&&
				!(isTelGlassFrame(neighbors.get("up"))  && isAirBlock(neighbors.get("up")))
				)
		{
			position = Position.TopWalled;
			return;
		}
		else if(	!(isTelGlassFrame(neighbors.get("up"))  && isAirBlock(neighbors.get("up"))) &&
				!(isTelGlassFrame(neighbors.get("down"))  && isAirBlock(neighbors.get("down")))
				&& !(isTelGlassFrame(neighbors.get("south"))|| isTelGlassFrame(neighbors.get("west")))
				&& !(isAirBlock(neighbors.get("south"))|| isAirBlock(neighbors.get("west")))
				)
		{
			position = Position.LeftWalled;
			return;
		}
		
		else if(	!(isTelGlassFrame(neighbors.get("up"))  && isAirBlock(neighbors.get("up"))) &&
				!(isTelGlassFrame(neighbors.get("down"))  && isAirBlock(neighbors.get("down")))
				&& !(isTelGlassFrame(neighbors.get("north"))|| isTelGlassFrame(neighbors.get("east")))
				&& !(isAirBlock(neighbors.get("north"))|| isAirBlock(neighbors.get("east")))
			) {
			position = Position.RightWalled;
			return;
		}
		else if((isTelGlassFrame(neighbors.get("up"))  && isAirBlock(neighbors.get("up"))) &&
				!(isTelGlassFrame(neighbors.get("down"))  && isAirBlock(neighbors.get("down")))) {
			position = Position.CenterWalledHorizontal;
			return;
		}
		else if(!(isTelGlassFrame(neighbors.get("north") )|| isTelGlassFrame(neighbors.get("east"))) 
				&&
				!(isAirBlock(neighbors.get("north") )|| isAirBlock(neighbors.get("east")))
				&& 
				!(isTelGlassFrame(neighbors.get("south") )|| isTelGlassFrame(neighbors.get("west"))) 
				&&
				!(isAirBlock(neighbors.get("south") )|| isAirBlock(neighbors.get("west")))) {
			position = Position.CenterWalledVertical;
			return;
		}
		else if(!(isTelGlassFrame(neighbors.get("south") )|| isTelGlassFrame(neighbors.get("west")))  
				&& !(isAirBlock(neighbors.get("south")) ||isAirBlock(neighbors.get("west")))
				&& !(isTelGlassFrame(neighbors.get("down")) && isAirBlock(neighbors.get("down"))))
		{
			position = Position.BottomLeft;
			return;
		}
		else if(!(isTelGlassFrame(neighbors.get("down")) && isAirBlock(neighbors.get("down")))
				&&!(isTelGlassFrame(neighbors.get("north") )|| isTelGlassFrame(neighbors.get("east")))  
				&& !(isAirBlock(neighbors.get("north")) ||isAirBlock(neighbors.get("east"))) )
		{
			position = Position.BottomRight;
			return;
		}
		else if(!(isTelGlassFrame(neighbors.get("up")) && isAirBlock(neighbors.get("up")))
				&&!(isTelGlassFrame(neighbors.get("north") )|| isTelGlassFrame(neighbors.get("east")))  
				&& !(isAirBlock(neighbors.get("north")) ||isAirBlock(neighbors.get("east"))))
		{
			position = Position.TopRight;
			return;
		}
		else if(!(isTelGlassFrame(neighbors.get("south") )|| isTelGlassFrame(neighbors.get("west")))  
				&& !(isAirBlock(neighbors.get("south")) ||isAirBlock(neighbors.get("west")))
				&& !(isTelGlassFrame(neighbors.get("up")) && isAirBlock(neighbors.get("up"))))
		{
			position = Position.TopLeft;
			return;
		}
		else if(!(isTelGlassFrame(neighbors.get("down")) && isAirBlock(neighbors.get("down"))))
		{
			position = Position.Bottom;
			return;
		}
		else if(!(isTelGlassFrame(neighbors.get("up")) && isAirBlock(neighbors.get("up"))))
		{
			position = Position.Top;
			return;
		}
		else if(!(isTelGlassFrame(neighbors.get("north") )|| isTelGlassFrame(neighbors.get("east")))  
				&& !(isAirBlock(neighbors.get("north")) ||isAirBlock(neighbors.get("east"))))
		{
			position = Position.Right;
			return;
				}
		else if(!(isTelGlassFrame(neighbors.get("south") )|| isTelGlassFrame(neighbors.get("west")))  
				&& !(isAirBlock(neighbors.get("south")) ||isAirBlock(neighbors.get("west"))))
		{
			position = Position.Left;
			return;
		}
	}
	//1.7.10
	@Deprecated 
	private void SetPosition(HashMap<String, Block> neighbors, int x, int y) {
	
		if( !(neighbors.get("north") instanceof TelGlassPane)  && !(neighbors.get("north") instanceof BlockAir)&&
				!(neighbors.get("south") instanceof TelGlassPane)	&&!(neighbors.get("south") instanceof BlockAir)&&
				!(neighbors.get("east") instanceof TelGlassPane) &&!(neighbors.get("east") instanceof BlockAir)&&
				!(neighbors.get("west") instanceof TelGlassPane) && !(neighbors.get("west") instanceof BlockAir))
		{
			position = Position.CenterFull;
			return;
		}
		else if(!(neighbors.get("south") instanceof TelGlassPane)	&&!(neighbors.get("south") instanceof BlockAir)&&
				!(neighbors.get("east") instanceof TelGlassPane) &&!(neighbors.get("east") instanceof BlockAir)&&
				!(neighbors.get("west") instanceof TelGlassPane) && !(neighbors.get("west") instanceof BlockAir))
		{
			position = Position.BottomWalled; 
			return;
		}
		else if(!(neighbors.get("north") instanceof TelGlassPane)	&&!(neighbors.get("north") instanceof BlockAir)&&
				!(neighbors.get("east") instanceof TelGlassPane) &&!(neighbors.get("east") instanceof BlockAir)&&
				!(neighbors.get("west") instanceof TelGlassPane) && !(neighbors.get("west") instanceof BlockAir))
		{
			position =Position.TopWalled;
			return;
		}
		else if(!(neighbors.get("south") instanceof TelGlassPane)	&&!(neighbors.get("south") instanceof BlockAir)&&
				!(neighbors.get("north") instanceof TelGlassPane) &&!(neighbors.get("north") instanceof BlockAir)&&
				!(neighbors.get("west") instanceof TelGlassPane) && !(neighbors.get("west") instanceof BlockAir))
		{
			position = Position.LeftWalled;
			return;
		}
		else if(!(neighbors.get("south") instanceof TelGlassPane)	&&!(neighbors.get("south") instanceof BlockAir)&&
				!(neighbors.get("north") instanceof TelGlassPane) &&!(neighbors.get("north") instanceof BlockAir)&&
				!(neighbors.get("east") instanceof TelGlassPane) && !(neighbors.get("east") instanceof BlockAir))
		{
			position = Position.RightWalled;
			return;
		}
		else if(!(neighbors.get("south") instanceof TelGlassPane)	&&!(neighbors.get("south") instanceof BlockAir)&&
				!(neighbors.get("north") instanceof TelGlassPane) &&!(neighbors.get("north") instanceof BlockAir))
		{
			position = Position.CenterWalledHorizontal;
			return;
		}
		else if(!(neighbors.get("east") instanceof TelGlassPane)	&&!(neighbors.get("east") instanceof BlockAir)&&
				!(neighbors.get("west") instanceof TelGlassPane) &&!(neighbors.get("west") instanceof BlockAir))
		{
			position = Position.CenterWalledVertical;
			return;
		}
	
		else if(!(neighbors.get("south") instanceof TelGlassPane)	&&!(neighbors.get("south") instanceof BlockAir)&&
				!(neighbors.get("west") instanceof TelGlassPane) &&!(neighbors.get("west") instanceof BlockAir))
		{
			position = Position.BottomLeft;
			return;
		}
		else if(!(neighbors.get("south") instanceof TelGlassPane)	&&!(neighbors.get("south") instanceof BlockAir)&&
				!(neighbors.get("east") instanceof TelGlassPane) &&!(neighbors.get("east") instanceof BlockAir))
		{
			position = Position.BottomRight;
			return;
		}
		else if(!(neighbors.get("north") instanceof TelGlassPane)	&&!(neighbors.get("north") instanceof BlockAir)&&
				!(neighbors.get("east") instanceof TelGlassPane) &&!(neighbors.get("east") instanceof BlockAir))
		{
			position = Position.TopRight;
			return;
		}
		else if(!(neighbors.get("north") instanceof TelGlassPane)	&&!(neighbors.get("north") instanceof BlockAir)&&
				!(neighbors.get("west") instanceof TelGlassPane) &&!(neighbors.get("west") instanceof BlockAir))
		{
			position = Position.TopLeft;
			return;
		}
		else if(!(neighbors.get("south") instanceof TelGlassPane)	&&!(neighbors.get("south") instanceof BlockAir))
		{
			position = Position.Bottom;
			return;
		}
		else if(!(neighbors.get("north") instanceof TelGlassPane)	&&!(neighbors.get("north") instanceof BlockAir))
		{
			position = Position.Top;
			return;
		}
		else if(!(neighbors.get("east") instanceof TelGlassPane)	&&!(neighbors.get("east") instanceof BlockAir))
		{
			position = Position.Right;
			return;
				}
		else if(!(neighbors.get("west") instanceof TelGlassPane)	&&!(neighbors.get("wast") instanceof BlockAir))
		{
			position = Position.Left;
			return;
		}
		position =  Position.Center;
	}
	
	/*
	 public boolean paneIsOnAxis(World world, BlockPos pos, EnumFacing dir)
	    {
	        return canPaneConnectToBlock(world.getBlockState(pos).getBlock()) || 
	                world.isSideSolid(x, y, z, dir.getOpposite(), false);
	    }
*/
	
}

 

 

Link to comment
Share on other sites

Have a look at MBE05 in Minecraft by example, I think it’s exactly what you want

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

On 8/5/2018 at 9:30 PM, Cadiboo said:

Have a look at MBE05 in Minecraft by example, I think it’s exactly what you want

I looked at the example, and there isn't really much i can go off of given what I was trying to do. In the Block3DWeb it only seems to care about what next to the block first. Say if there were two blocks on the bottom and the left side, if the block saw the left block first, It would only care about the left block, not noticing the bottom block. What I need to determine is how block is facing in the world so i know which neighbors to use. It's a good example, but a bit over complicated for what I'm trying to do now ^^".

Link to comment
Share on other sites

  • 1 year later...

Post from the dead here. I finally got around to implementing the glass pane frames for my mod. Code wise I think I got the blockstate thing down. There are 16 states that these glass pane frames can be in along with the 4 states that I'm inheriting (North, South, East, West). I took a look at how the blockstate json handles its 4 states. Example here for Cyan Stained Glass Pane:

Spoiler

{
    "multipart": [
        {   "apply": { "model": "cyan_stained_glass_pane_post" }},
        {   "when": { "north": true },
            "apply": { "model": "cyan_stained_glass_pane_side" }
        },
        {   "when": { "east": true },
            "apply": { "model": "cyan_stained_glass_pane_side", "y": 90 }
        },
        {   "when": { "south": true },
            "apply": { "model": "cyan_stained_glass_pane_side_alt" }
        },
        {   "when": { "west": true },
            "apply": { "model": "cyan_stained_glass_pane_side_alt", "y": 90 }
        },
        {   "when": { "north": false },
            "apply": { "model": "cyan_stained_glass_pane_noside" }
        },
        {   "when": { "east": false },
            "apply": { "model": "cyan_stained_glass_pane_noside_alt" }
        },
        {   "when": { "south": false },
            "apply": { "model": "cyan_stained_glass_pane_noside_alt", "y": 90 }
        },
        {   "when": { "west": false },
            "apply": { "model": "cyan_stained_glass_pane_noside", "y": 270 }
        }
    ]
}
 

Now the 16 blockstates I've created are to go with the original 4 from BlockPane. If I do what I am thinking of doing, I will have to have 128 models just for 1 glass pane frame. Here is the question, is there a less painful way to create the blockstate and models?

Link to comment
Share on other sites

49 minutes ago, Cilka said:

If I do what I am thinking of doing, I will have to have 128 models just for 1 glass pane frame.

I believe that is wrong...cant you just have two models per color/whatever(16). And then an extra one that's empty. Then an extra one that is empty to apply when it is false. Which comes to 16x2+1=33 which is a lot, but you can just parent the glass pane model and change the texture.

 

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Quote

I believe that is wrong...cant you just have two models per color/whatever(16). And then an extra one that's empty. Then an extra one that is empty to apply when it is false. Which comes to 16x2+1=33 which is a lot, but you can just parent the glass pane model and change the texture.

I see what you are saying. My concern would be if leave the false sides without a model, the model would not look as intended. I'll try what you are suggesting. It will be a good starting point at least. 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

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