Jump to content

Recommended Posts

Posted

Hy, everyone! I have a problem)

 

I create simple TileCounter and BlockCounter.

If I click on different sides of the block, then my counter value is updated. And server displays correct value, but client don't.

How to sync it?

 

P.S. I'm a beginner in modding.

P.S.S. I understand much better if there is a  example.

Spoiler

public class BlockCounter extends Block implements ITileEntityProvider
{

    public BlockCounter(String name) {

        super(Material.IRON);
        
        this.setRegistryName(name);
        this.setUnlocalizedName(name);
        this.setCreativeTab(CreativeTabs.BUILDING_BLOCKS);
      
    }

    @Override
    public boolean onBlockActivated(World world, BlockPos position, IBlockState blockState, EntityPlayer player, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) 
    {
    	TileCounter tileEntity =  (TileCounter) world.getTileEntity(position);
    	
        if (!world.isRemote) 
        {          
            if(tileEntity != null)
            {
    
            if(side == EnumFacing.DOWN) 
            {
            	tileEntity.decrementCount();
            }
                    
            
            if(side == EnumFacing.UP)
            {
            	tileEntity.incrementCount();
            }
            
            if(side == EnumFacing.NORTH || side == EnumFacing.SOUTH)
            {
            	tileEntity.resetCount();
            }

            TextComponentTranslation component = new TextComponentTranslation("[Server] Counter" + tileEntity.getCount(), tileEntity.getCount());
            component.getStyle().setColor(TextFormatting.GREEN);
            player.sendStatusMessage(component, false);
            }
        }
        
        else
        {      	
        	TextComponentTranslation component = new TextComponentTranslation("[CLIENT] Counter" + tileEntity.getCount(), 1);
            component.getStyle().setColor(TextFormatting.RED);
            player.sendStatusMessage(component, false);
        }

        return true;
    }

    public Class<TileCounter> getTileEntityClass() {

        return TileCounter.class;
    }
    

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

 

Spoiler

public class TileCounter extends TileEntity
{
	public int count;
	
	public TileCounter() 
	{
		this.count=0;
	}

	public int getCount() 
	{
        return count;
    }

    public void incrementCount()
    {
        count++;
        this.markDirty();     
    }

    public void decrementCount()
    {
        count--;
        this.markDirty();     
    }
    
    public void resetCount()
    {
    	count=0;
    	this.markDirty();
    }

    @Override
    public void readFromNBT(NBTTagCompound compound) 
    {
        this.count = compound.getInteger("count");
        super.readFromNBT(compound); 
        
    }

    @Override
    public NBTTagCompound writeToNBT(NBTTagCompound compound) 
    {
        
        compound.setInteger("count", this.count);
        
        return super.writeToNBT(compound);
    }
    
    @Override
    @Nullable
    public SPacketUpdateTileEntity getUpdatePacket() 
    {
    	NBTTagCompound nbt = new NBTTagCompound();
    	this.writeToNBT(nbt);
    	int meta = getBlockMetadata();
        return new SPacketUpdateTileEntity(this.pos, meta, nbt);
    }

    @Override
    public NBTTagCompound getUpdateTag() 
    {
        return this.writeToNBT(new NBTTagCompound());
    }

    @Override
    public void onDataPacket(NetworkManager networkManager, SPacketUpdateTileEntity packet) 
    {
        this.readFromNBT(packet.getNbtCompound());
    }
    
    @Override
    public void handleUpdateTag(NBTTagCompound nbt)
    {
    	this.readFromNBT(nbt);
    }
}

 

 

Posted

Don’t use ITileEntityProvider, it’s legacy Minecraft code. Just override hasTileEntity(IBlockState) and getTileEntity in your block class

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

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.