Jump to content

World and position empty in tile Entity


Raycoms

Recommended Posts

I have a special TileEntity but when I try to get the world and position object in it's constructor they both are empty.

Is it impossible to get the world and position in the constructor or is there some way?

 

 

 

/**
* The scarecrow tile entity to store extra data.
*/
public class ScarecrowTileEntity extends TileEntityChest
{
    /**
     * NBTTag to store the type.
     */
    private static final String TAG_TYPE = "type";
    /**
     * Random generator.
     */
    private final Random random = new Random();
    /**
     * The inventory connected with the scarecrow.
     */
    private InventoryField inventoryField;
    /**
     * The type of the scarecrow.
     */
    private ScareCrowType  type;

    /**
     * Creates an instance of the tileEntity.
     */
    public ScarecrowTileEntity()
    {
        super();
        World world = getWorld();
        this.inventoryField = new InventoryField(LanguageHandler.getString("com.minecolonies.gui.inventory.scarecrow"), true);

        if(world != null)
        {
            Colony colony = ColonyManager.getColony(world, pos);

            if (colony != null && colony.getField(pos) == null)
            {
                Entity entity = EntityUtils.getEntityFromUUID(world, colony.getPermissions().getOwner());

                if (entity instanceof EntityPlayer)
                {
                    colony.addNewField(inventoryField, ((EntityPlayer) entity).inventory, pos, world);
                }
            }
        }
    }

    @Override
    public void readFromNBT(NBTTagCompound compound)
    {
        super.readFromNBT(compound);

        type = ScareCrowType.values()[compound.getInteger(TAG_TYPE)];
        getInventoryField().readFromNBT(compound);
    }

    @Override
    public void writeToNBT(NBTTagCompound compound)
    {
        super.writeToNBT(compound);

        compound.setInteger(TAG_TYPE, this.getType().ordinal());
        getInventoryField().writeToNBT(compound);
    }

    /**
     * Returns the type of the scarecrow (Important for the rendering).
     *
     * @return the enum type.
     */
    public ScareCrowType getType()
    {
        if (this.type == null)
        {
            this.type = ScareCrowType.values()[this.random.nextInt(1)];
        }
        return this.type;
    }
    /**
     * Set the inventory connected with the scarecrow.
     *
     * @param inventoryField the field to set it to
     */
    public final void setInventoryField(final InventoryField inventoryField)
    {
        this.inventoryField = inventoryField;
    }

    /**
     * Get the inventory connected with the scarecrow.
     *
     * @return the inventory field of this scarecrow
     */
    public InventoryField getInventoryField()
    {
        return inventoryField;
    }

    /**
     * Enum describing the different textures the scarecrow has.
     */
    public enum ScareCrowType
    {
        PUMPKINHEAD,
        NORMAL
    }
}

 

Creation of tileEntity:

   @Override
    public TileEntity createNewTileEntity(World worldIn, int meta)
    {
        return new ScarecrowTileEntity();
    }

Link to comment
Share on other sites

TileEntities do not have a BlockPos when they are initialized. Only once they are read from NBT do they have one.

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

Is it impossible to get the world and position in the constructor or is there some way?

 

As Animefan stated, no you cannot use a TileEntity's worldObj or pos variables until they are set (which happens after the tile entity is constructed). However, Forge provides a handy function called onLoad that you can override in your tile entity where you can reference the its world and position.

Link to comment
Share on other sites

I highly recommend that anyone using tile entities should, at least once in their lives, set a breakpoint in createNewTileEntity and then step into/through the whole process (including following the return to see what happens after that).

 

Trace the whole gory execution path to see what's called and/or set when. Only then can one begin to understand perverse phenomena such as why createNewTileEntity() can never do anything fancy, and the TE trash-on-update problem (and its converse gotcha, the TE that's never cleaned up). If you pay attention, you'll also see how an unsuspecting modder could accidentally create an endless loop.

 

Take notes so you can connect all the dots (like overriding the function that replaces the TE with every state change) and avoid all of the pitfalls (make sure your TE is marked dirty when it needs to be saved, and make sure it's deleted on time.)

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

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.



×
×
  • Create New...

Important Information

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