Jump to content

[Solved] Custom block-like Entity problem


Grumdot

Recommended Posts

As a learning step on my way to building a custom ship mod, I am trying to get working a wand item that deletes a block and replaces it with an entity that looks and feels exactly like the block.

 

Seems reasonably straightforward, and it mostly works, but when my renderer tries to access the blockId of the deleted block (which I store in the new entity) it is rendering, the code fails (so currently the best I can do is render all the entities as the same arbitrary block type...)

 

This is because the default entity constructor, Entity(World) is called (not by me!) by cpw.mods.fml.client.FMLClientHandler.spawnEntityIntoClientWorld() at some point after my world.spawnEntityInWorld( entity ) call, and before the renderer starts rendering, which resets the blockId stored in the entity to 0 before the renderer can use it.

 

I assume this all happens because of something I've done wrong somewhere...

Is it related to something happening on the client side that shouldn't be happening?

Is it anything to do with how/where I have registered my entity/renderer, etc?

What can I do to stop this extra call to the default constructor of my entity?

 

My wand item that turns a block to an entity:

 


public class WandItem extends Item {
        
    public WandItem(int par1) {
          super(par1);
          this.setCreativeTab(CreativeTabs.tabMaterials);          
    }
    
    public String getTextureFile()    {
            return "/smpShipsBlocks.png";
    }
             
    public boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int par4, int par5, int par6, int par7, float par8, float par9, float par10)    {
        
        if( !par3World.isRemote  &&  par3World.blockExists(par4, par5, par6)  )          {//###only place on the server side
        
            int blockID = par3World.getBlockId( par4,  par5 , par6 ); //###get block type
            int blockMeta = par3World.getBlockMetadata( par4,  par5 , par6 ); //###get block meta data 
            
            par3World.setBlock(par4, par5, par6, 0 ); //  set block to air
            
            BlockEntity e = new BlockEntity(par3World, par4 , par5 , par6 , blockID , blockMeta );  //###create entity, passing in blockId
            
            par3World.spawnEntityInWorld(   e   );  //###is this the best way to place the entity in world?
            
            //############### at this point in the code there is no problem: e.blockId is correct
            
            return true;
        }//if
        return false;
    }//on use   
                    
}//class

 

 

My entity that looks and feels like a block:

 

public class BlockEntity extends Entity  //###an entity that looks and feels like a block
{
    
    public int blockId;    //###stores blockId of the deleted block, so the renderer can check and render correct texture
    public int meta;    

    public BlockEntity(World par1World)    { //###constructor #1 - called in cpw.mods.fml.client.FMLClientHandler.spawnEntityIntoClientWorld()
        super(par1World);
        this.setSize(1.0F ,1.0F);        
        this.yOffset = 0.5F; 
        new Exception().printStackTrace();       
    }

    public BlockEntity(World par1World, int x, int y, int z , int blockId , int meta )   { //###constructor #2 - the one that I call from my wand item       
        this(par1World);
        this.blockId = blockId;                //###save the blockId
        this.meta = meta;
        this.setPosition(x+0.5F, y+0.0F, z+0.5F); 
    }
        
    @Override 
    public AxisAlignedBB getBoundingBox()    { 
        return this.boundingBox; 
    }
    
    protected void writeEntityToNBT(NBTTagCompound par1NBTTagCompound)
    {
        par1NBTTagCompound.setByte("Tile", (byte)this.blockId);
        par1NBTTagCompound.setByte("Data", (byte)this.meta);
    }
    
    protected void readEntityFromNBT(NBTTagCompound par1NBTTagCompound)
    {
        this.blockId = par1NBTTagCompound.getByte("Tile") ;
        this.meta = par1NBTTagCompound.getByte("Data");
    }

}

 

 

My renderer for the entity:

 

@SideOnly(Side.CLIENT)
public class BlockEntityRender extends Render
{
    private RenderBlocks blockRenderer = new RenderBlocks();
    
    public void doRender(Entity theEntity, double par2, double par4, double par6, float par8, float par9)
    {
        //##########  at this point theEntity.blockId has already been reset to 0 by a call to constructor #1 of the entity
        this.renderBlockEntity((BlockEntity)theEntity, par2, par4, par6, par8, par9);
    }
    
    public void renderBlockEntity(BlockEntity theEntity, double par2, double par4, double par6, float par8, float par9)    {
        
        Block b = Block.blocksList[theEntity.blockId];   //###code crashes here, because theEntityBlockId always = 0
        
        GL11.glPushMatrix();
        GL11.glTranslatef((float)par2, (float)par4, (float)par6);
        
        this.loadTexture("/terrain.png");
        
        this.blockRenderer.renderBlockAsItem( b , 0 , theEntity.getBrightness(par9));  //###render entity as the correct block (b), according to theEntity.blockId

        GL11.glPopMatrix();
    }
    
}

 

 

And just in case, here is my mod class, and client proxy (my common proxy has nothing happening in it):

 

public class SMPships {    
    
    public static Item wandItem;    
    
    @SidedProxy(clientSide = "SMPships.ClientProxySMPships", serverSide = "SMPships.CommonProxySMPships")    
    public static CommonProxySMPships proxy;
    
    @Init    
    public void load(FMLInitializationEvent event) 
    {
        proxy.registerRenderThings(); 
                
        wandItem = new WandItem( 5000  ).setIconIndex( 5 ).setItemName( "wand" ); 
        LanguageRegistry.addName(wandItem,  "Wand" );
        
        EntityRegistry.registerModEntity(BlockEntity.class, "grumdot_myentityblock", 2, this, 80, 3, true); //###is this the best way to register my entity type?                     
        
    }//load

}//class

 

 

 

public class ClientProxySMPships extends CommonProxySMPships{
    
    @Override
    public void registerRenderThings()
    {
        MinecraftForgeClient.preloadTexture("smpShipsBlocks.png");         
        
        RenderingRegistry.registerEntityRenderingHandler( BlockEntity.class, new BlockEntityRender() ); //###is this the best/only way to link an entity with renderer?
        
    }//register render things    
    
}//class

 

Link to comment
Share on other sites

You should use a custom spawning packet.

 

I don't know what's the proper way to do it, but take a look at:

 

cpw.mods.fml.common.registry.EntityRegistry.EntityRegistration

cpw.mods.fml.common.registry.EntityRegistry.EntityRegistration.setCustomSpawning

cpw.mods.fml.common.registry.EntityRegistry.doModEntityRegistration

Link to comment
Share on other sites

Thanks for the reply N1xx1.

 

I had looked at those classes already, and made an attempt to setCustomSpawning() in EntityRegistry, but failed (perhaps because I didn't use a custom spawn packet???)....

 

Why do I need a custom spawning packet?

What (specifically) should I be planning to achieve with it?

I can handle packet sending in general, but where can I get more info on custom spawning packets in particular?

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I recently updated my mods and now my game is crashing on startup i tried adding them one at a time and couldn't find anything wrong and no crash log is being made the only thing I have is the debug log. If it helps I'm also using the curseforge launcher to launch the game. Minecraft version- 1.20.1 Forge Version-47.2.0 https://gist.github.com/keepinitreal247/d3105af6bb70e30f183505715113c61a
    • i managed to use another launcher but it still says logging in
    • i tried to open the modpack with multimc by importing it from Ftb and it gave me an error https://paste.ee/p/YDqWN
    • Hi, I'm trying to render a single quad in the world. I'm mixing into the ChestRenderer class. If I understand correctly, BufferSource#getBuffer opens a buffer according to the supplied RenderType (Quads with POSITION_COLOR in my case). Then, I can supply my vertices (a simple 1-block plane along the Z Axis) and close the buffer using BufferSource#endBatch for rendering. This is the code I'm using: @Inject(at = @At("TAIL"), method = "render(...)V") public void render(T blockEntity, float partialTick, PoseStack poseStack, MultiBufferSource multiBufferSource, int packedLight, int packedOverlay, CallbackInfo ci) { BlockPos pos = blockEntity.getBlockPos(); AABB box = new AABB(pos, pos.offset(1, 1, 1)); BufferSource buffer = Minecraft.getInstance().renderBuffers().bufferSource(); VertexConsumer consumer = buffer.getBuffer(RenderType.guiOverlay()); poseStack.pushPose(); poseStack.translate(-pos.getX(), -pos.getY(), -pos.getZ()); consumer.vertex(box.minX, box.maxY, box.minZ).color(1, 1, 1, 1).endVertex(); consumer.vertex(box.minX, box.maxY, box.maxZ).color(1, 1, 1, 1).endVertex(); consumer.vertex(box.minX, box.minY, box.maxZ).color(1, 1, 1, 1).endVertex(); consumer.vertex(box.minX, box.minY, box.minZ).color(1, 1, 1, 1).endVertex(); buffer.endBatch(RenderType.guiOverlay()); poseStack.popPose(); } However, the plane does not get rendered. However, if I replace those 4 vertices with a call to LevelRenderer#renderLineBox and set the RenderType to LINES, it works. Do I need something else to render planes other than the 4 edges of the quad? I used QUADS back in 1.8 where it was still the raw OpenGL type and it worked then. Or am I missing something else entirely? Thanks!
  • Topics

×
×
  • Create New...

Important Information

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