Jump to content

[1.8.9] When does ModelbakeEvent happen?


paradoxbomb

Recommended Posts

I'm following GreyGhost's MinecraftByExample example project on ISBMs, and I can't for the life of me figure out when ModelBakeEventHandler "catches" events. My intent is to have a mechanic to replace existing blocks with TEs that I can then perform rendering on, but I don't know how to tell the model factory what type of block to paint itself as.

 

double edit for additional information: I know that GG has a method of choosing a random nearby block and then clones that, but I want to only copy the block it is replacing.

 

edited for clarity of version

<signature></signature>

Link to comment
Share on other sites

I kind of think that he's actually replacing the block with his own block, which has a tile entity.  He wants to know how to store the original so it can be rendered.

 

The answer to that is: store the original in the TE and reference it during rendering.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

After derping around with stuff, I managed to come up with

 

this.worldObj.setBlockState(blockpos, ModBlocks.blockCanvas.getExtendedState(iblockstate, this.worldObj, blockpos));

 

on my projectile (the thing that will actually be placing the block) code. However, it seems to just break a block and then immediately replace it with the vanilla version of itself, as determined by a simple item that prints out what block it was used on to the console.

 

The answer to that is: store the original in the TE and reference it during rendering.

 

I don't actually have a TE for my block, but it sounds like I should look into it. I assume you were referencing the TE in the canvas (camouflage) block?

<signature></signature>

Link to comment
Share on other sites

if I have one set default state, though, that seems like it would defeat the purpose of the block: to have a dynamic texture. As it stands, the block works in three parts:

 

[spoiler="BlockCanvas.java]

//block that will replace a block in the world with a copy of its texture that can be tinted

package com.paradoxbomb.inkcannon.common.tileEntities.canvasBlock;

import com.paradoxbomb.inkcannon.StringLib;
import com.paradoxbomb.inkcannon.common.items.ModItems;

import net.minecraft.block.Block;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.state.BlockState;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumWorldBlockLayer;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.property.IExtendedBlockState;
import net.minecraftforge.common.property.IUnlistedProperty;
import net.minecraftforge.common.property.ExtendedBlockState;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class BlockCanvas extends Block implements ITileEntityProvider
{

public static final UnlistedPropertyPaintedBlock PAINTED_BLOCK = new UnlistedPropertyPaintedBlock();

public BlockCanvas (String unlocalizedName)
{
	super(Material.cloth);
	this.setUnlocalizedName(StringLib.CANVAS_BLOCK);
	this.setCreativeTab(ModItems.tabInkCannon);
}

public BlockCanvas (Block disguiseBlock, World worldIn, BlockPos pos)
{
	super(disguiseBlock.getMaterial());
	this.setUnlocalizedName(StringLib.CANVAS_BLOCK);
	this.setHarvestLevel(disguiseBlock.getHarvestTool((IBlockState)disguiseBlock.getBlockState()),disguiseBlock.getHarvestLevel((IBlockState)disguiseBlock.getBlockState()));
	this.setHardness(disguiseBlock.getBlockHardness(worldIn, pos));
	this.setResistance(5.0f);
	this.isBlockContainer = true;
	this.setDefaultState((IBlockState)disguiseBlock.getDefaultState());
}

public TileEntity createNewTileEntity(World worldIn, int meta) 
{
	return new TECanvas();
}

@Override
protected BlockState createBlockState()
{
	IProperty [] listedProperties = new IProperty[0];	//no listed properties
	IUnlistedProperty [] unlistedProperties = new IUnlistedProperty [] {PAINTED_BLOCK};
	return new ExtendedBlockState (this, listedProperties, unlistedProperties);
}

@Override
public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
{
	super.onBlockPlacedBy(worldIn, pos, state, placer, stack);
	TileEntity canvasEntity = worldIn.getTileEntity(pos);
	if (canvasEntity instanceof TECanvas)
	{
		//initialize TE data
	}
}

@SideOnly(Side.CLIENT)
public EnumWorldBlockLayer getBlockLayer()
{
	return EnumWorldBlockLayer.SOLID;
}

@Override
public IBlockState getExtendedState(IBlockState state, IBlockAccess world, BlockPos pos)
{
	if (state instanceof IExtendedBlockState)
	{
		IExtendedBlockState returnState = (IExtendedBlockState)state;
		returnState = returnState.withProperty(PAINTED_BLOCK, state);
		return returnState;
	}
	return state;
}

@Override
public boolean isOpaqueCube()
{
	return true;
}

@Override
public boolean isFullCube()
{
	return true;
}

@Override
public int getRenderType()
{
	return 3;
}
}

 

(There are some things in there regarding TileEntities but they don't do anything at the moment; I plan on using the TE data to allow the block to be shaded)

 

[spoiler=CanvasBlockModelFactory.java]

//class to assist in creating the model for canvas blocks
//code based on http://bit.ly/1R1SbXO

package com.paradoxbomb.inkcannon.common.tileEntities.canvasBlock;

import java.util.List;

import com.paradoxbomb.inkcannon.LogHelper;

import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.BlockModelShapes;
import net.minecraft.client.renderer.BlockRendererDispatcher;
import net.minecraft.client.renderer.block.model.BakedQuad;
import net.minecraft.client.renderer.block.model.ItemCameraTransforms;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.model.IBakedModel;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.init.Blocks;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.client.model.ISmartBlockModel;
import net.minecraftforge.common.property.IExtendedBlockState;

@SuppressWarnings("deprecation")
public class CanvasBlockModelFactory implements ISmartBlockModel 
{
private IBakedModel modelWhenNotPainted;

public CanvasBlockModelFactory (IBakedModel unPaintedBlock)
{
	modelWhenNotPainted = unPaintedBlock;
}

//creates model resource location for canvas block
public static final ModelResourceLocation modelResourceLocation = new ModelResourceLocation("inkcannon:blockCanvas");

// creates an IBakedModel based on the IBlockState of the block state that is passed
@Override
public IBakedModel handleBlockState(IBlockState state) 
{
	IBakedModel returnModel = modelWhenNotPainted;	//default
	IBlockState unPaintedBlock = Blocks.air.getDefaultState();

	if (state instanceof IExtendedBlockState)
	{
		IExtendedBlockState extendedState = (IExtendedBlockState)state;
		IBlockState paintedBlockIBlockState = extendedState.getValue(BlockCanvas.PAINTED_BLOCK);

		if (paintedBlockIBlockState != unPaintedBlock)
		{
			Minecraft mc = Minecraft.getMinecraft();
			BlockRendererDispatcher blockRendererDispatcher = mc.getBlockRendererDispatcher();
			BlockModelShapes blockModelShapes = blockRendererDispatcher.getBlockModelShapes();
			IBakedModel copiedBlockModel = blockModelShapes.getModelForState(paintedBlockIBlockState);

			if(copiedBlockModel instanceof ISmartBlockModel)
			{
				copiedBlockModel = ((ISmartBlockModel)copiedBlockModel).handleBlockState(paintedBlockIBlockState);
			}
			returnModel = copiedBlockModel;
		}
	}

	return returnModel;
}

//used in case of a player being inside a block; game will crash unless something meaningful is used here
@Override
public TextureAtlasSprite getParticleTexture() 
{
	return modelWhenNotPainted.getParticleTexture();
}


//unused methods; would only be used in case of other mod blocks
@Override
public List<BakedQuad> getFaceQuads(EnumFacing p_177551_1_) 
{
	LogHelper.error("Unsupported render method getFaceQuads accessed from mod Ink Cannon!\nPlease contact the mod developer!");
	throw new UnsupportedOperationException();
}

@Override
public List<BakedQuad> getGeneralQuads() 
{
	LogHelper.error("Unsupported render method getgeneralQuads accessed from mod Ink Cannon!\nPlease contact the mod developer!");
	throw new UnsupportedOperationException();
}

@Override
public boolean isAmbientOcclusion() 
{
	LogHelper.error("Unsuppoerted method isAmbientOcclusion accessed from mod Ink Cannon!\nPlease contact the mod developer!");
	throw new UnsupportedOperationException();
}

@Override
public boolean isGui3d() 
{
	LogHelper.error("Unsuppoerted method isGui3d accessed from mod Ink Cannon!\nPlease contact the mod developer!");
	return false;
}

@Override
public boolean isBuiltInRenderer() 
{
	LogHelper.error("Unsuppoerted method isBuiltInRenderer accessed from mod Ink Cannon!\nPlease contact the mod developer!");
	throw new UnsupportedOperationException();
}

@Override
public ItemCameraTransforms getItemCameraTransforms()
{
	LogHelper.error("Unsuppoerted method getItemCameraTransform accessed from mod Ink Cannon!\nPlease contact the mod developer!");
	throw new UnsupportedOperationException();
}



}

 

 

 

[spoiler="ModelBakeEventHandler.java]

//class to allow dynamically altering Moderlmanager's registry

package com.paradoxbomb.inkcannon.client.render;

import com.paradoxbomb.inkcannon.common.tileEntities.canvasBlock.CanvasBlockModelFactory;

import net.minecraft.client.resources.model.IBakedModel;
import net.minecraftforge.client.event.ModelBakeEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

public class ModelBakeEventHandler 
{
public static final ModelBakeEventHandler instance = new ModelBakeEventHandler();

private ModelBakeEventHandler() {};

@SubscribeEvent
public void onModelBakeEvent(ModelBakeEvent e)
{
	Object object = e.modelRegistry.getObject(CanvasBlockModelFactory.modelResourceLocation);
	if (object instanceof IBakedModel)
	{
		IBakedModel existingModel = (IBakedModel)object;
		CanvasBlockModelFactory customModel = new CanvasBlockModelFactory(existingModel);
		e.modelRegistry.putObject(CanvasBlockModelFactory.modelResourceLocation, customModel);
	}
}
}

 

 

 

I think what I need to do is related to the ModelBake event so that I can pass the IBlockState into the factory, but I have no idea when that happens.

<signature></signature>

Link to comment
Share on other sites

I assume that I need to pass the IBlockState from the in-world block into the CanvasBlockModelFactory before it gets placed, but I'm stuck on how to do that. I've tried looking up topics that I think might be useful for figuring this out, but I am completely lost.

<signature></signature>

Link to comment
Share on other sites

I'm very confused then, because when I test the code that I have, it creates canvas blocks that are untextured.

[spoiler=ClientProxy.java]

/*
* File to hold commands that need to be run only on the client side (rendering, UI, etc)
*/

package com.paradoxbomb.inkcannon;

import com.paradoxbomb.inkcannon.client.render.BlockRenderRegister;
import com.paradoxbomb.inkcannon.client.render.ItemRenderRegister;
import com.paradoxbomb.inkcannon.client.render.ModelBakeEventHandler;
import com.paradoxbomb.inkcannon.common.blocks.ModBlocks;
import com.paradoxbomb.inkcannon.common.tileEntities.canvasBlock.BlockCanvas;
import com.paradoxbomb.inkcannon.common.tileEntities.canvasBlock.CanvasBlockModelFactory;

import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.block.statemap.StateMapperBase;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.item.Item;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.registry.GameRegistry;

public class ClientProxy extends CommonProxy 
{

@Override
public void preinit(FMLPreInitializationEvent event) 
{
	super.preinit(event);

	//tells Forge how to map BlockCanvas's IBlockState onto ModelResourceLocation
	//since this block is special, an anonymous class is used instead of the normal methos
	StateMapperBase ignoreState = new StateMapperBase()
			{
				@Override
				protected ModelResourceLocation getModelResourceLocation(IBlockState blockState)
				{
					return CanvasBlockModelFactory.modelResourceLocation;
				}
			};
	ModelLoader.setCustomStateMapper((BlockCanvas)ModBlocks.blockCanvas, ignoreState);

	//register the custom event handler
	MinecraftForge.EVENT_BUS.register(ModelBakeEventHandler.instance);

	//make canvas block render properly while an item
	Item itemBlockCanvas = GameRegistry.findItem(StringLib.MODID, StringLib.CANVAS_BLOCK);
	ModelResourceLocation itemModelResourceLoaction = new ModelResourceLocation(StringLib.MODID+":"+StringLib.CANVAS_BLOCK, "inventory");
	final int DEFAULT_ITEM_SUBTYPE = 0;
	ModelLoader.setCustomModelResourceLocation(itemBlockCanvas, DEFAULT_ITEM_SUBTYPE, itemModelResourceLoaction);
}

@Override
public void init(FMLInitializationEvent event) 
{
	super.init(event);
	//register items and block to be rendered
	ItemRenderRegister.registerItemRenderer();
	BlockRenderRegister.registerBlockRenderer();
}

@Override
public void postInit(FMLPostInitializationEvent event) 
{
	super.postInit(event);
}

}

 

 

[spoiler=CommonProxy.java]

/*
* File to hold commands that need to be run through both the server and client proxies
*/

package com.paradoxbomb.inkcannon;

import com.paradoxbomb.inkcannon.common.blocks.ModBlocks;
import com.paradoxbomb.inkcannon.common.items.ModItems;
import com.paradoxbomb.inkcannon.common.misc.Crafting;
import com.paradoxbomb.inkcannon.common.tileEntities.ModTileEntities;

import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;

public class CommonProxy 
{
public void preinit(FMLPreInitializationEvent event)
{
	ModItems.createItems();				//initialize mod items
	ModBlocks.createBlocks();			//initialize mod blocks
	ModTileEntities.init(); 			//initialize tile entities
}

public void init(FMLInitializationEvent event)
{
	Crafting.initCrafting();			//initialize crafting recipes
}

public void postInit(FMLPostInitializationEvent event)
{

}
}

 

 

 

Is the problem somewhere in my proxies?

 

 

edit: or could it be in here?

[spoiler=placement code on projectile]

  //checking for material rather than using isAirBlock() allows for mod blocks to not interfere with the flight path
        if (block.getMaterial() != Material.air)
        {
        	LogHelper.info("Collided successfully!");
        	LogHelper.info("Collided with:" + block.toString());
        	this.worldObj.destroyBlock(blockpos, false);
        	this.worldObj.setBlockState(blockpos, ModBlocks.blockCanvas.getDefaultState());
        	this.setDead();
        }

 

 

<signature></signature>

Link to comment
Share on other sites

World#setBlockState

and

World#getBlockState

only operate on property values that are stored in metadata. If you want to store more than 16 unique value combinations, you need to store the data in a

TileEntity

(or derive it from the location, e.g. neighbouring blocks) and then retrieve it in

Block#getExtendedState

/

Block#getActualState

.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

(or derive it from the location' date=' e.g. neighbouring blocks)[/quote']

I thought that's what I was doing in the

handleBlockState 

of

CanvasBlockModelFactory

?

 

If you have a single canvas

Block

and store the

IBlockState

it's imitating in the

TileEntity

, you need to override

Block#getExtendedState

to return an extended state with the

PAINTED_BLOCK

property set to the imitated

IBlockState

(retrieved from the

TileEntity

at that position).

 

If you have one canvas

Block

per

IBlockState

, you don't need the extended state at all; just get the imitated

IBlockState

directly from the canvas

Block

.

 

In

CanvasBlockModelFactory#handleBlockState

, retrieve the imitated

IBlockState

from the extended state or from the

Block

and create the model using it.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

I'm sorry for being such a noob, but could you possibly explain how to do that? I've tried looking up how to add data to a tile entitiy but I just can't seem to wrap my head around it. I tried

[spoiler=TECanvas.java]

package com.paradoxbomb.inkcannon.common.tileEntities.canvasBlock;

import com.paradoxbomb.inkcannon.LogHelper;
import com.paradoxbomb.inkcannon.NBTHelper;

import net.minecraft.block.state.IBlockState;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;

public class TECanvas extends TileEntity   
{
private IBlockState disguisedBlock;


public void setDisguiseState(IBlockState newState)
{
	this.disguisedBlock = newState;
}

public IBlockState getDisguisedState()
{
	return this.disguisedBlock;
}


@Override
public void writeToNBT(NBTTagCompound compound)
{
	LogHelper.info("Saving data to NBT");
	super.writeToNBT(compound);

	NBTTagCompound blockState = new NBTTagCompound();
	((TileEntity) this.disguisedBlock).writeToNBT(blockState);
	compound.setTag("PAINTED_BLOCK", blockState);
	LogHelper.info(compound.toString());
}

 @Override
 public void readFromNBT(NBTTagCompound compound)
 {

 }

}

 

 

 

but

writeToNBT

never seems to get called.

<signature></signature>

Link to comment
Share on other sites

There's no point in using it,

Block

itself has methods that allow you to create a

TileEntity

(i.e.

Block#hasTileEntity(IBlockState)

and

Block#createTileEntity(World, IBlockState)

).

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

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.