Jump to content

[1.14.4][SOLVED] TileEntity not working properly


JimiIT92

Recommended Posts

I made a block with a TileEntity in it wich stores the block custom name. For instance, if you rename the block "Test" in the Anvil an then place it, the block will display a nameplate on it with the name you set (test in this case). However i'm trying to make it working with the /setblock command too, but what happens is that the block is placed but the tile entity has no name. I'm trying using this command
 

/setblock ~ ~ ~ antiblaze:bedrock{Donator:"\"Test from command\""} replace

 

Also the text rendered is black for some reason

 

https://imgur.com/ywK3ZhG

This is the block code
 

package com.antiblaze.blocks;

import com.antiblaze.entities.tileentity.AntiBedrockTileEntity;
import com.antiblaze.settings.Settings;
import net.minecraft.block.BedrockBlock;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;

import javax.annotation.Nullable;

/**
 * AntiBlaze Bedrock Block
 * @author JimiIT92
 */
public class AntiBlazeBedrockBlock extends BedrockBlock {

    /**
     * Initialize the AntiBlaze Bedrock Block
     */
    public AntiBlazeBedrockBlock() {
        super(Properties.from(Blocks.BEDROCK).hardnessAndResistance(-1.0F, 3600000.0F).noDrops());
        setRegistryName(Settings.MODID,"bedrock");
    }

    /**
     * If this block has a TileEntity
     * @param state Block State
     * @return True
     */
    @Override
    public boolean hasTileEntity(BlockState state) {
        return true;
    }

    /**
     * Create a default Tile Entity for this block
     * @param state Block State
     * @param world World Instance
     * @return AntiBedrockTileEntity instance
     */
    @Nullable
    @Override
    public TileEntity createTileEntity(BlockState state, IBlockReader world) {
        return new AntiBedrockTileEntity();
    }

    /**
     * Set the TileEntity name when the block is placed
     * @param worldIn World Instance
     * @param pos Block Position
     * @param state Block State
     * @param placer Block Placer
     * @param stack Block's Item Stack
     */
    @Override
    public void onBlockPlacedBy(World worldIn, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack stack) {
        TileEntity tileEntity = worldIn.getTileEntity(pos);
        if(tileEntity instanceof AntiBedrockTileEntity && stack != null && stack.hasDisplayName()) {
            ((AntiBedrockTileEntity) tileEntity).setDonator(stack.getDisplayName().getString());
        }
        super.onBlockPlacedBy(worldIn, pos, state, placer, stack);
    }
}


This is the TileEntity Block

 

package com.antiblaze.entities.tileentity;

import com.antiblaze.core.AntiBlazeEntities;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.TileEntity;

/**
 * AntiBedrock Tile Entity
 * @author JimiIT92
 */
public class AntiBedrockTileEntity extends TileEntity {

    /**
     * NBT Donator Key
     */
    private final String NBT_KEY = "Donator";

    /**
     * AntiBedrock TileEntity stored donator
     */
    private String donator;

    /**
     * AntiBedrock TileEntity constructor
     */
    public AntiBedrockTileEntity() {
        super(AntiBlazeEntities.ANTIBEDROCK_TILE_ENTITY);
    }

    /**
     * Set the AntiBedrock TileEntity donator
     * @param donator Donator Name
     */
    public void setDonator(String donator) {
        this.donator = donator;
    }

    /**
     * Get the AntiBedrock TileEntity donator
     * @return AntiBedrock TileEntity donator
     */
    public String getDonator() {
        return this.donator;
    }

    /**
     * Writhe the AntiBedrock TileEntity donator name to NBT
     * @param compound NBT Compound
     * @return NBT Compound
     */
    @Override
    public CompoundNBT write(CompoundNBT compound) {
        if(this.donator != null) {
            compound.putString(NBT_KEY, this.donator);
        }
        return super.write(compound);
    }

    /**
     * Read the Donator name from the NBT Compound
     * @param compound NBT Compound
     */
    @Override
    public void read(CompoundNBT compound) {
        super.read(compound);
        this.donator = compound.getString(NBT_KEY);
    }

    /**
     * Get the NBT Update Tag
     * @return NBT Update Tag
     */
    @Override
    public CompoundNBT getUpdateTag() {
        return write(super.getUpdateTag());
    }

    /**
     * Handle the NBT Update Tag
     * @param tag NBT Update Tag
     */
    @Override
    public void handleUpdateTag(CompoundNBT tag) {
        read(tag);
    }
}


And this is the Renderer class

 

package com.antiblaze.entities.renderers;

import com.antiblaze.entities.tileentity.AntiBedrockTileEntity;
import net.minecraft.client.renderer.tileentity.TileEntityRenderer;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

/**
 * AntiBedrock TileEntity Renderer
 */
@OnlyIn(Dist.CLIENT)
public class AntiBedrockTileEntityRenderer extends TileEntityRenderer<AntiBedrockTileEntity> {

    /**
     * Render the Tile Entity
     * @param tileEntityIn AntiBedrock Tile Entity
     * @param x TileEntity X Position
     * @param y TileEntity Y Position
     * @param z TileEntity Z Position
     * @param partialTicks TileEntity Partial Ticks
     * @param destroyStage TileEntity Destroy Stages
     */
    public void render(AntiBedrockTileEntity tileEntityIn, double x, double y, double z, float partialTicks, int destroyStage) {
        if(tileEntityIn.getDonator() != null) {
            super.drawNameplate(tileEntityIn, tileEntityIn.getDonator(), x, y, z, 32);
        }
    }
}


What should i change to make it work with commands or make the text white?

EDIT: Solved by adding these two methods in the TileEntity code
 

@Nullable
    @Override
    public SUpdateTileEntityPacket getUpdatePacket() {
        CompoundNBT nbt = new CompoundNBT();
        nbt.putString(NBT_KEY, this.donator);
        return new SUpdateTileEntityPacket(getPos(), 1, nbt);
    }

    @Override
    public void onDataPacket(NetworkManager net, SUpdateTileEntityPacket pkt) {
        CompoundNBT nbt = pkt.getNbtCompound();
        this.donator = nbt.getString(NBT_KEY);
    }

 

Edited by JimiIT92
Solved

Don't blame me if i always ask for your help. I just want to learn to be better :)

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.