Jump to content

How would i register a tile entity using deferred register


FOGC123

Recommended Posts

As i said I would like to register a tile entity using deferred register:

Code

package com.fogc123.randomadditions.tilentities;

import com.fogc123.randomadditions.RandomAdditions;
import com.fogc123.randomadditions.blocks.ModBlocks;
import net.minecraft.tileentity.TileEntityType;
import net.minecraftforge.fml.RegistryObject;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;

public class ModTileEntities {
    public static final DeferredRegister<TileEntityType<?>> TILE_ENTITIES = new DeferredRegister<>(ForgeRegistries.TILE_ENTITIES, RandomAdditions.MODID);

    public static final RegistryObject<TileEntityType<TileEntityWandInfuser>> WAND_INFUSER = TILE_ENTITIES.register("firstblock", () -> TileEntityType.Builder.create(TileEntityWandInfuser::new, ModBlocks.WAND_INFUSER.get()).build(null));
}
package com.fogc123.randomadditions.blocks;

import com.fogc123.randomadditions.tilentities.ModTileEntities;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.material.Material;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;

import javax.annotation.Nullable;

public class BlockWandInfuser extends Block {
    public BlockWandInfuser()
    {
        super(Block.Properties.create(Material.WOOD));
    }

    @Override
    public boolean hasTileEntity(final BlockState state) {
        return true;
    }

    @Nullable
    @Override
    public TileEntity createTileEntity(final BlockState state, final IBlockReader world)
    {
        return ModTileEntities.WAND_INFUSER.get().create();
    }

}
package com.fogc123.randomadditions.tilentities;

import com.fogc123.randomadditions.blocks.ModBlocks;
import com.fogc123.randomadditions.containers.WandInfuserContainer;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.INamedContainerProvider;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.play.server.SUpdateTileEntityPacket;
import net.minecraft.tileentity.ITickableTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.fml.network.NetworkHooks;
import net.minecraftforge.items.ItemStackHandler;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class TileEntityWandInfuser extends TileEntity implements ITickableTileEntity, INamedContainerProvider {
    //only one slot for now for testing
    public static final int BOOK_SLOT = 0;


    //handles inventory
    public final ItemStackHandler inventory = new ItemStackHandler(1) {

        @Override
        public boolean isItemValid(int slot, @Nonnull ItemStack stack) {
            // check if item is valid for slot, just return true for now(all items will be valid)
            return true;
        }

        @Override
        protected void onContentsChanged(int slot) {
            //callled everytime contents of inventory is changed
            super.onContentsChanged(slot);
            //mark entity dirty so game will save chunk to disc
            TileEntityWandInfuser.this.markDirty();
            System.out.println("Tile Entity Inventory Contents Changed!");
        }
    };
    //reduces number of objects i create(more efficient)
    public final LazyOptional<ItemStackHandler> inventoryCapabilityExternal = LazyOptional.of(() -> this.inventory)

    public TileEntityWandInfuser(TileEntityType<?> tileEntityTypeIn) {
        super(tileEntityTypeIn);
    }

    @Override
    public void tick() {
        //called every tick

    }



    @Override
    public void remove() {
        super.remove();
        inventoryCapabilityExternal.invalidate();
    }

    @Nonnull
    public CompoundNBT getUpdateTag() {
        return this.write(new CompoundNBT());
    }



    @Override
    public ITextComponent getDisplayName() {
        return new TranslationTextComponent(ModBlocks.WAND_INFUSER.get().);
    }

    /**
     * Called from {@link NetworkHooks#openGui}
     * (which is called from {@link BlockWandInfuser#onBlockActivated} on the logical server)
     *
     * @return The logical-server-side Container for this TileEntity
     */
    @Nullable
    @Override
    public Container createMenu(int windowId, PlayerInventory inv, PlayerEntity player) {
        return new WandInfuserContainer(windowId, inv, this);
    }

}

 

Link to comment
Share on other sites

35 minutes ago, Ugdhar said:

You need to register TILE_ENTITIES on the mod event bus, as far as I've seen all deferred registers are usually registered on the mod event bus in the mod constructor.

Sorry i wasnt very clear with my question, I have registered the deferred register on my mod event bus but:  "TileEntityType.Builder.create(TileEntityWandInfuser::new, ModBlocks.WAND_INFUSER.get())" still returns error Cannot resolve method 'create(<method reference>, net.minecraftforge.registries.IForgeRegistryEntry)'

Link to comment
Share on other sites

On 4/15/2020 at 4:30 PM, TheGreyGhost said:

Hi

Just a guess because I haven't used DeferredRegistry yet, but your block doesn't appear to be typed properly?

 


 public static final RegistryObject WAND_INFUSER = BLOCKS.register("wand_infuser", () -> new BlockWandInfuser());

i.e. RegistryObject<Block> ?

 

-TGG

I fixed that but it still returns an error!

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.