package ru.dbotthepony.mc.otm.block

import net.minecraft.world.level.Level
import net.minecraft.world.level.block.entity.BlockEntity
import net.minecraft.world.level.block.entity.BlockEntityTicker
import net.minecraft.world.level.block.entity.BlockEntityType
import kotlin.reflect.KFunction1

inline fun <reified R, T : BlockEntity> KFunction1<R, Unit>.blockServerTicker(
	level: Level,
	expected: BlockEntityType<*>,
	provided: BlockEntityType<*>
): BlockEntityTicker<T>? {
	if (expected !== provided) {
		return null
	}

	if (level.isClientSide) {
		return null
	}

	return BlockEntityTicker<T> { _, _, _, tile -> if (tile is R) this.invoke(tile) }
}

inline fun <reified R, T : BlockEntity> KFunction1<R, Unit>.blockClientTicker(
	level: Level,
	expected: BlockEntityType<*>,
	provided: BlockEntityType<*>
): BlockEntityTicker<T>? {
	if (expected !== provided) {
		return null
	}

	if (!level.isClientSide) {
		return null
	}

	return BlockEntityTicker<T> { _, _, _, tile -> if (tile is R) this.invoke(tile) }
}
