GameRegistry.registerTileEntity(smallEnergyStorage.getTileEntityClass(), smallEnergyStorage.registryName)
(TileClass.class, "testname")
. (Класс заменить на свой, имя рандомное, для тестов).class SmallEnergyStorage(name: String): BlockEnergyTileEntity<SmallEnergyStorageTileEntity>(name) {
override fun getTileEntityClass(): Class<SmallEnergyStorageTileEntity> = SmallEnergyStorageTileEntity::class.java
override fun createTileEntity(world: World, blockstate: IBlockState): SmallEnergyStorageTileEntity = SmallEnergyStorageTileEntity(200000)
override fun onBlockActivated(world: World, pos: BlockPos, state: IBlockState, player: EntityPlayer, hand: EnumHand, facing: EnumFacing, hitX: Float, hitY: Float, hitZ: Float): Boolean {
if(!world.isRemote) {
val tileEntity = getTileEntity(world, pos)
if(facing == EnumFacing.DOWN) tileEntity.extractEnergy(1000)
else if(facing == EnumFacing.UP) tileEntity.receiveEnergy(1000)
println(tileEntity.getEnergyStored())
}
return true
}
}
class SmallEnergyStorageTileEntity(maxEnergy: Int): EnergyStorage(maxEnergy) {
private var energy = 0
override fun writeToNBT(compound: NBTTagCompound): NBTTagCompound {
compound.setInteger("energy", energy)
return super.writeToNBT(compound)
}
override fun readFromNBT(compound: NBTTagCompound) {
energy = compound.getInteger("energy")
super.readFromNBT(compound)
}
}
open class EnergyStorage(private val maxEnergy: Int): TileEntity() {
private var energy = 0
override fun writeToNBT(compound: NBTTagCompound): NBTTagCompound {
compound.setInteger("energy", energy)
return super.writeToNBT(compound)
}
override fun readFromNBT(compound: NBTTagCompound) {
energy = compound.getInteger("energy")
super.readFromNBT(compound)
}
fun getMaxEnergyStored(): Int = maxEnergy
fun getEnergyStored(): Int = energy
fun extractEnergy(count: Int): Boolean {
if(count <= energy) {
energy -= count
markDirty()
return true
}
return false
}
fun receiveEnergy(count: Int): Boolean = if(count + energy >= maxEnergy) { energy = maxEnergy; markDirty(); true }
else { energy += count; markDirty(); true }
}
fun register() {
setRegister(copperOre)
setRegister(smallEnergyStorage)
GameRegistry.registerTileEntity(SmallEnergyStorageTileEntity::class.java, "small_energy_storage")
}
override fun readFromNBT(compound: NBTTagCompound) {
energy = compound.getInteger("energy")
super.readFromNBT(compound)
}